Skip to content

Commit

Permalink
Array, ReadonlyArray: change scanLeft & scanRight to return NonEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
willheslam committed Feb 2, 2021
1 parent 239cf0e commit a864bf3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/modules/Array.ts.md
Expand Up @@ -895,7 +895,7 @@ Same as `reduce` but it carries over the intermediate steps
**Signature**

```ts
export declare const scanLeft: <A, B>(b: B, f: (b: B, a: A) => B) => (as: A[]) => B[]
export declare const scanLeft: <A, B>(b: B, f: (b: B, a: A) => B) => (as: A[]) => NonEmptyArray<B>
```

**Example**
Expand All @@ -915,7 +915,7 @@ Fold an array from the right, keeping all intermediate results instead of only t
**Signature**

```ts
export declare const scanRight: <A, B>(b: B, f: (a: A, b: B) => B) => (as: A[]) => B[]
export declare const scanRight: <A, B>(b: B, f: (a: A, b: B) => B) => (as: A[]) => NonEmptyArray<B>
```

**Example**
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ReadonlyArray.ts.md
Expand Up @@ -884,7 +884,7 @@ Same as `reduce` but it carries over the intermediate steps
**Signature**

```ts
export declare function scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray<A>) => ReadonlyArray<B>
export declare function scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray<A>) => ReadonlyNonEmptyArray<B>
```

**Example**
Expand All @@ -904,7 +904,7 @@ Fold an array from the right, keeping all intermediate results instead of only t
**Signature**

```ts
export declare function scanRight<A, B>(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray<A>) => ReadonlyArray<B>
export declare function scanRight<A, B>(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray<A>) => ReadonlyNonEmptyArray<B>
```

**Example**
Expand Down
4 changes: 2 additions & 2 deletions src/Array.ts
Expand Up @@ -189,7 +189,7 @@ export const foldRight: <A, B>(
* @category combinators
* @since 2.0.0
*/
export const scanLeft: <A, B>(b: B, f: (b: B, a: A) => B) => (as: Array<A>) => Array<B> = RA.scanLeft as any
export const scanLeft: <A, B>(b: B, f: (b: B, a: A) => B) => (as: Array<A>) => NonEmptyArray<B> = RA.scanLeft as any

/**
* Fold an array from the right, keeping all intermediate results instead of only the final result
Expand All @@ -202,7 +202,7 @@ export const scanLeft: <A, B>(b: B, f: (b: B, a: A) => B) => (as: Array<A>) => A
* @category combinators
* @since 2.0.0
*/
export const scanRight: <A, B>(b: B, f: (a: A, b: B) => B) => (as: Array<A>) => Array<B> = RA.scanRight as any
export const scanRight: <A, B>(b: B, f: (a: A, b: B) => B) => (as: Array<A>) => NonEmptyArray<B> = RA.scanRight as any

/**
* Test whether an array is empty
Expand Down
11 changes: 5 additions & 6 deletions src/ReadonlyArray.ts
Expand Up @@ -25,6 +25,7 @@ import { Monad1 } from './Monad'
import { Monoid } from './Monoid'
import * as O from './Option'
import { fromCompare, getMonoid as getOrdMonoid, Ord, ordNumber } from './Ord'
import { NonEmptyArray } from './NonEmptyArray'
import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
import { Show } from './Show'
import { PipeableTraverse1, Traversable1 } from './Traversable'
Expand Down Expand Up @@ -299,11 +300,10 @@ export function foldRight<A, B>(
* @category combinators
* @since 2.5.0
*/
export function scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray<A>) => ReadonlyArray<B> {
export function scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray<A>) => ReadonlyNonEmptyArray<B> {
return (as) => {
const l = as.length
// tslint:disable-next-line: readonly-array
const r: Array<B> = new Array(l + 1)
const r = new Array(l + 1) as NonEmptyArray<B>
r[0] = b
for (let i = 0; i < l; i++) {
r[i + 1] = f(r[i], as[i])
Expand All @@ -323,11 +323,10 @@ export function scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray<A
* @category combinators
* @since 2.5.0
*/
export function scanRight<A, B>(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray<A>) => ReadonlyArray<B> {
export function scanRight<A, B>(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray<A>) => ReadonlyNonEmptyArray<B> {
return (as) => {
const l = as.length
// tslint:disable-next-line: readonly-array
const r: Array<B> = new Array(l + 1)
const r = new Array(l + 1) as NonEmptyArray<B>
r[l] = b
for (let i = l - 1; i >= 0; i--) {
r[i] = f(as[i], r[i + 1])
Expand Down

0 comments on commit a864bf3

Please sign in to comment.