From a864bf3a2dfc20d1d66d109d4c6cb3939c1ba7ea Mon Sep 17 00:00:00 2001 From: Will Heslam Date: Tue, 2 Feb 2021 16:10:55 +0000 Subject: [PATCH] Array, ReadonlyArray: change scanLeft & scanRight to return NonEmpty --- docs/modules/Array.ts.md | 4 ++-- docs/modules/ReadonlyArray.ts.md | 4 ++-- src/Array.ts | 4 ++-- src/ReadonlyArray.ts | 11 +++++------ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 34225bebd..f8321bf68 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -895,7 +895,7 @@ Same as `reduce` but it carries over the intermediate steps **Signature** ```ts -export declare const scanLeft: (b: B, f: (b: B, a: A) => B) => (as: A[]) => B[] +export declare const scanLeft: (b: B, f: (b: B, a: A) => B) => (as: A[]) => NonEmptyArray ``` **Example** @@ -915,7 +915,7 @@ Fold an array from the right, keeping all intermediate results instead of only t **Signature** ```ts -export declare const scanRight: (b: B, f: (a: A, b: B) => B) => (as: A[]) => B[] +export declare const scanRight: (b: B, f: (a: A, b: B) => B) => (as: A[]) => NonEmptyArray ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index fba6db291..c257f2be6 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -884,7 +884,7 @@ Same as `reduce` but it carries over the intermediate steps **Signature** ```ts -export declare function scanLeft(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray) => ReadonlyArray +export declare function scanLeft(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray) => ReadonlyNonEmptyArray ``` **Example** @@ -904,7 +904,7 @@ Fold an array from the right, keeping all intermediate results instead of only t **Signature** ```ts -export declare function scanRight(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray) => ReadonlyArray +export declare function scanRight(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray) => ReadonlyNonEmptyArray ``` **Example** diff --git a/src/Array.ts b/src/Array.ts index 75d20121f..7d8953b78 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -189,7 +189,7 @@ export const foldRight: ( * @category combinators * @since 2.0.0 */ -export const scanLeft: (b: B, f: (b: B, a: A) => B) => (as: Array) => Array = RA.scanLeft as any +export const scanLeft: (b: B, f: (b: B, a: A) => B) => (as: Array) => NonEmptyArray = RA.scanLeft as any /** * Fold an array from the right, keeping all intermediate results instead of only the final result @@ -202,7 +202,7 @@ export const scanLeft: (b: B, f: (b: B, a: A) => B) => (as: Array) => A * @category combinators * @since 2.0.0 */ -export const scanRight: (b: B, f: (a: A, b: B) => B) => (as: Array) => Array = RA.scanRight as any +export const scanRight: (b: B, f: (a: A, b: B) => B) => (as: Array) => NonEmptyArray = RA.scanRight as any /** * Test whether an array is empty diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 2afcf0182..7160bba40 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -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' @@ -299,11 +300,10 @@ export function foldRight( * @category combinators * @since 2.5.0 */ -export function scanLeft(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray) => ReadonlyArray { +export function scanLeft(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray) => ReadonlyNonEmptyArray { return (as) => { const l = as.length - // tslint:disable-next-line: readonly-array - const r: Array = new Array(l + 1) + const r = new Array(l + 1) as NonEmptyArray r[0] = b for (let i = 0; i < l; i++) { r[i + 1] = f(r[i], as[i]) @@ -323,11 +323,10 @@ export function scanLeft(b: B, f: (b: B, a: A) => B): (as: ReadonlyArray(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray) => ReadonlyArray { +export function scanRight(b: B, f: (a: A, b: B) => B): (as: ReadonlyArray) => ReadonlyNonEmptyArray { return (as) => { const l = as.length - // tslint:disable-next-line: readonly-array - const r: Array = new Array(l + 1) + const r = new Array(l + 1) as NonEmptyArray r[l] = b for (let i = l - 1; i >= 0; i--) { r[i] = f(as[i], r[i + 1])