From dc3b3ced31357e5bfa56ce67b5fc5726f57cbe24 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 22 Mar 2021 17:16:05 +0000 Subject: [PATCH 001/162] Turn TaskOptions to TaskEithers and vice versa --- docs/modules/TaskEither.ts.md | 11 +++++++++++ docs/modules/TaskOption.ts.md | 11 +++++++++++ dtslint/ts3.5/TaskEither.ts | 11 +++++++++++ dtslint/ts3.5/TaskOption.ts | 11 +++++++++++ src/TaskEither.ts | 8 ++++++++ src/TaskOption.ts | 9 +++++++++ test/TaskEither.ts | 18 ++++++++++++++++++ test/TaskOption.ts | 10 ++++++++++ 8 files changed, 89 insertions(+) create mode 100644 dtslint/ts3.5/TaskOption.ts diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 91a9346b0..12c09c515 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -70,6 +70,7 @@ Added in v2.0.0 - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [fromTask](#fromtask) + - [fromTaskOption](#fromtaskoption) - [left](#left) - [leftIO](#leftio) - [leftTask](#lefttask) @@ -716,6 +717,16 @@ export declare const fromTask: (fa: T.Task) => TaskEither Added in v2.7.0 +## fromTaskOption + +**Signature** + +```ts +export declare const fromTaskOption: (onNone: Lazy) => (e: TaskOption) => TaskEither +``` + +Added in v2.11.0 + ## left **Signature** diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index fad6f6b63..8d23d8b62 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -53,6 +53,7 @@ Added in v2.10.0 - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [fromTask](#fromtask) + - [fromTaskEither](#fromtaskeither) - [none](#none) - [some](#some) - [destructors](#destructors) @@ -463,6 +464,16 @@ export declare const fromTask: (fa: T.Task) => TaskOption Added in v2.10.0 +## fromTaskEither + +**Signature** + +```ts +export declare const fromTaskEither: (e: TaskEither) => TaskOption +``` + +Added in v2.11.0 + ## none **Signature** diff --git a/dtslint/ts3.5/TaskEither.ts b/dtslint/ts3.5/TaskEither.ts index 5a08e02aa..b1678307c 100644 --- a/dtslint/ts3.5/TaskEither.ts +++ b/dtslint/ts3.5/TaskEither.ts @@ -1,6 +1,7 @@ import * as _ from '../../src/TaskEither' import * as T from '../../src/Task' import * as E from '../../src/Either' +import * as TO from '../../src/TaskOption' import * as IOE from '../../src/IOEither' import { pipe } from '../../src/function' @@ -44,6 +45,16 @@ pipe( _.chainIOEitherKW(() => IOE.right(1)) ) +// +// fromTaskOption +// + +// $ExpectType TaskEither +pipe( + TO.some(1), + _.fromTaskOption(() => 'a') +) + // // taskify // diff --git a/dtslint/ts3.5/TaskOption.ts b/dtslint/ts3.5/TaskOption.ts new file mode 100644 index 000000000..7026f1bab --- /dev/null +++ b/dtslint/ts3.5/TaskOption.ts @@ -0,0 +1,11 @@ +import * as _ from '../../src/TaskOption' +import * as TE from '../../src/TaskEither' +import { pipe } from '../../src/function' + +declare const tesn: TE.TaskEither + +// +// fromTaskEither +// + +pipe(tesn, _.fromTaskEither) // $ExpectType TaskOption diff --git a/src/TaskEither.ts b/src/TaskEither.ts index f8c642fb8..77fc2aeae 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -60,6 +60,7 @@ import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import { Semigroup } from './Semigroup' import * as T from './Task' +import { TaskOption } from './TaskOption' // ------------------------------------------------------------------------------------- // model @@ -828,6 +829,13 @@ export const chainOptionK = /*#__PURE__*/ chainOptionK_(FromEither, Chain) +/** + * @category constructors + * @since 2.11.0 + */ +export const fromTaskOption: (onNone: Lazy) => (e: TaskOption) => TaskEither = (onNone) => + T.map(E.fromOption(onNone)) + /** * @category combinators * @since 2.4.0 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index a57ba65e6..ef0650686 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -32,6 +32,7 @@ import * as OT from './OptionT' import { Pointed1 } from './Pointed' import { Separated } from './Separated' import * as T from './Task' +import { TaskEither } from './TaskEither' import Task = T.Task import Option = O.Option @@ -97,6 +98,14 @@ export const fromTask: FromTask1['fromTask'] = /*#__PURE__*/ OT.fromF(T.Functor) +/** + * @category constructors + * @since 2.11.0 + */ +export const fromTaskEither: (e: TaskEither) => TaskOption = + /*#__PURE__*/ + T.map(O.fromEither) + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- diff --git a/test/TaskEither.ts b/test/TaskEither.ts index 316eeb4d0..171aea141 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -10,6 +10,7 @@ import { none, some } from '../src/Option' import { pipeable } from '../src/pipeable' import * as N from '../src/number' import * as T from '../src/Task' +import * as TO from '../src/TaskOption' import * as _ from '../src/TaskEither' import * as S from '../src/string' import { left, right } from '../src/Separated' @@ -471,6 +472,23 @@ describe('TaskEither', () => { ) }) + it('fromTaskOption', async () => { + U.deepStrictEqual( + await pipe( + TO.none, + _.fromTaskOption(() => 'none') + )(), + E.left('none') + ) + U.deepStrictEqual( + await pipe( + TO.some(1), + _.fromTaskOption(() => 'none') + )(), + E.right(1) + ) + }) + it('fromPredicate', async () => { const gt2 = _.fromPredicate( (n: number) => n >= 2, diff --git a/test/TaskOption.ts b/test/TaskOption.ts index 43ef52017..7736f3d45 100644 --- a/test/TaskOption.ts +++ b/test/TaskOption.ts @@ -2,6 +2,7 @@ import * as U from './util' import { pipe } from '../src/function' import * as O from '../src/Option' import * as T from '../src/Task' +import * as TE from '../src/TaskEither' import * as _ from '../src/TaskOption' describe('TaskOption', () => { @@ -118,6 +119,15 @@ describe('TaskOption', () => { U.deepStrictEqual(await f(3)(), O.some(3)) }) + it('fromTaskEither', async () => { + const pl = TE.left('a') + const pr = TE.right('a') + const fl = _.fromTaskEither(pl) + const fr = _.fromTaskEither(pr) + U.deepStrictEqual(await fl(), O.none) + U.deepStrictEqual(await fr(), O.some('a')) + }) + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- From c3af22b045bdbec7b7cd258c4d384dc9d211c595 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 23 Mar 2021 10:08:47 +0000 Subject: [PATCH 002/162] add appendW, prependW to arrays --- docs/modules/Array.ts.md | 26 ++++++++++++++++++++++++++ docs/modules/ReadonlyArray.ts.md | 26 ++++++++++++++++++++++++++ dtslint/ts3.5/Array.ts | 10 ++++++++++ dtslint/ts3.5/ReadonlyArray.ts | 10 ++++++++++ src/Array.ts | 16 ++++++++++++++++ src/NonEmptyArray.ts | 14 ++++++++++++-- src/ReadonlyArray.ts | 16 ++++++++++++++++ src/ReadonlyNonEmptyArray.ts | 14 ++++++++++++-- test/Array.ts | 10 ++++++++++ test/ReadonlyArray.ts | 10 ++++++++++ 10 files changed, 148 insertions(+), 4 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index c3eed3884..0ec5b1cc6 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -98,8 +98,10 @@ Added in v2.0.0 - [~~prependToAll~~](#prependtoall) - [constructors](#constructors) - [append](#append) + - [appendW](#appendw) - [makeBy](#makeby) - [prepend](#prepend) + - [prependW](#prependw) - [range](#range) - [replicate](#replicate) - [~~cons~~](#cons) @@ -1340,6 +1342,18 @@ assert.deepStrictEqual(pipe([1, 2, 3], append(4)), [1, 2, 3, 4]) Added in v2.10.0 +## appendW + +Less strict version of [`append`](#append). + +**Signature** + +```ts +export declare const appendW: (end: B) => (init: A[]) => NEA.NonEmptyArray +``` + +Added in v2.11.0 + ## makeBy Return a `Array` of length `n` with element `i` initialized with `f(i)`. @@ -1384,6 +1398,18 @@ assert.deepStrictEqual(pipe([2, 3, 4], prepend(1)), [1, 2, 3, 4]) Added in v2.10.0 +## prependW + +Less strict version of [`prepend`](#prepend). + +**Signature** + +```ts +export declare const prependW: (head: B) => (tail: A[]) => NEA.NonEmptyArray +``` + +Added in v2.11.0 + ## range Create an `Array` containing a range of integers, including both endpoints. diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index c078b0924..d4b672775 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -96,8 +96,10 @@ Added in v2.5.0 - [~~prependToAll~~](#prependtoall) - [constructors](#constructors) - [append](#append) + - [appendW](#appendw) - [makeBy](#makeby) - [prepend](#prepend) + - [prependW](#prependw) - [range](#range) - [replicate](#replicate) - [~~cons~~](#cons) @@ -1350,6 +1352,18 @@ assert.deepStrictEqual(pipe([1, 2, 3], append(4)), [1, 2, 3, 4]) Added in v2.10.0 +## appendW + +Less strict version of [`append`](#append). + +**Signature** + +```ts +export declare const appendW: (end: B) => (init: readonly A[]) => RNEA.ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## makeBy Return a `ReadonlyArray` of length `n` with element `i` initialized with `f(i)`. @@ -1394,6 +1408,18 @@ assert.deepStrictEqual(pipe([2, 3, 4], prepend(1)), [1, 2, 3, 4]) Added in v2.10.0 +## prependW + +Less strict version of [`prepend`](#prepend). + +**Signature** + +```ts +export declare const prependW: (head: B) => (tail: readonly A[]) => RNEA.ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## range Create a `ReadonlyArray` containing a range of integers, including both endpoints. diff --git a/dtslint/ts3.5/Array.ts b/dtslint/ts3.5/Array.ts index 8d448c98f..c2cba40fe 100644 --- a/dtslint/ts3.5/Array.ts +++ b/dtslint/ts3.5/Array.ts @@ -9,6 +9,16 @@ declare const ns: Array declare const ss: Array declare const tns: Array<[number, string]> +// prepend + +pipe(ss, _.prepend('a')) // $ExpectType NonEmptyArray +pipe(ss, _.prependW(1)) // $ExpectType NonEmptyArray + +// append + +pipe(ss, _.append('a')) // $ExpectType NonEmptyArray +pipe(ss, _.appendW(1)) // $ExpectType NonEmptyArray + // // zip // diff --git a/dtslint/ts3.5/ReadonlyArray.ts b/dtslint/ts3.5/ReadonlyArray.ts index 7d4c1ffc1..45b5edde9 100644 --- a/dtslint/ts3.5/ReadonlyArray.ts +++ b/dtslint/ts3.5/ReadonlyArray.ts @@ -9,6 +9,16 @@ declare const rns: ReadonlyArray declare const rss: ReadonlyArray declare const rtns: ReadonlyArray +// prepend + +pipe(rss, _.prepend('a')) // $ExpectType ReadonlyNonEmptyArray +pipe(rss, _.prependW(1)) // $ExpectType ReadonlyNonEmptyArray + +// append + +pipe(rss, _.append('a')) // $ExpectType ReadonlyNonEmptyArray +pipe(rss, _.appendW(1)) // $ExpectType ReadonlyNonEmptyArray + // // zip // diff --git a/src/Array.ts b/src/Array.ts index cd7143350..f95d490ae 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -54,6 +54,14 @@ import Option = O.Option */ export const prepend: (head: A) => (tail: Array) => NEA.NonEmptyArray = NEA.prepend +/** + * Less strict version of [`prepend`](#prepend). + * + * @category constructors + * @since 2.11.0 + */ +export const prependW: (head: B) => (tail: Array) => NEA.NonEmptyArray = NEA.prependW + /** * Append an element to the end of a `Array`, creating a new `NonEmptyArray`. * @@ -68,6 +76,14 @@ export const prepend: (head: A) => (tail: Array) => NEA.NonEmptyArray = */ export const append: (end: A) => (init: Array) => NEA.NonEmptyArray = NEA.append +/** + * Less strict version of [`append`](#append). + * + * @category constructors + * @since 2.11.0 + */ +export const appendW: (end: B) => (init: Array) => NEA.NonEmptyArray = NEA.appendW + /** * Return a `Array` of length `n` with element `i` initialized with `f(i)`. * diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 465578827..e35c59a28 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -69,12 +69,22 @@ export const isOutOfBound = (i: number, as: Array): boolean => i < 0 || i /** * @internal */ -export const prepend = (head: A) => (tail: Array): NonEmptyArray => [head, ...tail] +export const prependW = (head: B) => (tail: Array): NonEmptyArray => [head, ...tail] /** * @internal */ -export const append = (end: A) => (init: Array): NonEmptyArray => concat(init, [end]) +export const prepend: (head: A) => (tail: Array) => NonEmptyArray = prependW + +/** + * @internal + */ +export const appendW = (end: B) => (init: Array): NonEmptyArray => [...init, end] as any + +/** + * @internal + */ +export const append: (end: A) => (init: Array) => NonEmptyArray = appendW /** * @internal diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index ed4eb9547..14955c19c 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -55,6 +55,14 @@ import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray */ export const prepend = RNEA.prepend +/** + * Less strict version of [`prepend`](#prepend). + * + * @category constructors + * @since 2.11.0 + */ +export const prependW = RNEA.prependW + /** * Append an element to the end of a `ReadonlyArray`, creating a new `ReadonlyNonEmptyArray`. * @@ -69,6 +77,14 @@ export const prepend = RNEA.prepend */ export const append = RNEA.append +/** + * Less strict version of [`append`](#append). + * + * @category constructors + * @since 2.11.0 + */ +export const appendW = RNEA.appendW + /** * Return a `ReadonlyArray` of length `n` with element `i` initialized with `f(i)`. * diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 5c0877fac..e8159ba57 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -73,12 +73,22 @@ export const isOutOfBound = (i: number, as: ReadonlyArray): boolean => i < /** * @internal */ -export const prepend = (head: A) => (tail: ReadonlyArray): ReadonlyNonEmptyArray => [head, ...tail] +export const prependW = (head: B) => (tail: ReadonlyArray): ReadonlyNonEmptyArray => [head, ...tail] /** * @internal */ -export const append = (end: A) => (init: ReadonlyArray): ReadonlyNonEmptyArray => concat(init, [end]) +export const prepend: (head: A) => (tail: ReadonlyArray) => ReadonlyNonEmptyArray = prependW + +/** + * @internal + */ +export const appendW = (end: B) => (init: ReadonlyArray): ReadonlyNonEmptyArray => [...init, end] as any + +/** + * @internal + */ +export const append: (end: A) => (init: ReadonlyArray) => ReadonlyNonEmptyArray = appendW /** * @internal diff --git a/test/Array.ts b/test/Array.ts index dc4f8abf2..1f5a74f0b 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -973,6 +973,16 @@ describe('Array', () => { }) }) + it('prepend', () => { + U.deepStrictEqual(pipe(['a', 'b'], _.prepend('c')), ['c', 'a', 'b']) + U.deepStrictEqual(pipe(['a', 'b'], _.prependW(3)), [3, 'a', 'b']) + }) + + it('append', () => { + U.deepStrictEqual(pipe(['a', 'b'], _.append('c')), ['a', 'b', 'c']) + U.deepStrictEqual(pipe(['a', 'b'], _.appendW(3)), ['a', 'b', 3]) + }) + it('makeBy', () => { U.deepStrictEqual(_.makeBy(5, U.double), [0, 2, 4, 6, 8]) U.deepStrictEqual(_.makeBy(0, U.double), []) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index a96be5918..b2ee1feba 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1040,6 +1040,16 @@ describe('ReadonlyArray', () => { }) }) + it('prepend', () => { + U.deepStrictEqual(pipe(['a', 'b'], _.prepend('c')), ['c', 'a', 'b']) + U.deepStrictEqual(pipe(['a', 'b'], _.prependW(3)), [3, 'a', 'b']) + }) + + it('append', () => { + U.deepStrictEqual(pipe(['a', 'b'], _.append('c')), ['a', 'b', 'c']) + U.deepStrictEqual(pipe(['a', 'b'], _.appendW(3)), ['a', 'b', 3]) + }) + it('makeBy', () => { U.deepStrictEqual(_.makeBy(5, U.double), [0, 2, 4, 6, 8]) U.strictEqual(_.makeBy(0, U.double), _.empty) From 0d8a1b42ccac838e2d9c3f7b27ae351e42e0ba63 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 23 Mar 2021 11:30:29 +0100 Subject: [PATCH 003/162] chore --- dtslint/ts3.5/NonEmptyArray.ts | 8 ++++++++ dtslint/ts3.5/ReadonlyNonEmptyArray.ts | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/dtslint/ts3.5/NonEmptyArray.ts b/dtslint/ts3.5/NonEmptyArray.ts index 8afd17eac..7087afee4 100644 --- a/dtslint/ts3.5/NonEmptyArray.ts +++ b/dtslint/ts3.5/NonEmptyArray.ts @@ -2,6 +2,7 @@ import * as _ from '../../src/NonEmptyArray' import { Ord } from '../../src/Ord' import { pipe } from '../../src/function' +declare const as: Array declare const neas: _.NonEmptyArray declare const nens: _.NonEmptyArray declare const netns: _.NonEmptyArray<[number, string]> @@ -105,3 +106,10 @@ _.filter(isNumber1)(neasn) pipe(neasn, _.filter(isNumber2)) // $ExpectType Option> _.filter(isNumber2)(neasn) + +// +// concat +// + +_.concat(as, neas) // $ExpectType NonEmptyArray +_.concat(neas, as) // $ExpectType NonEmptyArray diff --git a/dtslint/ts3.5/ReadonlyNonEmptyArray.ts b/dtslint/ts3.5/ReadonlyNonEmptyArray.ts index f0057b2da..fc9fe701d 100644 --- a/dtslint/ts3.5/ReadonlyNonEmptyArray.ts +++ b/dtslint/ts3.5/ReadonlyNonEmptyArray.ts @@ -106,3 +106,10 @@ _.filter(isNumber1)(neasn) pipe(neasn, _.filter(isNumber2)) // $ExpectType Option> _.filter(isNumber2)(neasn) + +// +// concat +// + +_.concat(ras, rneas) // $ExpectType ReadonlyNonEmptyArray +_.concat(rneas, ras) // $ExpectType ReadonlyNonEmptyArray From 11515dcdadcd77dc60dd2aaad6267b408b9acef1 Mon Sep 17 00:00:00 2001 From: cdimitroulas Date: Sat, 27 Mar 2021 17:29:30 +0000 Subject: [PATCH 004/162] Add matchLeft, matchRight, modifyHead & modifyLast to NonEmptyArray --- src/NonEmptyArray.ts | 35 +++++++++++++++++++++++++++++++++++ test/NonEmptyArray.ts | 16 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index e35c59a28..5874dcb2e 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -1064,6 +1064,41 @@ export const max: (ord: Ord) => (nea: NonEmptyArray) => A = RNEA.max */ export const concatAll = (S: Semigroup) => (as: NonEmptyArray): A => as.reduce(S.concat) +/** + * Break an array into its first element and remaining elements + * + * @category destructors + * @since 2.11.0 + */ +export const matchLeft = (f: (head: A, tail: Array) => B) => (as: NonEmptyArray): B => f(head(as), tail(as)) + +/** + * Break an array into its initial elements and the last element + * + * @category destructors + * @since 2.11.0 + */ +export const matchRight = (f: (init: Array, last: A) => B) => (as: NonEmptyArray): B => + f(init(as), last(as)) + +/** + * Modifies the first element of the array + * + * @since 2.11.0 + */ +export const modifyHead = (f: (a: A) => A): ((nea: NonEmptyArray) => NonEmptyArray) => { + return matchLeft((head, tail) => pipe(tail, prepend(f(head)))) +} + +/** + * Modifies the last element of the array + * + * @since 2.11.0 + */ +export const modifyLast = (f: (a: A) => A): ((nea: NonEmptyArray) => NonEmptyArray) => { + return matchRight((init, last) => pipe(init, append(f(last)))) +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index a9f589517..63da4cfac 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -483,4 +483,20 @@ describe('NonEmptyArray', () => { // n out of bounds assertSingleChunk([1, 2], 3) }) + + it('matchLeft', () => { + U.deepStrictEqual(_.matchLeft((head, tail) => [head, tail])([1, 2, 3]), [1, [2, 3]]) + }) + + it('matchRight', () => { + U.deepStrictEqual(_.matchRight((init, last) => [init, last])([1, 2, 3]), [[1, 2], 3]) + }) + + it('modifyHead', () => { + U.deepStrictEqual(_.modifyHead((x: number) => x + 10)([1, 2, 3]), [11, 2, 3]) + }) + + it('modifyLast', () => { + U.deepStrictEqual(_.modifyLast((x: number) => x + 10)([1, 2, 3]), [1, 2, 13]) + }) }) From 56b3b4cbbb79425a5f677a4b7b3595f0da244bb0 Mon Sep 17 00:00:00 2001 From: cdimitroulas Date: Sat, 27 Mar 2021 17:36:32 +0000 Subject: [PATCH 005/162] Add matchLeft, matchRight, modifyHead & modifyLast to ReadonlyNonEmptyArray --- docs/modules/NonEmptyArray.ts.md | 52 ++++++++++++++++++++++++ docs/modules/ReadonlyNonEmptyArray.ts.md | 52 ++++++++++++++++++++++++ src/ReadonlyNonEmptyArray.ts | 36 ++++++++++++++++ test/ReadonlyNonEmptyArray.ts | 16 ++++++++ 4 files changed, 156 insertions(+) diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index b216ebdd2..25c6dcafd 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -81,6 +81,8 @@ Added in v2.0.0 - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) + - [matchLeft](#matchleft) + - [matchRight](#matchright) - [unappend](#unappend) - [unprepend](#unprepend) - [~~uncons~~](#uncons) @@ -119,6 +121,8 @@ Added in v2.0.0 - [last](#last) - [max](#max) - [min](#min) + - [modifyHead](#modifyhead) + - [modifyLast](#modifylast) - [sequence](#sequence) - [tail](#tail) - [traverse](#traverse) @@ -738,6 +742,30 @@ Added in v2.0.0 # destructors +## matchLeft + +Break an array into its first element and remaining elements + +**Signature** + +```ts +export declare const matchLeft: (f: (head: A, tail: A[]) => B) => (as: NonEmptyArray) => B +``` + +Added in v2.11.0 + +## matchRight + +Break an array into its initial elements and the last element + +**Signature** + +```ts +export declare const matchRight: (f: (init: A[], last: A) => B) => (as: NonEmptyArray) => B +``` + +Added in v2.11.0 + ## unappend Return the tuple of the `init` and the `last`. @@ -1158,6 +1186,30 @@ export declare const min: (ord: Ord) => (nea: NonEmptyArray) => A Added in v2.0.0 +## modifyHead + +Modifies the first element of the array + +**Signature** + +```ts +export declare const modifyHead: (f: (a: A) => A) => (nea: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + +## modifyLast + +Modifies the last element of the array + +**Signature** + +```ts +export declare const modifyLast: (f: (a: A) => A) => (nea: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + ## sequence **Signature** diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 23c5ac5d8..db31266f3 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -86,6 +86,8 @@ Added in v2.5.0 - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) + - [matchLeft](#matchleft) + - [matchRight](#matchright) - [unappend](#unappend) - [unprepend](#unprepend) - [~~uncons~~](#uncons) @@ -125,6 +127,8 @@ Added in v2.5.0 - [last](#last) - [max](#max) - [min](#min) + - [modifyHead](#modifyhead) + - [modifyLast](#modifylast) - [tail](#tail) - [~~fold~~](#fold) @@ -825,6 +829,30 @@ Added in v2.5.0 # destructors +## matchLeft + +Break an array into its first element and remaining elements + +**Signature** + +```ts +export declare const matchLeft: (f: (head: A, tail: readonly A[]) => B) => (as: ReadonlyNonEmptyArray) => B +``` + +Added in v2.11.0 + +## matchRight + +Break an array into its initial elements and the last element + +**Signature** + +```ts +export declare const matchRight: (f: (init: readonly A[], last: A) => B) => (as: ReadonlyNonEmptyArray) => B +``` + +Added in v2.11.0 + ## unappend Return the tuple of the `init` and the `last`. @@ -1246,6 +1274,30 @@ export declare const min: (O: Ord) => (as: ReadonlyNonEmptyArray) => A Added in v2.5.0 +## modifyHead + +Modifies the first element of the array + +**Signature** + +```ts +export declare const modifyHead: (f: (a: A) => A) => (nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + +## modifyLast + +Modifies the last element of the array + +**Signature** + +```ts +export declare const modifyLast: (f: (a: A) => A) => (nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## tail **Signature** diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index e8159ba57..dbad7eead 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -1116,6 +1116,42 @@ export const max = (O: Ord): ((as: ReadonlyNonEmptyArray) => A) => { */ export const concatAll = (S: Semigroup) => (as: ReadonlyNonEmptyArray): A => as.reduce(S.concat) +/** + * Break an array into its first element and remaining elements + * + * @category destructors + * @since 2.11.0 + */ +export const matchLeft = (f: (head: A, tail: ReadonlyArray) => B) => (as: ReadonlyNonEmptyArray): B => + f(head(as), tail(as)) + +/** + * Break an array into its initial elements and the last element + * + * @category destructors + * @since 2.11.0 + */ +export const matchRight = (f: (init: ReadonlyArray, last: A) => B) => (as: ReadonlyNonEmptyArray): B => + f(init(as), last(as)) + +/** + * Modifies the first element of the array + * + * @since 2.11.0 + */ +export const modifyHead = (f: (a: A) => A): ((nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { + return matchLeft((head, tail) => pipe(tail, prepend(f(head)))) +} + +/** + * Modifies the last element of the array + * + * @since 2.11.0 + */ +export const modifyLast = (f: (a: A) => A): ((nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { + return matchRight((init, last) => pipe(init, append(f(last)))) +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index e810de356..1338135ba 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -620,4 +620,20 @@ describe('ReadonlyNonEmptyArray', () => { U.deepStrictEqual(_.makeBy(0, U.double), [0]) U.deepStrictEqual(_.makeBy(-1, U.double), [0]) }) + + it('matchLeft', () => { + U.deepStrictEqual(_.matchLeft((head, tail) => [head, tail])([1, 2, 3]), [1, [2, 3]]) + }) + + it('matchRight', () => { + U.deepStrictEqual(_.matchRight((init, last) => [init, last])([1, 2, 3]), [[1, 2], 3]) + }) + + it('modifyHead', () => { + U.deepStrictEqual(_.modifyHead((x: number) => x + 10)([1, 2, 3]), [11, 2, 3]) + }) + + it('modifyLast', () => { + U.deepStrictEqual(_.modifyLast((x: number) => x + 10)([1, 2, 3]), [1, 2, 13]) + }) }) From 70f1ab262e19d12ba2412118b71434b85b69bffc Mon Sep 17 00:00:00 2001 From: gcanti Date: Sun, 28 Mar 2021 19:26:09 +0200 Subject: [PATCH 006/162] align with v3 and update changelog --- CHANGELOG.md | 16 ++++++++++++++ docs/modules/NonEmptyArray.ts.md | 12 +++++----- docs/modules/ReadonlyNonEmptyArray.ts.md | 12 +++++----- src/NonEmptyArray.ts | 22 +++++++++---------- src/ReadonlyNonEmptyArray.ts | 22 +++++++++---------- test/NonEmptyArray.ts | 28 +++++++++++++++++++----- test/ReadonlyNonEmptyArray.ts | 28 +++++++++++++++++++----- 7 files changed, 96 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb204eff..191625a07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,22 @@ **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. +# 2.11 + +- **New Feature** + - `Array` + - add `prependW`, `appendW` (@thewilkybarkid) + - `NonEmptyArray` + - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - `ReadonlyArray` + - add `prependW`, `appendW` (@thewilkybarkid) + - `ReadonlyNonEmptyArray` + - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - `TaskEither` + - add `fromTaskOption` (@thewilkybarkid) + - `TaskOption` + - add `fromTaskEither` (@thewilkybarkid) + # 2.10.0-rc.5 - **Bug Fix** diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 25c6dcafd..b7f6819fd 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -744,7 +744,7 @@ Added in v2.0.0 ## matchLeft -Break an array into its first element and remaining elements +Break an `Array` into its first element and remaining elements. **Signature** @@ -756,7 +756,7 @@ Added in v2.11.0 ## matchRight -Break an array into its initial elements and the last element +Break an `Array` into its initial elements and the last element. **Signature** @@ -1188,24 +1188,24 @@ Added in v2.0.0 ## modifyHead -Modifies the first element of the array +Apply a function to the head, creating a new `NonEmptyArray`. **Signature** ```ts -export declare const modifyHead: (f: (a: A) => A) => (nea: NonEmptyArray) => NonEmptyArray +export declare const modifyHead: (f: Endomorphism) => (as: NonEmptyArray) => NonEmptyArray ``` Added in v2.11.0 ## modifyLast -Modifies the last element of the array +Apply a function to the last element, creating a new `NonEmptyArray`. **Signature** ```ts -export declare const modifyLast: (f: (a: A) => A) => (nea: NonEmptyArray) => NonEmptyArray +export declare const modifyLast: (f: Endomorphism) => (as: NonEmptyArray) => NonEmptyArray ``` Added in v2.11.0 diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index db31266f3..8a46b73bb 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -831,7 +831,7 @@ Added in v2.5.0 ## matchLeft -Break an array into its first element and remaining elements +Break a `ReadonlyArray` into its first element and remaining elements. **Signature** @@ -843,7 +843,7 @@ Added in v2.11.0 ## matchRight -Break an array into its initial elements and the last element +Break a `ReadonlyArray` into its initial elements and the last element. **Signature** @@ -1276,24 +1276,24 @@ Added in v2.5.0 ## modifyHead -Modifies the first element of the array +Apply a function to the head, creating a new `ReadonlyNonEmptyArray`. **Signature** ```ts -export declare const modifyHead: (f: (a: A) => A) => (nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +export declare const modifyHead: (f: Endomorphism) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray ``` Added in v2.11.0 ## modifyLast -Modifies the last element of the array +Apply a function to the last element, creating a new `ReadonlyNonEmptyArray`. **Signature** ```ts -export declare const modifyLast: (f: (a: A) => A) => (nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +export declare const modifyLast: (f: Endomorphism) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray ``` Added in v2.11.0 diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 5874dcb2e..cc3d3208a 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -21,7 +21,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement } from './function' +import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -1065,7 +1065,7 @@ export const max: (ord: Ord) => (nea: NonEmptyArray) => A = RNEA.max export const concatAll = (S: Semigroup) => (as: NonEmptyArray): A => as.reduce(S.concat) /** - * Break an array into its first element and remaining elements + * Break an `Array` into its first element and remaining elements. * * @category destructors * @since 2.11.0 @@ -1073,7 +1073,7 @@ export const concatAll = (S: Semigroup) => (as: NonEmptyArray): A => as export const matchLeft = (f: (head: A, tail: Array) => B) => (as: NonEmptyArray): B => f(head(as), tail(as)) /** - * Break an array into its initial elements and the last element + * Break an `Array` into its initial elements and the last element. * * @category destructors * @since 2.11.0 @@ -1082,22 +1082,22 @@ export const matchRight = (f: (init: Array, last: A) => B) => (as: NonE f(init(as), last(as)) /** - * Modifies the first element of the array + * Apply a function to the head, creating a new `NonEmptyArray`. * * @since 2.11.0 */ -export const modifyHead = (f: (a: A) => A): ((nea: NonEmptyArray) => NonEmptyArray) => { - return matchLeft((head, tail) => pipe(tail, prepend(f(head)))) -} +export const modifyHead = (f: Endomorphism) => (as: NonEmptyArray): NonEmptyArray => [ + f(head(as)), + ...tail(as) +] /** - * Modifies the last element of the array + * Apply a function to the last element, creating a new `NonEmptyArray`. * * @since 2.11.0 */ -export const modifyLast = (f: (a: A) => A): ((nea: NonEmptyArray) => NonEmptyArray) => { - return matchRight((init, last) => pipe(init, append(f(last)))) -} +export const modifyLast = (f: Endomorphism) => (as: NonEmptyArray): NonEmptyArray => + pipe(init(as), append(f(last(as)))) // ------------------------------------------------------------------------------------- // deprecated diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index dbad7eead..091cc9b5f 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -21,7 +21,7 @@ import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement } from './function' +import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -1117,7 +1117,7 @@ export const max = (O: Ord): ((as: ReadonlyNonEmptyArray) => A) => { export const concatAll = (S: Semigroup) => (as: ReadonlyNonEmptyArray): A => as.reduce(S.concat) /** - * Break an array into its first element and remaining elements + * Break a `ReadonlyArray` into its first element and remaining elements. * * @category destructors * @since 2.11.0 @@ -1126,7 +1126,7 @@ export const matchLeft = (f: (head: A, tail: ReadonlyArray) => B) => (a f(head(as), tail(as)) /** - * Break an array into its initial elements and the last element + * Break a `ReadonlyArray` into its initial elements and the last element. * * @category destructors * @since 2.11.0 @@ -1135,22 +1135,22 @@ export const matchRight = (f: (init: ReadonlyArray, last: A) => B) => ( f(init(as), last(as)) /** - * Modifies the first element of the array + * Apply a function to the head, creating a new `ReadonlyNonEmptyArray`. * * @since 2.11.0 */ -export const modifyHead = (f: (a: A) => A): ((nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { - return matchLeft((head, tail) => pipe(tail, prepend(f(head)))) -} +export const modifyHead = (f: Endomorphism) => (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray => [ + f(head(as)), + ...tail(as) +] /** - * Modifies the last element of the array + * Apply a function to the last element, creating a new `ReadonlyNonEmptyArray`. * * @since 2.11.0 */ -export const modifyLast = (f: (a: A) => A): ((nea: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { - return matchRight((init, last) => pipe(init, append(f(last)))) -} +export const modifyLast = (f: Endomorphism) => (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray => + pipe(init(as), append(f(last(as)))) // ------------------------------------------------------------------------------------- // deprecated diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 63da4cfac..37eecb794 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -1,5 +1,5 @@ import * as assert from 'assert' -import { identity, pipe } from '../src/function' +import { Endomorphism, identity, pipe } from '../src/function' import * as _ from '../src/NonEmptyArray' import * as N from '../src/number' import * as O from '../src/Option' @@ -485,18 +485,36 @@ describe('NonEmptyArray', () => { }) it('matchLeft', () => { - U.deepStrictEqual(_.matchLeft((head, tail) => [head, tail])([1, 2, 3]), [1, [2, 3]]) + U.deepStrictEqual( + pipe( + [1, 2, 3], + _.matchLeft((head, tail) => [head, tail]) + ), + [1, [2, 3]] + ) }) it('matchRight', () => { - U.deepStrictEqual(_.matchRight((init, last) => [init, last])([1, 2, 3]), [[1, 2], 3]) + U.deepStrictEqual( + pipe( + [1, 2, 3], + _.matchRight((init, last) => [init, last]) + ), + [[1, 2], 3] + ) }) it('modifyHead', () => { - U.deepStrictEqual(_.modifyHead((x: number) => x + 10)([1, 2, 3]), [11, 2, 3]) + const f: Endomorphism = (s) => s + '!' + U.deepStrictEqual(pipe(['a'], _.modifyHead(f)), ['a!']) + U.deepStrictEqual(pipe(['a', 'b'], _.modifyHead(f)), ['a!', 'b']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyHead(f)), ['a!', 'b', 'c']) }) it('modifyLast', () => { - U.deepStrictEqual(_.modifyLast((x: number) => x + 10)([1, 2, 3]), [1, 2, 13]) + const f: Endomorphism = (s) => s + '!' + U.deepStrictEqual(pipe(['a'], _.modifyLast(f)), ['a!']) + U.deepStrictEqual(pipe(['a', 'b'], _.modifyLast(f)), ['a', 'b!']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyLast(f)), ['a', 'b', 'c!']) }) }) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index 1338135ba..77d9f2866 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -1,7 +1,7 @@ import * as assert from 'assert' import * as B from '../src/boolean' import * as Eq from '../src/Eq' -import { identity, pipe } from '../src/function' +import { Endomorphism, identity, pipe } from '../src/function' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' @@ -622,18 +622,36 @@ describe('ReadonlyNonEmptyArray', () => { }) it('matchLeft', () => { - U.deepStrictEqual(_.matchLeft((head, tail) => [head, tail])([1, 2, 3]), [1, [2, 3]]) + U.deepStrictEqual( + pipe( + [1, 2, 3], + _.matchLeft((head, tail) => [head, tail]) + ), + [1, [2, 3]] + ) }) it('matchRight', () => { - U.deepStrictEqual(_.matchRight((init, last) => [init, last])([1, 2, 3]), [[1, 2], 3]) + U.deepStrictEqual( + pipe( + [1, 2, 3], + _.matchRight((init, last) => [init, last]) + ), + [[1, 2], 3] + ) }) it('modifyHead', () => { - U.deepStrictEqual(_.modifyHead((x: number) => x + 10)([1, 2, 3]), [11, 2, 3]) + const f: Endomorphism = (s) => s + '!' + U.deepStrictEqual(pipe(['a'], _.modifyHead(f)), ['a!']) + U.deepStrictEqual(pipe(['a', 'b'], _.modifyHead(f)), ['a!', 'b']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyHead(f)), ['a!', 'b', 'c']) }) it('modifyLast', () => { - U.deepStrictEqual(_.modifyLast((x: number) => x + 10)([1, 2, 3]), [1, 2, 13]) + const f: Endomorphism = (s) => s + '!' + U.deepStrictEqual(pipe(['a'], _.modifyLast(f)), ['a!']) + U.deepStrictEqual(pipe(['a', 'b'], _.modifyLast(f)), ['a', 'b!']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyLast(f)), ['a', 'b', 'c!']) }) }) From 438d9354c3e29812101011b66952bab0ecaf2b9a Mon Sep 17 00:00:00 2001 From: cdimitroulas Date: Sat, 27 Mar 2021 18:07:23 +0000 Subject: [PATCH 007/162] Add fromPredicate & fromOption to Array --- docs/modules/Array.ts.md | 23 +++++++++++++++++++++++ src/Array.ts | 18 ++++++++++++++++++ test/Array.ts | 21 ++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 0ec5b1cc6..de9772e9a 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -99,6 +99,8 @@ Added in v2.0.0 - [constructors](#constructors) - [append](#append) - [appendW](#appendw) + - [fromOption](#fromoption) + - [fromPredicate](#frompredicate) - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) @@ -1354,6 +1356,27 @@ export declare const appendW: (end: B) => (init: A[]) => NEA.NonEmptyArray Added in v2.11.0 +## fromOption + +**Signature** + +```ts +export declare const fromOption: (ma: O.Option) => A[] +``` + +Added in v2.11.0 + +## fromPredicate + +**Signature** + +```ts +export declare function fromPredicate(refinement: Refinement): (a: A) => Array +export declare function fromPredicate(predicate: Predicate): (a: A) => Array +``` + +Added in v2.11.0 + ## makeBy Return a `Array` of length `n` with element `i` initialized with `f(i)`. diff --git a/src/Array.ts b/src/Array.ts index f95d490ae..0cb8c95fe 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -129,6 +129,24 @@ export const range = (start: number, end: number): Array => */ export const replicate = (n: number, a: A): Array => makeBy(n, () => a) +/** + * @category constructors + * @since 2.11.0 + */ +export function fromPredicate(refinement: Refinement): (a: A) => Array +export function fromPredicate(predicate: Predicate): (a: A) => Array +export function fromPredicate(predicate: Predicate): (a: A) => Array { + return (a) => (predicate(a) ? [a] : []) +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const fromOption = (ma: Option): Array => { + return O.isSome(ma) ? [ma.value] : [] +} + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- diff --git a/test/Array.ts b/test/Array.ts index 1f5a74f0b..e4abf0c77 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -4,7 +4,7 @@ import * as _ from '../src/Array' import * as B from '../src/boolean' import * as E from '../src/Either' import * as Eq from '../src/Eq' -import { identity, pipe, Predicate, tuple } from '../src/function' +import { identity, pipe, Refinement, Predicate, tuple } from '../src/function' import * as M from '../src/Monoid' import * as N from '../src/number' import * as O from '../src/Option' @@ -1115,4 +1115,23 @@ describe('Array', () => { it('copy', () => { U.deepStrictEqual(pipe([1, 2, 3], _.copy), [1, 2, 3]) }) + + describe('fromPredicate', () => { + it('can create an array from a Refinement', () => { + const refinement: Refinement = (a): a is string => typeof a === 'string' + U.deepStrictEqual(_.fromPredicate(refinement)('hello'), ['hello']) + U.deepStrictEqual(_.fromPredicate(refinement)(null), []) + }) + + it('can create an array from a Predicate', () => { + const predicate = (a: string) => a.length > 0 + U.deepStrictEqual(_.fromPredicate(predicate)('hi'), ['hi']) + U.deepStrictEqual(_.fromPredicate(predicate)(''), []) + }) + }) + + it('fromOption', () => { + U.deepStrictEqual(_.fromOption(O.some('hello')), ['hello']) + U.deepStrictEqual(_.fromOption(O.none), []) + }) }) From 22aea4a8998b0e89c58b61b380eeda6b272cff4b Mon Sep 17 00:00:00 2001 From: gcanti Date: Sun, 28 Mar 2021 19:35:43 +0200 Subject: [PATCH 008/162] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 191625a07..7be3c3ccd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ high state of flux, you're at risk of it changing without notice. - **New Feature** - `Array` - add `prependW`, `appendW` (@thewilkybarkid) + - add `fromOption`, `fromPredicate` (@cdimitroulas) - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - `ReadonlyArray` From c5d5d3c460b231c1d990b1f43c2544188a55331a Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Mon, 29 Mar 2021 18:48:21 +0200 Subject: [PATCH 009/162] Add filterE utils to Witherable --- docs/modules/Witherable.ts.md | 30 ++++++++++++++++++++++++++++++ src/Witherable.ts | 33 ++++++++++++++++++++++++++++++++- test/Witherable.ts | 25 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/Witherable.ts diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index f2934ae74..3e0c7e72f 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -43,6 +43,7 @@ Added in v2.0.0 - [Wither2 (interface)](#wither2-interface) - [Wither2C (interface)](#wither2c-interface) - [Wither3 (interface)](#wither3-interface) + - [filterE](#filtere) --- @@ -691,3 +692,32 @@ export interface Wither3 { ``` Added in v2.0.0 + +## filterE + +**Signature** + +```ts +export declare function filterE( + W: Witherable1 +): { + (F: Applicative3): ( + predicate: (a: A) => Kind3 + ) => (as: Kind) => Kind3> + (F: Applicative3C): ( + predicate: (a: A) => Kind3 + ) => (as: Kind) => Kind3> + (F: Applicative2): ( + predicate: (a: A) => Kind2 + ) => (as: Kind) => Kind2> + (F: Applicative2C): ( + predicate: (a: A) => Kind2 + ) => (ga: Kind) => Kind2> + (F: Applicative1): ( + predicate: (a: A) => Kind + ) => (ga: Kind) => Kind> + (F: Applicative): (predicate: (a: A) => HKT) => (ga: Kind) => HKT> +} +``` + +Added in v2.11.0 diff --git a/src/Witherable.ts b/src/Witherable.ts index 602686c6d..73da0bd4c 100644 --- a/src/Witherable.ts +++ b/src/Witherable.ts @@ -6,7 +6,7 @@ * @since 2.0.0 */ import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' -import { Option } from './Option' +import { none, some, Option } from './Option' import { Traversable, Traversable1, Traversable2, Traversable2C, Traversable3 } from './Traversable' import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3, Applicative3C } from './Applicative' import { Filterable, Filterable1, Filterable2, Filterable2C, Filterable3 } from './Filterable' @@ -546,3 +546,34 @@ export interface PipeableWilt3 { f: (a: A) => HKT> ) => (wa: Kind3) => HKT, Kind3>> } + +/** + * @since 2.11.0 + */ +export function filterE( + W: Witherable1 +): { + (F: Applicative3): ( + predicate: (a: A) => Kind3 + ) => (as: Kind) => Kind3> + (F: Applicative3C): ( + predicate: (a: A) => Kind3 + ) => (as: Kind) => Kind3> + (F: Applicative2): ( + predicate: (a: A) => Kind2 + ) => (as: Kind) => Kind2> + (F: Applicative2C): ( + predicate: (a: A) => Kind2 + ) => (ga: Kind) => Kind2> + (F: Applicative1): ( + predicate: (a: A) => Kind + ) => (ga: Kind) => Kind> + (F: Applicative): (predicate: (a: A) => HKT) => (ga: Kind) => HKT> +} { + return ( + F: Applicative + ): ((predicate: (a: A) => HKT) => (ga: Kind) => HKT>) => { + const witherF = W.wither(F) + return (predicate) => (ga) => witherF(ga, (a) => F.map(predicate(a), (b) => (b ? some(a) : none))) + } +} diff --git a/test/Witherable.ts b/test/Witherable.ts new file mode 100644 index 000000000..28cf31774 --- /dev/null +++ b/test/Witherable.ts @@ -0,0 +1,25 @@ +import * as U from './util' +import * as T from '../src/Task' +import * as RT from '../src/ReaderTask' +import * as _ from '../src/Witherable' +import * as RA from '../src/ReadonlyArray' +import * as RR from '../src/ReadonlyRecord' + +describe('Witherable', () => { + describe('filterE', () => { + it('Applicative1', async () => { + const filterEArray = _.filterE(RA.Witherable)(T.ApplicativePar)((n: number) => T.of(n % 2 === 0)) + U.deepStrictEqual(await filterEArray([1, 2])(), [2]) + + const filterERecord = _.filterE(RR.Witherable)(T.ApplicativePar)((n: number) => T.of(n % 2 === 0)) + U.deepStrictEqual(await filterERecord({ a: 1, b: 2 })(), { b: 2 }) + }) + it('Applicative2', async () => { + const filterEArray = _.filterE(RA.Witherable)(RT.ApplicativePar)((n: number) => RT.of(n % 2 === 0)) + U.deepStrictEqual(await filterEArray([1, 2])({})(), [2]) + + const filterERecord = _.filterE(RR.Witherable)(RT.ApplicativePar)((n: number) => RT.of(n % 2 === 0)) + U.deepStrictEqual(await filterERecord({ a: 1, b: 2 })({})(), { b: 2 }) + }) + }) +}) From 45cacd74e4c0e23657241c94e511f39df7385c2d Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 30 Mar 2021 08:02:30 +0200 Subject: [PATCH 010/162] add `filterE` to ReadonlyArray, ReadonlyRecord --- CHANGELOG.md | 8 +++++ docs/modules/Array.ts.md | 13 +++++++ docs/modules/ReadonlyArray.ts.md | 33 ++++++++++++++++++ docs/modules/ReadonlyRecord.ts.md | 33 ++++++++++++++++++ docs/modules/Record.ts.md | 13 +++++++ docs/modules/Witherable.ts.md | 56 ++++++++++++++++++++----------- src/Array.ts | 11 +++++- src/ReadonlyArray.ts | 28 +++++++++++++++- src/ReadonlyRecord.ts | 28 +++++++++++++++- src/Record.ts | 11 +++++- src/Witherable.ts | 31 +++++++++++------ test/Witherable.ts | 27 ++++++++------- 12 files changed, 245 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7be3c3ccd..8951b6c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,16 +21,24 @@ high state of flux, you're at risk of it changing without notice. - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) + - add `filterE` - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) + - add `filterE` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - `ReadonlyRecord` + - add `filterE` + - `Record` + - add `filterE` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) + - `Witherable` + - add `filterE`, #1458 (@vinassefranche) # 2.10.0-rc.5 diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index de9772e9a..72eed59c2 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -165,6 +165,7 @@ Added in v2.0.0 - [deleteAt](#deleteat) - [elem](#elem) - [every](#every) + - [filterE](#filtere) - [findIndex](#findindex) - [findLastIndex](#findlastindex) - [insertAt](#insertat) @@ -2288,6 +2289,18 @@ export declare const every: (predicate: Predicate) => (as: A[]) => boolean Added in v2.9.0 +## filterE + +Filter values inside a context. + +**Signature** + +```ts +export declare const filterE: FilterE1<'Array'> +``` + +Added in v2.11.0 + ## findIndex Find the first index for which a predicate holds diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index d4b672775..cd45fd109 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -156,6 +156,7 @@ Added in v2.5.0 - [elem](#elem) - [empty](#empty) - [every](#every) + - [filterE](#filtere) - [findFirst](#findfirst) - [findFirstMap](#findfirstmap) - [findIndex](#findindex) @@ -2102,6 +2103,38 @@ assert.deepStrictEqual(pipe([1, 2, -3], every(isPositive)), false) Added in v2.9.0 +## filterE + +Filter values inside a context. + +**Signature** + +```ts +export declare const filterE: FilterE1<'ReadonlyArray'> +``` + +**Example** + +```ts +import { pipe } from 'fp-ts/function' +import * as RA from 'fp-ts/ReadonlyArray' +import * as T from 'fp-ts/Task' + +const filterE = RA.filterE(T.ApplicativePar) +async function test() { + assert.deepStrictEqual( + await pipe( + [-1, 2, 3], + filterE((n) => T.of(n > 0)) + )(), + [2, 3] + ) +} +test() +``` + +Added in v2.11.0 + ## findFirst Find the first element which satisfies a predicate (or a refinement) function diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 5e4dec11b..6d928a612 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -66,6 +66,7 @@ Added in v2.5.0 - [elem](#elem) - [empty](#empty) - [every](#every) + - [filterE](#filtere) - [filterWithIndex](#filterwithindex) - [foldMapWithIndex](#foldmapwithindex) - [fromFoldable](#fromfoldable) @@ -626,6 +627,38 @@ export declare function every(predicate: Predicate): (r: ReadonlyRecord +``` + +**Example** + +```ts +import { pipe } from 'fp-ts/function' +import * as RR from 'fp-ts/ReadonlyRecord' +import * as T from 'fp-ts/Task' + +const filterE = RR.filterE(T.ApplicativePar) +async function test() { + assert.deepStrictEqual( + await pipe( + { a: -1, b: 2, c: 3 }, + filterE((n) => T.of(n > 0)) + )(), + { b: 2, c: 3 } + ) +} +test() +``` + +Added in v2.11.0 + ## filterWithIndex **Signature** diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index dba70f83f..3c1484fd8 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -52,6 +52,7 @@ Added in v2.0.0 - [deleteAt](#deleteat) - [elem](#elem) - [every](#every) + - [filterE](#filtere) - [filterMapWithIndex](#filtermapwithindex) - [filterWithIndex](#filterwithindex) - [foldMapWithIndex](#foldmapwithindex) @@ -483,6 +484,18 @@ export declare const every: (predicate: Predicate) => (r: Record +``` + +Added in v2.11.0 + ## filterMapWithIndex **Signature** diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index 3e0c7e72f..da5642473 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -23,6 +23,7 @@ Added in v2.0.0 - [Witherable2C (interface)](#witherable2c-interface) - [Witherable3 (interface)](#witherable3-interface) - [utils](#utils) + - [FilterE1 (interface)](#filtere1-interface) - [PipeableWilt (interface)](#pipeablewilt-interface) - [PipeableWilt1 (interface)](#pipeablewilt1-interface) - [PipeableWilt2 (interface)](#pipeablewilt2-interface) @@ -123,6 +124,33 @@ Added in v2.0.0 # utils +## FilterE1 (interface) + +**Signature** + +```ts +export interface FilterE1 { + (F: Applicative3): ( + predicate: (a: A) => Kind3 + ) => (ga: Kind) => Kind3> + (F: Applicative3C): ( + predicate: (a: A) => Kind3 + ) => (ga: Kind) => Kind3> + (F: Applicative2): ( + predicate: (a: A) => Kind2 + ) => (ga: Kind) => Kind2> + (F: Applicative2C): ( + predicate: (a: A) => Kind2 + ) => (ga: Kind) => Kind2> + (F: Applicative1): ( + predicate: (a: A) => Kind + ) => (ga: Kind) => Kind> + (F: Applicative): (predicate: (a: A) => HKT) => (ga: Kind) => HKT> +} +``` + +Added in v2.11.0 + ## PipeableWilt (interface) **Signature** @@ -695,29 +723,17 @@ Added in v2.0.0 ## filterE +Filter values inside a `F` context. + +See `ReadonlyArray`'s `filterE` for an example of usage. + **Signature** ```ts -export declare function filterE( - W: Witherable1 -): { - (F: Applicative3): ( - predicate: (a: A) => Kind3 - ) => (as: Kind) => Kind3> - (F: Applicative3C): ( - predicate: (a: A) => Kind3 - ) => (as: Kind) => Kind3> - (F: Applicative2): ( - predicate: (a: A) => Kind2 - ) => (as: Kind) => Kind2> - (F: Applicative2C): ( - predicate: (a: A) => Kind2 - ) => (ga: Kind) => Kind2> - (F: Applicative1): ( - predicate: (a: A) => Kind - ) => (ga: Kind) => Kind> - (F: Applicative): (predicate: (a: A) => HKT) => (ga: Kind) => HKT> -} +export declare function filterE(W: Witherable1): FilterE1 +export declare function filterE( + W: Witherable +): (F: Applicative) => (predicate: (a: A) => HKT) => (ga: HKT) => HKT> ``` Added in v2.11.0 diff --git a/src/Array.ts b/src/Array.ts index 0cb8c95fe..c2512b9ca 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -31,7 +31,7 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' import NonEmptyArray = NEA.NonEmptyArray import Option = O.Option @@ -1994,6 +1994,15 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * Filter values inside a context. + * + * @since 2.11.0 + */ +export const filterE = + /*#__PURE__*/ + filterE_(Witherable) + // ------------------------------------------------------------------------------------- // unsafe // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 14955c19c..bf9ae91fd 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -32,7 +32,7 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' import Option = O.Option import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray @@ -2106,6 +2106,32 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * Filter values inside a context. + * + * @example + * import { pipe } from 'fp-ts/function' + * import * as RA from 'fp-ts/ReadonlyArray' + * import * as T from 'fp-ts/Task' + * + * const filterE = RA.filterE(T.ApplicativePar) + * async function test() { + * assert.deepStrictEqual( + * await pipe( + * [-1, 2, 3], + * filterE((n) => T.of(n > 0)) + * )(), + * [2, 3] + * ) + * } + * test() + * + * @since 2.11.0 + */ +export const filterE = + /*#__PURE__*/ + filterE_(Witherable) + // ------------------------------------------------------------------------------------- // unsafe // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 2734e6243..1d525bd70 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -23,7 +23,7 @@ import { Show } from './Show' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' import Option = O.Option @@ -1223,6 +1223,32 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * Filter values inside a context. + * + * @example + * import { pipe } from 'fp-ts/function' + * import * as RR from 'fp-ts/ReadonlyRecord' + * import * as T from 'fp-ts/Task' + * + * const filterE = RR.filterE(T.ApplicativePar) + * async function test() { + * assert.deepStrictEqual( + * await pipe( + * { a: -1, b: 2, c: 3 }, + * filterE((n) => T.of(n > 0)) + * )(), + * { b: 2, c: 3 } + * ) + * } + * test() + * + * @since 2.11.0 + */ +export const filterE = + /*#__PURE__*/ + filterE_(Witherable) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/Record.ts b/src/Record.ts index 86a52a0ef..b6339737f 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -23,7 +23,7 @@ import { Show } from './Show' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' import Option = O.Option @@ -845,6 +845,15 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * Filter values inside a context. + * + * @since 2.11.0 + */ +export const filterE = + /*#__PURE__*/ + filterE_(Witherable) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/Witherable.ts b/src/Witherable.ts index 73da0bd4c..92df723ba 100644 --- a/src/Witherable.ts +++ b/src/Witherable.ts @@ -550,18 +550,16 @@ export interface PipeableWilt3 { /** * @since 2.11.0 */ -export function filterE( - W: Witherable1 -): { +export interface FilterE1 { (F: Applicative3): ( predicate: (a: A) => Kind3 - ) => (as: Kind) => Kind3> + ) => (ga: Kind) => Kind3> (F: Applicative3C): ( predicate: (a: A) => Kind3 - ) => (as: Kind) => Kind3> + ) => (ga: Kind) => Kind3> (F: Applicative2): ( predicate: (a: A) => Kind2 - ) => (as: Kind) => Kind2> + ) => (ga: Kind) => Kind2> (F: Applicative2C): ( predicate: (a: A) => Kind2 ) => (ga: Kind) => Kind2> @@ -569,10 +567,23 @@ export function filterE( predicate: (a: A) => Kind ) => (ga: Kind) => Kind> (F: Applicative): (predicate: (a: A) => HKT) => (ga: Kind) => HKT> -} { - return ( - F: Applicative - ): ((predicate: (a: A) => HKT) => (ga: Kind) => HKT>) => { +} + +/** + * Filter values inside a `F` context. + * + * See `ReadonlyArray`'s `filterE` for an example of usage. + * + * @since 2.11.0 + */ +export function filterE(W: Witherable1): FilterE1 +export function filterE( + W: Witherable +): (F: Applicative) => (predicate: (a: A) => HKT) => (ga: HKT) => HKT> +export function filterE( + W: Witherable +): (F: Applicative) => (predicate: (a: A) => HKT) => (ga: HKT) => HKT> { + return (F) => { const witherF = W.wither(F) return (predicate) => (ga) => witherF(ga, (a) => F.map(predicate(a), (b) => (b ? some(a) : none))) } diff --git a/test/Witherable.ts b/test/Witherable.ts index 28cf31774..e76d25633 100644 --- a/test/Witherable.ts +++ b/test/Witherable.ts @@ -1,25 +1,26 @@ -import * as U from './util' -import * as T from '../src/Task' +import { pipe } from '../src/function' import * as RT from '../src/ReaderTask' -import * as _ from '../src/Witherable' import * as RA from '../src/ReadonlyArray' import * as RR from '../src/ReadonlyRecord' +import * as T from '../src/Task' +import * as _ from '../src/Witherable' +import * as U from './util' describe('Witherable', () => { describe('filterE', () => { - it('Applicative1', async () => { - const filterEArray = _.filterE(RA.Witherable)(T.ApplicativePar)((n: number) => T.of(n % 2 === 0)) - U.deepStrictEqual(await filterEArray([1, 2])(), [2]) + const filterERA = _.filterE(RA.Witherable) + const filterERR = _.filterE(RR.Witherable) - const filterERecord = _.filterE(RR.Witherable)(T.ApplicativePar)((n: number) => T.of(n % 2 === 0)) - U.deepStrictEqual(await filterERecord({ a: 1, b: 2 })(), { b: 2 }) + it('Applicative1', async () => { + const f = (n: number) => T.of(n % 2 === 0) + U.deepStrictEqual(await pipe([1, 2], filterERA(T.ApplicativePar)(f))(), [2]) + U.deepStrictEqual(await pipe({ a: 1, b: 2 }, filterERR(T.ApplicativePar)(f))(), { b: 2 }) }) - it('Applicative2', async () => { - const filterEArray = _.filterE(RA.Witherable)(RT.ApplicativePar)((n: number) => RT.of(n % 2 === 0)) - U.deepStrictEqual(await filterEArray([1, 2])({})(), [2]) - const filterERecord = _.filterE(RR.Witherable)(RT.ApplicativePar)((n: number) => RT.of(n % 2 === 0)) - U.deepStrictEqual(await filterERecord({ a: 1, b: 2 })({})(), { b: 2 }) + it('Applicative2', async () => { + const f = (n: number) => RT.of(n % 2 === 0) + U.deepStrictEqual(await pipe([1, 2], filterERA(RT.ApplicativePar)(f))({})(), [2]) + U.deepStrictEqual(await pipe({ a: 1, b: 2 }, filterERR(RT.ApplicativePar)(f))({})(), { b: 2 }) }) }) }) From b3db0134114159dd798e167956b9a516ca523f30 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 30 Mar 2021 08:23:05 +0200 Subject: [PATCH 011/162] remove `filterE` from ReadonlyRecord --- CHANGELOG.md | 4 ---- docs/modules/ReadonlyRecord.ts.md | 33 ------------------------------- docs/modules/Record.ts.md | 13 ------------ src/ReadonlyRecord.ts | 28 +------------------------- src/Record.ts | 11 +---------- 5 files changed, 2 insertions(+), 87 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8951b6c55..0844868f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,10 +29,6 @@ high state of flux, you're at risk of it changing without notice. - add `filterE` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - - `ReadonlyRecord` - - add `filterE` - - `Record` - - add `filterE` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - `TaskOption` diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 6d928a612..5e4dec11b 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -66,7 +66,6 @@ Added in v2.5.0 - [elem](#elem) - [empty](#empty) - [every](#every) - - [filterE](#filtere) - [filterWithIndex](#filterwithindex) - [foldMapWithIndex](#foldmapwithindex) - [fromFoldable](#fromfoldable) @@ -627,38 +626,6 @@ export declare function every(predicate: Predicate): (r: ReadonlyRecord -``` - -**Example** - -```ts -import { pipe } from 'fp-ts/function' -import * as RR from 'fp-ts/ReadonlyRecord' -import * as T from 'fp-ts/Task' - -const filterE = RR.filterE(T.ApplicativePar) -async function test() { - assert.deepStrictEqual( - await pipe( - { a: -1, b: 2, c: 3 }, - filterE((n) => T.of(n > 0)) - )(), - { b: 2, c: 3 } - ) -} -test() -``` - -Added in v2.11.0 - ## filterWithIndex **Signature** diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 3c1484fd8..dba70f83f 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -52,7 +52,6 @@ Added in v2.0.0 - [deleteAt](#deleteat) - [elem](#elem) - [every](#every) - - [filterE](#filtere) - [filterMapWithIndex](#filtermapwithindex) - [filterWithIndex](#filterwithindex) - [foldMapWithIndex](#foldmapwithindex) @@ -484,18 +483,6 @@ export declare const every: (predicate: Predicate) => (r: Record -``` - -Added in v2.11.0 - ## filterMapWithIndex **Signature** diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 1d525bd70..2734e6243 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -23,7 +23,7 @@ import { Show } from './Show' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' import Option = O.Option @@ -1223,32 +1223,6 @@ export const Witherable: Witherable1 = { wilt: _wilt } -/** - * Filter values inside a context. - * - * @example - * import { pipe } from 'fp-ts/function' - * import * as RR from 'fp-ts/ReadonlyRecord' - * import * as T from 'fp-ts/Task' - * - * const filterE = RR.filterE(T.ApplicativePar) - * async function test() { - * assert.deepStrictEqual( - * await pipe( - * { a: -1, b: 2, c: 3 }, - * filterE((n) => T.of(n > 0)) - * )(), - * { b: 2, c: 3 } - * ) - * } - * test() - * - * @since 2.11.0 - */ -export const filterE = - /*#__PURE__*/ - filterE_(Witherable) - // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/Record.ts b/src/Record.ts index b6339737f..86a52a0ef 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -23,7 +23,7 @@ import { Show } from './Show' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' +import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' import Option = O.Option @@ -845,15 +845,6 @@ export const Witherable: Witherable1 = { wilt: _wilt } -/** - * Filter values inside a context. - * - * @since 2.11.0 - */ -export const filterE = - /*#__PURE__*/ - filterE_(Witherable) - // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- From cc6e29b6025fc78247f9edaf4346ffd18cfc98a2 Mon Sep 17 00:00:00 2001 From: Nikita Volodin Date: Fri, 13 Nov 2020 16:05:14 -0500 Subject: [PATCH 012/162] A,RA: add depth-first and breadth-first ChainRec --- src/Array.ts | 30 ++++++++++++++++++ src/ReadonlyArray.ts | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/Array.ts b/src/Array.ts index c2512b9ca..9feb663e9 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -6,6 +6,7 @@ import { Alternative1 } from './Alternative' import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import { ChainRec1 } from './ChainRec' import { Compactable1 } from './Compactable' import { Either } from './Either' import { Eq } from './Eq' @@ -1273,6 +1274,10 @@ const _wilt: Witherable1['wilt'] = ( const wiltF = wilt(F) return (fa, f) => pipe(fa, wiltF(f)) } +/* istanbul ignore next */ +const _chainRecDepthFirst: ChainRec1['chainRec'] = RA.ChainRecDepthFirst.chainRec as any +/* istanbul ignore next */ +const _chainRecBreadthFirst: ChainRec1['chainRec'] = RA.ChainRecBreadthFirst.chainRec as any // ------------------------------------------------------------------------------------- // type class members @@ -1994,6 +1999,31 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * Exposing depth first recursion by default + * @category instances + * @since 2.10.0 + */ +export const ChainRecDepthFirst: ChainRec1 = { + URI, + map: _map, + ap: _ap, + chain: _chain, + chainRec: _chainRecDepthFirst +} + +/** + * @category instances + * @since 2.10.0 + */ +export const ChainRecBreadthFirst: ChainRec1 = { + URI, + map: _map, + ap: _ap, + chain: _chain, + chainRec: _chainRecBreadthFirst +} + /** * Filter values inside a context. * diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index bf9ae91fd..ed11a9f3c 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -6,6 +6,7 @@ import { Alternative1 } from './Alternative' import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import { ChainRec1 } from './ChainRec' import { Compactable1 } from './Compactable' import { Either } from './Either' import { Eq, fromEquals } from './Eq' @@ -1337,6 +1338,54 @@ const _wilt: Witherable1['wilt'] = ( const wiltF = wilt(F) return (fa, f) => pipe(fa, wiltF(f)) } +const _chainRecDepthFirst: ChainRec1['chainRec'] = ( + a: A, + f: (a: A) => ReadonlyArray> +): ReadonlyArray => { + // tslint:disable-next-line: readonly-array + const todo: Array> = [...f(a)] + // tslint:disable-next-line: readonly-array + const result: Array = [] + + while (todo.length > 0) { + const e = todo.shift()! + if (e._tag === 'Left') { + todo.unshift(...f(e.left)) + } else { + result.push(e.right) + } + } + + return result +} +const _chainRecBreadthFirst: ChainRec1['chainRec'] = ( + a: A, + f: (a: A) => ReadonlyArray> +): ReadonlyArray => { + const initial = f(a) + // tslint:disable-next-line: readonly-array + const todo: Array> = [] + // tslint:disable-next-line: readonly-array + const result: Array = [] + + function go(e: Either): void { + if (e._tag === 'Left') { + f(e.left).forEach((v) => todo.push(v)) + } else { + result.push(e.right) + } + } + + for (const e of initial) { + go(e) + } + + while (todo.length > 0) { + go(todo.shift()!) + } + + return result +} // ------------------------------------------------------------------------------------- // type class members @@ -2084,6 +2133,31 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex } +/** + * Exposing depth first recursion by default + * @category instances + * @since 2.10.0 + */ +export const ChainRecDepthFirst: ChainRec1 = { + URI, + map: _map, + ap: _ap, + chain: _chain, + chainRec: _chainRecDepthFirst +} + +/** + * @category instances + * @since 2.10.0 + */ +export const ChainRecBreadthFirst: ChainRec1 = { + URI, + map: _map, + ap: _ap, + chain: _chain, + chainRec: _chainRecBreadthFirst +} + /** * @category instances * @since 2.7.0 From 3a9309beeda5ca6e8e6ebc4b856865fece64422a Mon Sep 17 00:00:00 2001 From: Nikita Volodin Date: Fri, 13 Nov 2020 16:37:26 -0500 Subject: [PATCH 013/162] RA: add depth-first and bread-firth chainRec tests --- test/ReadonlyArray.ts | 131 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index b2ee1feba..4b4a576de 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1220,4 +1220,135 @@ describe('ReadonlyArray', () => { U.deepStrictEqual(_.size([]), 0) U.deepStrictEqual(_.size(['a']), 1) }) + + describe('chainRec', () => { + it('depth-first', () => { + const chainRec = _.ChainRecDepthFirst.chainRec + assert.deepStrictEqual( + chainRec(1, () => []), + [] + ) + assert.deepStrictEqual( + chainRec(1, () => [E.right('foo')]), + ['foo'] + ) + assert.deepStrictEqual( + chainRec(1, (a) => { + if (a < 5) { + return [E.right(a), E.left(a + 1)] + } else { + return [E.right(a)] + } + }), + [1, 2, 3, 4, 5] + ) + assert.deepStrictEqual( + chainRec(1, (a) => { + if (a < 5) { + return [E.left(a + 1), E.right(a)] + } else { + return [E.right(a)] + } + }), + [5, 4, 3, 2, 1] + ) + assert.deepStrictEqual( + chainRec(1, (a) => { + if (a < 5) { + return a % 2 === 0 ? [E.right(a), E.left(a + 1)] : [E.left(a + 1), E.right(a)] + } else { + return [E.right(a)] + } + }), + [2, 4, 5, 3, 1] + ) + assert.deepStrictEqual( + chainRec(0, (a) => { + if (a === 0) { + return [E.right(a), E.left(a - 1), E.left(a + 1)] + } else if (0 < a && a < 5) { + return [E.right(a), E.left(a + 1)] + } else if (-5 < a && a < 0) { + return [E.right(a), E.left(a - 1)] + } else { + return [E.right(a)] + } + }), + [0, -1, -2, -3, -4, -5, 1, 2, 3, 4, 5] + ) + assert.deepStrictEqual( + chainRec(0, (a) => { + if (a === 0) { + return [E.left(a - 1), E.right(a), E.left(a + 1)] + } else if (0 < a && a < 5) { + return [E.right(a), E.left(a + 1)] + } else if (-5 < a && a < 0) { + return [E.left(a - 1), E.right(a)] + } else { + return [E.right(a)] + } + }), + [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] + ) + }) + it('breadth-first', () => { + const chainRec = _.ChainRecBreadthFirst.chainRec + assert.deepStrictEqual( + chainRec(1, () => []), + [] + ) + assert.deepStrictEqual( + chainRec(1, () => [E.right('foo')]), + ['foo'] + ) + assert.deepStrictEqual( + chainRec(1, (a) => { + if (a < 5) { + return [E.right(a), E.left(a + 1)] + } else { + return [E.right(a)] + } + }), + [1, 2, 3, 4, 5] + ) + assert.deepStrictEqual( + chainRec(1, (a) => { + if (a < 5) { + return [E.left(a + 1), E.right(a)] + } else { + return [E.right(a)] + } + }), + [1, 2, 3, 4, 5] + ) + assert.deepStrictEqual( + chainRec(0, (a) => { + if (a === 0) { + return [E.right(a), E.left(a - 1), E.left(a + 1)] + } else if (0 < a && a < 5) { + return [E.right(a), E.left(a + 1)] + } else if (-5 < a && a < 0) { + return [E.right(a), E.left(a - 1)] + } else { + return [E.right(a)] + } + }), + [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5] + ) + assert.deepStrictEqual( + chainRec(0, (a) => { + if (a === 0) { + return [E.left(a - 1), E.right(a), E.left(a + 1)] + } else if (0 < a && a < 5) { + return [E.right(a), E.left(a + 1)] + } else if (-5 < a && a < 0) { + return [E.left(a - 1), E.right(a)] + } else { + return [E.right(a)] + } + }), + [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5] + ) + }) + }) }) From a9c3d8b7574f723c59fc339d5f478af95d9c329c Mon Sep 17 00:00:00 2001 From: cdimitroulas Date: Tue, 30 Mar 2021 16:34:38 +0100 Subject: [PATCH 014/162] Backport SK from 3.0.0 --- docs/modules/function.ts.md | 11 +++++++++++ src/ReadonlyMap.ts | 4 ++-- src/ReadonlyNonEmptyArray.ts | 4 ++-- src/ReadonlyRecord.ts | 4 ++-- src/function.ts | 5 +++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/modules/function.ts.md b/docs/modules/function.ts.md index 90672c5f7..914ca72f6 100644 --- a/docs/modules/function.ts.md +++ b/docs/modules/function.ts.md @@ -25,6 +25,7 @@ Added in v2.0.0 - [Lazy (interface)](#lazy-interface) - [Predicate (interface)](#predicate-interface) - [Refinement (interface)](#refinement-interface) + - [SK](#sk) - [absurd](#absurd) - [constFalse](#constfalse) - [constNull](#constnull) @@ -227,6 +228,16 @@ export interface Refinement { Added in v2.0.0 +## SK + +**Signature** + +```ts +export declare const SK: (_: A, b: B) => B +``` + +Added in v2.11.0 + ## absurd **Signature** diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 554f2d0a9..178b28815 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -9,7 +9,7 @@ import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' import { Foldable, Foldable1, Foldable2, Foldable2C, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' -import { pipe, Predicate, Refinement } from './function' +import { pipe, Predicate, Refinement, SK } from './function' import { flap as flap_, Functor2 } from './Functor' import { FunctorWithIndex2C } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -937,7 +937,7 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex2C(F: Applicative): ((ta: ReadonlyMap>) => HKT>) => { const traverseWithIndexF = traverseWithIndex(F) - return (ta) => traverseWithIndexF(ta, (_, a) => a) + return (ta) => traverseWithIndexF(ta, SK) } return { URI, diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 091cc9b5f..73600abb1 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -21,7 +21,7 @@ import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -749,7 +749,7 @@ export const traverse: PipeableTraverse1 = ( */ export const sequence: Traversable1['sequence'] = ( F: ApplicativeHKT -): ((as: ReadonlyNonEmptyArray>) => HKT>) => traverseWithIndex(F)((_, a) => a) +): ((as: ReadonlyNonEmptyArray>) => HKT>) => traverseWithIndex(F)(SK) /** * @category TraversableWithIndex diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 2734e6243..87417eb1b 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, pipe, Predicate, Refinement } from './function' +import { identity, pipe, Predicate, Refinement, SK } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -508,7 +508,7 @@ export function sequence( export function sequence( F: Applicative ): (ta: ReadonlyRecord>) => HKT> { - return traverseWithIndex(F)((_, a) => a) + return traverseWithIndex(F)(SK) } /** diff --git a/src/function.ts b/src/function.ts index 5e114f355..05d68ff56 100644 --- a/src/function.ts +++ b/src/function.ts @@ -748,3 +748,8 @@ export function pipe( * @since 2.7.0 */ export const hole: () => T = absurd as any + +/** + * @since 2.11.0 + */ +export const SK = (_: A, b: B): B => b From d6ccecbe295076f0b0824834706614d57e50d7b8 Mon Sep 17 00:00:00 2001 From: Anthony Gabriele Date: Wed, 31 Mar 2021 01:11:00 -0400 Subject: [PATCH 015/162] backport ReadonlyRecord & Record --- docs/modules/ReadonlyRecord.ts.md | 297 +++++++++++++--- docs/modules/Record.ts.md | 190 +++++++--- dtslint/ts3.5/ReadonlyRecord.ts | 14 + dtslint/ts3.5/Record.ts | 14 + src/ReadonlyRecord.ts | 568 ++++++++++++++++++++++++++---- src/Record.ts | 381 +++++++++++++++++--- test/ReadonlyRecord.ts | 174 ++++++++- test/Record.ts | 115 +++++- 8 files changed, 1511 insertions(+), 242 deletions(-) diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 5e4dec11b..c7a4be8d1 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -43,18 +43,27 @@ Added in v2.5.0 - [Compactable](#compactable-1) - [Filterable](#filterable-1) - [FilterableWithIndex](#filterablewithindex) - - [Foldable](#foldable-1) - - [FoldableWithIndex](#foldablewithindex) - [Functor](#functor) - [FunctorWithIndex](#functorwithindex) - - [Traversable](#traversable) - - [TraversableWithIndex](#traversablewithindex) - [URI](#uri) - [URI (type alias)](#uri-type-alias) - - [Witherable](#witherable-1) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) + - [getFoldable](#getfoldable) + - [getFoldableWithIndex](#getfoldablewithindex) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getShow](#getshow) + - [getTraversable](#gettraversable) + - [getTraversableWithIndex](#gettraversablewithindex) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) + - [getWitherable](#getwitherable) + - [~~FoldableWithIndex~~](#foldablewithindex) + - [~~Foldable~~](#foldable) + - [~~TraversableWithIndex~~](#traversablewithindex) + - [~~Traversable~~](#traversable) + - [~~Witherable~~](#witherable) - [~~readonlyRecord~~](#readonlyrecord) - [interop](#interop) - [fromRecord](#fromrecord) @@ -63,6 +72,7 @@ Added in v2.5.0 - [ReadonlyRecord (type alias)](#readonlyrecord-type-alias) - [utils](#utils) - [collect](#collect) + - [difference](#difference) - [elem](#elem) - [empty](#empty) - [every](#every) @@ -71,6 +81,7 @@ Added in v2.5.0 - [fromFoldable](#fromfoldable) - [fromFoldableMap](#fromfoldablemap) - [has](#has) + - [intersection](#intersection) - [isEmpty](#isempty) - [isSubrecord](#issubrecord) - [keys](#keys) @@ -87,6 +98,7 @@ Added in v2.5.0 - [toReadonlyArray](#toreadonlyarray) - [traverse](#traverse) - [traverseWithIndex](#traversewithindex) + - [union](#union) - [updateAt](#updateat) - [~~hasOwnProperty (function)~~](#hasownproperty-function) @@ -179,7 +191,10 @@ Added in v2.5.0 **Signature** ```ts -export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M +export declare function foldMap( + O: Ord +): (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M +export declare function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Readonly>) => M ``` Added in v2.5.0 @@ -189,7 +204,10 @@ Added in v2.5.0 **Signature** ```ts -export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B +export declare function reduce( + O: Ord +): (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B +export declare function reduce(b: B, f: (b: B, a: A) => B): (fa: Readonly>) => B ``` Added in v2.5.0 @@ -199,10 +217,13 @@ Added in v2.5.0 **Signature** ```ts -export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B +export declare function reduceRight( + O: Ord +): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B +export declare function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B ``` -Added in v2.5.0 +Added in v2.11.0 # Witherable @@ -381,105 +402,95 @@ export declare const FilterableWithIndex: FilterableWithIndex1<'ReadonlyRecord', Added in v2.7.0 -## Foldable - -**Signature** - -```ts -export declare const Foldable: Foldable1<'ReadonlyRecord'> -``` - -Added in v2.7.0 - -## FoldableWithIndex +## Functor **Signature** ```ts -export declare const FoldableWithIndex: FoldableWithIndex1<'ReadonlyRecord', string> +export declare const Functor: Functor1<'ReadonlyRecord'> ``` Added in v2.7.0 -## Functor +## FunctorWithIndex **Signature** ```ts -export declare const Functor: Functor1<'ReadonlyRecord'> +export declare const FunctorWithIndex: FunctorWithIndex1<'ReadonlyRecord', string> ``` Added in v2.7.0 -## FunctorWithIndex +## URI **Signature** ```ts -export declare const FunctorWithIndex: FunctorWithIndex1<'ReadonlyRecord', string> +export declare const URI: 'ReadonlyRecord' ``` -Added in v2.7.0 +Added in v2.5.0 -## Traversable +## URI (type alias) **Signature** ```ts -export declare const Traversable: Traversable1<'ReadonlyRecord'> +export type URI = typeof URI ``` -Added in v2.7.0 +Added in v2.5.0 -## TraversableWithIndex +## getDifferenceMagma **Signature** ```ts -export declare const TraversableWithIndex: TraversableWithIndex1<'ReadonlyRecord', string> +export declare const getDifferenceMagma: () => Magma>> ``` -Added in v2.7.0 +Added in v2.11.0 -## URI +## getEq **Signature** ```ts -export declare const URI: 'ReadonlyRecord' +export declare function getEq(E: Eq): Eq> ``` Added in v2.5.0 -## URI (type alias) +## getFoldable **Signature** ```ts -export type URI = typeof URI +export declare const getFoldable: (O: Ord) => Foldable1 ``` -Added in v2.5.0 +Added in v2.7.0 -## Witherable +## getFoldableWithIndex **Signature** ```ts -export declare const Witherable: Witherable1<'ReadonlyRecord'> +export declare const getFoldableWithIndex: (O: Ord) => FoldableWithIndex1 ``` -Added in v2.7.0 +Added in v2.11.0 -## getEq +## getIntersectionSemigroup **Signature** ```ts -export declare function getEq(E: Eq): Eq> +export declare const getIntersectionSemigroup: (S: Semigroup) => Semigroup>> ``` -Added in v2.5.0 +Added in v2.11.0 ## getMonoid @@ -508,11 +519,122 @@ Added in v2.5.0 **Signature** ```ts +export declare function getShow(O: Ord): (S: Show) => Show> export declare function getShow(S: Show): Show> ``` Added in v2.5.0 +## getTraversable + +**Signature** + +```ts +export declare const getTraversable: (O: Ord) => Traversable1 +``` + +Added in v2.11.0 + +## getTraversableWithIndex + +**Signature** + +```ts +export declare const getTraversableWithIndex: (O: Ord) => TraversableWithIndex1 +``` + +Added in v2.11.0 + +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (S: Semigroup) => Monoid>> +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (S: Semigroup) => Semigroup>> +``` + +Added in v2.11.0 + +## getWitherable + +**Signature** + +```ts +export declare const getWitherable: (O: Ord) => Witherable1 +``` + +Added in v2.11.0 + +## ~~FoldableWithIndex~~ + +Use `getFoldableWithIndex` instead + +**Signature** + +```ts +export declare const FoldableWithIndex: FoldableWithIndex1<'ReadonlyRecord', string> +``` + +Added in v2.7.0 + +## ~~Foldable~~ + +Use `getFoldable` instead + +**Signature** + +```ts +export declare const Foldable: Foldable1<'ReadonlyRecord'> +``` + +Added in v2.7.0 + +## ~~TraversableWithIndex~~ + +Use `getTraversableWithIndex` instead + +**Signature** + +```ts +export declare const TraversableWithIndex: TraversableWithIndex1<'ReadonlyRecord', string> +``` + +Added in v2.7.0 + +## ~~Traversable~~ + +Use `getTraversable` instead + +**Signature** + +```ts +export declare const Traversable: Traversable1<'ReadonlyRecord'> +``` + +Added in v2.7.0 + +## ~~Witherable~~ + +Use `getWitherable` instead + +**Signature** + +```ts +export declare const Witherable: Witherable1<'ReadonlyRecord'> +``` + +Added in v2.7.0 + ## ~~readonlyRecord~~ Use small, specific instances instead. @@ -572,24 +694,40 @@ Map a `ReadonlyRecord` into an `ReadonlyArray`. **Signature** ```ts -export declare const collect: ( +export declare function collect( + O: Ord +): (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => ReadonlyArray +export declare function collect( f: (k: K, a: A) => B -) => (r: Readonly>) => readonly B[] +): (r: ReadonlyRecord) => ReadonlyArray ``` **Example** ```ts import { collect } from 'fp-ts/ReadonlyRecord' +import { Ord } from 'fp-ts/string' const x: { readonly a: string; readonly b: boolean } = { a: 'c', b: false } -assert.deepStrictEqual(collect((key, val) => ({ key: key, value: val }))(x), [ +assert.deepStrictEqual(collect(Ord)((key, val) => ({ key: key, value: val }))(x), [ { key: 'a', value: 'c' }, { key: 'b', value: false }, ]) ``` -Added in v2.5.0 +Added in v2.11.0 + +## difference + +**Signature** + +```ts +export declare const difference: ( + second: Readonly> +) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 ## elem @@ -646,12 +784,21 @@ Added in v2.5.0 **Signature** ```ts +export declare function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M +export declare function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M export declare function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M +export declare function foldMapWithIndex( + M: Monoid +): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M ``` -Added in v2.5.0 +Added in v2.11.0 ## fromFoldable @@ -760,6 +907,18 @@ export declare const has: (k: string, r: Readonly( + M: Magma +) => (second: Readonly>) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 + ## isEmpty Test whether a `ReadonlyRecord` is empty. @@ -873,26 +1032,52 @@ Added in v2.5.0 **Signature** ```ts +export declare function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: K, a: A, b: B) => B) => (fa: ReadonlyRecord) => B +export declare function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B export declare function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B ): (fa: ReadonlyRecord) => B +export declare function reduceRightWithIndex( + b: B, + f: (k: string, a: A, b: B) => B +): (fa: ReadonlyRecord) => B ``` -Added in v2.5.0 +Added in v2.11.0 ## reduceWithIndex **Signature** ```ts +export declare function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export declare function reduceWithIndex( + O: Ord +): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B export declare function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B +export declare function reduceWithIndex( + b: B, + f: (k: string, b: B, a: A) => B +): (fa: ReadonlyRecord) => B +export declare function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export declare function reduceWithIndex( + O: Ord +): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B ``` -Added in v2.5.0 +Added in v2.11.0 ## sequence @@ -1027,6 +1212,18 @@ export declare function traverseWithIndex( Added in v2.5.0 +## union + +**Signature** + +```ts +export declare const union: ( + M: Magma +) => (second: Readonly>) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 + ## updateAt **Signature** diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index dba70f83f..c613e13d3 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -34,18 +34,23 @@ Added in v2.0.0 - [Compactable](#compactable-1) - [Filterable](#filterable-1) - [FilterableWithIndex](#filterablewithindex) - - [Foldable](#foldable-1) - - [FoldableWithIndex](#foldablewithindex) - [Functor](#functor) - [FunctorWithIndex](#functorwithindex) - - [Traversable](#traversable) - - [TraversableWithIndex](#traversablewithindex) - [URI](#uri) - [URI (type alias)](#uri-type-alias) - - [Witherable](#witherable-1) - [getEq](#geteq) + - [getFoldable](#getfoldable) + - [getFoldableWithIndex](#getfoldablewithindex) - [getMonoid](#getmonoid) - [getShow](#getshow) + - [getTraversable](#gettraversable) + - [getTraversableWithIndex](#gettraversablewithindex) + - [getWitherable](#getwitherable) + - [~~FoldableWithIndex~~](#foldablewithindex) + - [~~Foldable~~](#foldable) + - [~~TraversableWithIndex~~](#traversablewithindex) + - [~~Traversable~~](#traversable) + - [~~Witherable~~](#witherable) - [~~record~~](#record) - [utils](#utils) - [collect](#collect) @@ -168,7 +173,10 @@ Added in v2.0.0 **Signature** ```ts -export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M +export declare function foldMap( + O: Ord +): (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M +export declare function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M ``` Added in v2.0.0 @@ -178,7 +186,8 @@ Added in v2.0.0 **Signature** ```ts -export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Record) => B +export declare function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Record) => B +export declare function reduce(b: B, f: (b: B, a: A) => B): (fa: Record) => B ``` Added in v2.0.0 @@ -188,7 +197,8 @@ Added in v2.0.0 **Signature** ```ts -export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Record) => B +export declare function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) => (fa: Record) => B +export declare function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record) => B ``` Added in v2.0.0 @@ -273,137 +283,198 @@ export declare const FilterableWithIndex: FilterableWithIndex1<'Record', string> Added in v2.7.0 -## Foldable +## Functor **Signature** ```ts -export declare const Foldable: Foldable1<'Record'> +export declare const Functor: Functor1<'Record'> ``` Added in v2.7.0 -## FoldableWithIndex +## FunctorWithIndex **Signature** ```ts -export declare const FoldableWithIndex: FoldableWithIndex1<'Record', string> +export declare const FunctorWithIndex: FunctorWithIndex1<'Record', string> ``` Added in v2.7.0 -## Functor +## URI **Signature** ```ts -export declare const Functor: Functor1<'Record'> +export declare const URI: 'Record' ``` -Added in v2.7.0 +Added in v2.0.0 -## FunctorWithIndex +## URI (type alias) **Signature** ```ts -export declare const FunctorWithIndex: FunctorWithIndex1<'Record', string> +export type URI = typeof URI ``` -Added in v2.7.0 +Added in v2.0.0 -## Traversable +## getEq **Signature** ```ts -export declare const Traversable: Traversable1<'Record'> +export declare const getEq: (E: Eq) => Eq> +``` + +Added in v2.0.0 + +## getFoldable + +**Signature** + +```ts +export declare const getFoldable: (O: Ord) => Foldable1 ``` Added in v2.7.0 -## TraversableWithIndex +## getFoldableWithIndex **Signature** ```ts -export declare const TraversableWithIndex: TraversableWithIndex1<'Record', string> +export declare const getFoldableWithIndex: (O: Ord) => FoldableWithIndex1 ``` Added in v2.7.0 -## URI +## getMonoid + +Returns a `Monoid` instance for `Record`s given a `Semigroup` instance for their values. **Signature** ```ts -export declare const URI: 'Record' +export declare const getMonoid: (S: Semigroup) => Monoid> +``` + +**Example** + +```ts +import { SemigroupSum } from 'fp-ts/number' +import { getMonoid } from 'fp-ts/Record' + +const M = getMonoid(SemigroupSum) +assert.deepStrictEqual(M.concat({ foo: 123 }, { foo: 456 }), { foo: 579 }) ``` Added in v2.0.0 -## URI (type alias) +## getShow **Signature** ```ts -export type URI = typeof URI +export declare function getShow(O: Ord): (S: Show) => Show> +export declare function getShow(S: Show): Show> ``` Added in v2.0.0 -## Witherable +## getTraversable **Signature** ```ts -export declare const Witherable: Witherable1<'Record'> +export declare const getTraversable: (O: Ord) => Traversable1 +``` + +Added in v2.11.0 + +## getTraversableWithIndex + +**Signature** + +```ts +export declare const getTraversableWithIndex: (O: Ord) => TraversableWithIndex1 ``` Added in v2.7.0 -## getEq +## getWitherable **Signature** ```ts -export declare const getEq: (E: Eq) => Eq> +export declare const getWitherable: (O: Ord) => Witherable1 ``` -Added in v2.0.0 +Added in v2.7.0 -## getMonoid +## ~~FoldableWithIndex~~ -Returns a `Monoid` instance for `Record`s given a `Semigroup` instance for their values. +Use `getFoldableWithIndex` instead **Signature** ```ts -export declare const getMonoid: (S: Semigroup) => Monoid> +export declare const FoldableWithIndex: FoldableWithIndex1<'Record', string> ``` -**Example** +Added in v2.7.0 + +## ~~Foldable~~ + +Use `getFoldable` instead + +**Signature** ```ts -import { SemigroupSum } from 'fp-ts/number' -import { getMonoid } from 'fp-ts/Record' +export declare const Foldable: Foldable1<'Record'> +``` -const M = getMonoid(SemigroupSum) -assert.deepStrictEqual(M.concat({ foo: 123 }, { foo: 456 }), { foo: 579 }) +Added in v2.7.0 + +## ~~TraversableWithIndex~~ + +Use the `getTraversableWithIndex` instead + +**Signature** + +```ts +export declare const TraversableWithIndex: TraversableWithIndex1<'Record', string> ``` -Added in v2.0.0 +Added in v2.7.0 -## getShow +## ~~Traversable~~ + +Use `getTraversable` instead **Signature** ```ts -export declare const getShow: (S: Show) => Show> +export declare const Traversable: Traversable1<'Record'> ``` -Added in v2.0.0 +Added in v2.7.0 + +## ~~Witherable~~ + +Use `getWitherable` instead + +**Signature** + +```ts +export declare const Witherable: Witherable1<'Record'> +``` + +Added in v2.7.0 ## ~~record~~ @@ -430,16 +501,20 @@ Map a `Record` into an `Array`. **Signature** ```ts -export declare const collect: (f: (k: K, a: A) => B) => (r: Record) => B[] +export declare function collect( + O: Ord +): (f: (k: K, a: A) => B) => (r: Record) => Array +export declare function collect(f: (k: K, a: A) => B): (r: Record) => Array ``` **Example** ```ts import { collect } from 'fp-ts/Record' +import { Ord } from 'fp-ts/string' const x: { readonly a: string; readonly b: boolean } = { a: 'c', b: false } -assert.deepStrictEqual(collect((key, val) => ({ key: key, value: val }))(x), [ +assert.deepStrictEqual(collect(Ord)((key, val) => ({ key: key, value: val }))(x), [ { key: 'a', value: 'c' }, { key: 'b', value: false }, ]) @@ -515,9 +590,12 @@ Added in v2.0.0 **Signature** ```ts -export declare const foldMapWithIndex: ( +export declare function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: Record) => M +export declare function foldMapWithIndex( M: Monoid -) => (f: (k: K, a: A) => M) => (fa: Record) => M +): (f: (k: K, a: A) => M) => (fa: Record) => M ``` Added in v2.0.0 @@ -764,26 +842,32 @@ Added in v2.0.0 **Signature** ```ts -export declare const reduceRightWithIndex: ( +export declare function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: K, a: A, b: B) => B) => (fa: Record) => B +export declare function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B -) => (fa: Record) => B +): (fa: Record) => B ``` -Added in v2.0.0 +Added in v2.11.0 ## reduceWithIndex **Signature** ```ts -export declare const reduceWithIndex: ( +export declare function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: Record) => B +export declare function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B -) => (fa: Record) => B +): (fa: Record) => B ``` -Added in v2.0.0 +Added in v2.11.0 ## sequence diff --git a/dtslint/ts3.5/ReadonlyRecord.ts b/dtslint/ts3.5/ReadonlyRecord.ts index 86be63e9a..0ba2ab16a 100644 --- a/dtslint/ts3.5/ReadonlyRecord.ts +++ b/dtslint/ts3.5/ReadonlyRecord.ts @@ -66,6 +66,11 @@ _.collect((_k: 'a', n: number) => n)(l1) // $ExpectType readonly number[] _.collect((_k, n: number) => n)(d1) // $ExpectType readonly number[] _.collect((_k: 'a' | 'b', n: number) => n)(r1) // $ExpectType readonly number[] +_.collect(S.Ord)((_k: 'a', n: number) => n)({ a: 1 }) // $ExpectType readonly number[] +_.collect(S.Ord)((_k: 'a', n: number) => n)(l1) // $ExpectType readonly number[] +_.collect(S.Ord)((_k, n: number) => n)(d1) // $ExpectType readonly number[] +_.collect(S.Ord)((_k: 'a' | 'b', n: number) => n)(r1) // $ExpectType readonly number[] + // // insertAt // @@ -108,12 +113,21 @@ _.map((n: number) => n > 2)(r1) // $ExpectType Readonly k)(d1) // $ExpectType string +_.reduceWithIndex(S.Ord)('', (k: 'a' | 'b', _n) => k)(r1) // $ExpectType string + _.reduceWithIndex('', (k: string, _n) => k)(d1) // $ExpectType string _.reduceWithIndex('', (k: 'a' | 'b', _n) => k)(r1) // $ExpectType string +_.foldMapWithIndex(S.Ord)(S.Monoid)((k: string, _n) => k)(d1) // $ExpectType string +_.foldMapWithIndex(S.Ord)(S.Monoid)((k: 'a' | 'b', _n) => k)(r1) // $ExpectType string + _.foldMapWithIndex(S.Monoid)((k: string, _n) => k)(d1) // $ExpectType string _.foldMapWithIndex(S.Monoid)((k: 'a' | 'b', _n) => k)(r1) // $ExpectType string +_.reduceRightWithIndex(S.Ord)('', (k: string, _n, _b) => k)(d1) // $ExpectType string +_.reduceRightWithIndex(S.Ord)('', (k: 'a' | 'b', _n, _b) => k)(r1) // $ExpectType string + _.reduceRightWithIndex('', (k: string, _n, _b) => k)(d1) // $ExpectType string _.reduceRightWithIndex('', (k: 'a' | 'b', _n, _b) => k)(r1) // $ExpectType string diff --git a/dtslint/ts3.5/Record.ts b/dtslint/ts3.5/Record.ts index d3d33537c..3c5f50c80 100644 --- a/dtslint/ts3.5/Record.ts +++ b/dtslint/ts3.5/Record.ts @@ -66,6 +66,11 @@ _.collect((_k: 'a', n: number) => n)(l1) // $ExpectType number[] _.collect((_k, n: number) => n)(d1) // $ExpectType number[] _.collect((_k: 'a' | 'b', n: number) => n)(r1) // $ExpectType number[] +_.collect(S.Ord)((_k: 'a', n: number) => n)({ a: 1 }) // $ExpectType number[] +_.collect(S.Ord)((_k: 'a', n: number) => n)(l1) // $ExpectType number[] +_.collect(S.Ord)((_k, n: number) => n)(d1) // $ExpectType number[] +_.collect(S.Ord)((_k: 'a' | 'b', n: number) => n)(r1) // $ExpectType number[] + // // toArray // @@ -117,12 +122,21 @@ _.map((n: number) => n > 2)(r1) // $ExpectType Record<"a" | "b", boolean> // reduceWithIndex // +_.reduceWithIndex(S.Ord)('', (k: string, _n) => k)(d1) // $ExpectType string +_.reduceWithIndex(S.Ord)('', (k: 'a' | 'b', _n) => k)(r1) // $ExpectType string + _.reduceWithIndex('', (k: string, _n) => k)(d1) // $ExpectType string _.reduceWithIndex('', (k: 'a' | 'b', _n) => k)(r1) // $ExpectType string +_.foldMapWithIndex(S.Ord)(S.Monoid)((k: string, _n) => k)(d1) // $ExpectType string +_.foldMapWithIndex(S.Ord)(S.Monoid)((k: 'a' | 'b', _n) => k)(r1) // $ExpectType string + _.foldMapWithIndex(S.Monoid)((k: string, _n) => k)(d1) // $ExpectType string _.foldMapWithIndex(S.Monoid)((k: 'a' | 'b', _n) => k)(r1) // $ExpectType string +_.reduceRightWithIndex(S.Ord)('', (k: string, _n, _b) => k)(d1) // $ExpectType string +_.reduceRightWithIndex(S.Ord)('', (k: 'a' | 'b', _n, _b) => k)(r1) // $ExpectType string + _.reduceRightWithIndex('', (k: string, _n, _b) => k)(d1) // $ExpectType string _.reduceRightWithIndex('', (k: 'a' | 'b', _n, _b) => k)(r1) // $ExpectType string diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 87417eb1b..d893b33df 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -17,9 +17,11 @@ import * as _ from './internal' import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' +import { Ord } from './Ord' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' +import { Ord as OrdString } from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' @@ -84,28 +86,61 @@ export const isEmpty = (r: ReadonlyRecord): boolean => { export const keys = (r: ReadonlyRecord): ReadonlyArray => (Object.keys(r) as any).sort() +/** + * + * @internal + * @since 2.11.0 + */ +const keysWithOrd = (O: Ord) => (r: ReadonlyRecord): ReadonlyArray => + (Object.keys(r) as any).sort(O.compare) + /** * Map a `ReadonlyRecord` into an `ReadonlyArray`. * * @example * import { collect } from 'fp-ts/ReadonlyRecord' + * import { Ord } from 'fp-ts/string' * * const x: { readonly a: string, readonly b: boolean } = { a: 'c', b: false } * assert.deepStrictEqual( - * collect((key, val) => ({ key: key, value: val }))(x), + * collect(Ord)((key, val) => ({ key: key, value: val }))(x), * [{ key: 'a', value: 'c' }, { key: 'b', value: false }] * ) * + * @since 2.11.0 + */ +export function collect( + O: Ord +): (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => ReadonlyArray + +/** + * Use the other overload instead + * + * @deprecated * @since 2.5.0 */ -export const collect = (f: (k: K, a: A) => B) => ( - r: ReadonlyRecord -): ReadonlyArray => { - const out: Array = [] - for (const key of keys(r)) { - out.push(f(key, r[key])) +export function collect(f: (k: K, a: A) => B): (r: ReadonlyRecord) => ReadonlyArray +export function collect( + O: Ord | ((k: string, a: unknown) => unknown) +): + | ((f: (k: K, a: A) => B) => (r: ReadonlyRecord) => ReadonlyArray) + | ((r: ReadonlyRecord) => ReadonlyArray) { + if (typeof O === 'function') { + return (r: ReadonlyRecord) => { + const out: Array = [] + for (const key of keys(r)) { + out.push(O(key, r[key])) + } + return out + } + } + return (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => { + const out: Array = [] + for (const key of keysWithOrd(O)(r)) { + out.push(f(key, r[key])) + } + return out } - return out } /** @@ -115,7 +150,7 @@ export const collect = (f: (k: K, a: A) => B) => ( */ export const toReadonlyArray: (r: ReadonlyRecord) => ReadonlyArray = /*#__PURE__*/ - collect((k, a) => [k, a]) + collect(OrdString)((k, a) => [k, a]) /** * Unfolds a `ReadonlyRecord` into a list of key/value pairs. @@ -324,16 +359,59 @@ export function map(f: (a: A) => B): (fa: ReadonlyRecord) => Re } /** + * @since 2.11.0 + */ +export function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export function reduceWithIndex( + O: Ord +): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B + +/** + * Use the other overloads instead + * * @since 2.5.0 + * @deprecated */ export function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B -export function reduceWithIndex(b: B, f: (k: string, b: B, a: A) => B): (fa: ReadonlyRecord) => B { - return (fa) => { +export function reduceWithIndex(b: B, f: (k: string, b: B, a: A) => B): (fa: ReadonlyRecord) => B + +/** + * @since 2.11.0 + */ +export function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export function reduceWithIndex( + O: Ord +): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export function reduceWithIndex( + ...args: [Ord] | [unknown, (k: string, b: unknown, a: unknown) => unknown] +): + | (( + b: unknown, + f: (k: string, b: unknown, a: unknown) => unknown + ) => (fa: ReadonlyRecord) => unknown) + | ((fa: ReadonlyRecord) => unknown) { + if (args.length === 2) { + return (fa: ReadonlyRecord) => { + let out = args[0] + const ks = keys(fa) + const len = ks.length + for (let i = 0; i < len; i++) { + const k = ks[i] + out = args[1](k, out, fa[k]) + } + return out + } + } + return (b, f) => (fa) => { let out = b - const ks = keys(fa) + const ks = keysWithOrd(args[0])(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] @@ -344,40 +422,103 @@ export function reduceWithIndex(b: B, f: (k: string, b: B, a: A) => B): (f } /** + * @since 2.11.0 + */ +export function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M +export function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M + +/** + * Use the other overloads instead + * * @since 2.5.0 + * @deprecated */ export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M export function foldMapWithIndex( M: Monoid -): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M { - return (f) => (fa) => { - let out = M.empty +): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M + +export function foldMapWithIndex( + arg: Ord | Monoid +): + | (( + M: Monoid + ) => (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) + | ((f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) { + if ('compare' in arg) { + return (M: Monoid) => (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => { + let out = M.empty + const ks = keysWithOrd(arg)(fa) + const len = ks.length + for (let i = 0; i < len; i++) { + const k = ks[i] + out = M.concat(out, f(k, fa[k])) + } + return out + } + } + return (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => { + let out = arg.empty const ks = keys(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] - out = M.concat(out, f(k, fa[k])) + out = arg.concat(out, f(k, fa[k])) } return out } } /** + * @since 2.11.0 + */ +export function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: K, a: A, b: B) => B) => (fa: ReadonlyRecord) => B +export function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B + +/** + * Use the other overloads instead + * * @since 2.5.0 + * @deprecated */ export function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B ): (fa: ReadonlyRecord) => B -export function reduceRightWithIndex( - b: B, - f: (k: string, a: A, b: B) => B -): (fa: ReadonlyRecord) => B { - return (fa) => { +export function reduceRightWithIndex(b: B, f: (k: string, a: A, b: B) => B): (fa: ReadonlyRecord) => B +export function reduceRightWithIndex( + ...args: [Ord] | [unknown, (k: string, a: unknown, b: unknown) => unknown] +): + | (( + b: unknown, + f: (k: string, a: unknown, b: unknown) => unknown + ) => (fa: ReadonlyRecord) => unknown) + | ((fa: ReadonlyRecord) => unknown) { + if (args.length === 2) { + return (fa: ReadonlyRecord) => { + let out = args[0] + const ks = keys(fa) + const len = ks.length + for (let i = len - 1; i >= 0; i--) { + const k = ks[i] + out = args[1](k, fa[k], out) + } + return out + } + } + return (b, f) => (fa) => { let out = b - const ks = keys(fa) + const ks = keysWithOrd(args[0])(fa) const len = ks.length for (let i = len - 1; i >= 0; i--) { const k = ks[i] @@ -795,6 +936,78 @@ export function elem( } } +/** + * @since 2.11.0 + */ +export const union = (M: Magma) => (second: ReadonlyRecord) => ( + first: ReadonlyRecord +): ReadonlyRecord => { + if (isEmpty(first)) { + return second + } + if (isEmpty(second)) { + return first + } + const out: Record = {} + for (const k in first) { + if (has(k, second)) { + out[k] = M.concat(first[k], second[k]) + } else { + out[k] = first[k] + } + } + for (const k in second) { + if (!has(k, out)) { + out[k] = second[k] + } + } + return out +} + +/** + * @since 2.11.0 + */ +export const intersection = (M: Magma) => (second: ReadonlyRecord) => ( + first: ReadonlyRecord +): ReadonlyRecord => { + if (isEmpty(first) || isEmpty(second)) { + return empty + } + const out: Record = {} + for (const k in first) { + if (has(k, second)) { + out[k] = M.concat(first[k], second[k]) + } + } + return out +} + +/** + * @since 2.11.0 + */ +export const difference = (second: ReadonlyRecord) => ( + first: ReadonlyRecord +): ReadonlyRecord => { + if (isEmpty(first)) { + return second + } + if (isEmpty(second)) { + return first + } + const out: Record = {} + for (const k in first) { + if (!has(k, second)) { + out[k] = first[k] + } + } + for (const k in second) { + if (!has(k, first)) { + out[k] = second[k] + } + } + return out +} + // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- @@ -803,14 +1016,16 @@ const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) /* istanbul ignore next */ const _mapWithIndex: FunctorWithIndex1['mapWithIndex'] = (fa, f) => pipe(fa, mapWithIndex(f)) /* istanbul ignore next */ -const _reduce: Foldable1['reduce'] = (fa, b, f) => pipe(fa, reduce(b, f)) +const _reduce: (O: Ord) => Foldable1['reduce'] = (O: Ord) => (fa, b, f) => + pipe(fa, reduce(O)(b, f)) /* istanbul ignore next */ -const _foldMap: Foldable1['foldMap'] = (M) => { - const foldMapM = foldMap(M) +const _foldMap: (O: Ord) => Foldable1['foldMap'] = (O) => (M) => { + const foldMapM = foldMap(O)(M) return (fa, f) => pipe(fa, foldMapM(f)) } /* istanbul ignore next */ -const _reduceRight: Foldable1['reduceRight'] = (fa, b, f) => pipe(fa, reduceRight(b, f)) +const _reduceRight: (O: Ord) => Foldable1['reduceRight'] = (O) => (fa, b, f) => + pipe(fa, reduceRight(O)(b, f)) /* istanbul ignore next */ const _traverse = ( F: Applicative @@ -831,16 +1046,19 @@ const _partition = ( /* istanbul ignore next */ const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) /* istanbul ignore next */ -const _reduceWithIndex: FoldableWithIndex1['reduceWithIndex'] = (fa, b, f) => - pipe(fa, reduceWithIndex(b, f)) +const _reduceWithIndex: (O: Ord) => FoldableWithIndex1['reduceWithIndex'] = (O) => (fa, b, f) => + pipe(fa, reduceWithIndex(O)(b, f)) /* istanbul ignore next */ -const _foldMapWithIndex: FoldableWithIndex1['foldMapWithIndex'] = (M) => { - const foldMapWithIndexM = foldMapWithIndex(M) +const _foldMapWithIndex: (O: Ord) => FoldableWithIndex1['foldMapWithIndex'] = (O) => (M) => { + const foldMapWithIndexM = foldMapWithIndex(O)(M) return (fa, f) => pipe(fa, foldMapWithIndexM(f)) } /* istanbul ignore next */ -const _reduceRightWithIndex: FoldableWithIndex1['reduceRightWithIndex'] = (fa, b, f) => - pipe(fa, reduceRightWithIndex(b, f)) +const _reduceRightWithIndex: (O: Ord) => FoldableWithIndex1['reduceRightWithIndex'] = (O) => ( + fa, + b, + f +) => pipe(fa, reduceRightWithIndex(O)(b, f)) /* istanbul ignore next */ const _partitionMapWithIndex = ( fa: ReadonlyRecord, @@ -931,24 +1149,87 @@ export const partitionMap: ( * @category Foldable * @since 2.5.0 */ -export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B = (b, f) => - reduceWithIndex(b, (_, b, a) => f(b, a)) +export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B /** + * Use other overload instead + * * @category Foldable * @since 2.5.0 + * @deprecated */ -export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M = (M) => { - const foldMapWithIndexM = foldMapWithIndex(M) - return (f) => foldMapWithIndexM((_, a) => f(a)) +export function reduce(b: B, f: (b: B, a: A) => B): (fa: Readonly>) => B +export function reduce( + ...args: [Ord] | [unknown, (b: unknown, A: unknown) => unknown] +): + | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: Readonly>) => unknown) + | ((fa: Readonly>) => unknown) { + if (args.length === 1) { + return (b: unknown, f: (b: unknown, a: unknown) => unknown) => reduceWithIndex(args[0])(b, (_, b, a) => f(b, a)) + } + // tslint:disable-next-line: deprecation + return reduceWithIndex(args[0], (_, b, a) => args[1](b, a)) } /** * @category Foldable * @since 2.5.0 */ -export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B = (b, f) => - reduceRightWithIndex(b, (_, a, b) => f(a, b)) +export function foldMap( + O: Ord +): (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M + +/** + * Use the other overload instead + * + * @category Foldable + * @since 2.5.0 + * @deprecated + */ +export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Readonly>) => M +export function foldMap( + arg: Ord | Monoid +): + | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: Readonly>) => unknown) + | ((f: (a: unknown) => unknown) => (fa: Readonly>) => unknown) { + if ('compare' in arg) { + return (M: Monoid) => { + const foldMapWithIndexM = foldMapWithIndex(arg)(M) + return (f: (a: unknown) => unknown) => foldMapWithIndexM((_, a) => f(a)) + } + } + // tslint:disable-next-line: deprecation + const foldMapWithIndexM = foldMapWithIndex(arg) + return (f: (a: unknown) => unknown) => foldMapWithIndexM((_, a) => f(a)) +} + +/** + * @category Foldable + * @since 2.11.0 + */ +export function reduceRight( + O: Ord +): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B + +/** + * Use the other overload instead + * + * @category Foldable + * @since 2.5.0 + * @deprecated + */ +export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B +export function reduceRight( + ...args: [Ord] | [unknown, (a: unknown, b: unknown) => unknown] +): + | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: Readonly>) => unknown) + | ((fa: Readonly>) => unknown) { + if (args.length === 2) { + // tslint:disable-next-line: deprecation + return reduceRightWithIndex(args[0], (_, a, b) => args[1](a, b)) + } + return (b, f) => reduceRightWithIndex(args[0])(b, (_, a, b) => f(a, b)) +} /** * @category Compactable @@ -1015,10 +1296,31 @@ declare module './HKT' { * @category instances * @since 2.5.0 */ -export function getShow(S: Show): Show> { +export function getShow(O: Ord): (S: Show) => Show> + +/** + * Use the other overload instead + * + * @category instances + * @since 2.5.0 + * @deprecated + */ +export function getShow(S: Show): Show> +export function getShow( + arg: Ord | Show +): ((S: Show) => Show>) | Show> { + if ('compare' in arg) { + return (S: Show) => ({ + show: (r: ReadonlyRecord) => { + const elements = collect(arg)((k, a: unknown) => `${JSON.stringify(k)}: ${S.show(a)}`)(r).join(', ') + return elements === '' ? '{}' : `{ ${elements} }` + } + }) + } return { - show: (r) => { - const elements = collect((k, a: A) => `${JSON.stringify(k)}: ${S.show(a)}`)(r).join(', ') + show: (r: ReadonlyRecord) => { + // tslint:disable-next-line: deprecation + const elements = collect((k, a: unknown) => `${JSON.stringify(k)}: ${arg.show(a)}`)(r).join(', ') return elements === '' ? '{}' : `{ ${elements} }` } } @@ -1099,30 +1401,61 @@ export const FunctorWithIndex: FunctorWithIndex1 = { } /** + * Use `getFoldable` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const Foldable: Foldable1 = { URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString) } /** * @category instances * @since 2.7.0 */ +export const getFoldable = (O: Ord): Foldable1 => ({ + URI, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O) +}) + +/** + * Use `getFoldableWithIndex` instead + * + * @category instances + * @since 2.7.0 + * @deprecated + */ export const FoldableWithIndex: FoldableWithIndex1 = { URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString) } +/** + * @category instances + * @since 2.11.0 + */ +export const getFoldableWithIndex = (O: Ord): FoldableWithIndex1 => ({ + URI, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + reduceWithIndex: _reduceWithIndex(O), + foldMapWithIndex: _foldMapWithIndex(O), + reduceRightWithIndex: _reduceRightWithIndex(O) +}) + /** * @category instances * @since 2.7.0 @@ -1169,48 +1502,90 @@ export const FilterableWithIndex: FilterableWithIndex1 = { } /** + * Use `getTraversable` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const Traversable: Traversable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence } /** + * @category instances + * @since 2.11.0 + */ +export const getTraversable = (O: Ord): Traversable1 => ({ + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: _traverse, + sequence +}) + +/** + * Use `getTraversableWithIndex` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const TraversableWithIndex: TraversableWithIndex1 = { URI, map: _map, mapWithIndex: _mapWithIndex, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString), traverse: _traverse, sequence, traverseWithIndex: _traverseWithIndex } /** + * @category instances + * @since 2.11.0 + */ +export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 => ({ + URI, + map: _map, + mapWithIndex: _mapWithIndex, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + reduceWithIndex: _reduceWithIndex(O), + foldMapWithIndex: _foldMapWithIndex(O), + reduceRightWithIndex: _reduceRightWithIndex(O), + traverse: _traverse, + sequence, + traverseWithIndex: _traverseWithIndex +}) + +/** + * Use `getWitherable` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const Witherable: Witherable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence, compact, @@ -1223,6 +1598,61 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * @category instances + * @since 2.11.0 + */ +export const getWitherable: (O: Ord) => Witherable1 = (O) => ({ + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: _traverse, + sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: _wither, + wilt: _wilt +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (S: Semigroup): Semigroup> => ({ + concat: (first, second) => union(S)(second)(first) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (S: Semigroup): Monoid> => ({ + concat: getUnionSemigroup(S).concat, + empty +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (S: Semigroup): Semigroup> => ({ + concat: (first, second) => intersection(S)(second)(first) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (): Magma> => ({ + concat: (first, second) => difference(second)(first) +}) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- @@ -1264,9 +1694,9 @@ export const readonlyRecord: FunctorWithIndex1 & Witherable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence, compact, @@ -1276,9 +1706,9 @@ export const readonlyRecord: FunctorWithIndex1 & partition: _partition, partitionMap: _partitionMap, mapWithIndex: _mapWithIndex, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex, + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString), filterMapWithIndex: _filterMapWithIndex, filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, diff --git a/src/Record.ts b/src/Record.ts index 86a52a0ef..ef2952cc2 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -16,10 +16,12 @@ import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' +import { Ord } from './Ord' import * as RR from './ReadonlyRecord' import { Semigroup } from './Semigroup' import { Separated } from './Separated' import { Show } from './Show' +import { Ord as OrdString } from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' @@ -50,26 +52,59 @@ export const isEmpty: (r: Record) => boolean = RR.isEmpty */ export const keys: (r: Record) => Array = RR.keys as any +/** + * + * @internal + * @since 2.11.0 + */ +const keysWithOrd = (O: Ord) => (r: Record): ReadonlyArray => + (Object.keys(r) as any).sort(O.compare) + /** * Map a `Record` into an `Array`. * * @example * import { collect } from 'fp-ts/Record' + * import { Ord } from 'fp-ts/string' * * const x: { readonly a: string, readonly b: boolean } = { a: 'c', b: false } * assert.deepStrictEqual( - * collect((key, val) => ({ key: key, value: val }))(x), + * collect(Ord)((key, val) => ({ key: key, value: val }))(x), * [{ key: 'a', value: 'c' }, { key: 'b', value: false }] * ) * * @since 2.0.0 */ -export const collect = (f: (k: K, a: A) => B) => (r: Record): Array => { - const out: Array = [] - for (const key of keys(r)) { - out.push(f(key, r[key])) +export function collect(O: Ord): (f: (k: K, a: A) => B) => (r: Record) => Array + +/** + * Use the other overload + * + * @since 2.0.0 + * @deprecated + */ +export function collect(f: (k: K, a: A) => B): (r: Record) => Array +export function collect( + arg: Ord | ((k: string, a: unknown) => unknown) +): + | ((r: Record) => Array) + | ((f: (k: string, a: unknown) => unknown) => (r: Record) => Array) { + if (typeof arg === 'function') { + return (r: Record) => { + const out: Array = [] + for (const key of keys(r)) { + out.push(arg(key, r[key])) + } + return out + } + } + return (f: (k: string, a: unknown) => unknown) => (r: Record) => { + const out: Array = [] + for (const key of keysWithOrd(arg)(r)) { + out.push(f(key, r[key])) + } + return out } - return out } /** @@ -79,7 +114,7 @@ export const collect = (f: (k: K, a: A) => B) => (r: Rec */ export const toArray: (r: Record) => Array<[K, A]> = /*#__PURE__*/ - collect((k, a) => [k, a]) + collect(OrdString)((k, a) => [k, a]) /** * Unfolds a `Record` into a list of key/value pairs. @@ -209,26 +244,85 @@ export const mapWithIndex: (f: (k: K, a: A) => B) => (fa */ export const map: (f: (a: A) => B) => (fa: Record) => Record = RR.map +/** + * @since 2.11.0 + */ +export function reduceWithIndex( + O: Ord +): (b: B, f: (k: K, b: B, a: A) => B) => (fa: Record) => B + +/** + * Use the other overload + * + * @since 2.0.0 + * @deprecated + */ +export function reduceWithIndex(b: B, f: (k: K, b: B, a: A) => B): (fa: Record) => B +export function reduceWithIndex( + ...args: [Ord] | [unknown, (k: unknown, b: unknown, a: unknown) => unknown] +): + | ((b: unknown, f: (k: unknown, b: unknown, a: unknown) => unknown) => (fa: Record) => unknown) + | ((fa: Record) => unknown) { + if (args.length === 2) { + // tslint:disable-next-line: deprecation + return RR.reduceWithIndex(args[0], args[1]) + } + return RR.reduceWithIndex(args[0]) +} + /** * @since 2.0.0 */ -export const reduceWithIndex: (b: B, f: (k: K, b: B, a: A) => B) => (fa: Record) => B = - RR.reduceWithIndex +export function foldMapWithIndex( + O: Ord +): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: Record) => M /** + * Use the other overload + * * @since 2.0.0 + * @deprecated */ -export const foldMapWithIndex: ( +export function foldMapWithIndex( M: Monoid -) => (f: (k: K, a: A) => M) => (fa: Record) => M = RR.foldMapWithIndex +): (f: (k: K, a: A) => M) => (fa: Record) => M +export function foldMapWithIndex( + arg: Ord | Monoid +): + | ((M: Monoid) => (f: (k: string, a: unknown) => unknown) => (fa: Record) => unknown) + | ((f: (k: string, a: unknown) => unknown) => (fa: Record) => unknown) { + if ('compare' in arg) { + return RR.foldMapWithIndex(arg) + } + // tslint:disable-next-line: deprecation + return RR.foldMapWithIndex(arg) +} + +/** + * @since 2.11.0 + */ +export function reduceRightWithIndex( + O: Ord +): (b: B, f: (k: K, a: A, b: B) => B) => (fa: Record) => B /** + * Use the other overload + * * @since 2.0.0 + * @deprecated */ -export const reduceRightWithIndex: ( - b: B, - f: (k: K, a: A, b: B) => B -) => (fa: Record) => B = RR.reduceRightWithIndex +export function reduceRightWithIndex(b: B, f: (k: K, a: A, b: B) => B): (fa: Record) => B +export function reduceRightWithIndex( + ...args: [Ord] | [unknown, (k: string, a: unknown, b: unknown) => unknown] +): + | ((b: unknown, f: (k: string, a: unknown, b: unknown) => unknown) => (fa: Record) => unknown) + | ((fa: Record) => unknown) { + if (args.length === 2) { + // tslint:disable-next-line: deprecation + return RR.reduceRightWithIndex(args[0], args[1]) + } + return RR.reduceRightWithIndex(args[0]) +} /** * Create a `Record` with one key/value pair. @@ -497,14 +591,14 @@ const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) /* istanbul ignore next */ const _mapWithIndex: FunctorWithIndex1['mapWithIndex'] = (fa, f) => pipe(fa, mapWithIndex(f)) /* istanbul ignore next */ -const _reduce: Foldable1['reduce'] = (fa, b, f) => pipe(fa, reduce(b, f)) +const _reduce = (O: Ord): Foldable1['reduce'] => (fa, b, f) => pipe(fa, reduce(O)(b, f)) /* istanbul ignore next */ -const _foldMap: Foldable1['foldMap'] = (M) => { - const foldMapM = foldMap(M) - return (fa, f) => pipe(fa, foldMapM(f)) +const _foldMap = (O: Ord): Foldable1['foldMap'] => (M) => { + const foldMapOM = foldMap(O)(M) + return (fa, f) => pipe(fa, foldMapOM(f)) } /* istanbul ignore next */ -const _reduceRight: Foldable1['reduceRight'] = (fa, b, f) => pipe(fa, reduceRight(b, f)) +const _reduceRight = (O: Ord): Foldable1['reduceRight'] => (fa, b, f) => pipe(fa, reduceRight(O)(b, f)) /* istanbul ignore next */ const _traverse = ( F: Applicative @@ -524,16 +618,16 @@ const _partition = ( /* istanbul ignore next */ const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) /* istanbul ignore next */ -const _reduceWithIndex: FoldableWithIndex1['reduceWithIndex'] = (fa, b, f) => - pipe(fa, reduceWithIndex(b, f)) +const _reduceWithIndex = (O: Ord): FoldableWithIndex1['reduceWithIndex'] => (fa, b, f) => + pipe(fa, reduceWithIndex(O)(b, f)) /* istanbul ignore next */ -const _foldMapWithIndex: FoldableWithIndex1['foldMapWithIndex'] = (M) => { - const foldMapWithIndexM = foldMapWithIndex(M) - return (fa, f) => pipe(fa, foldMapWithIndexM(f)) +const _foldMapWithIndex = (O: Ord): FoldableWithIndex1['foldMapWithIndex'] => (M) => { + const foldMapWithIndexOM = foldMapWithIndex(O)(M) + return (fa, f) => pipe(fa, foldMapWithIndexOM(f)) } /* istanbul ignore next */ -const _reduceRightWithIndex: FoldableWithIndex1['reduceRightWithIndex'] = (fa, b, f) => - pipe(fa, reduceRightWithIndex(b, f)) +const _reduceRightWithIndex = (O: Ord): FoldableWithIndex1['reduceRightWithIndex'] => (fa, b, f) => + pipe(fa, reduceRightWithIndex(O)(b, f)) /* istanbul ignore next */ const _partitionMapWithIndex = ( fa: Record, @@ -596,7 +690,27 @@ export const filterMap: (f: (a: A) => Option) => (fa: Record * @category Foldable * @since 2.0.0 */ -export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M = RR.foldMap +export function foldMap(O: Ord): (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M + +/** + * Use the other overload + * + * @category Foldable + * @since 2.0.0 + * @deprecated + */ +export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M +export function foldMap( + O: Ord | Monoid +): + | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: Record) => unknown) + | ((f: (a: unknown) => unknown) => (fa: Record) => unknown) { + if ('compare' in O) { + return RR.foldMap(O) + } + // tslint:disable-next-line: deprecation + return RR.foldMap(O) +} /** * @category Filterable @@ -621,13 +735,52 @@ export const partitionMap: ( * @category Foldable * @since 2.0.0 */ -export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Record) => B = RR.reduce +export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Record) => B + +/** + * Use the other overload + * + * @category Foldable + * @since 2.0.0 + * @deprecated + */ +export function reduce(b: B, f: (b: B, a: A) => B): (fa: Record) => B +export function reduce( + ...args: [Ord] | [unknown, (b: unknown, a: unknown) => unknown] +): + | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: Record) => unknown) + | ((fa: Record) => unknown) { + if (args.length === 2) { + // tslint:disable-next-line: deprecation + return RR.reduce(args[0], args[1]) + } + return RR.reduce(args[0]) +} +/** + * @category Foldable + * @since 2.0.0 + */ +export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) => (fa: Record) => B /** + * Use the other overload + * * @category Foldable * @since 2.0.0 + * @deprecated */ -export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Record) => B = RR.reduceRight +export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record) => B +export function reduceRight( + ...args: [Ord] | [unknown, (a: unknown, b: unknown) => unknown] +): + | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: Record) => unknown) + | ((fa: Record) => unknown) { + if (args.length === 2) { + // tslint:disable-next-line: deprecation + return RR.reduceRight(args[0], args[1]) + } + return RR.reduceRight(args[0]) +} /** * @category Compactable @@ -668,7 +821,25 @@ declare module './HKT' { * @category instances * @since 2.0.0 */ -export const getShow: (S: Show) => Show> = RR.getShow +export function getShow(O: Ord): (S: Show) => Show> + +/** + * Use the other overload + * + * @category instances + * @since 2.0.0 + * @deprecated + */ +export function getShow(S: Show): Show> +export function getShow( + arg: Ord | Show +): ((S: Show) => Show>) | Show> { + if ('compare' in arg) { + return RR.getShow(arg) + } + // tslint:disable-next-line: deprecation + return RR.getShow(arg) +} /** * @category instances @@ -721,30 +892,61 @@ export const FunctorWithIndex: FunctorWithIndex1 = { } /** + * Use `getFoldable` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const Foldable: Foldable1 = { URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString) } /** * @category instances * @since 2.7.0 */ +export const getFoldable = (O: Ord): Foldable1 => ({ + URI, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O) +}) + +/** + * Use `getFoldableWithIndex` instead + * + * @category instances + * @since 2.7.0 + * @deprecated + */ export const FoldableWithIndex: FoldableWithIndex1 = { URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString) } +/** + * @category instances + * @since 2.7.0 + */ +export const getFoldableWithIndex = (O: Ord): FoldableWithIndex1 => ({ + URI, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + reduceWithIndex: _reduceWithIndex(O), + foldMapWithIndex: _foldMapWithIndex(O), + reduceRightWithIndex: _reduceRightWithIndex(O) +}) + /** * @category instances * @since 2.7.0 @@ -791,33 +993,52 @@ export const FilterableWithIndex: FilterableWithIndex1 = { } /** + * Use `getTraversable` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const Traversable: Traversable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence } +/** + * @category instances + * @since 2.11.0 + */ +export const getTraversable = (O: Ord): Traversable1 => ({ + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: _traverse, + sequence +}) /** + * Use the `getTraversableWithIndex` instead + * * @category instances * @since 2.7.0 + * @deprecated */ export const TraversableWithIndex: TraversableWithIndex1 = { URI, map: _map, mapWithIndex: _mapWithIndex, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString), traverse: _traverse, sequence, traverseWithIndex: _traverseWithIndex @@ -827,12 +1048,34 @@ export const TraversableWithIndex: TraversableWithIndex1 = { * @category instances * @since 2.7.0 */ +export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 => ({ + URI, + map: _map, + mapWithIndex: _mapWithIndex, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + reduceWithIndex: _reduceWithIndex(O), + foldMapWithIndex: _foldMapWithIndex(O), + reduceRightWithIndex: _reduceRightWithIndex(O), + traverse: _traverse, + sequence, + traverseWithIndex: _traverseWithIndex +}) + +/** + * Use `getWitherable` instead + * + * @category instances + * @since 2.7.0 + * @deprecated + */ export const Witherable: Witherable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence, compact, @@ -845,6 +1088,28 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * @category instances + * @since 2.7.0 + */ +export const getWitherable = (O: Ord): Witherable1 => ({ + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: _traverse, + sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: _wither, + wilt: _wilt +}) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- @@ -887,9 +1152,9 @@ export const record: FunctorWithIndex1 & Witherable1 = { URI, map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, + reduce: _reduce(OrdString), + foldMap: _foldMap(OrdString), + reduceRight: _reduceRight(OrdString), traverse: _traverse, sequence, compact, @@ -899,9 +1164,9 @@ export const record: FunctorWithIndex1 & partition: _partition, partitionMap: _partitionMap, mapWithIndex: _mapWithIndex, - reduceWithIndex: _reduceWithIndex, - foldMapWithIndex: _foldMapWithIndex, - reduceRightWithIndex: _reduceRightWithIndex, + reduceWithIndex: _reduceWithIndex(OrdString), + foldMapWithIndex: _foldMapWithIndex(OrdString), + reduceRightWithIndex: _reduceRightWithIndex(OrdString), filterMapWithIndex: _filterMapWithIndex, filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, diff --git a/test/ReadonlyRecord.ts b/test/ReadonlyRecord.ts index 9afd52355..8150305a4 100644 --- a/test/ReadonlyRecord.ts +++ b/test/ReadonlyRecord.ts @@ -28,6 +28,21 @@ describe('ReadonlyRecord', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduce(S.Ord)('', (b, a) => b + a) + ), + 'ab' + ) + U.deepStrictEqual( + pipe( + { k2: 'b', k1: 'a' }, + _.reduce(S.Ord)('', (b, a) => b + a) + ), + 'ab' + ) + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduce('', (b, a) => b + a) ), 'ab' @@ -35,6 +50,7 @@ describe('ReadonlyRecord', () => { U.deepStrictEqual( pipe( { k2: 'b', k1: 'a' }, + // tslint:disable-next-line: deprecation _.reduce('', (b, a) => b + a) ), 'ab' @@ -42,11 +58,20 @@ describe('ReadonlyRecord', () => { }) it('foldMap', () => { + U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.foldMap(S.Ord)(S.Monoid)(identity)), 'ab') + U.deepStrictEqual(_.getFoldable(S.Ord).foldMap(S.Monoid)({ a: 'a', b: 'b' }, identity), 'ab') + + // tslint:disable-next-line: deprecation U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.foldMap(S.Monoid)(identity)), 'ab') + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.Foldable.foldMap(S.Monoid)({ a: 'a', b: 'b' }, identity), 'ab') }) it('reduceRight', () => { const f = (a: string, acc: string) => acc + a + U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.reduceRight(S.Ord)('', f)), 'ba') + + // tslint:disable-next-line: deprecation U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.reduceRight('', f)), 'ba') }) @@ -107,6 +132,22 @@ describe('ReadonlyRecord', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduceWithIndex(S.Ord)('', (k, b, a) => b + k + a) + ), + 'k1ak2b' + ) + U.deepStrictEqual( + pipe( + { k2: 'b', k1: 'a' }, + _.reduceWithIndex(S.Ord)('', (k, b, a) => b + k + a) + ), + 'k1ak2b' + ) + + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduceWithIndex('', (k, b, a) => b + k + a) ), 'k1ak2b' @@ -114,6 +155,7 @@ describe('ReadonlyRecord', () => { U.deepStrictEqual( pipe( { k2: 'b', k1: 'a' }, + // tslint:disable-next-line: deprecation _.reduceWithIndex('', (k, b, a) => b + k + a) ), 'k1ak2b' @@ -124,16 +166,42 @@ describe('ReadonlyRecord', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.foldMapWithIndex(S.Ord)(S.Monoid)((k, a) => k + a) + ), + 'k1ak2b' + ) + U.deepStrictEqual( + _.getFoldableWithIndex(S.Ord).foldMapWithIndex(S.Monoid)({ k1: 'a', k2: 'b' }, (k, a) => k + a), + 'k1ak2b' + ) + + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.foldMapWithIndex(S.Monoid)((k, a) => k + a) ), 'k1ak2b' ) + U.deepStrictEqual( + // tslint:disable-next-line: deprecation + _.FoldableWithIndex.foldMapWithIndex(S.Monoid)({ k1: 'a', k2: 'b' }, (k, a) => k + a), + 'k1ak2b' + ) }) it('reduceRightWithIndex', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduceRightWithIndex(S.Ord)('', (k, a, b) => b + k + a) + ), + 'k2bk1a' + ) + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduceRightWithIndex('', (k, a, b) => b + k + a) ), 'k2bk1a' @@ -180,26 +248,48 @@ describe('ReadonlyRecord', () => { O.some({ a: 1, b: 2 }) ) U.deepStrictEqual(_.traverse(O.Applicative)((n: number) => (n >= 2 ? O.some(n) : O.none))({ a: 1, b: 2 }), O.none) + + U.deepStrictEqual( + _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? O.some(n) : O.none)), + O.some({ a: 1, b: 2 }) + ) + U.deepStrictEqual( + _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? O.some(n) : O.none)), + O.none + ) }) it('sequence', () => { const sequence = _.sequence(O.Applicative) U.deepStrictEqual(sequence({ a: O.some(1), b: O.some(2) }), O.some({ a: 1, b: 2 })) U.deepStrictEqual(sequence({ a: O.none, b: O.some(2) }), O.none) + + U.deepStrictEqual( + // tslint:disable-next-line: deprecation + _.readonlyRecord.sequence(O.Applicative)({ a: O.some(1), b: O.some(2) }), + O.some({ a: 1, b: 2 }) + ) }) it('traverseWithIndex', () => { - const traverseWithIndex = _.traverseWithIndex(O.Applicative)( - (k, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) - ) + const f = (k: string, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) + const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f) U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none) U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 })) + + U.deepStrictEqual( + _.getTraversableWithIndex(S.Ord).traverseWithIndex(O.Applicative)({ b: 2 }, f), + O.some({ b: 2 }) + ) }) it('wither', async () => { - const wither = _.wither(T.ApplicativePar)((n: number) => T.of(p(n) ? O.some(n + 1) : O.none)) + const f = (n: number) => T.of(p(n) ? O.some(n + 1) : O.none) + const wither = _.wither(T.ApplicativePar)(f) U.deepStrictEqual(await pipe({}, wither)(), {}) U.deepStrictEqual(await pipe({ a: 1, b: 3 }, wither)(), { b: 4 }) + + U.deepStrictEqual(await _.getWitherable(S.Ord).wither(T.ApplicativePar)({ a: 1, b: 3 }, f)(), { b: 4 }) }) it('wilt', async () => { @@ -386,10 +476,16 @@ describe('ReadonlyRecord', () => { }) it('getShow', () => { - const Sh = _.getShow(S.Show) + const Sh = _.getShow(S.Ord)(S.Show) U.deepStrictEqual(Sh.show({}), `{}`) U.deepStrictEqual(Sh.show({ a: 'a' }), `{ "a": "a" }`) U.deepStrictEqual(Sh.show({ a: 'a', b: 'b' }), `{ "a": "a", "b": "b" }`) + + // tslint:disable-next-line: deprecation + const DepSh = _.getShow(S.Show) + U.deepStrictEqual(DepSh.show({}), `{}`) + U.deepStrictEqual(DepSh.show({ a: 'a' }), `{ "a": "a" }`) + U.deepStrictEqual(DepSh.show({ a: 'a', b: 'b' }), `{ "a": "a", "b": "b" }`) }) it('singleton', () => { @@ -454,6 +550,74 @@ describe('ReadonlyRecord', () => { assert.notStrictEqual(bs, as) }) + it('getUnionMonoid', () => { + const M = _.getUnionMonoid(S.Semigroup) + const x: _.ReadonlyRecord = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: _.ReadonlyRecord = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.strictEqual(M.concat(x, M.empty), x) + U.strictEqual(M.concat(M.empty, x), x) + U.strictEqual(M.concat(x, {}), x) + U.strictEqual(M.concat({}, x), x) + U.deepStrictEqual(M.concat(x, y), { + a: 'a1', + b: 'b1b2', + c: 'c1c2', + d: 'd2' + }) + }) + + it('getIntersectionSemigroup', () => { + const M = _.getIntersectionSemigroup(S.Semigroup) + const x: _.ReadonlyRecord = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: _.ReadonlyRecord = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.strictEqual(M.concat(x, _.empty), _.empty) + U.strictEqual(M.concat(x, _.empty), _.empty) + U.strictEqual(M.concat(x, {}), _.empty) + U.strictEqual(M.concat(x, {}), _.empty) + U.deepStrictEqual(M.concat(x, y), { + b: 'b1b2', + c: 'c1c2' + }) + }) + + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma() + const x: _.ReadonlyRecord = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: _.ReadonlyRecord = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.strictEqual(M.concat(_.empty, x), x) + U.strictEqual(M.concat(x, _.empty), x) + U.strictEqual(M.concat({}, x), x) + U.strictEqual(M.concat(x, {}), x) + U.deepStrictEqual(M.concat(x, y), { + a: 'a1', + d: 'd2' + }) + }) + it('mapWithIndex', () => { // should ignore non own properties const o: _.ReadonlyRecord = Object.create({ a: 1 }) diff --git a/test/Record.ts b/test/Record.ts index 6e9da06df..bc5fdfbb1 100644 --- a/test/Record.ts +++ b/test/Record.ts @@ -18,6 +18,19 @@ const noPrototype = Object.create(null) describe('Record', () => { describe('pipeables', () => { + it('collect', () => { + const x: { readonly a: string; readonly b: boolean } = { a: 'c', b: false } + U.deepStrictEqual(_.collect(S.Ord)((key, val) => ({ key: key, value: val }))(x), [ + { key: 'a', value: 'c' }, + { key: 'b', value: false } + ]) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.collect((key, val) => ({ key: key, value: val }))(x), [ + { key: 'a', value: 'c' }, + { key: 'b', value: false } + ]) + }) + it('map', () => { U.deepStrictEqual(pipe({ k1: 1, k2: 2 }, _.map(U.double)), { k1: 2, k2: 4 }) U.deepStrictEqual(pipe({ a: 1, b: 2 }, _.map(U.double)), { a: 2, b: 4 }) @@ -32,6 +45,22 @@ describe('Record', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduce(S.Ord)('', (b, a) => b + a) + ), + 'ab' + ) + U.deepStrictEqual( + pipe( + { k2: 'b', k1: 'a' }, + _.reduce(S.Ord)('', (b, a) => b + a) + ), + 'ab' + ) + + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduce('', (b, a) => b + a) ), 'ab' @@ -39,6 +68,7 @@ describe('Record', () => { U.deepStrictEqual( pipe( { k2: 'b', k1: 'a' }, + // tslint:disable-next-line: deprecation _.reduce('', (b, a) => b + a) ), 'ab' @@ -46,11 +76,20 @@ describe('Record', () => { }) it('foldMap', () => { + U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.foldMap(S.Ord)(S.Monoid)(identity)), 'ab') + U.deepStrictEqual(_.getFoldable(S.Ord).foldMap(S.Monoid)({ a: 'a', b: 'b' }, identity), 'ab') + + // tslint:disable-next-line: deprecation U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.foldMap(S.Monoid)(identity)), 'ab') + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.Foldable.foldMap(S.Monoid)({ a: 'a', b: 'b' }, identity), 'ab') }) it('reduceRight', () => { const f = (a: string, acc: string) => acc + a + U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.reduceRight(S.Ord)('', f)), 'ba') + + // tslint:disable-next-line: deprecation U.deepStrictEqual(pipe({ a: 'a', b: 'b' }, _.reduceRight('', f)), 'ba') }) @@ -105,6 +144,22 @@ describe('Record', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduceWithIndex(S.Ord)('', (k, b, a) => b + k + a) + ), + 'k1ak2b' + ) + U.deepStrictEqual( + pipe( + { k2: 'b', k1: 'a' }, + _.reduceWithIndex(S.Ord)('', (k, b, a) => b + k + a) + ), + 'k1ak2b' + ) + + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduceWithIndex('', (k, b, a) => b + k + a) ), 'k1ak2b' @@ -112,6 +167,7 @@ describe('Record', () => { U.deepStrictEqual( pipe( { k2: 'b', k1: 'a' }, + // tslint:disable-next-line: deprecation _.reduceWithIndex('', (k, b, a) => b + k + a) ), 'k1ak2b' @@ -122,16 +178,42 @@ describe('Record', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.foldMapWithIndex(S.Ord)(S.Monoid)((k, a) => k + a) + ), + 'k1ak2b' + ) + U.deepStrictEqual( + _.getFoldableWithIndex(S.Ord).foldMapWithIndex(S.Monoid)({ k1: 'a', k2: 'b' }, (k, a) => k + a), + 'k1ak2b' + ) + + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.foldMapWithIndex(S.Monoid)((k, a) => k + a) ), 'k1ak2b' ) + U.deepStrictEqual( + // tslint:disable-next-line: deprecation + _.FoldableWithIndex.foldMapWithIndex(S.Monoid)({ k1: 'a', k2: 'b' }, (k, a) => k + a), + 'k1ak2b' + ) }) it('reduceRightWithIndex', () => { U.deepStrictEqual( pipe( { k1: 'a', k2: 'b' }, + _.reduceRightWithIndex(S.Ord)('', (k, a, b) => b + k + a) + ), + 'k2bk1a' + ) + U.deepStrictEqual( + pipe( + { k1: 'a', k2: 'b' }, + // tslint:disable-next-line: deprecation _.reduceRightWithIndex('', (k, a, b) => b + k + a) ), 'k2bk1a' @@ -186,11 +268,11 @@ describe('Record', () => { U.deepStrictEqual(_.traverse(O.Applicative)((n: number) => (n >= 2 ? O.some(n) : O.none))({ a: 1, b: 2 }), O.none) U.deepStrictEqual( - _.Traversable.traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? O.some(n) : O.none)), + _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? O.some(n) : O.none)), O.some({ a: 1, b: 2 }) ) U.deepStrictEqual( - _.Traversable.traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? O.some(n) : O.none)), + _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? O.some(n) : O.none)), O.none ) }) @@ -199,20 +281,33 @@ describe('Record', () => { const sequence = _.sequence(O.Applicative) U.deepStrictEqual(sequence({ a: O.some(1), b: O.some(2) }), O.some({ a: 1, b: 2 })) U.deepStrictEqual(sequence({ a: O.none, b: O.some(2) }), O.none) + + U.deepStrictEqual( + // tslint:disable-next-line: deprecation + _.record.sequence(O.Applicative)({ a: O.some(1), b: O.some(2) }), + O.some({ a: 1, b: 2 }) + ) }) it('traverseWithIndex', () => { - const traverseWithIndex = _.traverseWithIndex(O.Applicative)( - (k, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) - ) + const f = (k: string, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) + const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f) U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none) U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 })) + + U.deepStrictEqual( + _.getTraversableWithIndex(S.Ord).traverseWithIndex(O.Applicative)({ b: 2 }, f), + O.some({ b: 2 }) + ) }) it('wither', async () => { - const wither = _.wither(T.ApplicativePar)((n: number) => T.of(p(n) ? O.some(n + 1) : O.none)) + const f = (n: number) => T.of(p(n) ? O.some(n + 1) : O.none) + const wither = _.wither(T.ApplicativePar)(f) U.deepStrictEqual(await pipe({}, wither)(), {}) U.deepStrictEqual(await pipe({ a: 1, b: 3 }, wither)(), { b: 4 }) + + U.deepStrictEqual(await _.getWitherable(S.Ord).wither(T.ApplicativePar)({ a: 1, b: 3 }, f)(), { b: 4 }) }) it('wilt', async () => { @@ -382,10 +477,16 @@ describe('Record', () => { }) it('getShow', () => { - const Sh = _.getShow(S.Show) + const Sh = _.getShow(S.Ord)(S.Show) U.deepStrictEqual(Sh.show({}), `{}`) U.deepStrictEqual(Sh.show({ a: 'a' }), `{ "a": "a" }`) U.deepStrictEqual(Sh.show({ a: 'a', b: 'b' }), `{ "a": "a", "b": "b" }`) + + // tslint:disable-next-line: deprecation + const DepSh = _.getShow(S.Show) + U.deepStrictEqual(DepSh.show({}), `{}`) + U.deepStrictEqual(DepSh.show({ a: 'a' }), `{ "a": "a" }`) + U.deepStrictEqual(DepSh.show({ a: 'a', b: 'b' }), `{ "a": "a", "b": "b" }`) }) it('singleton', () => { From 0a933c342765c53913d5e313463ca7433d3f19d9 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 08:33:37 +0200 Subject: [PATCH 016/162] update changelog --- CHANGELOG.md | 29 +++++++++++++++ docs/modules/Array.ts.md | 22 +++++++++++ docs/modules/ReadonlyArray.ts.md | 22 +++++++++++ docs/modules/ReadonlyRecord.ts.md | 22 +++++------ docs/modules/Record.ts.md | 22 +++++------ package.json | 2 +- src/Array.ts | 5 +-- src/ReadonlyArray.ts | 5 +-- src/ReadonlyRecord.ts | 52 ++++++++------------------ src/Record.ts | 61 +++++++++++-------------------- test/Witherable.ts | 3 +- 11 files changed, 138 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0844868f9..33d227c9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,18 +17,47 @@ high state of flux, you're at risk of it changing without notice. # 2.11 +- **Deprecation** + - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): + - `collect` + - `reduce` + - `foldMap` + - `reduceRight` + - `reduceWithIndex` + - `foldMapWithIndex` + - `reduceRightWithIndex` + - `getShow` + - deprecate `Foldable` in favour of `getFoldable` (@anthonyjoeseph) + - deprecate `FoldableWithIndex` in favour of `getFoldableWithIndex` (@anthonyjoeseph) + - deprecate `Traversable` in favour of `getTraversable` (@anthonyjoeseph) + - deprecate `TraversableWithIndex` in favour of `getTraversableWithIndex` (@anthonyjoeseph) + - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - **New Feature** - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) - add `filterE` + - add `ChainRecDepthFirst` instance (@qlonik) + - add `ChainRecBreadthFirst` instance (@qlonik) + - `function` + - add `SK` (@cdimitroulas) - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` + - add `ChainRecDepthFirst` instance (@qlonik) + - add `ChainRecBreadthFirst` instance (@qlonik) - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - `ReadonlyRecord` + - add `union` (@anthonyjoeseph) + - add `intersection` (@anthonyjoeseph) + - add `difference` (@anthonyjoeseph) + - add `getUnionSemigroup` (@anthonyjoeseph) + - add `getUnionMonoid` (@anthonyjoeseph) + - add `getIntersectionSemigroup` (@anthonyjoeseph) + - add `getDifferenceMagma` (@anthonyjoeseph) - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - `TaskOption` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 72eed59c2..8d04e5c8e 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -130,6 +130,8 @@ Added in v2.0.0 - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) + - [ChainRecBreadthFirst](#chainrecbreadthfirst) + - [ChainRecDepthFirst](#chainrecdepthfirst) - [Compactable](#compactable-1) - [Extend](#extend-1) - [Filterable](#filterable-1) @@ -1864,6 +1866,26 @@ export declare const Chain: Chain1<'Array'> Added in v2.10.0 +## ChainRecBreadthFirst + +**Signature** + +```ts +export declare const ChainRecBreadthFirst: ChainRec1<'Array'> +``` + +Added in v2.11.0 + +## ChainRecDepthFirst + +**Signature** + +```ts +export declare const ChainRecDepthFirst: ChainRec1<'Array'> +``` + +Added in v2.11.0 + ## Compactable **Signature** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index cd45fd109..158b12e67 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -117,6 +117,8 @@ Added in v2.5.0 - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) + - [ChainRecBreadthFirst](#chainrecbreadthfirst) + - [ChainRecDepthFirst](#chainrecdepthfirst) - [Compactable](#compactable-1) - [Extend](#extend-1) - [Filterable](#filterable-1) @@ -1627,6 +1629,26 @@ export declare const Chain: Chain1<'ReadonlyArray'> Added in v2.10.0 +## ChainRecBreadthFirst + +**Signature** + +```ts +export declare const ChainRecBreadthFirst: ChainRec1<'ReadonlyArray'> +``` + +Added in v2.11.0 + +## ChainRecDepthFirst + +**Signature** + +```ts +export declare const ChainRecDepthFirst: ChainRec1<'ReadonlyArray'> +``` + +Added in v2.11.0 + ## Compactable **Signature** diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index c7a4be8d1..4c37f6286 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -223,7 +223,7 @@ export declare function reduceRight( export declare function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B ``` -Added in v2.11.0 +Added in v2.5.0 # Witherable @@ -470,7 +470,7 @@ Added in v2.5.0 export declare const getFoldable: (O: Ord) => Foldable1 ``` -Added in v2.7.0 +Added in v2.11.0 ## getFoldableWithIndex @@ -577,7 +577,7 @@ Added in v2.11.0 ## ~~FoldableWithIndex~~ -Use `getFoldableWithIndex` instead +Use `getFoldableWithIndex` instead. **Signature** @@ -589,7 +589,7 @@ Added in v2.7.0 ## ~~Foldable~~ -Use `getFoldable` instead +Use `getFoldable` instead. **Signature** @@ -601,7 +601,7 @@ Added in v2.7.0 ## ~~TraversableWithIndex~~ -Use `getTraversableWithIndex` instead +Use `getTraversableWithIndex` instead. **Signature** @@ -613,7 +613,7 @@ Added in v2.7.0 ## ~~Traversable~~ -Use `getTraversable` instead +Use `getTraversable` instead. **Signature** @@ -625,7 +625,7 @@ Added in v2.7.0 ## ~~Witherable~~ -Use `getWitherable` instead +Use `getWitherable` instead. **Signature** @@ -715,7 +715,7 @@ assert.deepStrictEqual(collect(Ord)((key, val) => ({ key: key, value: val }))(x) ]) ``` -Added in v2.11.0 +Added in v2.5.0 ## difference @@ -798,7 +798,7 @@ export declare function foldMapWithIndex( ): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M ``` -Added in v2.11.0 +Added in v2.5.0 ## fromFoldable @@ -1048,7 +1048,7 @@ export declare function reduceRightWithIndex( ): (fa: ReadonlyRecord) => B ``` -Added in v2.11.0 +Added in v2.5.0 ## reduceWithIndex @@ -1077,7 +1077,7 @@ export declare function reduceWithIndex( ): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B ``` -Added in v2.11.0 +Added in v2.5.0 ## sequence diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index c613e13d3..688c7a1f4 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -341,7 +341,7 @@ Added in v2.0.0 export declare const getFoldable: (O: Ord) => Foldable1 ``` -Added in v2.7.0 +Added in v2.11.0 ## getFoldableWithIndex @@ -351,7 +351,7 @@ Added in v2.7.0 export declare const getFoldableWithIndex: (O: Ord) => FoldableWithIndex1 ``` -Added in v2.7.0 +Added in v2.11.0 ## getMonoid @@ -404,7 +404,7 @@ Added in v2.11.0 export declare const getTraversableWithIndex: (O: Ord) => TraversableWithIndex1 ``` -Added in v2.7.0 +Added in v2.11.0 ## getWitherable @@ -414,11 +414,11 @@ Added in v2.7.0 export declare const getWitherable: (O: Ord) => Witherable1 ``` -Added in v2.7.0 +Added in v2.11.0 ## ~~FoldableWithIndex~~ -Use `getFoldableWithIndex` instead +Use `getFoldableWithIndex` instead. **Signature** @@ -430,7 +430,7 @@ Added in v2.7.0 ## ~~Foldable~~ -Use `getFoldable` instead +Use `getFoldable` instead. **Signature** @@ -442,7 +442,7 @@ Added in v2.7.0 ## ~~TraversableWithIndex~~ -Use the `getTraversableWithIndex` instead +Use the `getTraversableWithIndex` instead. **Signature** @@ -454,7 +454,7 @@ Added in v2.7.0 ## ~~Traversable~~ -Use `getTraversable` instead +Use `getTraversable` instead. **Signature** @@ -466,7 +466,7 @@ Added in v2.7.0 ## ~~Witherable~~ -Use `getWitherable` instead +Use `getWitherable` instead. **Signature** @@ -851,7 +851,7 @@ export declare function reduceRightWithIndex( ): (fa: Record) => B ``` -Added in v2.11.0 +Added in v2.0.0 ## reduceWithIndex @@ -867,7 +867,7 @@ export declare function reduceWithIndex( ): (fa: Record) => B ``` -Added in v2.11.0 +Added in v2.0.0 ## sequence diff --git a/package.json b/package.json index 093a2ff11..84a54dcf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fp-ts", - "version": "2.10.0-rc.5", + "version": "2.11.0-rc.1", "description": "Functional programming in TypeScript", "main": "lib/index.js", "module": "es6/index.js", diff --git a/src/Array.ts b/src/Array.ts index 9feb663e9..7b833b2c3 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -2000,9 +2000,8 @@ export const Witherable: Witherable1 = { } /** - * Exposing depth first recursion by default * @category instances - * @since 2.10.0 + * @since 2.11.0 */ export const ChainRecDepthFirst: ChainRec1 = { URI, @@ -2014,7 +2013,7 @@ export const ChainRecDepthFirst: ChainRec1 = { /** * @category instances - * @since 2.10.0 + * @since 2.11.0 */ export const ChainRecBreadthFirst: ChainRec1 = { URI, diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index ed11a9f3c..e7146744e 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -2134,9 +2134,8 @@ export const TraversableWithIndex: TraversableWithIndex1 = { } /** - * Exposing depth first recursion by default * @category instances - * @since 2.10.0 + * @since 2.11.0 */ export const ChainRecDepthFirst: ChainRec1 = { URI, @@ -2148,7 +2147,7 @@ export const ChainRecDepthFirst: ChainRec1 = { /** * @category instances - * @since 2.10.0 + * @since 2.11.0 */ export const ChainRecBreadthFirst: ChainRec1 = { URI, diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index d893b33df..bd59b9a81 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -86,11 +86,6 @@ export const isEmpty = (r: ReadonlyRecord): boolean => { export const keys = (r: ReadonlyRecord): ReadonlyArray => (Object.keys(r) as any).sort() -/** - * - * @internal - * @since 2.11.0 - */ const keysWithOrd = (O: Ord) => (r: ReadonlyRecord): ReadonlyArray => (Object.keys(r) as any).sort(O.compare) @@ -107,17 +102,15 @@ const keysWithOrd = (O: Ord) => (r: ReadonlyRecord ): (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => ReadonlyArray - /** - * Use the other overload instead + * Use the overload constrained by `Ord` instead. * * @deprecated - * @since 2.5.0 */ export function collect(f: (k: K, a: A) => B): (r: ReadonlyRecord) => ReadonlyArray export function collect( @@ -359,7 +352,7 @@ export function map(f: (a: A) => B): (fa: ReadonlyRecord) => Re } /** - * @since 2.11.0 + * @since 2.5.0 */ export function reduceWithIndex( O: Ord @@ -367,11 +360,9 @@ export function reduceWithIndex( export function reduceWithIndex( O: Ord ): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B - /** - * Use the other overloads instead + * Use the overload constrained by `Ord` instead. * - * @since 2.5.0 * @deprecated */ export function reduceWithIndex( @@ -379,10 +370,6 @@ export function reduceWithIndex( f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B export function reduceWithIndex(b: B, f: (k: string, b: B, a: A) => B): (fa: ReadonlyRecord) => B - -/** - * @since 2.11.0 - */ export function reduceWithIndex( O: Ord ): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B @@ -422,7 +409,7 @@ export function reduceWithIndex( } /** - * @since 2.11.0 + * @since 2.5.0 */ export function foldMapWithIndex( O: Ord @@ -430,11 +417,9 @@ export function foldMapWithIndex( export function foldMapWithIndex( O: Ord ): (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M - /** - * Use the other overloads instead + * Use the overload constrained by `Ord` instead. * - * @since 2.5.0 * @deprecated */ export function foldMapWithIndex( @@ -443,7 +428,6 @@ export function foldMapWithIndex( export function foldMapWithIndex( M: Monoid ): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M - export function foldMapWithIndex( arg: Ord | Monoid ): @@ -476,7 +460,7 @@ export function foldMapWithIndex( } /** - * @since 2.11.0 + * @since 2.5.0 */ export function reduceRightWithIndex( O: Ord @@ -484,11 +468,9 @@ export function reduceRightWithIndex( export function reduceRightWithIndex( O: Ord ): (b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B - /** - * Use the other overloads instead + * Use the overload constrained by `Ord` instead. * - * @since 2.5.0 * @deprecated */ export function reduceRightWithIndex( @@ -1205,17 +1187,15 @@ export function foldMap( /** * @category Foldable - * @since 2.11.0 + * @since 2.5.0 */ export function reduceRight( O: Ord ): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B - /** * Use the other overload instead * * @category Foldable - * @since 2.5.0 * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B @@ -1297,12 +1277,10 @@ declare module './HKT' { * @since 2.5.0 */ export function getShow(O: Ord): (S: Show) => Show> - /** * Use the other overload instead * * @category instances - * @since 2.5.0 * @deprecated */ export function getShow(S: Show): Show> @@ -1401,7 +1379,7 @@ export const FunctorWithIndex: FunctorWithIndex1 = { } /** - * Use `getFoldable` instead + * Use `getFoldable` instead. * * @category instances * @since 2.7.0 @@ -1416,7 +1394,7 @@ export const Foldable: Foldable1 = { /** * @category instances - * @since 2.7.0 + * @since 2.11.0 */ export const getFoldable = (O: Ord): Foldable1 => ({ URI, @@ -1426,7 +1404,7 @@ export const getFoldable = (O: Ord): Foldable1 => ({ }) /** - * Use `getFoldableWithIndex` instead + * Use `getFoldableWithIndex` instead. * * @category instances * @since 2.7.0 @@ -1502,7 +1480,7 @@ export const FilterableWithIndex: FilterableWithIndex1 = { } /** - * Use `getTraversable` instead + * Use `getTraversable` instead. * * @category instances * @since 2.7.0 @@ -1533,7 +1511,7 @@ export const getTraversable = (O: Ord): Traversable1 => ({ }) /** - * Use `getTraversableWithIndex` instead + * Use `getTraversableWithIndex` instead. * * @category instances * @since 2.7.0 @@ -1574,7 +1552,7 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1) => boolean = RR.isEmpty */ export const keys: (r: Record) => Array = RR.keys as any -/** - * - * @internal - * @since 2.11.0 - */ const keysWithOrd = (O: Ord) => (r: Record): ReadonlyArray => (Object.keys(r) as any).sort(O.compare) @@ -76,11 +71,9 @@ const keysWithOrd = (O: Ord) => (r: Record * @since 2.0.0 */ export function collect(O: Ord): (f: (k: K, a: A) => B) => (r: Record) => Array - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * - * @since 2.0.0 * @deprecated */ export function collect(f: (k: K, a: A) => B): (r: Record) => Array @@ -245,16 +238,14 @@ export const mapWithIndex: (f: (k: K, a: A) => B) => (fa export const map: (f: (a: A) => B) => (fa: Record) => Record = RR.map /** - * @since 2.11.0 + * @since 2.0.0 */ export function reduceWithIndex( O: Ord ): (b: B, f: (k: K, b: B, a: A) => B) => (fa: Record) => B - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * - * @since 2.0.0 * @deprecated */ export function reduceWithIndex(b: B, f: (k: K, b: B, a: A) => B): (fa: Record) => B @@ -276,11 +267,9 @@ export function reduceWithIndex( export function foldMapWithIndex( O: Ord ): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: Record) => M - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * - * @since 2.0.0 * @deprecated */ export function foldMapWithIndex( @@ -299,16 +288,14 @@ export function foldMapWithIndex( } /** - * @since 2.11.0 + * @since 2.0.0 */ export function reduceRightWithIndex( O: Ord ): (b: B, f: (k: K, a: A, b: B) => B) => (fa: Record) => B - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * - * @since 2.0.0 * @deprecated */ export function reduceRightWithIndex(b: B, f: (k: K, a: A, b: B) => B): (fa: Record) => B @@ -691,12 +678,10 @@ export const filterMap: (f: (a: A) => Option) => (fa: Record * @since 2.0.0 */ export function foldMap(O: Ord): (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * * @category Foldable - * @since 2.0.0 * @deprecated */ export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M @@ -736,12 +721,10 @@ export const partitionMap: ( * @since 2.0.0 */ export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Record) => B - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * * @category Foldable - * @since 2.0.0 * @deprecated */ export function reduce(b: B, f: (b: B, a: A) => B): (fa: Record) => B @@ -756,17 +739,16 @@ export function reduce( } return RR.reduce(args[0]) } + /** * @category Foldable * @since 2.0.0 */ export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) => (fa: Record) => B - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * * @category Foldable - * @since 2.0.0 * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record) => B @@ -822,12 +804,10 @@ declare module './HKT' { * @since 2.0.0 */ export function getShow(O: Ord): (S: Show) => Show> - /** - * Use the other overload + * Use the overload constrained by `Ord` instead. * * @category instances - * @since 2.0.0 * @deprecated */ export function getShow(S: Show): Show> @@ -892,7 +872,7 @@ export const FunctorWithIndex: FunctorWithIndex1 = { } /** - * Use `getFoldable` instead + * Use `getFoldable` instead. * * @category instances * @since 2.7.0 @@ -907,7 +887,7 @@ export const Foldable: Foldable1 = { /** * @category instances - * @since 2.7.0 + * @since 2.11.0 */ export const getFoldable = (O: Ord): Foldable1 => ({ URI, @@ -917,7 +897,7 @@ export const getFoldable = (O: Ord): Foldable1 => ({ }) /** - * Use `getFoldableWithIndex` instead + * Use `getFoldableWithIndex` instead. * * @category instances * @since 2.7.0 @@ -935,7 +915,7 @@ export const FoldableWithIndex: FoldableWithIndex1 = { /** * @category instances - * @since 2.7.0 + * @since 2.11.0 */ export const getFoldableWithIndex = (O: Ord): FoldableWithIndex1 => ({ URI, @@ -993,7 +973,7 @@ export const FilterableWithIndex: FilterableWithIndex1 = { } /** - * Use `getTraversable` instead + * Use `getTraversable` instead. * * @category instances * @since 2.7.0 @@ -1008,6 +988,7 @@ export const Traversable: Traversable1 = { traverse: _traverse, sequence } + /** * @category instances * @since 2.11.0 @@ -1023,7 +1004,7 @@ export const getTraversable = (O: Ord): Traversable1 => ({ }) /** - * Use the `getTraversableWithIndex` instead + * Use the `getTraversableWithIndex` instead. * * @category instances * @since 2.7.0 @@ -1046,7 +1027,7 @@ export const TraversableWithIndex: TraversableWithIndex1 = { /** * @category instances - * @since 2.7.0 + * @since 2.11.0 */ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 => ({ URI, @@ -1064,7 +1045,7 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { /** * @category instances - * @since 2.7.0 + * @since 2.11.0 */ export const getWitherable = (O: Ord): Witherable1 => ({ URI, diff --git a/test/Witherable.ts b/test/Witherable.ts index e76d25633..fa80b393f 100644 --- a/test/Witherable.ts +++ b/test/Witherable.ts @@ -3,13 +3,14 @@ import * as RT from '../src/ReaderTask' import * as RA from '../src/ReadonlyArray' import * as RR from '../src/ReadonlyRecord' import * as T from '../src/Task' +import * as S from '../src/string' import * as _ from '../src/Witherable' import * as U from './util' describe('Witherable', () => { describe('filterE', () => { const filterERA = _.filterE(RA.Witherable) - const filterERR = _.filterE(RR.Witherable) + const filterERR = _.filterE(RR.getWitherable(S.Ord)) it('Applicative1', async () => { const f = (n: number) => T.of(n % 2 === 0) From 826c65a86c7a5a2cedc9ce718b74da82088e95e4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 09:42:01 +0200 Subject: [PATCH 017/162] rename object module to struct --- docs/modules/Option.ts.md | 2 +- docs/modules/OptionT.ts.md | 2 +- docs/modules/Ord.ts.md | 2 +- docs/modules/Ordering.ts.md | 2 +- docs/modules/Pointed.ts.md | 2 +- docs/modules/Profunctor.ts.md | 2 +- docs/modules/Random.ts.md | 2 +- docs/modules/Reader.ts.md | 2 +- docs/modules/ReaderEither.ts.md | 2 +- docs/modules/ReaderT.ts.md | 2 +- docs/modules/ReaderTask.ts.md | 2 +- docs/modules/ReaderTaskEither.ts.md | 2 +- docs/modules/ReadonlyArray.ts.md | 2 +- docs/modules/ReadonlyMap.ts.md | 2 +- docs/modules/ReadonlyNonEmptyArray.ts.md | 2 +- docs/modules/ReadonlyRecord.ts.md | 2 +- docs/modules/ReadonlySet.ts.md | 2 +- docs/modules/ReadonlyTuple.ts.md | 2 +- docs/modules/Record.ts.md | 2 +- docs/modules/Ring.ts.md | 2 +- docs/modules/Semigroup.ts.md | 2 +- docs/modules/Semigroupoid.ts.md | 2 +- docs/modules/Semiring.ts.md | 2 +- docs/modules/Separated.ts.md | 2 +- docs/modules/Set.ts.md | 2 +- docs/modules/Show.ts.md | 2 +- docs/modules/State.ts.md | 2 +- docs/modules/StateReaderTaskEither.ts.md | 2 +- docs/modules/StateT.ts.md | 2 +- docs/modules/Store.ts.md | 2 +- docs/modules/Strong.ts.md | 2 +- docs/modules/index.ts.md | 22 ++++++++++----------- docs/modules/pipeable.ts.md | 2 +- docs/modules/string.ts.md | 2 +- docs/modules/{object.ts.md => struct.ts.md} | 8 ++++---- src/index.ts | 10 +++++----- src/{object.ts => struct.ts} | 2 +- test/{object.ts => struct.ts} | 4 ++-- 38 files changed, 56 insertions(+), 56 deletions(-) rename docs/modules/{object.ts.md => struct.ts.md} (86%) rename src/{object.ts => struct.ts} (93%) rename test/{object.ts => struct.ts} (85%) diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 690d8a432..7115619c2 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1,6 +1,6 @@ --- title: Option.ts -nav_order: 64 +nav_order: 63 parent: Modules --- diff --git a/docs/modules/OptionT.ts.md b/docs/modules/OptionT.ts.md index 46f202546..fa6e2fc45 100644 --- a/docs/modules/OptionT.ts.md +++ b/docs/modules/OptionT.ts.md @@ -1,6 +1,6 @@ --- title: OptionT.ts -nav_order: 65 +nav_order: 64 parent: Modules --- diff --git a/docs/modules/Ord.ts.md b/docs/modules/Ord.ts.md index a36edc446..1dda34782 100644 --- a/docs/modules/Ord.ts.md +++ b/docs/modules/Ord.ts.md @@ -1,6 +1,6 @@ --- title: Ord.ts -nav_order: 66 +nav_order: 65 parent: Modules --- diff --git a/docs/modules/Ordering.ts.md b/docs/modules/Ordering.ts.md index 76390b92a..66472aab8 100644 --- a/docs/modules/Ordering.ts.md +++ b/docs/modules/Ordering.ts.md @@ -1,6 +1,6 @@ --- title: Ordering.ts -nav_order: 67 +nav_order: 66 parent: Modules --- diff --git a/docs/modules/Pointed.ts.md b/docs/modules/Pointed.ts.md index 1023184ca..f5697a148 100644 --- a/docs/modules/Pointed.ts.md +++ b/docs/modules/Pointed.ts.md @@ -1,6 +1,6 @@ --- title: Pointed.ts -nav_order: 69 +nav_order: 68 parent: Modules --- diff --git a/docs/modules/Profunctor.ts.md b/docs/modules/Profunctor.ts.md index e904b742f..65e5bbb89 100644 --- a/docs/modules/Profunctor.ts.md +++ b/docs/modules/Profunctor.ts.md @@ -1,6 +1,6 @@ --- title: Profunctor.ts -nav_order: 70 +nav_order: 69 parent: Modules --- diff --git a/docs/modules/Random.ts.md b/docs/modules/Random.ts.md index 704057a69..6cf875b75 100644 --- a/docs/modules/Random.ts.md +++ b/docs/modules/Random.ts.md @@ -1,6 +1,6 @@ --- title: Random.ts -nav_order: 71 +nav_order: 70 parent: Modules --- diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index ff5e111d6..2647ca91c 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -1,6 +1,6 @@ --- title: Reader.ts -nav_order: 72 +nav_order: 71 parent: Modules --- diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 26885f053..1061357c3 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderEither.ts -nav_order: 73 +nav_order: 72 parent: Modules --- diff --git a/docs/modules/ReaderT.ts.md b/docs/modules/ReaderT.ts.md index fbda0b450..c387f5ea8 100644 --- a/docs/modules/ReaderT.ts.md +++ b/docs/modules/ReaderT.ts.md @@ -1,6 +1,6 @@ --- title: ReaderT.ts -nav_order: 74 +nav_order: 73 parent: Modules --- diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index e94a5235e..2ac8d5fb2 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTask.ts -nav_order: 75 +nav_order: 74 parent: Modules --- diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index c104aab6d..295097d9b 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTaskEither.ts -nav_order: 76 +nav_order: 75 parent: Modules --- diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 158b12e67..e9f7d47a8 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyArray.ts -nav_order: 77 +nav_order: 76 parent: Modules --- diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index d1d6aceea..0fd6a94c6 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyMap.ts -nav_order: 78 +nav_order: 77 parent: Modules --- diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 8a46b73bb..adf9a3aed 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyNonEmptyArray.ts -nav_order: 79 +nav_order: 78 parent: Modules --- diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 4c37f6286..56477ecd5 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyRecord.ts -nav_order: 80 +nav_order: 79 parent: Modules --- diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 988027bbb..0878364e7 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlySet.ts -nav_order: 81 +nav_order: 80 parent: Modules --- diff --git a/docs/modules/ReadonlyTuple.ts.md b/docs/modules/ReadonlyTuple.ts.md index df1b0d7fb..30ae39420 100644 --- a/docs/modules/ReadonlyTuple.ts.md +++ b/docs/modules/ReadonlyTuple.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyTuple.ts -nav_order: 82 +nav_order: 81 parent: Modules --- diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 688c7a1f4..0a46ed255 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -1,6 +1,6 @@ --- title: Record.ts -nav_order: 83 +nav_order: 82 parent: Modules --- diff --git a/docs/modules/Ring.ts.md b/docs/modules/Ring.ts.md index 0ee71ef23..5d44a01c9 100644 --- a/docs/modules/Ring.ts.md +++ b/docs/modules/Ring.ts.md @@ -1,6 +1,6 @@ --- title: Ring.ts -nav_order: 84 +nav_order: 83 parent: Modules --- diff --git a/docs/modules/Semigroup.ts.md b/docs/modules/Semigroup.ts.md index 10f026520..80c1d755d 100644 --- a/docs/modules/Semigroup.ts.md +++ b/docs/modules/Semigroup.ts.md @@ -1,6 +1,6 @@ --- title: Semigroup.ts -nav_order: 85 +nav_order: 84 parent: Modules --- diff --git a/docs/modules/Semigroupoid.ts.md b/docs/modules/Semigroupoid.ts.md index bdb014485..f5da9f31e 100644 --- a/docs/modules/Semigroupoid.ts.md +++ b/docs/modules/Semigroupoid.ts.md @@ -1,6 +1,6 @@ --- title: Semigroupoid.ts -nav_order: 86 +nav_order: 85 parent: Modules --- diff --git a/docs/modules/Semiring.ts.md b/docs/modules/Semiring.ts.md index 391d92d1e..2dc848d74 100644 --- a/docs/modules/Semiring.ts.md +++ b/docs/modules/Semiring.ts.md @@ -1,6 +1,6 @@ --- title: Semiring.ts -nav_order: 87 +nav_order: 86 parent: Modules --- diff --git a/docs/modules/Separated.ts.md b/docs/modules/Separated.ts.md index a2d835700..a327b4a61 100644 --- a/docs/modules/Separated.ts.md +++ b/docs/modules/Separated.ts.md @@ -1,6 +1,6 @@ --- title: Separated.ts -nav_order: 88 +nav_order: 87 parent: Modules --- diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 931d9d9c9..2861fd36d 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -1,6 +1,6 @@ --- title: Set.ts -nav_order: 89 +nav_order: 88 parent: Modules --- diff --git a/docs/modules/Show.ts.md b/docs/modules/Show.ts.md index 81e39ea11..a21ec9435 100644 --- a/docs/modules/Show.ts.md +++ b/docs/modules/Show.ts.md @@ -1,6 +1,6 @@ --- title: Show.ts -nav_order: 90 +nav_order: 89 parent: Modules --- diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index 3b7790f8b..a44624301 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -1,6 +1,6 @@ --- title: State.ts -nav_order: 91 +nav_order: 90 parent: Modules --- diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 4eba70da7..ea079662c 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: StateReaderTaskEither.ts -nav_order: 92 +nav_order: 91 parent: Modules --- diff --git a/docs/modules/StateT.ts.md b/docs/modules/StateT.ts.md index 78ca24aac..83182aad9 100644 --- a/docs/modules/StateT.ts.md +++ b/docs/modules/StateT.ts.md @@ -1,6 +1,6 @@ --- title: StateT.ts -nav_order: 93 +nav_order: 92 parent: Modules --- diff --git a/docs/modules/Store.ts.md b/docs/modules/Store.ts.md index 4a899f00c..90df33d43 100644 --- a/docs/modules/Store.ts.md +++ b/docs/modules/Store.ts.md @@ -1,6 +1,6 @@ --- title: Store.ts -nav_order: 94 +nav_order: 93 parent: Modules --- diff --git a/docs/modules/Strong.ts.md b/docs/modules/Strong.ts.md index 7f356e75c..b4ea62010 100644 --- a/docs/modules/Strong.ts.md +++ b/docs/modules/Strong.ts.md @@ -1,6 +1,6 @@ --- title: Strong.ts -nav_order: 96 +nav_order: 95 parent: Modules --- diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index d8d0826ee..ac9dc8a58 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -73,7 +73,6 @@ Added in v2.0.0 - [monoid](#monoid) - [nonEmptyArray](#nonemptyarray) - [number](#number) - - [object](#object) - [option](#option) - [optionT](#optiont) - [ord](#ord) @@ -107,6 +106,7 @@ Added in v2.0.0 - [store](#store) - [string](#string) - [strong](#strong) + - [struct](#struct) - [task](#task) - [taskEither](#taskeither) - [taskOption](#taskoption) @@ -728,16 +728,6 @@ export declare const number: typeof number Added in v2.10.0 -## object - -**Signature** - -```ts -export declare const object: typeof object -``` - -Added in v2.10.0 - ## option **Signature** @@ -1068,6 +1058,16 @@ export declare const strong: typeof strong Added in v2.0.0 +## struct + +**Signature** + +```ts +export declare const struct: typeof struct +``` + +Added in v2.10.0 + ## task **Signature** diff --git a/docs/modules/pipeable.ts.md b/docs/modules/pipeable.ts.md index 0fde60f66..4d3646173 100644 --- a/docs/modules/pipeable.ts.md +++ b/docs/modules/pipeable.ts.md @@ -1,6 +1,6 @@ --- title: pipeable.ts -nav_order: 68 +nav_order: 67 parent: Modules --- diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index e61f31cfe..0447861e1 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -1,6 +1,6 @@ --- title: string.ts -nav_order: 95 +nav_order: 94 parent: Modules --- diff --git a/docs/modules/object.ts.md b/docs/modules/struct.ts.md similarity index 86% rename from docs/modules/object.ts.md rename to docs/modules/struct.ts.md index 20f5a9732..87660a046 100644 --- a/docs/modules/object.ts.md +++ b/docs/modules/struct.ts.md @@ -1,10 +1,10 @@ --- -title: object.ts -nav_order: 63 +title: struct.ts +nav_order: 96 parent: Modules --- -## object overview +## struct overview Added in v2.10.0 @@ -32,7 +32,7 @@ export declare const getAssignSemigroup: () => Semigro **Example** ```ts -import { getAssignSemigroup } from 'fp-ts/object' +import { getAssignSemigroup } from 'fp-ts/struct' interface Person { readonly name: string diff --git a/src/index.ts b/src/index.ts index 61430b9f1..10bcaab18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,7 +62,6 @@ import * as monadThrow from './MonadThrow' import * as monoid from './Monoid' import * as nonEmptyArray from './NonEmptyArray' import * as number from './number' -import * as object from './object' import * as option from './Option' import * as optionT from './OptionT' import * as ord from './Ord' @@ -96,6 +95,7 @@ import * as stateT from './StateT' import * as store from './Store' import * as string from './string' import * as strong from './Strong' +import * as struct from './struct' import * as task from './Task' import * as taskEither from './TaskEither' import * as taskOption from './TaskOption' @@ -349,10 +349,6 @@ export { * @since 2.10.0 */ number, - /** - * @since 2.10.0 - */ - object, /** * @since 2.0.0 */ @@ -489,6 +485,10 @@ export { * @since 2.0.0 */ strong, + /** + * @since 2.10.0 + */ + struct, /** * @since 2.0.0 */ diff --git a/src/object.ts b/src/struct.ts similarity index 93% rename from src/object.ts rename to src/struct.ts index 46b7d5298..8dde2104a 100644 --- a/src/object.ts +++ b/src/struct.ts @@ -11,7 +11,7 @@ import { getObjectSemigroup, Semigroup } from './Semigroup' * Return a semigroup which works like `Object.assign`. * * @example - * import { getAssignSemigroup } from 'fp-ts/object' + * import { getAssignSemigroup } from 'fp-ts/struct' * * interface Person { * readonly name: string diff --git a/test/object.ts b/test/struct.ts similarity index 85% rename from test/object.ts rename to test/struct.ts index cbd58d503..8295d5d3b 100644 --- a/test/object.ts +++ b/test/struct.ts @@ -1,7 +1,7 @@ -import * as _ from '../src/object' +import * as _ from '../src/struct' import * as U from './util' -describe('object', () => { +describe('struct', () => { it('getAssignSemigroup', () => { type T = { readonly foo?: number From 566ec190a86f85f5c85cee776ee1855779e8f580 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 10:16:03 +0200 Subject: [PATCH 018/162] Record / ReadonlyRecord: clean up overloads --- CHANGELOG.md | 2 + docs/modules/ReadonlyRecord.ts.md | 26 ------- src/ReadonlyRecord.ts | 112 ++++++++++++------------------ src/Record.ts | 73 ++++++++++--------- 4 files changed, 81 insertions(+), 132 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6593ed03..1c90daf9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ high state of flux, you're at risk of it changing without notice. - add `SK` (@cdimitroulas) - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - `Ord` + - add `trivial` instance - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 56477ecd5..837022278 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -787,15 +787,9 @@ Added in v2.5.0 export declare function foldMapWithIndex( O: Ord ): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M -export declare function foldMapWithIndex( - O: Ord -): (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M export declare function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M -export declare function foldMapWithIndex( - M: Monoid -): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M ``` Added in v2.5.0 @@ -1035,17 +1029,10 @@ Added in v2.5.0 export declare function reduceRightWithIndex( O: Ord ): (b: B, f: (k: K, a: A, b: B) => B) => (fa: ReadonlyRecord) => B -export declare function reduceRightWithIndex( - O: Ord -): (b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B export declare function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B ): (fa: ReadonlyRecord) => B -export declare function reduceRightWithIndex( - b: B, - f: (k: string, a: A, b: B) => B -): (fa: ReadonlyRecord) => B ``` Added in v2.5.0 @@ -1058,23 +1045,10 @@ Added in v2.5.0 export declare function reduceWithIndex( O: Ord ): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B -export declare function reduceWithIndex( - O: Ord -): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B export declare function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B -export declare function reduceWithIndex( - b: B, - f: (k: string, b: B, a: A) => B -): (fa: ReadonlyRecord) => B -export declare function reduceWithIndex( - O: Ord -): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B -export declare function reduceWithIndex( - O: Ord -): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B ``` Added in v2.5.0 diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index bd59b9a81..99e322343 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -21,7 +21,7 @@ import { Ord } from './Ord' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' -import { Ord as OrdString } from './string' +import * as S from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' @@ -80,14 +80,15 @@ export const isEmpty = (r: ReadonlyRecord): boolean => { return true } +const keys_ = (O: Ord) => (r: ReadonlyRecord): ReadonlyArray => + (Object.keys(r) as any).sort(O.compare) + /** * @since 2.5.0 */ -export const keys = (r: ReadonlyRecord): ReadonlyArray => - (Object.keys(r) as any).sort() - -const keysWithOrd = (O: Ord) => (r: ReadonlyRecord): ReadonlyArray => - (Object.keys(r) as any).sort(O.compare) +export const keys: (r: ReadonlyRecord) => ReadonlyArray = + /*#__PURE__*/ + keys_(S.Ord) /** * Map a `ReadonlyRecord` into an `ReadonlyArray`. @@ -129,7 +130,7 @@ export function collect( } return (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => { const out: Array = [] - for (const key of keysWithOrd(O)(r)) { + for (const key of keys_(O)(r)) { out.push(f(key, r[key])) } return out @@ -143,7 +144,7 @@ export function collect( */ export const toReadonlyArray: (r: ReadonlyRecord) => ReadonlyArray = /*#__PURE__*/ - collect(OrdString)((k, a) => [k, a]) + collect(S.Ord)((k, a) => [k, a]) /** * Unfolds a `ReadonlyRecord` into a list of key/value pairs. @@ -357,9 +358,6 @@ export function map(f: (a: A) => B): (fa: ReadonlyRecord) => Re export function reduceWithIndex( O: Ord ): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B -export function reduceWithIndex( - O: Ord -): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B /** * Use the overload constrained by `Ord` instead. * @@ -369,13 +367,6 @@ export function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B -export function reduceWithIndex(b: B, f: (k: string, b: B, a: A) => B): (fa: ReadonlyRecord) => B -export function reduceWithIndex( - O: Ord -): (b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord) => B -export function reduceWithIndex( - O: Ord -): (b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B export function reduceWithIndex( ...args: [Ord] | [unknown, (k: string, b: unknown, a: unknown) => unknown] ): @@ -398,7 +389,7 @@ export function reduceWithIndex( } return (b, f) => (fa) => { let out = b - const ks = keysWithOrd(args[0])(fa) + const ks = keys_(args[0])(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] @@ -414,9 +405,6 @@ export function reduceWithIndex( export function foldMapWithIndex( O: Ord ): (M: Monoid) => (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M -export function foldMapWithIndex( - O: Ord -): (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M /** * Use the overload constrained by `Ord` instead. * @@ -425,9 +413,6 @@ export function foldMapWithIndex( export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M -export function foldMapWithIndex( - M: Monoid -): (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M export function foldMapWithIndex( arg: Ord | Monoid ): @@ -438,7 +423,7 @@ export function foldMapWithIndex( if ('compare' in arg) { return (M: Monoid) => (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => { let out = M.empty - const ks = keysWithOrd(arg)(fa) + const ks = keys_(arg)(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] @@ -465,9 +450,6 @@ export function foldMapWithIndex( export function reduceRightWithIndex( O: Ord ): (b: B, f: (k: K, a: A, b: B) => B) => (fa: ReadonlyRecord) => B -export function reduceRightWithIndex( - O: Ord -): (b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B /** * Use the overload constrained by `Ord` instead. * @@ -477,7 +459,6 @@ export function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B ): (fa: ReadonlyRecord) => B -export function reduceRightWithIndex(b: B, f: (k: string, a: A, b: B) => B): (fa: ReadonlyRecord) => B export function reduceRightWithIndex( ...args: [Ord] | [unknown, (k: string, a: unknown, b: unknown) => unknown] ): @@ -500,7 +481,7 @@ export function reduceRightWithIndex( } return (b, f) => (fa) => { let out = b - const ks = keysWithOrd(args[0])(fa) + const ks = keys_(args[0])(fa) const len = ks.length for (let i = len - 1; i >= 0; i--) { const k = ks[i] @@ -1132,12 +1113,9 @@ export const partitionMap: ( * @since 2.5.0 */ export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B - /** - * Use other overload instead + * Use the overload constrained by `Ord` instead. * - * @category Foldable - * @since 2.5.0 * @deprecated */ export function reduce(b: B, f: (b: B, a: A) => B): (fa: Readonly>) => B @@ -1160,12 +1138,9 @@ export function reduce( export function foldMap( O: Ord ): (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M - /** - * Use the other overload instead + * Use the overload constrained by `Ord` instead. * - * @category Foldable - * @since 2.5.0 * @deprecated */ export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Readonly>) => M @@ -1193,9 +1168,8 @@ export function reduceRight( O: Ord ): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B /** - * Use the other overload instead + * Use the overload constrained by `Ord` instead. * - * @category Foldable * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B @@ -1278,7 +1252,7 @@ declare module './HKT' { */ export function getShow(O: Ord): (S: Show) => Show> /** - * Use the other overload instead + * Use the overload constrained by `Ord` instead. * * @category instances * @deprecated @@ -1387,9 +1361,9 @@ export const FunctorWithIndex: FunctorWithIndex1 = { */ export const Foldable: Foldable1 = { URI, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString) + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord) } /** @@ -1412,12 +1386,12 @@ export const getFoldable = (O: Ord): Foldable1 => ({ */ export const FoldableWithIndex: FoldableWithIndex1 = { URI, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString) + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord) } /** @@ -1489,9 +1463,9 @@ export const FilterableWithIndex: FilterableWithIndex1 = { export const Traversable: Traversable1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence } @@ -1521,12 +1495,12 @@ export const TraversableWithIndex: TraversableWithIndex1 = { URI, map: _map, mapWithIndex: _mapWithIndex, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), traverse: _traverse, sequence, traverseWithIndex: _traverseWithIndex @@ -1561,9 +1535,9 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence, compact, @@ -1672,9 +1646,9 @@ export const readonlyRecord: FunctorWithIndex1 & Witherable1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence, compact, @@ -1684,9 +1658,9 @@ export const readonlyRecord: FunctorWithIndex1 & partition: _partition, partitionMap: _partitionMap, mapWithIndex: _mapWithIndex, - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), filterMapWithIndex: _filterMapWithIndex, filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, diff --git a/src/Record.ts b/src/Record.ts index 7767fe3aa..72d07f58f 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -21,7 +21,7 @@ import * as RR from './ReadonlyRecord' import { Semigroup } from './Semigroup' import { Separated } from './Separated' import { Show } from './Show' -import { Ord as OrdString } from './string' +import * as S from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' @@ -47,13 +47,15 @@ export const size: (r: Record) => number = RR.size */ export const isEmpty: (r: Record) => boolean = RR.isEmpty +const keys_ = (O: Ord) => (r: Record): Array => + (Object.keys(r) as any).sort(O.compare) + /** * @since 2.0.0 */ -export const keys: (r: Record) => Array = RR.keys as any - -const keysWithOrd = (O: Ord) => (r: Record): ReadonlyArray => - (Object.keys(r) as any).sort(O.compare) +export const keys: (r: Record) => Array = + /*#__PURE__*/ + keys_(S.Ord) /** * Map a `Record` into an `Array`. @@ -93,7 +95,7 @@ export function collect( } return (f: (k: string, a: unknown) => unknown) => (r: Record) => { const out: Array = [] - for (const key of keysWithOrd(arg)(r)) { + for (const key of keys_(arg)(r)) { out.push(f(key, r[key])) } return out @@ -107,7 +109,7 @@ export function collect( */ export const toArray: (r: Record) => Array<[K, A]> = /*#__PURE__*/ - collect(OrdString)((k, a) => [k, a]) + collect(S.Ord)((k, a) => [k, a]) /** * Unfolds a `Record` into a list of key/value pairs. @@ -681,7 +683,6 @@ export function foldMap(O: Ord): (M: Monoid) => (f: (a: A) => M /** * Use the overload constrained by `Ord` instead. * - * @category Foldable * @deprecated */ export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M @@ -724,7 +725,6 @@ export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (f /** * Use the overload constrained by `Ord` instead. * - * @category Foldable * @deprecated */ export function reduce(b: B, f: (b: B, a: A) => B): (fa: Record) => B @@ -748,7 +748,6 @@ export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) /** * Use the overload constrained by `Ord` instead. * - * @category Foldable * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record) => B @@ -880,9 +879,9 @@ export const FunctorWithIndex: FunctorWithIndex1 = { */ export const Foldable: Foldable1 = { URI, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString) + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord) } /** @@ -905,12 +904,12 @@ export const getFoldable = (O: Ord): Foldable1 => ({ */ export const FoldableWithIndex: FoldableWithIndex1 = { URI, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString) + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord) } /** @@ -982,9 +981,9 @@ export const FilterableWithIndex: FilterableWithIndex1 = { export const Traversable: Traversable1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence } @@ -1014,12 +1013,12 @@ export const TraversableWithIndex: TraversableWithIndex1 = { URI, map: _map, mapWithIndex: _mapWithIndex, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), traverse: _traverse, sequence, traverseWithIndex: _traverseWithIndex @@ -1054,9 +1053,9 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence, compact, @@ -1133,9 +1132,9 @@ export const record: FunctorWithIndex1 & Witherable1 = { URI, map: _map, - reduce: _reduce(OrdString), - foldMap: _foldMap(OrdString), - reduceRight: _reduceRight(OrdString), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence, compact, @@ -1145,9 +1144,9 @@ export const record: FunctorWithIndex1 & partition: _partition, partitionMap: _partitionMap, mapWithIndex: _mapWithIndex, - reduceWithIndex: _reduceWithIndex(OrdString), - foldMapWithIndex: _foldMapWithIndex(OrdString), - reduceRightWithIndex: _reduceRightWithIndex(OrdString), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), filterMapWithIndex: _filterMapWithIndex, filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, From 22952c3d32a096db869e0cb281d9cf8562d86ee2 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 10:19:44 +0200 Subject: [PATCH 019/162] Record / ReadonlyRecord: move deprecated instances to the dedicated section --- src/ReadonlyRecord.ts | 190 +++++++++++++++++++++--------------------- src/Record.ts | 164 ++++++++++++++++++------------------ 2 files changed, 177 insertions(+), 177 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 99e322343..229f71d3a 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1352,20 +1352,6 @@ export const FunctorWithIndex: FunctorWithIndex1 = { mapWithIndex: _mapWithIndex } -/** - * Use `getFoldable` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const Foldable: Foldable1 = { - URI, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord) -} - /** * @category instances * @since 2.11.0 @@ -1377,23 +1363,6 @@ export const getFoldable = (O: Ord): Foldable1 => ({ reduceRight: _reduceRight(O) }) -/** - * Use `getFoldableWithIndex` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const FoldableWithIndex: FoldableWithIndex1 = { - URI, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), - reduceWithIndex: _reduceWithIndex(S.Ord), - foldMapWithIndex: _foldMapWithIndex(S.Ord), - reduceRightWithIndex: _reduceRightWithIndex(S.Ord) -} - /** * @category instances * @since 2.11.0 @@ -1453,23 +1422,6 @@ export const FilterableWithIndex: FilterableWithIndex1 = { partitionWithIndex: _partitionWithIndex } -/** - * Use `getTraversable` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const Traversable: Traversable1 = { - URI, - map: _map, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), - traverse: _traverse, - sequence -} - /** * @category instances * @since 2.11.0 @@ -1484,28 +1436,6 @@ export const getTraversable = (O: Ord): Traversable1 => ({ sequence }) -/** - * Use `getTraversableWithIndex` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const TraversableWithIndex: TraversableWithIndex1 = { - URI, - map: _map, - mapWithIndex: _mapWithIndex, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), - reduceWithIndex: _reduceWithIndex(S.Ord), - foldMapWithIndex: _foldMapWithIndex(S.Ord), - reduceRightWithIndex: _reduceRightWithIndex(S.Ord), - traverse: _traverse, - sequence, - traverseWithIndex: _traverseWithIndex -} - /** * @category instances * @since 2.11.0 @@ -1525,31 +1455,6 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { - URI, - map: _map, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), - traverse: _traverse, - sequence, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap, - wither: _wither, - wilt: _wilt -} - /** * @category instances * @since 2.11.0 @@ -1609,6 +1514,101 @@ export const getDifferenceMagma = (): Magma> => ({ // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `getFoldable` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const Foldable: Foldable1 = { + URI, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord) +} + +/** + * Use `getFoldableWithIndex` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const FoldableWithIndex: FoldableWithIndex1 = { + URI, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord) +} + +/** + * Use `getTraversable` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const Traversable: Traversable1 = { + URI, + map: _map, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + traverse: _traverse, + sequence +} + +/** + * Use `getTraversableWithIndex` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const TraversableWithIndex: TraversableWithIndex1 = { + URI, + map: _map, + mapWithIndex: _mapWithIndex, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), + traverse: _traverse, + sequence, + traverseWithIndex: _traverseWithIndex +} + +/** + * Use `getWitherable` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const Witherable: Witherable1 = { + URI, + map: _map, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), + traverse: _traverse, + sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: _wither, + wilt: _wilt +} + /** * Use `upsertAt` instead. * diff --git a/src/Record.ts b/src/Record.ts index 72d07f58f..b1b9e3fba 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -870,20 +870,6 @@ export const FunctorWithIndex: FunctorWithIndex1 = { mapWithIndex: _mapWithIndex } -/** - * Use `getFoldable` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const Foldable: Foldable1 = { - URI, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord) -} - /** * @category instances * @since 2.11.0 @@ -895,23 +881,6 @@ export const getFoldable = (O: Ord): Foldable1 => ({ reduceRight: _reduceRight(O) }) -/** - * Use `getFoldableWithIndex` instead. - * - * @category instances - * @since 2.7.0 - * @deprecated - */ -export const FoldableWithIndex: FoldableWithIndex1 = { - URI, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), - reduceWithIndex: _reduceWithIndex(S.Ord), - foldMapWithIndex: _foldMapWithIndex(S.Ord), - reduceRightWithIndex: _reduceRightWithIndex(S.Ord) -} - /** * @category instances * @since 2.11.0 @@ -972,112 +941,147 @@ export const FilterableWithIndex: FilterableWithIndex1 = { } /** - * Use `getTraversable` instead. - * * @category instances - * @since 2.7.0 - * @deprecated + * @since 2.11.0 */ -export const Traversable: Traversable1 = { +export const getTraversable = (O: Ord): Traversable1 => ({ URI, map: _map, - reduce: _reduce(S.Ord), - foldMap: _foldMap(S.Ord), - reduceRight: _reduceRight(S.Ord), + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), traverse: _traverse, sequence -} +}) /** * @category instances * @since 2.11.0 */ -export const getTraversable = (O: Ord): Traversable1 => ({ +export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 => ({ URI, map: _map, + mapWithIndex: _mapWithIndex, reduce: _reduce(O), foldMap: _foldMap(O), reduceRight: _reduceRight(O), + reduceWithIndex: _reduceWithIndex(O), + foldMapWithIndex: _foldMapWithIndex(O), + reduceRightWithIndex: _reduceRightWithIndex(O), traverse: _traverse, - sequence + sequence, + traverseWithIndex: _traverseWithIndex }) /** - * Use the `getTraversableWithIndex` instead. + * @category instances + * @since 2.11.0 + */ +export const getWitherable = (O: Ord): Witherable1 => ({ + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: _traverse, + sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: _wither, + wilt: _wilt +}) + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +/** + * Use `getFoldable` instead. * * @category instances * @since 2.7.0 * @deprecated */ -export const TraversableWithIndex: TraversableWithIndex1 = { +export const Foldable: Foldable1 = { + URI, + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord) +} + +/** + * Use `getFoldableWithIndex` instead. + * + * @category instances + * @since 2.7.0 + * @deprecated + */ +export const FoldableWithIndex: FoldableWithIndex1 = { URI, - map: _map, - mapWithIndex: _mapWithIndex, reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), reduceWithIndex: _reduceWithIndex(S.Ord), foldMapWithIndex: _foldMapWithIndex(S.Ord), - reduceRightWithIndex: _reduceRightWithIndex(S.Ord), - traverse: _traverse, - sequence, - traverseWithIndex: _traverseWithIndex + reduceRightWithIndex: _reduceRightWithIndex(S.Ord) } /** + * Use `getTraversable` instead. + * * @category instances - * @since 2.11.0 + * @since 2.7.0 + * @deprecated */ -export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 => ({ +export const Traversable: Traversable1 = { URI, map: _map, - mapWithIndex: _mapWithIndex, - reduce: _reduce(O), - foldMap: _foldMap(O), - reduceRight: _reduceRight(O), - reduceWithIndex: _reduceWithIndex(O), - foldMapWithIndex: _foldMapWithIndex(O), - reduceRightWithIndex: _reduceRightWithIndex(O), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, - sequence, - traverseWithIndex: _traverseWithIndex -}) + sequence +} /** - * Use `getWitherable` instead. + * Use the `getTraversableWithIndex` instead. * * @category instances * @since 2.7.0 * @deprecated */ -export const Witherable: Witherable1 = { +export const TraversableWithIndex: TraversableWithIndex1 = { URI, map: _map, + mapWithIndex: _mapWithIndex, reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), + reduceWithIndex: _reduceWithIndex(S.Ord), + foldMapWithIndex: _foldMapWithIndex(S.Ord), + reduceRightWithIndex: _reduceRightWithIndex(S.Ord), traverse: _traverse, sequence, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap, - wither: _wither, - wilt: _wilt + traverseWithIndex: _traverseWithIndex } /** + * Use `getWitherable` instead. + * * @category instances - * @since 2.11.0 + * @since 2.7.0 + * @deprecated */ -export const getWitherable = (O: Ord): Witherable1 => ({ +export const Witherable: Witherable1 = { URI, map: _map, - reduce: _reduce(O), - foldMap: _foldMap(O), - reduceRight: _reduceRight(O), + reduce: _reduce(S.Ord), + foldMap: _foldMap(S.Ord), + reduceRight: _reduceRight(S.Ord), traverse: _traverse, sequence, compact, @@ -1088,11 +1092,7 @@ export const getWitherable = (O: Ord): Witherable1 => ({ partitionMap: _partitionMap, wither: _wither, wilt: _wilt -}) - -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- +} /** * Use a new `{}` instead. From 15acbf8742043fb4d00d691b2f2831f6608cfc63 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 10:39:46 +0200 Subject: [PATCH 020/162] `Record`: - add `union` - add `intersection` - add `difference` - add `getUnionSemigroup` - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` --- CHANGELOG.md | 8 +++++ src/ReadonlyRecord.ts | 18 ++++++---- src/Record.ts | 82 +++++++++++++++++++++++++++++++++++++++++++ test/Record.ts | 68 +++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c90daf9a..f18aae419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,14 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` (@anthonyjoeseph) - add `getIntersectionSemigroup` (@anthonyjoeseph) - add `getDifferenceMagma` (@anthonyjoeseph) + - `Record` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - `TaskOption` diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 229f71d3a..8b4289d3d 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1481,9 +1481,12 @@ export const getWitherable: (O: Ord) => Witherable1 = (O) => ({ * @category instances * @since 2.11.0 */ -export const getUnionSemigroup = (S: Semigroup): Semigroup> => ({ - concat: (first, second) => union(S)(second)(first) -}) +export const getUnionSemigroup = (S: Semigroup): Semigroup> => { + const unionS = union(S) + return { + concat: (first, second) => unionS(second)(first) + } +} /** * @category instances @@ -1498,9 +1501,12 @@ export const getUnionMonoid = (S: Semigroup): Monoid(S: Semigroup): Semigroup> => ({ - concat: (first, second) => intersection(S)(second)(first) -}) +export const getIntersectionSemigroup = (S: Semigroup): Semigroup> => { + const intersectionS = intersection(S) + return { + concat: (first, second) => intersectionS(second)(first) + } +} /** * @category instances diff --git a/src/Record.ts b/src/Record.ts index b1b9e3fba..e25d86a4f 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -571,6 +571,49 @@ export const elem: ( (a: A, fa: Record): boolean } = RR.elem +/** + * @since 2.11.0 + */ +export const union = ( + M: Magma +): ((second: Record) => (first: Record) => Record) => { + const unionM = RR.union(M) + return (second) => (first) => { + if (isEmpty(first)) { + return { ...second } + } + if (isEmpty(second)) { + return { ...first } + } + return unionM(second)(first) + } +} + +/** + * @since 2.11.0 + */ +export const intersection = (M: Magma) => (second: Record) => ( + first: Record +): Record => { + if (isEmpty(first) || isEmpty(second)) { + return {} + } + return RR.intersection(M)(second)(first) +} + +/** + * @since 2.11.0 + */ +export const difference = (second: Record) => (first: Record): Record => { + if (isEmpty(first)) { + return { ...second } + } + if (isEmpty(second)) { + return { ...first } + } + return RR.difference(second)(first) +} + // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- @@ -995,6 +1038,45 @@ export const getWitherable = (O: Ord): Witherable1 => ({ wilt: _wilt }) +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (S: Semigroup): Semigroup> => { + const unionS = union(S) + return { + concat: (first, second) => unionS(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (S: Semigroup): Monoid> => ({ + concat: getUnionSemigroup(S).concat, + empty: {} +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (S: Semigroup): Semigroup> => { + const intersectionS = intersection(S) + return { + concat: (first, second) => intersectionS(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (): Magma> => ({ + concat: (first, second) => difference(second)(first) +}) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/Record.ts b/test/Record.ts index bc5fdfbb1..bdf4d7487 100644 --- a/test/Record.ts +++ b/test/Record.ts @@ -526,4 +526,72 @@ describe('Record', () => { O.some(false) ) }) + + it('getUnionMonoid', () => { + const M = _.getUnionMonoid(S.Semigroup) + const x: Record = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: Record = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.deepStrictEqual(M.concat(x, M.empty), x) + U.deepStrictEqual(M.concat(M.empty, x), x) + U.deepStrictEqual(M.concat(x, {}), x) + U.deepStrictEqual(M.concat({}, x), x) + U.deepStrictEqual(M.concat(x, y), { + a: 'a1', + b: 'b1b2', + c: 'c1c2', + d: 'd2' + }) + }) + + it('getIntersectionSemigroup', () => { + const M = _.getIntersectionSemigroup(S.Semigroup) + const x: Record = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: Record = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.deepStrictEqual(M.concat(x, {}), {}) + U.deepStrictEqual(M.concat(x, {}), {}) + U.deepStrictEqual(M.concat(x, {}), {}) + U.deepStrictEqual(M.concat(x, {}), {}) + U.deepStrictEqual(M.concat(x, y), { + b: 'b1b2', + c: 'c1c2' + }) + }) + + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma() + const x: Record = { + a: 'a1', + b: 'b1', + c: 'c1' + } + const y: Record = { + b: 'b2', + c: 'c2', + d: 'd2' + } + U.deepStrictEqual(M.concat({}, x), x) + U.deepStrictEqual(M.concat(x, {}), x) + U.deepStrictEqual(M.concat({}, x), x) + U.deepStrictEqual(M.concat(x, {}), x) + U.deepStrictEqual(M.concat(x, y), { + a: 'a1', + d: 'd2' + }) + }) }) From 36b5e1e56b57952193af9658a1b548344e50e607 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 11:44:50 +0200 Subject: [PATCH 021/162] expand internal module --- src/Array.ts | 24 ++++++++--------- src/Either.ts | 8 +++--- src/FromEither.ts | 15 +++++------ src/IO.ts | 9 ++++--- src/IOEither.ts | 3 ++- src/Identity.ts | 9 ++++--- src/Map.ts | 31 ++++++++++----------- src/NonEmptyArray.ts | 11 ++++---- src/Option.ts | 6 ++--- src/Reader.ts | 13 ++++----- src/ReaderEither.ts | 11 ++++---- src/ReaderTask.ts | 3 ++- src/ReaderTaskEither.ts | 3 ++- src/ReadonlyArray.ts | 52 ++++++++++++++++++------------------ src/ReadonlyMap.ts | 37 ++++++++++++------------- src/ReadonlyNonEmptyArray.ts | 21 +++++++-------- src/ReadonlyRecord.ts | 22 +++++++-------- src/Record.ts | 15 +++++------ src/StateReaderTaskEither.ts | 7 ++--- src/Task.ts | 3 ++- src/TaskEither.ts | 13 ++++----- src/TaskOption.ts | 3 ++- src/Tree.ts | 5 ++-- src/ValidationT.ts | 5 ++-- src/internal.ts | 52 +++++++++++++++++++++++++++++++++--- 25 files changed, 217 insertions(+), 164 deletions(-) diff --git a/src/Array.ts b/src/Array.ts index 7b833b2c3..9870eb54b 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -19,10 +19,11 @@ import { identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' +import * as _ from './internal' import { Monad1 } from './Monad' import { Monoid } from './Monoid' import * as NEA from './NonEmptyArray' -import * as O from './Option' +import { Option } from './Option' import { Ord } from './Ord' import { Pointed1 } from './Pointed' import * as RA from './ReadonlyArray' @@ -32,10 +33,9 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' +import { filterE as filterE_, PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' import NonEmptyArray = NEA.NonEmptyArray -import Option = O.Option // ------------------------------------------------------------------------------------- // constructors @@ -145,7 +145,7 @@ export function fromPredicate(predicate: Predicate): (a: A) => Array { * @since 2.11.0 */ export const fromOption = (ma: Option): Array => { - return O.isSome(ma) ? [ma.value] : [] + return _.isSome(ma) ? [ma.value] : [] } // ------------------------------------------------------------------------------------- @@ -352,7 +352,7 @@ export const last: (as: Array) => Option = RA.last * @category destructors * @since 2.0.0 */ -export const tail = (as: Array): Option> => (isNonEmpty(as) ? O.some(NEA.tail(as)) : O.none) +export const tail = (as: Array): Option> => (isNonEmpty(as) ? _.some(NEA.tail(as)) : _.none) /** * Get all but the last element of an array, creating a new array, or `None` if the array is empty @@ -367,7 +367,7 @@ export const tail = (as: Array): Option> => (isNonEmpty(as) ? O.s * @category destructors * @since 2.0.0 */ -export const init = (as: Array): Option> => (isNonEmpty(as) ? O.some(NEA.init(as)) : O.none) +export const init = (as: Array): Option> => (isNonEmpty(as) ? _.some(NEA.init(as)) : _.none) /** * Keep only a max number of elements from the start of an `Array`, creating a new `Array`. @@ -657,7 +657,7 @@ export const copy = (as: Array): Array => as.slice() * @since 2.0.0 */ export const insertAt = (i: number, a: A) => (as: Array): Option> => - i < 0 || i > as.length ? O.none : O.some(unsafeInsertAt(i, a, as)) + i < 0 || i > as.length ? _.none : _.some(unsafeInsertAt(i, a, as)) /** * Change the element at the specified index, creating a new array, or returning `None` if the index is out of bounds @@ -686,7 +686,7 @@ export const updateAt = (i: number, a: A): ((as: Array) => Option * @since 2.0.0 */ export const deleteAt = (i: number) => (as: Array): Option> => - isOutOfBound(i, as) ? O.none : O.some(unsafeDeleteAt(i, as)) + isOutOfBound(i, as) ? _.none : _.some(unsafeDeleteAt(i, as)) /** * Apply a function to the element at the specified index, creating a new array, or returning `None` if the index is out @@ -703,7 +703,7 @@ export const deleteAt = (i: number) => (as: Array): Option> => * @since 2.0.0 */ export const modifyAt = (i: number, f: (a: A) => A) => (as: Array): Option> => - isOutOfBound(i, as) ? O.none : O.some(unsafeUpdateAt(i, f(as[i]), as)) + isOutOfBound(i, as) ? _.none : _.some(unsafeUpdateAt(i, f(as[i]), as)) /** * Reverse an array, creating a new array @@ -1349,7 +1349,7 @@ export const filterMapWithIndex = (f: (i: number, a: A) => Option) => ( const out: Array = [] for (let i = 0; i < fa.length; i++) { const optionB = f(i, fa[i]) - if (O.isSome(optionB)) { + if (_.isSome(optionB)) { out.push(optionB.value) } } @@ -1611,7 +1611,7 @@ export const unfold = (b: B, f: (b: B) => Option): Array< let bb: B = b while (true) { const mt = f(bb) - if (O.isSome(mt)) { + if (_.isSome(mt)) { const [a, b] = mt.value out.push(a) bb = b @@ -2082,7 +2082,7 @@ export const some = (predicate: Predicate) => (as: Array): as is NonEmp */ export const Do: Array<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/Either.ts b/src/Either.ts index 8cd16dbda..9344808c0 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -92,7 +92,7 @@ export const isLeft: (ma: Either) => ma is Left = _.isLeft * @category guards * @since 2.0.0 */ -export const isRight = (ma: Either): ma is Right => ma._tag === 'Right' +export const isRight: (ma: Either) => ma is Right = _.isRight // ------------------------------------------------------------------------------------- // constructors @@ -105,7 +105,7 @@ export const isRight = (ma: Either): ma is Right => ma._tag === ' * @category constructors * @since 2.0.0 */ -export const left = (e: E): Either => ({ _tag: 'Left', left: e }) +export const left: (e: E) => Either = _.left /** * Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias @@ -114,7 +114,7 @@ export const left = (e: E): Either => ({ _tag: 'Left * @category constructors * @since 2.0.0 */ -export const right = (a: A): Either => ({ _tag: 'Right', right: a }) +export const right: (a: A) => Either = _.right /** * @example @@ -1265,7 +1265,7 @@ export function exists(predicate: Predicate): (ma: Either) => boo */ export const Do: Either = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/FromEither.ts b/src/FromEither.ts index afc7586f4..4706669b4 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -5,13 +5,12 @@ */ import { Chain, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' -import * as E from './Either' +import { Either } from './Either' import { flow, Lazy, Predicate, Refinement } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import * as _ from './internal' import { Option } from './Option' -import Either = E.Either - // ------------------------------------------------------------------------------------- // model // ------------------------------------------------------------------------------------- @@ -96,7 +95,7 @@ export function fromOption( ): (onNone: Lazy) => (ma: Option) => Kind2 export function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 export function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 { - return (onNone) => flow(E.fromOption(onNone), F.fromEither) + return (onNone) => (ma) => F.fromEither(_.isNone(ma) ? _.left(onNone()) : _.right(ma.value)) } /** @@ -144,8 +143,8 @@ export function fromPredicate( (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => - flow(E.fromPredicate(predicate, onFalse), F.fromEither) + return (predicate: Predicate, onFalse: (a: A) => E) => (a: A) => + F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a))) } // ------------------------------------------------------------------------------------- @@ -351,6 +350,6 @@ export function filterOrElse( (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => (ma: Kind2): Kind2 => - M.chain(ma, flow(E.fromPredicate(predicate, onFalse), F.fromEither)) + return (predicate: Predicate, onFalse: (a: A) => E) => (ma: Kind2): Kind2 => + M.chain(ma, (a) => F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)))) } diff --git a/src/IO.ts b/src/IO.ts index 601d12c42..823db30cd 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -12,17 +12,18 @@ * @since 2.0.0 */ import { Applicative1, getApplicativeMonoid } from './Applicative' -import { apFirst as apFirst_, Apply1, apSecond as apSecond_, apS as apS_, getApplySemigroup } from './Apply' +import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_, getApplySemigroup } from './Apply' +import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' import { ChainRec1 } from './ChainRec' import { FromIO1 } from './FromIO' import { constant, identity } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' -import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import * as _ from './internal' +import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { Monoid } from './Monoid' import { Pointed1 } from './Pointed' import { Semigroup } from './Semigroup' -import { Monad1 } from './Monad' // ------------------------------------------------------------------------------------- // model @@ -277,7 +278,7 @@ export const FromIO: FromIO1 = { */ export const Do: IO<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/IOEither.ts b/src/IOEither.ts index 70a551c37..5ab1da4fb 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -39,6 +39,7 @@ import { import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_ } from './FromIO' import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' +import * as _ from './internal' import * as I from './IO' import { Monad2, Monad2C } from './Monad' import { MonadIO2, MonadIO2C } from './MonadIO' @@ -816,7 +817,7 @@ export const bracket = ( */ export const Do: IOEither = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/Identity.ts b/src/Identity.ts index 962538184..afa497f25 100644 --- a/src/Identity.ts +++ b/src/Identity.ts @@ -3,7 +3,8 @@ */ import { Alt1 } from './Alt' import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' -import { apFirst as apFirst_, Apply1, apSecond as apSecond_, apS as apS_ } from './Apply' +import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' +import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' import { ChainRec1, tailRec } from './ChainRec' import { Comonad1 } from './Comonad' import { Eq } from './Eq' @@ -12,12 +13,12 @@ import { Foldable1 } from './Foldable' import { identity as id, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { HKT } from './HKT' -import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import * as _ from './internal' +import { Monad1 } from './Monad' import { Monoid } from './Monoid' import { Pointed1 } from './Pointed' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' -import { Monad1 } from './Monad' // ------------------------------------------------------------------------------------- // model @@ -383,7 +384,7 @@ export const ChainRec: ChainRec1 = { */ export const Do: Identity<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/Map.ts b/src/Map.ts index 9b4d6e880..13074f124 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -3,7 +3,7 @@ */ import { Applicative } from './Applicative' import { Compactable2 } from './Compactable' -import { Either, isLeft } from './Either' +import { Either } from './Either' import { Eq } from './Eq' import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' @@ -12,6 +12,7 @@ import { FoldableWithIndex2C } from './FoldableWithIndex' import { pipe, Predicate, Refinement } from './function' import { flap as flap_, Functor2 } from './Functor' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' +import * as _ from './internal' import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' @@ -122,7 +123,7 @@ export function toUnfoldable(ord: Ord, U: Unfoldable): (d: Map { const kas = toArrayO(d) const len = kas.length - return U.unfold(0, (b) => (b < len ? O.some([kas[b], b + 1]) : O.none)) + return U.unfold(0, (b) => (b < len ? _.some([kas[b], b + 1]) : _.none)) } } @@ -138,7 +139,7 @@ export const upsertAt = (E: Eq): ((k: K, a: A) => (m: Map) => Map const lookupWithKeyEk = lookupWithKeyE(k) return (m) => { const found = lookupWithKeyEk(m) - if (O.isNone(found)) { + if (_.isNone(found)) { const out = new Map(m) out.set(k, a) return out @@ -162,7 +163,7 @@ export const deleteAt = (E: Eq): ((k: K) => (m: Map) => Map const lookupWithKeyE = lookupWithKey(E) return (k) => (m) => { const found = lookupWithKeyE(k, m) - if (O.isSome(found)) { + if (_.isSome(found)) { const r = new Map(m) r.delete(found.value[0]) return r @@ -186,12 +187,12 @@ export const modifyAt = (E: Eq): ((k: K, f: (a: A) => A) => (m: Map (m) => { const found = lookupWithKeyE(k, m) - if (O.isNone(found)) { - return O.none + if (_.isNone(found)) { + return _.none } const r = new Map(m) r.set(found.value[0], f(found.value[1])) - return O.some(r) + return _.some(r) } } @@ -245,10 +246,10 @@ export function lookupWithKey( while (!(e = entries.next()).done) { const [ka, a] = e.value if (E.equals(ka, k)) { - return O.some([ka, a]) + return _.some([ka, a]) } } - return O.none + return _.none } } @@ -308,7 +309,7 @@ export function getMonoid(SK: Eq, SA: Semigroup): Monoid> while (!(e = entries.next()).done) { const [k, a] = e.value const mxOptA = lookupWithKeyS(k, mx) - if (O.isSome(mxOptA)) { + if (_.isSome(mxOptA)) { r.set(mxOptA.value[0], SA.concat(mxOptA.value[1], a)) } else { r.set(k, a) @@ -355,7 +356,7 @@ export function fromFoldable(E: Eq, M: Magma, F: Foldable): (f const lookupWithKeyE = lookupWithKey(E) return F.reduce<[K, A], Map>(fka, new Map(), (b, [k, a]) => { const bOpt = lookupWithKeyE(k, b) - if (O.isSome(bOpt)) { + if (_.isSome(bOpt)) { b.set(bOpt.value[0], M.concat(bOpt.value[1], a)) } else { b.set(k, a) @@ -392,7 +393,7 @@ export const partitionMapWithIndex = (f: (k: K, a: A) => Either(f: (k: K, a: A) => Option) => (fa while (!(e = entries.next()).done) { const [k, a] = e.value const o = f(k, a) - if (O.isSome(o)) { + if (_.isSome(o)) { m.set(k, o.value) } } @@ -493,7 +494,7 @@ export const compact = (fa: Map>): Map => { // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { const [k, oa] = e.value - if (O.isSome(oa)) { + if (_.isSome(oa)) { m.set(k, oa.value) } } @@ -561,7 +562,7 @@ export const separate = (fa: Map>): Separated // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { const [k, ei] = e.value - if (isLeft(ei)) { + if (_.isLeft(ei)) { left.set(k, ei.left) } else { right.set(k, ei.right) diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index a0631dd27..1b778ea1a 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -27,7 +27,7 @@ import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' import * as _ from './internal' import { Monad1 } from './Monad' -import * as O from './Option' +import { Option } from './Option' import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' import * as RNEA from './ReadonlyNonEmptyArray' @@ -37,7 +37,6 @@ import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import Semigroup = Se.Semigroup -import Option = O.Option import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray // ------------------------------------------------------------------------------------- @@ -190,7 +189,7 @@ export const fromReadonlyNonEmptyArray: (as: ReadonlyNonEmptyArray) => Non * @category constructors * @since 2.0.0 */ -export const fromArray = (as: Array): Option> => (isNonEmpty(as) ? O.some(as) : O.none) +export const fromArray = (as: Array): Option> => (isNonEmpty(as) ? _.some(as) : _.none) // ------------------------------------------------------------------------------------- // destructors @@ -353,7 +352,7 @@ export const sort = (O: Ord) => (as: NonEmptyArray): NonEm * @since 2.0.0 */ export const insertAt = (i: number, a: A) => (as: Array): Option> => - i < 0 || i > as.length ? O.none : O.some(unsafeInsertAt(i, a, as)) + i < 0 || i > as.length ? _.none : _.some(unsafeInsertAt(i, a, as)) /** * @category combinators @@ -367,7 +366,7 @@ export const updateAt = (i: number, a: A): ((as: NonEmptyArray) => Option< * @since 2.0.0 */ export const modifyAt = (i: number, f: (a: A) => A) => (as: NonEmptyArray): Option> => - isOutOfBound(i, as) ? O.none : O.some(unsafeUpdateAt(i, f(as[i]), as)) + isOutOfBound(i, as) ? _.none : _.some(unsafeUpdateAt(i, f(as[i]), as)) /** * @category combinators @@ -992,7 +991,7 @@ export const Comonad: Comonad1 = { */ export const Do: NonEmptyArray<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/Option.ts b/src/Option.ts index d0c62f8f4..161bce8ca 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -113,7 +113,7 @@ export const isNone = (fa: Option): fa is None => fa._tag === 'None' * @category constructors * @since 2.0.0 */ -export const none: Option = { _tag: 'None' } +export const none: Option = _.none /** * Constructs a `Some`. Represents an optional value that exists. @@ -121,7 +121,7 @@ export const none: Option = { _tag: 'None' } * @category constructors * @since 2.0.0 */ -export const some = (a: A): Option => ({ _tag: 'Some', value: a }) +export const some: (a: A) => Option = _.some /** * Returns a *smart constructor* based on the given predicate. @@ -1220,7 +1220,7 @@ export function getRefinement(getOption: (a: A) => Option): R */ export const Do: Option<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/Reader.ts b/src/Reader.ts index 6a6ec5fa1..7f26eb127 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -2,19 +2,20 @@ * @since 2.0.0 */ import { Applicative2, getApplicativeMonoid } from './Applicative' -import { apFirst as apFirst_, Apply2, apSecond as apSecond_, apS as apS_, getApplySemigroup } from './Apply' +import { apFirst as apFirst_, Apply2, apS as apS_, apSecond as apSecond_, getApplySemigroup } from './Apply' import { Category2 } from './Category' +import { bind as bind_, Chain2, chainFirst as chainFirst_ } from './Chain' import { Choice2 } from './Choice' import * as E from './Either' import { constant, flow, identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' -import { bind as bind_, Chain2, chainFirst as chainFirst_ } from './Chain' +import * as _ from './internal' +import { Monad2 } from './Monad' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import { Profunctor2 } from './Profunctor' import { Semigroup } from './Semigroup' import { Strong2 } from './Strong' -import { Monad2 } from './Monad' // ------------------------------------------------------------------------------------- // model @@ -176,13 +177,13 @@ export const second: Strong2['second'] = (pbc) => ([a, b]) => [a, pbc(b)] * @category Choice * @since 2.10.0 */ -export const left: Choice2['left'] = (pab) => E.fold((a) => E.left(pab(a)), E.right) +export const left: Choice2['left'] = (pab) => E.fold((a) => _.left(pab(a)), E.right) /** * @category Choice * @since 2.10.0 */ -export const right: Choice2['right'] = (pbc) => E.fold(E.left, (b) => E.right(pbc(b))) +export const right: Choice2['right'] = (pbc) => E.fold(E.left, (b) => _.right(pbc(b))) // ------------------------------------------------------------------------------------- // instances @@ -394,7 +395,7 @@ export const bindW: ( */ export const Do: Reader = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 85dce3956..1fe2c5996 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -35,6 +35,7 @@ import { } from './FromEither' import { flow, identity, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' +import * as _ from './internal' import { Monad3, Monad3C } from './Monad' import { MonadThrow3, MonadThrow3C } from './MonadThrow' import { Monoid } from './Monoid' @@ -42,8 +43,8 @@ import { Pointed3 } from './Pointed' import * as R from './Reader' import { Semigroup } from './Semigroup' -import Either = E.Either import Reader = R.Reader +import Either = E.Either // ------------------------------------------------------------------------------------- // model @@ -95,13 +96,13 @@ export const leftReader: (me: Reader) => ReaderEi * @category constructors * @since 2.0.0 */ -export const ask: () => ReaderEither = () => E.right +export const ask: () => ReaderEither = () => _.right /** * @category constructors * @since 2.0.0 */ -export const asks: (f: (r: R) => A) => ReaderEither = (f) => flow(f, E.right) +export const asks: (f: (r: R) => A) => ReaderEither = (f) => flow(f, _.right) /** * @category constructors @@ -133,7 +134,7 @@ export const match: ( export const matchW: ( onLeft: (e: E) => B, onRight: (a: A) => C -) => (ma: Reader>) => Reader = match as any +) => (ma: Reader>) => Reader = match as any /** * @category destructors @@ -722,7 +723,7 @@ export const fromEitherK = */ export const Do: ReaderEither = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 1d6f87c60..d43f09a5e 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -19,6 +19,7 @@ import { } from './FromTask' import { flow, identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' +import * as _ from './internal' import { Monad2 } from './Monad' import { MonadIO2 } from './MonadIO' import { MonadTask2 } from './MonadTask' @@ -425,7 +426,7 @@ export const chainFirstTaskK = */ export const Do: ReaderTask = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c3671f45a..d720776fa 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -43,6 +43,7 @@ import { } from './FromTask' import { flow, identity, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' +import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' import { Monad3, Monad3C } from './Monad' @@ -1024,7 +1025,7 @@ export function bracket( */ export const Do: ReaderTaskEither = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index e7146744e..b88f7b398 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -19,11 +19,12 @@ import { identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' +import * as _ from './internal' import { Monad1 } from './Monad' import { Monoid } from './Monoid' import { NonEmptyArray } from './NonEmptyArray' import * as N from './number' -import * as O from './Option' +import { Option } from './Option' import { fromCompare, Ord } from './Ord' import { Pointed1 } from './Pointed' import * as RNEA from './ReadonlyNonEmptyArray' @@ -33,9 +34,8 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1, filterE as filterE_ } from './Witherable' +import { filterE as filterE_, PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' -import Option = O.Option import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray // ------------------------------------------------------------------------------------- @@ -297,7 +297,7 @@ export const isOutOfBound: (i: number, as: ReadonlyArray) => boolean = RNE export function lookup(i: number): (as: ReadonlyArray) => Option export function lookup(i: number, as: ReadonlyArray): Option export function lookup(i: number, as?: ReadonlyArray): Option | ((as: ReadonlyArray) => Option) { - return as === undefined ? (as) => lookup(i, as) : isOutOfBound(i, as) ? O.none : O.some(as[i]) + return as === undefined ? (as) => lookup(i, as) : isOutOfBound(i, as) ? _.none : _.some(as[i]) } /** @@ -312,7 +312,7 @@ export function lookup(i: number, as?: ReadonlyArray): Option | ((as * * @since 2.5.0 */ -export const head = (as: ReadonlyArray): Option => (isNonEmpty(as) ? O.some(RNEA.head(as)) : O.none) +export const head = (as: ReadonlyArray): Option => (isNonEmpty(as) ? _.some(RNEA.head(as)) : _.none) /** * Get the last element in an array, or `None` if the array is empty @@ -326,7 +326,7 @@ export const head = (as: ReadonlyArray): Option => (isNonEmpty(as) ? O. * * @since 2.5.0 */ -export const last = (as: ReadonlyArray): Option => (isNonEmpty(as) ? O.some(RNEA.last(as)) : O.none) +export const last = (as: ReadonlyArray): Option => (isNonEmpty(as) ? _.some(RNEA.last(as)) : _.none) /** * Get all but the first element of an array, creating a new array, or `None` if the array is empty @@ -341,7 +341,7 @@ export const last = (as: ReadonlyArray): Option => (isNonEmpty(as) ? O. * @since 2.5.0 */ export const tail = (as: ReadonlyArray): Option> => - isNonEmpty(as) ? O.some(RNEA.tail(as)) : O.none + isNonEmpty(as) ? _.some(RNEA.tail(as)) : _.none /** * Get all but the last element of an array, creating a new array, or `None` if the array is empty @@ -356,7 +356,7 @@ export const tail = (as: ReadonlyArray): Option> => * @since 2.5.0 */ export const init = (as: ReadonlyArray): Option> => - isNonEmpty(as) ? O.some(RNEA.init(as)) : O.none + isNonEmpty(as) ? _.some(RNEA.init(as)) : _.none /** * Keep only a max number of elements from the start of an `ReadonlyArray`, creating a new `ReadonlyArray`. @@ -539,10 +539,10 @@ export const dropLeftWhile = (predicate: Predicate) => (as: ReadonlyArray< export const findIndex = (predicate: Predicate) => (as: ReadonlyArray): Option => { for (let i = 0; i < as.length; i++) { if (predicate(as[i])) { - return O.some(i) + return _.some(i) } } - return O.none + return _.none } /** @@ -567,10 +567,10 @@ export function findFirst(predicate: Predicate): (as: ReadonlyArray) => return (as) => { for (let i = 0; i < as.length; i++) { if (predicate(as[i])) { - return O.some(as[i]) + return _.some(as[i]) } } - return O.none + return _.none } } @@ -596,11 +596,11 @@ export function findFirst(predicate: Predicate): (as: ReadonlyArray) => export const findFirstMap = (f: (a: A) => Option) => (as: ReadonlyArray): Option => { for (let i = 0; i < as.length; i++) { const out = f(as[i]) - if (O.isSome(out)) { + if (_.isSome(out)) { return out } } - return O.none + return _.none } /** @@ -625,10 +625,10 @@ export function findLast(predicate: Predicate): (as: ReadonlyArray) => return (as) => { for (let i = as.length - 1; i >= 0; i--) { if (predicate(as[i])) { - return O.some(as[i]) + return _.some(as[i]) } } - return O.none + return _.none } } @@ -654,11 +654,11 @@ export function findLast(predicate: Predicate): (as: ReadonlyArray) => export const findLastMap = (f: (a: A) => Option) => (as: ReadonlyArray): Option => { for (let i = as.length - 1; i >= 0; i--) { const out = f(as[i]) - if (O.isSome(out)) { + if (_.isSome(out)) { return out } } - return O.none + return _.none } /** @@ -682,10 +682,10 @@ export const findLastMap = (f: (a: A) => Option) => (as: ReadonlyArray< export const findLastIndex = (predicate: Predicate) => (as: ReadonlyArray): Option => { for (let i = as.length - 1; i >= 0; i--) { if (predicate(as[i])) { - return O.some(i) + return _.some(i) } } - return O.none + return _.none } /** @@ -699,7 +699,7 @@ export const findLastIndex = (predicate: Predicate) => (as: ReadonlyArray< * * @since 2.5.0 */ -export const insertAt: (i: number, a: A) => (as: ReadonlyArray) => O.Option> = +export const insertAt: (i: number, a: A) => (as: ReadonlyArray) => Option> = // tslint:disable-next-line: deprecation RNEA.insertAt @@ -731,7 +731,7 @@ export const updateAt = (i: number, a: A): ((as: ReadonlyArray) => Option< * @since 2.5.0 */ export const deleteAt = (i: number) => (as: ReadonlyArray): Option> => - isOutOfBound(i, as) ? O.none : O.some(unsafeDeleteAt(i, as)) + isOutOfBound(i, as) ? _.none : _.some(unsafeDeleteAt(i, as)) /** * Apply a function to the element at the specified index, creating a new array, or returning `None` if the index is out @@ -748,7 +748,7 @@ export const deleteAt = (i: number) => (as: ReadonlyArray): Option(i: number, f: (a: A) => A) => (as: ReadonlyArray): Option> => - isOutOfBound(i, as) ? O.none : O.some(unsafeUpdateAt(i, f(as[i]), as)) + isOutOfBound(i, as) ? _.none : _.some(unsafeUpdateAt(i, f(as[i]), as)) /** * Reverse an array, creating a new array @@ -1506,7 +1506,7 @@ export const filterMapWithIndex = (f: (i: number, a: A) => Option) => ( const out: Array = [] for (let i = 0; i < fa.length; i++) { const optionB = f(i, fa[i]) - if (O.isSome(optionB)) { + if (_.isSome(optionB)) { out.push(optionB.value) } } @@ -1751,7 +1751,7 @@ export const unfold = (b: B, f: (b: B) => Option): Readon let bb: B = b while (true) { const mt = f(bb) - if (O.isSome(mt)) { + if (_.isSome(mt)) { const [a, b] = mt.value out.push(a) bb = b @@ -2302,7 +2302,7 @@ export const some = (predicate: Predicate) => (as: ReadonlyArray): as i */ export const Do: ReadonlyArray<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 178b28815..e90a6f07a 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -3,7 +3,7 @@ */ import { Applicative } from './Applicative' import { Compactable2 } from './Compactable' -import { Either, isLeft } from './Either' +import { Either } from './Either' import { Eq, fromEquals } from './Eq' import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' @@ -13,6 +13,7 @@ import { pipe, Predicate, Refinement, SK } from './function' import { flap as flap_, Functor2 } from './Functor' import { FunctorWithIndex2C } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' +import * as _ from './internal' import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' @@ -100,7 +101,7 @@ export function member(E: Eq): (k: K, m?: ReadonlyMap) => boolean const memberE = member(E) return (m) => memberE(k, m) } - return O.isSome(lookupE(k, m)) + return _.isSome(lookupE(k, m)) } } @@ -198,7 +199,7 @@ export function toUnfoldable( return (d) => { const kas = toReadonlyArrayO(d) const len = kas.length - return U.unfold(0, (b) => (b < len ? O.some([kas[b], b + 1]) : O.none)) + return U.unfold(0, (b) => (b < len ? _.some([kas[b], b + 1]) : _.none)) } } @@ -214,7 +215,7 @@ export const upsertAt = (E: Eq): ((k: K, a: A) => (m: ReadonlyMap const lookupWithKeyEk = lookupWithKeyE(k) return (m) => { const found = lookupWithKeyEk(m) - if (O.isNone(found)) { + if (_.isNone(found)) { const out = new Map(m) out.set(k, a) return out @@ -238,7 +239,7 @@ export const deleteAt = (E: Eq): ((k: K) => (m: ReadonlyMap) => R const lookupWithKeyE = lookupWithKey(E) return (k) => (m) => { const found = lookupWithKeyE(k, m) - if (O.isSome(found)) { + if (_.isSome(found)) { const r = new Map(m) r.delete(found.value[0]) return r @@ -264,17 +265,17 @@ export const modifyAt = ( const lookupWithKeyE = lookupWithKey(E) return (k, f) => (m) => { const found = lookupWithKeyE(k, m) - if (O.isNone(found)) { - return O.none + if (_.isNone(found)) { + return _.none } const [fk, fv] = found.value const next = f(fv) if (next === fv) { - return O.some(m) + return _.some(m) } const r = new Map(m) r.set(fk, next) - return O.some(r) + return _.some(r) } } @@ -323,10 +324,10 @@ export function lookupWithKey( while (!(e = entries.next()).done) { const [ka, a] = e.value if (E.equals(ka, k)) { - return O.some([ka, a]) + return _.some([ka, a]) } } - return O.none + return _.none } } @@ -387,7 +388,7 @@ export function isSubmap( while (!(e = entries.next()).done) { const [k, a] = e.value const d2OptA = lookupWithKeyS(k, that) - if (O.isNone(d2OptA) || !SK.equals(k, d2OptA.value[0]) || !SA.equals(a, d2OptA.value[1])) { + if (_.isNone(d2OptA) || !SK.equals(k, d2OptA.value[0]) || !SA.equals(a, d2OptA.value[1])) { return false } } @@ -434,7 +435,7 @@ export function getMonoid(SK: Eq, SA: Semigroup): Monoid( const lookupWithKeyE = lookupWithKey(E) return F.reduce>(fka, new Map(), (b, [k, a]) => { const bOpt = lookupWithKeyE(k, b) - if (O.isSome(bOpt)) { + if (_.isSome(bOpt)) { b.set(bOpt.value[0], M.concat(bOpt.value[1], a)) } else { b.set(k, a) @@ -527,7 +528,7 @@ export const partitionMapWithIndex = (f: (k: K, a: A) => Either(f: (k: K, a: A) => Option) => ( while (!(e = entries.next()).done) { const [k, a] = e.value const o = f(k, a) - if (O.isSome(o)) { + if (_.isSome(o)) { m.set(k, o.value) } } @@ -631,7 +632,7 @@ export const compact = (fa: ReadonlyMap>): ReadonlyMap // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { const [k, oa] = e.value - if (O.isSome(oa)) { + if (_.isSome(oa)) { m.set(k, oa.value) } } @@ -705,7 +706,7 @@ export const separate = ( // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { const [k, ei] = e.value - if (isLeft(ei)) { + if (_.isLeft(ei)) { left.set(k, ei.left) } else { right.set(k, ei.right) diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 8dd605775..54570fd82 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -28,7 +28,7 @@ import { HKT } from './HKT' import * as _ from './internal' import { Monad1 } from './Monad' import { NonEmptyArray } from './NonEmptyArray' -import * as O from './Option' +import { Option } from './Option' import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' import { ReadonlyRecord } from './ReadonlyRecord' @@ -38,7 +38,6 @@ import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import Semigroup = Se.Semigroup -import Option = O.Option // ------------------------------------------------------------------------------------- // model @@ -59,12 +58,12 @@ export type ReadonlyNonEmptyArray = ReadonlyArray & { /** * @internal */ -export const empty: ReadonlyArray = [] +export const empty: ReadonlyArray = _.emptyReadonlyArray /** * @internal */ -export const isNonEmpty = (as: ReadonlyArray): as is ReadonlyNonEmptyArray => as.length > 0 +export const isNonEmpty: (as: ReadonlyArray) => as is ReadonlyNonEmptyArray = _.isNonEmpty /** * @internal @@ -194,7 +193,7 @@ export const makeBy = (n: number, f: (i: number) => A): ReadonlyNonEmptyArray * @since 2.5.0 */ export const fromReadonlyArray = (as: ReadonlyArray): Option> => - isNonEmpty(as) ? O.some(as) : O.none + isNonEmpty(as) ? _.some(as) : _.none // ------------------------------------------------------------------------------------- // destructors @@ -377,7 +376,7 @@ export const updateAt = (i: number, a: A): ((as: ReadonlyNonEmptyArray) => */ export const modifyAt = (i: number, f: (a: A) => A) => ( as: ReadonlyNonEmptyArray -): Option> => (isOutOfBound(i, as) ? O.none : O.some(unsafeUpdateAt(i, f(as[i]), as))) +): Option> => (isOutOfBound(i, as) ? _.none : _.some(unsafeUpdateAt(i, f(as[i]), as))) /** * @category combinators @@ -584,7 +583,7 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' * @category Pointed * @since 2.5.0 */ -export const of: Pointed1['of'] = (a) => [a] +export const of: Pointed1['of'] = _.singleton /** * Less strict version of [`alt`](#alt). @@ -773,7 +772,7 @@ export const traverseWithIndex: PipeableTraverseWithIndex1 = (F: * @category Comonad * @since 2.6.3 */ -export const extract: Comonad1['extract'] = (as) => as[0] +export const extract: Comonad1['extract'] = _.head // ------------------------------------------------------------------------------------- // instances @@ -1037,7 +1036,7 @@ export const Comonad: Comonad1 = { */ export const Do: ReadonlyNonEmptyArray<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 @@ -1076,7 +1075,7 @@ export const head: (as: ReadonlyNonEmptyArray) => A = extract /** * @since 2.5.0 */ -export const tail = (as: ReadonlyNonEmptyArray): ReadonlyArray => as.slice(1) +export const tail: (as: ReadonlyNonEmptyArray) => ReadonlyArray = _.tail /** * @since 2.5.0 @@ -1236,7 +1235,7 @@ export const snoc = (init: ReadonlyArray, end: A): ReadonlyNonEmptyArray(i: number, a: A) => (as: ReadonlyArray): Option> => - i < 0 || i > as.length ? O.none : O.some(unsafeInsertAt(i, a, as)) + i < 0 || i > as.length ? _.none : _.some(unsafeInsertAt(i, a, as)) /** * Use `prependAll` instead. diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 8b4289d3d..5d71ac206 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -16,7 +16,7 @@ import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' import * as _ from './internal' import { Magma } from './Magma' import { Monoid } from './Monoid' -import * as O from './Option' +import { Option } from './Option' import { Ord } from './Ord' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' @@ -27,8 +27,6 @@ import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' -import Option = O.Option - // ------------------------------------------------------------------------------------- // model // ------------------------------------------------------------------------------------- @@ -162,7 +160,7 @@ export function toUnfoldable(U: Unfoldable): (r: ReadonlyRecord { const sas = toReadonlyArray(r) const len = sas.length - return U.unfold(0, (b) => (b < len ? O.some([sas[b], b + 1]) : O.none)) + return U.unfold(0, (b) => (b < len ? _.some([sas[b], b + 1]) : _.none)) } } @@ -217,14 +215,14 @@ export const updateAt = (k: string, a: A) => ( r: ReadonlyRecord ): Option> => { if (!has(k, r)) { - return O.none + return _.none } if (r[k] === a) { - return O.some(r) + return _.some(r) } const out: Record = Object.assign({}, r) out[k] = a - return O.some(out) + return _.some(out) } /** @@ -234,15 +232,15 @@ export const modifyAt = (k: string, f: (a: A) => A) => ( r: ReadonlyRecord ): Option> => { if (!has(k, r)) { - return O.none + return _.none } const next = f(r[k]) if (next === r[k]) { - return O.some(r) + return _.some(r) } const out: Record = Object.assign({}, r) out[k] = next - return O.some(out) + return _.some(out) } /** @@ -259,7 +257,7 @@ export function pop(k: string): (r: ReadonlyRecord) => Option { const oa = lookup(k, r) - return O.isNone(oa) ? O.none : O.some([oa.value, deleteAtk(r)]) + return _.isNone(oa) ? _.none : _.some([oa.value, deleteAtk(r)]) } } @@ -310,7 +308,7 @@ export function lookup( if (r === undefined) { return (r) => lookup(k, r) } - return _.hasOwnProperty.call(r, k) ? O.some(r[k]) : O.none + return _.hasOwnProperty.call(r, k) ? _.some(r[k]) : _.none } /** diff --git a/src/Record.ts b/src/Record.ts index e25d86a4f..4be3ff536 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -13,9 +13,10 @@ import { pipe, Predicate, Refinement } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' +import * as _ from './internal' import { Magma } from './Magma' import { Monoid } from './Monoid' -import * as O from './Option' +import { Option } from './Option' import { Ord } from './Ord' import * as RR from './ReadonlyRecord' import { Semigroup } from './Semigroup' @@ -27,8 +28,6 @@ import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' -import Option = O.Option - // ------------------------------------------------------------------------------------- // model // ------------------------------------------------------------------------------------- @@ -124,7 +123,7 @@ export function toUnfoldable(U: Unfoldable): (r: Record) => return (r) => { const sas = toArray(r) const len = sas.length - return U.unfold(0, (b) => (b < len ? O.some([sas[b], b + 1]) : O.none)) + return U.unfold(0, (b) => (b < len ? _.some([sas[b], b + 1]) : _.none)) } } @@ -177,11 +176,11 @@ export const updateAt = (k: string, a: A): ((r: Record(k: string, f: (a: A) => A) => (r: Record): Option> => { if (!has(k, r)) { - return O.none + return _.none } const out: Record = Object.assign({}, r) out[k] = f(r[k]) - return O.some(out) + return _.some(out) } /** @@ -196,7 +195,7 @@ export function pop(k: string): (r: Record) => Option<[A, Record { const oa = lookup(k, r) - return O.isNone(oa) ? O.none : O.some([oa.value, deleteAtk(r)]) + return _.isNone(oa) ? _.none : _.some([oa.value, deleteAtk(r)]) } } @@ -451,7 +450,7 @@ export function partitionWithIndex( * @since 2.0.0 */ export const filterMapWithIndex: ( - f: (key: K, a: A) => O.Option + f: (key: K, a: A) => Option ) => (fa: Record) => Record = RR.filterMapWithIndex /** diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index dc1c090c8..0a6d7c76c 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -26,6 +26,7 @@ import { } from './FromTask' import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor4 } from './Functor' +import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' import { Monad4 } from './Monad' @@ -933,13 +934,13 @@ export const traverseArrayWithIndex = ( as.reduce, S]>>>( (acc, a, i) => acc.then((ebs) => - E.isLeft(ebs) + _.isLeft(ebs) ? acc : f( i, a )(s)(r)().then((eb) => { - if (E.isLeft(eb)) { + if (_.isLeft(eb)) { return eb } const [b, s] = eb.right @@ -948,7 +949,7 @@ export const traverseArrayWithIndex = ( return ebs }) ), - Promise.resolve(E.right([[], s])) + Promise.resolve(_.right([[], s])) ) /** diff --git a/src/Task.ts b/src/Task.ts index c2c018de2..f6748da88 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -23,6 +23,7 @@ import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO1, fromIO import { FromTask1 } from './FromTask' import { identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' +import * as _ from './internal' import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { MonadTask1 } from './MonadTask' @@ -428,7 +429,7 @@ export const never: Task = () => new Promise((_) => undefined) */ export const Do: Task<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 20a1ce592..0f034a34d 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -50,6 +50,7 @@ import { } from './FromTask' import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' +import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' import { Monad2, Monad2C } from './Monad' @@ -256,7 +257,7 @@ export const getOrElseW: ( * @since 2.0.0 */ export const tryCatch = (f: Lazy>, onRejected: (reason: unknown) => E): TaskEither => () => - f().then(E.right, (reason) => E.left(onRejected(reason))) + f().then(_.right, (reason) => _.left(onRejected(reason))) /** * Converts a function returning a `Promise` to one returning a `TaskEither`. @@ -1029,7 +1030,7 @@ export function taskify(f: Function): () => TaskEither { const args = Array.prototype.slice.call(arguments) return () => new Promise((resolve) => { - const cbResolver = (e: L, r: R) => (e != null ? resolve(E.left(e)) : resolve(E.right(r))) + const cbResolver = (e: L, r: R) => (e != null ? resolve(_.left(e)) : resolve(_.right(r))) f.apply(null, args.concat(cbResolver)) }) } @@ -1072,7 +1073,7 @@ export const bracket = ( */ export const Do: TaskEither = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 @@ -1162,17 +1163,17 @@ export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => T as.reduce>>>( (acc, a, i) => acc.then((ebs) => - E.isLeft(ebs) + _.isLeft(ebs) ? acc : f(i, a)().then((eb) => { - if (E.isLeft(eb)) { + if (_.isLeft(eb)) { return eb } ebs.right.push(eb.right) return ebs }) ), - Promise.resolve(E.right([])) + Promise.resolve(_.right([])) ) /** diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 2c4958429..c57ac0701 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -24,6 +24,7 @@ import { } from './FromTask' import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' +import * as _ from './internal' import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { MonadTask1 } from './MonadTask' @@ -717,7 +718,7 @@ export const chainFirstTaskK = */ export const Do: TaskOption<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.10.0 diff --git a/src/Tree.ts b/src/Tree.ts index f28a29037..d2194eed8 100644 --- a/src/Tree.ts +++ b/src/Tree.ts @@ -10,6 +10,7 @@ import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' import * as A from './Array' +import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' import { Comonad1 } from './Comonad' import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' @@ -17,7 +18,7 @@ import { Foldable1 } from './Foldable' import { identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' -import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import * as _ from './internal' import { Monad as MonadHKT, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad' import { Monoid } from './Monoid' import { Pointed1 } from './Pointed' @@ -608,7 +609,7 @@ export const Comonad: Comonad1 = { */ export const Do: Tree<{}> = /*#__PURE__*/ - of({}) + of(_.emptyRecord) /** * @since 2.8.0 diff --git a/src/ValidationT.ts b/src/ValidationT.ts index 64fa67a24..b6d30df83 100644 --- a/src/ValidationT.ts +++ b/src/ValidationT.ts @@ -10,6 +10,7 @@ import { import * as E from './Either' import { Lazy } from './function' import { HKT, Kind, Kind2, URIS, URIS2 } from './HKT' +import * as _ from './internal' import { Monad, Monad1, Monad2 } from './Monad' import { Semigroup } from './Semigroup' @@ -99,10 +100,10 @@ export function getValidationM(S: Semigroup, M: Monad): ValidationM< map: A.map, ap: A.ap, of: A.of, - chain: (ma, f) => M.chain(ma, (e) => (E.isLeft(e) ? M.of(E.left(e.left)) : f(e.right))), + chain: (ma, f) => M.chain(ma, (e) => (_.isLeft(e) ? M.of(_.left(e.left)) : f(e.right))), alt: (me, that) => M.chain(me, (e1) => - E.isRight(e1) ? M.of(e1) : M.map(that(), (e2) => (E.isLeft(e2) ? E.left(S.concat(e1.left, e2.left)) : e2)) + _.isRight(e1) ? M.of(e1) : M.map(that(), (e2) => (_.isLeft(e2) ? _.left(S.concat(e1.left, e2.left)) : e2)) ) } } diff --git a/src/internal.ts b/src/internal.ts index c923ad7a0..52cc20aaf 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -1,18 +1,27 @@ /** * @since 2.10.0 */ -import { Either, Left } from './Either' -import { NonEmptyArray } from './NonEmptyArray' -import { Option, Some } from './Option' -import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' +import type { Either, Left, Right } from './Either' +import type { NonEmptyArray } from './NonEmptyArray' +import type { None, Option, Some } from './Option' +import type { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' // ------------------------------------------------------------------------------------- // Option // ------------------------------------------------------------------------------------- +/** @internal */ +export const isNone = (fa: Option): fa is None => fa._tag === 'None' + /** @internal */ export const isSome = (fa: Option): fa is Some => fa._tag === 'Some' +/** @internal */ +export const none: Option = { _tag: 'None' } + +/** @internal */ +export const some = (a: A): Option => ({ _tag: 'Some', value: a }) + // ------------------------------------------------------------------------------------- // Either // ------------------------------------------------------------------------------------- @@ -20,6 +29,41 @@ export const isSome = (fa: Option): fa is Some => fa._tag === 'Some' /** @internal */ export const isLeft = (ma: Either): ma is Left => ma._tag === 'Left' +/** @internal */ +export const isRight = (ma: Either): ma is Right => ma._tag === 'Right' + +/** @internal */ +export const left = (e: E): Either => ({ _tag: 'Left', left: e }) + +/** @internal */ +export const right = (a: A): Either => ({ _tag: 'Right', right: a }) + +// ------------------------------------------------------------------------------------- +// ReadonlyNonEmptyArray +// ------------------------------------------------------------------------------------- + +/** @internal */ +export const singleton = (a: A): NonEmptyArray => [a] + +/** @internal */ +export const isNonEmpty = (as: ReadonlyArray): as is ReadonlyNonEmptyArray => as.length > 0 + +/** @internal */ +export const head = (as: ReadonlyNonEmptyArray): A => as[0] + +/** @internal */ +export const tail = (as: ReadonlyNonEmptyArray): ReadonlyArray => as.slice(1) + +// ------------------------------------------------------------------------------------- +// empty +// ------------------------------------------------------------------------------------- + +/** @internal */ +export const emptyReadonlyArray: readonly [] = [] + +/** @internal */ +export const emptyRecord: {} = {} + // ------------------------------------------------------------------------------------- // Record // ------------------------------------------------------------------------------------- From 912b2735c9e678223ec6d5b9a23f4379d4f6056a Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 11:47:44 +0200 Subject: [PATCH 022/162] These: remove `Option` dependency --- src/These.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/These.ts b/src/These.ts index 8c5306e45..9d3719801 100644 --- a/src/These.ts +++ b/src/These.ts @@ -30,10 +30,11 @@ import { FromEither2, fromOption as fromOption_, fromOptionK as fromOptionK_ } f import { identity, Lazy, pipe } from './function' import { flap as flap_, Functor2 } from './Functor' import { HKT } from './HKT' +import * as _ from './internal' import { Monad2C } from './Monad' import { MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' -import { isNone, none, Option, some } from './Option' +import { Option } from './Option' import { Pointed2 } from './Pointed' import { Semigroup } from './Semigroup' import { Show } from './Show' @@ -295,7 +296,7 @@ export function getMonad(S: Semigroup): Monad2C & MonadThrow2C(fa: These): Option { - return isLeft(fa) ? some(fa.left) : isRight(fa) ? none : some(fa.left) + return isLeft(fa) ? _.some(fa.left) : isRight(fa) ? _.none : _.some(fa.left) } /** @@ -313,7 +314,7 @@ export function getLeft(fa: These): Option { * @since 2.0.0 */ export function getRight(fa: These): Option { - return isLeft(fa) ? none : isRight(fa) ? some(fa.right) : some(fa.right) + return isLeft(fa) ? _.none : isRight(fa) ? _.some(fa.right) : _.some(fa.right) } /** @@ -359,7 +360,7 @@ export function isBoth(fa: These): fa is Both { * @since 2.0.0 */ export function leftOrBoth(e: E): (ma: Option) => These { - return (ma) => (isNone(ma) ? left(e) : both(e, ma.value)) + return (ma) => (_.isNone(ma) ? left(e) : both(e, ma.value)) } // TODO: make lazy in v3 @@ -375,7 +376,7 @@ export function leftOrBoth(e: E): (ma: Option) => These { * @since 2.0.0 */ export function rightOrBoth(a: A): (me: Option) => These { - return (me) => (isNone(me) ? right(a) : both(me.value, a)) + return (me) => (_.isNone(me) ? right(a) : both(me.value, a)) } /** @@ -393,7 +394,7 @@ export function rightOrBoth(a: A): (me: Option) => These { * @since 2.0.0 */ export function getLeftOnly(fa: These): Option { - return isLeft(fa) ? some(fa.left) : none + return isLeft(fa) ? _.some(fa.left) : _.none } /** @@ -411,7 +412,7 @@ export function getLeftOnly(fa: These): Option { * @since 2.0.0 */ export function getRightOnly(fa: These): Option { - return isRight(fa) ? some(fa.right) : none + return isRight(fa) ? _.some(fa.right) : _.none } /** @@ -430,13 +431,13 @@ export function getRightOnly(fa: These): Option { * @since 2.0.0 */ export function fromOptions(fe: Option, fa: Option): Option> { - return isNone(fe) - ? isNone(fa) - ? none - : some(right(fa.value)) - : isNone(fa) - ? some(left(fe.value)) - : some(both(fe.value, fa.value)) + return _.isNone(fe) + ? _.isNone(fa) + ? _.none + : _.some(right(fa.value)) + : _.isNone(fa) + ? _.some(left(fe.value)) + : _.some(both(fe.value, fa.value)) } // ------------------------------------------------------------------------------------- From 2fed4b64ef490223bff3bde3c95be7a07948cb5e Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 11:50:26 +0200 Subject: [PATCH 023/162] struct: add `evolve` --- src/struct.ts | 37 +++++++++++++++++++++++++++++++++++++ test/struct.ts | 19 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/struct.ts b/src/struct.ts index 8dde2104a..b2438f8af 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -1,6 +1,7 @@ /** * @since 2.10.0 */ +import * as _ from './internal' import { getObjectSemigroup, Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- @@ -26,3 +27,39 @@ import { getObjectSemigroup, Semigroup } from './Semigroup' */ // tslint:disable-next-line: deprecation export const getAssignSemigroup: () => Semigroup = getObjectSemigroup + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * Creates a new object by recursively evolving a shallow copy of `a`, according to the `transformation` functions. + * + * @example + * import { pipe } from 'fp-ts/function' + * import { evolve } from 'fp-ts/struct' + * + * assert.deepStrictEqual( + * pipe( + * { a: 'a', b: 1 }, + * evolve({ + * a: (a) => a.length, + * b: (b) => b * 2 + * }) + * ), + * { a: 1, b: 2 } + * ) + * + * @since 3.0.0 + */ +export const evolve = unknown }>(transformations: F) => ( + a: A +): { [K in keyof F]: ReturnType } => { + const out: Record = {} + for (const k in a) { + if (_.hasOwnProperty.call(a, k)) { + out[k] = transformations[k](a[k]) + } + } + return out as any +} diff --git a/test/struct.ts b/test/struct.ts index 8295d5d3b..73d1312aa 100644 --- a/test/struct.ts +++ b/test/struct.ts @@ -1,3 +1,4 @@ +import { pipe } from '../src/function' import * as _ from '../src/struct' import * as U from './util' @@ -17,4 +18,22 @@ describe('struct', () => { const S = _.getAssignSemigroup() U.deepStrictEqual(S.concat(foo, bar), Object.assign({}, foo, bar)) }) + + it('evolve', () => { + U.deepStrictEqual( + pipe( + { a: 'a', b: 1, c: true }, + _.evolve({ + a: (s) => s.length, + b: (b) => b > 0, + c: (c) => !c + }) + ), + { a: 1, b: true, c: false } + ) + // should ignore non own properties + const x: Record<'b', number> = Object.create({ a: 1 }) + x.b = 1 + U.deepStrictEqual(pipe(x, _.evolve({ b: (b) => b > 0 })), { b: true }) + }) }) From 14f6418a63bbd6f49dd043dc2173fedc653d918d Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 11:55:46 +0200 Subject: [PATCH 024/162] function: add apply --- src/function.ts | 5 +++++ test/function.ts | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/function.ts b/src/function.ts index 05d68ff56..233def38a 100644 --- a/src/function.ts +++ b/src/function.ts @@ -123,6 +123,11 @@ export const getEndomorphismMonoid = (): Monoid> => ( // utils // ------------------------------------------------------------------------------------- +/** + * @since 3.0.0 + */ +export const apply = (a: A) => (f: (a: A) => B): B => f(a) + /** * A *thunk* * diff --git a/test/function.ts b/test/function.ts index c8601e8bd..a407e43e3 100644 --- a/test/function.ts +++ b/test/function.ts @@ -181,4 +181,8 @@ describe('function', () => { const f = M.concat(_.increment, U.double) U.deepStrictEqual(f(3), 8) }) + + it('apply', () => { + U.deepStrictEqual(_.pipe(U.double, _.apply(1)), 2) + }) }) From 195bd9b3e7fec2116883c40c4bb518347e77bd8f Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 12:03:06 +0200 Subject: [PATCH 025/162] Ord: add equals and trivial --- CHANGELOG.md | 4 ++++ src/EitherT.ts | 2 +- src/Ord.ts | 16 +++++++++++++++- src/function.ts | 2 +- src/struct.ts | 2 +- test/Ord.ts | 19 +++++++++++++++++++ 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f18aae419..8f1398479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,10 +41,12 @@ high state of flux, you're at risk of it changing without notice. - add `ChainRecBreadthFirst` instance (@qlonik) - `function` - add `SK` (@cdimitroulas) + - add `apply` - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - `Ord` - add `trivial` instance + - add `equals` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -68,6 +70,8 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - `struct` + - add `evolve` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - `TaskOption` diff --git a/src/EitherT.ts b/src/EitherT.ts index 8e26b44f4..72b82a6cc 100644 --- a/src/EitherT.ts +++ b/src/EitherT.ts @@ -332,7 +332,7 @@ export function altValidation( // ------------------------------------------------------------------------------------- /** - * @since 3.0.0 + * @since 2.11.0 */ export function match( F: Functor3 diff --git a/src/Ord.ts b/src/Ord.ts index ecb0854d5..4ed92d9a9 100644 --- a/src/Ord.ts +++ b/src/Ord.ts @@ -11,7 +11,7 @@ */ import { Contravariant1 } from './Contravariant' import { Eq, eqStrict } from './Eq' -import { pipe } from './function' +import { constant, constTrue, pipe } from './function' import { Monoid } from './Monoid' import { Ordering } from './Ordering' import { Semigroup } from './Semigroup' @@ -228,6 +228,20 @@ export const Contravariant: Contravariant1 = { // utils // ------------------------------------------------------------------------------------- +/** + * @since 2.11.0 + */ +export const trivial: Ord = { + equals: constTrue, + compare: constant(0) +} + +/** + * @since 2.11.0 + */ +export const equals = (O: Ord) => (second: A) => (first: A): boolean => + first === second || O.compare(first, second) === 0 + // TODO: curry in v3 /** * Test whether one value is _strictly less than_ another diff --git a/src/function.ts b/src/function.ts index 233def38a..f6ee5011c 100644 --- a/src/function.ts +++ b/src/function.ts @@ -124,7 +124,7 @@ export const getEndomorphismMonoid = (): Monoid> => ( // ------------------------------------------------------------------------------------- /** - * @since 3.0.0 + * @since 2.11.0 */ export const apply = (a: A) => (f: (a: A) => B): B => f(a) diff --git a/src/struct.ts b/src/struct.ts index b2438f8af..9528ad6b1 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -50,7 +50,7 @@ export const getAssignSemigroup: () => Semigroup = * { a: 1, b: 2 } * ) * - * @since 3.0.0 + * @since 2.11.0 */ export const evolve = unknown }>(transformations: F) => ( a: A diff --git a/test/Ord.ts b/test/Ord.ts index 4baef5e8d..71b9402e2 100644 --- a/test/Ord.ts +++ b/test/Ord.ts @@ -4,6 +4,7 @@ import { concatAll } from '../src/Monoid' import * as N from '../src/number' import * as _ from '../src/Ord' import { sort } from '../src/ReadonlyArray' +import * as RR from '../src/ReadonlyRecord' import * as S from '../src/string' import * as U from './util' @@ -140,4 +141,22 @@ describe('Ord', () => { const second = { a: 1 } U.strictEqual(max(first, second), first) }) + + it('equals', () => { + const equals = _.equals(N.Ord) + U.deepStrictEqual(equals(1)(1), true) + U.deepStrictEqual(equals(1)(2), false) + }) + + it('trivial', () => { + const toReadonlyArray = RR.collect(_.trivial)((k, a) => [k, a]) + U.deepStrictEqual(toReadonlyArray({ a: 1, b: 2 }), [ + ['a', 1], + ['b', 2] + ]) + U.deepStrictEqual(toReadonlyArray({ b: 2, a: 1 }), [ + ['b', 2], + ['a', 1] + ]) + }) }) From 6c035e56eb2cfc46ba5bb3053794e1da4aabe041 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 13:25:11 +0200 Subject: [PATCH 026/162] add `Endomorphism` module --- CHANGELOG.md | 4 ++++ src/Endomorphism.ts | 43 +++++++++++++++++++++++++++++++++ src/Monoid.ts | 9 ++++--- src/NonEmptyArray.ts | 3 ++- src/ReadonlyNonEmptyArray.ts | 3 ++- src/Store.ts | 5 ++-- src/function.ts | 45 +++++++++++++++++++++-------------- src/index.ts | 5 ++++ test/Endomorphism.ts | 11 +++++++++ test/NonEmptyArray.ts | 3 ++- test/ReadonlyNonEmptyArray.ts | 3 ++- test/function.ts | 1 + 12 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 src/Endomorphism.ts create mode 100644 test/Endomorphism.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f1398479..6c36c88d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ high state of flux, you're at risk of it changing without notice. # 2.11 - **Deprecation** + - `function` + - deprecate `Endomorphism`, use `Endomorphism` module instead. + - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` @@ -33,6 +36,7 @@ high state of flux, you're at risk of it changing without notice. - deprecate `TraversableWithIndex` in favour of `getTraversableWithIndex` (@anthonyjoeseph) - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - **New Feature** + - add `Endomorphism` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/Endomorphism.ts b/src/Endomorphism.ts new file mode 100644 index 000000000..3ce89a7ac --- /dev/null +++ b/src/Endomorphism.ts @@ -0,0 +1,43 @@ +/** + * @since 2.11.0 + */ + +import { flow, identity } from './function' +import { Monoid } from './Monoid' +import { Semigroup } from './Semigroup' + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export interface Endomorphism { + (a: A): A +} + +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- + +/** + * Endomorphism form a `Semigroup` where the `concat` operation is the usual function composition. + * + * @category instances + * @since 2.11.0 + */ +export const getSemigroup = (): Semigroup> => ({ + concat: (first, second) => flow(first, second) +}) + +/** + * Endomorphism form a `Monoid` where the `empty` value is the `identity` function. + * + * @category instances + * @since 2.11.0 + */ +export const getMonoid = (): Monoid> => ({ + concat: getSemigroup().concat, + empty: identity +}) diff --git a/src/Monoid.ts b/src/Monoid.ts index 3a5933290..246f2db2c 100644 --- a/src/Monoid.ts +++ b/src/Monoid.ts @@ -33,7 +33,8 @@ * @since 2.0.0 */ import { Bounded } from './Bounded' -import { Endomorphism, getEndomorphismMonoid as getEM, getMonoid } from './function' +import { getMonoid as getFM } from './function' +import { Endomorphism, getMonoid as getEM } from './Endomorphism' import * as _ from './internal' import { ReadonlyRecord } from './ReadonlyRecord' import * as Se from './Semigroup' @@ -300,12 +301,10 @@ export const monoidAny: Monoid = { * @since 2.0.0 * @deprecated */ -export const getFunctionMonoid: (M: Monoid) => () => Monoid<(a: A) => M> = getMonoid +export const getFunctionMonoid: (M: Monoid) => () => Monoid<(a: A) => M> = getFM /** - * Use `function.getEndomorphismMonoid` instead. - * - * **Note**. The execution order in `function.getEndomorphismMonoid` is reversed. + * Use `Endomorphism` module instead. **Note**. The execution order in the `Endomorphism` module is reversed. * * @category instances * @since 2.0.0 diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 1b778ea1a..0d0f70116 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -17,11 +17,12 @@ import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' import { Comonad1 } from './Comonad' +import { Endomorphism } from './Endomorphism' import { Eq } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { identity, Lazy, pipe, Predicate, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 54570fd82..03c13a75b 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -17,11 +17,12 @@ import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' import { Comonad1 } from './Comonad' +import { Endomorphism } from './Endomorphism' import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { Endomorphism, identity, Lazy, pipe, Predicate, Refinement, SK } from './function' +import { identity, Lazy, pipe, Predicate, Refinement, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' diff --git a/src/Store.ts b/src/Store.ts index af1e54f51..ce348e314 100644 --- a/src/Store.ts +++ b/src/Store.ts @@ -2,10 +2,11 @@ * @since 2.0.0 */ import { Comonad2 } from './Comonad' -import { Endomorphism, identity, pipe } from './function' +import { Endomorphism } from './Endomorphism' +import { Extend2 } from './Extend' +import { identity, pipe } from './function' import { flap as flap_, Functor as FunctorHKT, Functor1, Functor2, Functor2C, Functor3, Functor3C } from './Functor' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' -import { Extend2 } from './Extend' // ------------------------------------------------------------------------------------- // model diff --git a/src/function.ts b/src/function.ts index f6ee5011c..1216b6c48 100644 --- a/src/function.ts +++ b/src/function.ts @@ -108,17 +108,6 @@ export const getRing = (R: Ring): Ring<(a: A) => B> => { } } -/** - * Endomorphism form a monoid where the `empty` value is the identity function. - * - * @category instances - * @since 2.10.0 - */ -export const getEndomorphismMonoid = (): Monoid> => ({ - concat: (x, y) => (a) => y(x(a)), - empty: identity -}) - // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- @@ -151,13 +140,6 @@ export interface Refinement { (a: A): a is B } -/** - * @since 2.0.0 - */ -export interface Endomorphism { - (a: A): A -} - /** * @example * import { FunctionN } from 'fp-ts/function' @@ -758,3 +740,30 @@ export const hole: () => T = absurd as any * @since 2.11.0 */ export const SK = (_: A, b: B): B => b + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +/** + * Use `Endomorphism` module instead. + * + * @since 2.0.0 + * @deprecated + */ +export interface Endomorphism { + (a: A): A +} + +/** + * Use `Endomorphism` module instead. + * + * @category instances + * @since 2.10.0 + * @deprecated + */ +// tslint:disable-next-line: deprecation +export const getEndomorphismMonoid = (): Monoid> => ({ + concat: (first, second) => flow(first, second), + empty: identity +}) diff --git a/src/index.ts b/src/index.ts index 10bcaab18..b405bb3a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,7 @@ import * as date from './Date' import * as distributiveLattice from './DistributiveLattice' import * as either from './Either' import * as eitherT from './EitherT' +import * as endomorphism from './Endomorphism' import * as eq from './Eq' import * as extend from './Extend' import * as field from './Field' @@ -217,6 +218,10 @@ export { * @since 2.0.0 */ eitherT, + /** + * @since 2.11.0 + */ + endomorphism, /** * @since 2.0.0 */ diff --git a/test/Endomorphism.ts b/test/Endomorphism.ts new file mode 100644 index 000000000..45538bd5c --- /dev/null +++ b/test/Endomorphism.ts @@ -0,0 +1,11 @@ +import * as _ from '../src/Endomorphism' +import * as U from './util' + +describe('Endomorphism', () => { + it('getMonoid', () => { + const M = _.getMonoid() + const inc = (n: number) => n + 1 + const f = M.concat(inc, U.double) + U.deepStrictEqual(f(3), 8) + }) +}) diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 37eecb794..85c2eec1d 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -1,5 +1,6 @@ import * as assert from 'assert' -import { Endomorphism, identity, pipe } from '../src/function' +import { Endomorphism } from '../src/Endomorphism' +import { identity, pipe } from '../src/function' import * as _ from '../src/NonEmptyArray' import * as N from '../src/number' import * as O from '../src/Option' diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index 77d9f2866..bb09d21a6 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -1,7 +1,8 @@ import * as assert from 'assert' import * as B from '../src/boolean' +import { Endomorphism } from '../src/Endomorphism' import * as Eq from '../src/Eq' -import { Endomorphism, identity, pipe } from '../src/function' +import { identity, pipe } from '../src/function' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' diff --git a/test/function.ts b/test/function.ts index a407e43e3..6a58f3499 100644 --- a/test/function.ts +++ b/test/function.ts @@ -177,6 +177,7 @@ describe('function', () => { }) it('getEndomorphismMonoid', () => { + // tslint:disable-next-line: deprecation const M = _.getEndomorphismMonoid() const f = M.concat(_.increment, U.double) U.deepStrictEqual(f(3), 8) From 74756a06c5718aea0a8a217c24eec74d50eb25ff Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 17:25:34 +0200 Subject: [PATCH 027/162] add `Predicate` module --- CHANGELOG.md | 3 + src/Array.ts | 3 +- src/Either.ts | 3 +- src/Endomorphism.ts | 22 +++++++ src/Filterable.ts | 5 +- src/FromEither.ts | 3 +- src/IOEither.ts | 3 +- src/Map.ts | 3 +- src/NonEmptyArray.ts | 3 +- src/Option.ts | 3 +- src/OptionT.ts | 3 +- src/Predicate.ts | 114 +++++++++++++++++++++++++++++++++++ src/ReaderEither.ts | 3 +- src/ReaderTaskEither.ts | 3 +- src/ReadonlyArray.ts | 3 +- src/ReadonlyMap.ts | 3 +- src/ReadonlyNonEmptyArray.ts | 3 +- src/ReadonlyRecord.ts | 3 +- src/ReadonlySet.ts | 7 ++- src/Record.ts | 3 +- src/Set.ts | 5 +- src/StateReaderTaskEither.ts | 3 +- src/TaskEither.ts | 3 +- src/TaskOption.ts | 3 +- src/function.ts | 37 +++++++----- src/index.ts | 5 ++ test/Array.ts | 3 +- test/Predicate.ts | 55 +++++++++++++++++ test/ReadonlyArray.ts | 3 +- test/function.ts | 1 + 30 files changed, 272 insertions(+), 42 deletions(-) create mode 100644 src/Predicate.ts create mode 100644 test/Predicate.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c36c88d5..c4e47c14a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ high state of flux, you're at risk of it changing without notice. - `function` - deprecate `Endomorphism`, use `Endomorphism` module instead. - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. + - deprecate `Predicate`, use `Predicate` module instead. + - deprecate `not`, use `Predicate` module instead. - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` @@ -37,6 +39,7 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - **New Feature** - add `Endomorphism` module + - add `Predicate` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/Array.ts b/src/Array.ts index 9870eb54b..af6fcb336 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -15,7 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement } from './function' +import { identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -26,6 +26,7 @@ import * as NEA from './NonEmptyArray' import { Option } from './Option' import { Ord } from './Ord' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import * as RA from './ReadonlyArray' import { Semigroup } from './Semigroup' import { separated, Separated } from './Separated' diff --git a/src/Either.ts b/src/Either.ts index 9344808c0..e12d99396 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -31,7 +31,7 @@ import { Extend2 } from './Extend' import { Filterable2C } from './Filterable' import { Foldable2 } from './Foldable' import { FromEither2 } from './FromEither' -import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import { HKT } from './HKT' import * as _ from './internal' @@ -40,6 +40,7 @@ import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Option } from './Option' import { Pointed2 } from './Pointed' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/Endomorphism.ts b/src/Endomorphism.ts index 3ce89a7ac..23872c53d 100644 --- a/src/Endomorphism.ts +++ b/src/Endomorphism.ts @@ -21,6 +21,28 @@ export interface Endomorphism { // instances // ------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- + +/** + * @category instances + * @since 2.11.0 + */ +export const URI = 'Endomorphism' + +/** + * @category instances + * @since 2.11.0 + */ +export type URI = typeof URI + +declare module './HKT' { + interface URItoKind { + readonly [URI]: Endomorphism + } +} + /** * Endomorphism form a `Semigroup` where the `concat` operation is the usual function composition. * diff --git a/src/Filterable.ts b/src/Filterable.ts index 65060154c..f215abd1f 100644 --- a/src/Filterable.ts +++ b/src/Filterable.ts @@ -6,6 +6,7 @@ * @since 2.0.0 */ import { + compact, Compactable, Compactable1, Compactable2, @@ -20,11 +21,10 @@ import { CompactableComposition21, CompactableComposition22, CompactableComposition23, - compact, separate } from './Compactable' import { Either } from './Either' -import { pipe, Predicate, Refinement } from './function' +import { pipe, Refinement } from './function' import { Functor, Functor1, @@ -44,6 +44,7 @@ import { } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { getLeft, getRight, Option } from './Option' +import { Predicate } from './Predicate' import { separated, Separated } from './Separated' // ------------------------------------------------------------------------------------- diff --git a/src/FromEither.ts b/src/FromEither.ts index 4706669b4..d2f617a63 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -6,10 +6,11 @@ import { Chain, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' import { Either } from './Either' -import { flow, Lazy, Predicate, Refinement } from './function' +import { flow, Lazy, Refinement } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' import * as _ from './internal' import { Option } from './Option' +import { Predicate } from './Predicate' // ------------------------------------------------------------------------------------- // model diff --git a/src/IOEither.ts b/src/IOEither.ts index 5ab1da4fb..f16362e1c 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -37,7 +37,7 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_ } from './FromIO' -import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import * as I from './IO' @@ -46,6 +46,7 @@ import { MonadIO2, MonadIO2C } from './MonadIO' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- diff --git a/src/Map.ts b/src/Map.ts index 13074f124..b97ac90c1 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -9,7 +9,7 @@ import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' import { Foldable, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' -import { pipe, Predicate, Refinement } from './function' +import { pipe, Refinement } from './function' import { flap as flap_, Functor2 } from './Functor' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' import * as _ from './internal' @@ -17,6 +17,7 @@ import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' import { Ord } from './Ord' +import { Predicate } from './Predicate' import * as RM from './ReadonlyMap' import { Semigroup } from './Semigroup' import { separated, Separated } from './Separated' diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 0d0f70116..d035bb4e8 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -22,7 +22,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement } from './function' +import { identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -31,6 +31,7 @@ import { Monad1 } from './Monad' import { Option } from './Option' import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import * as RNEA from './ReadonlyNonEmptyArray' import * as Se from './Semigroup' import { Show } from './Show' diff --git a/src/Option.ts b/src/Option.ts index 161bce8ca..2a526398a 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -29,7 +29,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Filterable1 } from './Filterable' import { Foldable1 } from './Foldable' -import { constNull, constUndefined, flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { constNull, constUndefined, flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { HKT } from './HKT' import * as _ from './internal' @@ -38,6 +38,7 @@ import { MonadThrow1 } from './MonadThrow' import { Monoid } from './Monoid' import { Ord } from './Ord' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/OptionT.ts b/src/OptionT.ts index a611aebef..4d409a791 100644 --- a/src/OptionT.ts +++ b/src/OptionT.ts @@ -10,12 +10,13 @@ import { import { ap as ap_, Apply, Apply1, Apply2, Apply2C, Apply3, Apply3C, Apply4 } from './Apply' import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' import { Either } from './Either' -import { constant, flow, Lazy, pipe, Predicate, Refinement } from './function' +import { constant, flow, Lazy, pipe, Refinement } from './function' import { Functor, Functor1, Functor2, Functor2C, Functor3, Functor3C, Functor4, map as map_ } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { Monad, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad' import * as O from './Option' import { Pointed, Pointed1, Pointed2, Pointed2C, Pointed3, Pointed3C, Pointed4 } from './Pointed' +import { Predicate } from './Predicate' import Option = O.Option diff --git a/src/Predicate.ts b/src/Predicate.ts new file mode 100644 index 000000000..92a27c988 --- /dev/null +++ b/src/Predicate.ts @@ -0,0 +1,114 @@ +/** + * @since 2.11.0 + */ +import { Contravariant1 } from './Contravariant' +import { constFalse, constTrue, flow, pipe } from './function' +import { Monoid } from './Monoid' +import { Semigroup } from './Semigroup' + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export interface Predicate { + (a: A): boolean +} + +// ------------------------------------------------------------------------------------- +// type class members +// ------------------------------------------------------------------------------------- + +const contramap_: Contravariant1['contramap'] = (predicate, f) => pipe(predicate, contramap(f)) + +/** + * @category Contravariant + * @since 2.11.0 + */ +export const contramap = (f: (b: B) => A) => (predicate: Predicate): Predicate => flow(f, predicate) + +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- + +/** + * @category instances + * @since 2.11.0 + */ +export const URI = 'Predicate' + +/** + * @category instances + * @since 2.11.0 + */ +export type URI = typeof URI + +declare module './HKT' { + interface URItoKind { + readonly [URI]: Predicate + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getSemigroupAny = (): Semigroup> => ({ + concat: (first, second) => pipe(first, or(second)) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getMonoidAny = (): Monoid> => ({ + concat: getSemigroupAny().concat, + empty: constFalse +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getSemigroupAll = (): Semigroup> => ({ + concat: (first, second) => pipe(first, and(second)) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getMonoidAll = (): Monoid> => ({ + concat: getSemigroupAll().concat, + empty: constTrue +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const Contravariant: Contravariant1 = { + URI, + contramap: contramap_ +} + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const not = (predicate: Predicate): Predicate => (a) => !predicate(a) + +/** + * @since 2.11.0 + */ +export const or = (second: Predicate) => (first: Predicate): Predicate => (a) => first(a) || second(a) + +/** + * @since 2.11.0 + */ +export const and = (second: Predicate) => (first: Predicate): Predicate => (a) => first(a) && second(a) diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 1fe2c5996..f0dbe223c 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -33,13 +33,14 @@ import { fromOptionK as fromOptionK_, fromPredicate as fromPredicate_ } from './FromEither' -import { flow, identity, pipe, Predicate, Refinement } from './function' +import { flow, identity, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { Monad3, Monad3C } from './Monad' import { MonadThrow3, MonadThrow3C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed3 } from './Pointed' +import { Predicate } from './Predicate' import * as R from './Reader' import { Semigroup } from './Semigroup' diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index d720776fa..49749ddc4 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -41,7 +41,7 @@ import { FromTask3, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, pipe, Predicate, Refinement } from './function' +import { flow, identity, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -52,6 +52,7 @@ import { MonadTask3, MonadTask3C } from './MonadTask' import { MonadThrow3, MonadThrow3C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed3 } from './Pointed' +import { Predicate } from './Predicate' import * as R from './Reader' import { ReaderEither } from './ReaderEither' import * as RT from './ReaderTask' diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index b88f7b398..0155a4bf4 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -15,7 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement } from './function' +import { identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -27,6 +27,7 @@ import * as N from './number' import { Option } from './Option' import { fromCompare, Ord } from './Ord' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import * as RNEA from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index e90a6f07a..bcd4a00c8 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -9,7 +9,7 @@ import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' import { Foldable, Foldable1, Foldable2, Foldable2C, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' -import { pipe, Predicate, Refinement, SK } from './function' +import { pipe, Refinement, SK } from './function' import { flap as flap_, Functor2 } from './Functor' import { FunctorWithIndex2C } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -18,6 +18,7 @@ import { Magma } from './Magma' import { Monoid } from './Monoid' import * as O from './Option' import { Ord } from './Ord' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 03c13a75b..a4e9a88bc 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -22,7 +22,7 @@ import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Predicate, Refinement, SK } from './function' +import { identity, Lazy, pipe, Refinement, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -32,6 +32,7 @@ import { NonEmptyArray } from './NonEmptyArray' import { Option } from './Option' import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import { ReadonlyRecord } from './ReadonlyRecord' import * as Se from './Semigroup' import { Show } from './Show' diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 5d71ac206..9edea410a 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, pipe, Predicate, Refinement, SK } from './function' +import { identity, pipe, Refinement, SK } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -18,6 +18,7 @@ import { Magma } from './Magma' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 8c0117147..772fc56a5 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -2,13 +2,14 @@ * @since 2.5.0 */ import { Either } from './Either' +import { Eq, fromEquals } from './Eq' +import { identity, Refinement } from './function' import { Monoid } from './Monoid' +import { Option } from './Option' import { Ord } from './Ord' +import { not, Predicate } from './Predicate' import { Semigroup } from './Semigroup' -import { Eq, fromEquals } from './Eq' -import { Predicate, not, Refinement, identity } from './function' import { Separated, separated } from './Separated' -import { Option } from './Option' import { Show } from './Show' /** diff --git a/src/Record.ts b/src/Record.ts index 4be3ff536..6a501094e 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { pipe, Predicate, Refinement } from './function' +import { pipe, Refinement } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -18,6 +18,7 @@ import { Magma } from './Magma' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' +import { Predicate } from './Predicate' import * as RR from './ReadonlyRecord' import { Semigroup } from './Semigroup' import { Separated } from './Separated' diff --git a/src/Set.ts b/src/Set.ts index 1649c0553..f349f975a 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -1,15 +1,16 @@ /** * @since 2.0.0 */ -import { separated, Separated } from './Separated' import { Either } from './Either' import { Eq } from './Eq' -import { identity, Predicate, Refinement } from './function' +import { identity, Refinement } from './function' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' +import { Predicate } from './Predicate' import * as RS from './ReadonlySet' import { Semigroup } from './Semigroup' +import { separated, Separated } from './Separated' import { Show } from './Show' /** diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 0a6d7c76c..3219fbb1d 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -24,7 +24,7 @@ import { FromTask4, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor4 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -34,6 +34,7 @@ import { MonadIO4 } from './MonadIO' import { MonadTask4 } from './MonadTask' import { MonadThrow4 } from './MonadThrow' import { Pointed4 } from './Pointed' +import { Predicate } from './Predicate' import { Reader } from './Reader' import { ReaderEither } from './ReaderEither' import * as RTE from './ReaderTaskEither' diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 0f034a34d..aabe04e23 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -48,7 +48,7 @@ import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -59,6 +59,7 @@ import { MonadTask2, MonadTask2C } from './MonadTask' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import * as T from './Task' import { TaskOption } from './TaskOption' diff --git a/src/TaskOption.ts b/src/TaskOption.ts index c57ac0701..d7151da12 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -22,7 +22,7 @@ import { FromTask1, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Predicate, Refinement } from './function' +import { flow, identity, Lazy, pipe, Refinement } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import * as _ from './internal' import { Monad1 } from './Monad' @@ -31,6 +31,7 @@ import { MonadTask1 } from './MonadTask' import * as O from './Option' import * as OT from './OptionT' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import { Separated } from './Separated' import * as T from './Task' import { TaskEither } from './TaskEither' diff --git a/src/function.ts b/src/function.ts index 1216b6c48..256e160b3 100644 --- a/src/function.ts +++ b/src/function.ts @@ -55,7 +55,7 @@ export const getSemigroup = (S: Semigroup) => (): Semigroup<(a: * Unary functions form a monoid as long as you can provide a monoid for the codomain. * * @example - * import { Predicate, getMonoid } from 'fp-ts/function' + * import { Predicate, getMonoid } from 'fp-ts/Predicate' * import * as B from 'fp-ts/boolean' * * const f: Predicate = (n) => n <= 2 @@ -126,13 +126,6 @@ export interface Lazy { (): A } -/** - * @since 2.0.0 - */ -export interface Predicate { - (a: A): boolean -} - /** * @since 2.0.0 */ @@ -164,13 +157,6 @@ export function identity(a: A): A { */ export const unsafeCoerce: (a: A) => B = identity as any -/** - * @since 2.0.0 - */ -export function not(predicate: Predicate): Predicate { - return (a) => !predicate(a) -} - /** * @since 2.0.0 */ @@ -745,6 +731,27 @@ export const SK = (_: A, b: B): B => b // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `Predicate` module instead. + * + * @since 2.0.0 + * @deprecated + */ +export interface Predicate { + (a: A): boolean +} + +/** + * Use `Predicate` module instead. + * + * @since 2.0.0 + * @deprecated + */ +// tslint:disable-next-line: deprecation +export function not(predicate: Predicate): Predicate { + return (a) => !predicate(a) +} + /** * Use `Endomorphism` module instead. * diff --git a/src/index.ts b/src/index.ts index b405bb3a9..fd7505ae5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,6 +69,7 @@ import * as ord from './Ord' import * as ordering from './Ordering' import * as pipeable from './pipeable' import * as pointed from './Pointed' +import * as predicate from './Predicate' import * as profunctor from './Profunctor' import * as random from './Random' import * as reader from './Reader' @@ -378,6 +379,10 @@ export { * @since 2.10.0 */ pointed, + /** + * @since 2.11.0 + */ + predicate, /** * @since 2.0.0 */ diff --git a/test/Array.ts b/test/Array.ts index e4abf0c77..34df1dcb4 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -4,11 +4,12 @@ import * as _ from '../src/Array' import * as B from '../src/boolean' import * as E from '../src/Either' import * as Eq from '../src/Eq' -import { identity, pipe, Refinement, Predicate, tuple } from '../src/function' +import { identity, pipe, Refinement, tuple } from '../src/function' import * as M from '../src/Monoid' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' +import { Predicate } from '../src/Predicate' import { separated } from '../src/Separated' import * as S from '../src/string' import * as T from '../src/Task' diff --git a/test/Predicate.ts b/test/Predicate.ts new file mode 100644 index 000000000..e23e1cb08 --- /dev/null +++ b/test/Predicate.ts @@ -0,0 +1,55 @@ +import { pipe } from '../src/function' +import * as _ from '../src/Predicate' +import * as U from './util' + +const isPositive: _.Predicate = (n) => n > 0 +const isNegative: _.Predicate = (n) => n < 0 +const lt2: _.Predicate = (n) => n < 2 + +describe('Predicate', () => { + it('contramap', () => { + type A = { + readonly a: number + } + const predicate = pipe( + isPositive, + _.contramap((a: A) => a.a) + ) + U.deepStrictEqual(predicate({ a: -1 }), false) + U.deepStrictEqual(predicate({ a: 0 }), false) + U.deepStrictEqual(predicate({ a: 1 }), true) + }) + + it('Contravariant.contramap', () => { + type A = { + readonly a: number + } + const predicate = _.Contravariant.contramap(isPositive, (a: A) => a.a) + U.deepStrictEqual(predicate({ a: -1 }), false) + U.deepStrictEqual(predicate({ a: 0 }), false) + U.deepStrictEqual(predicate({ a: 1 }), true) + }) + + it('not', () => { + const predicate = _.not(isPositive) + U.deepStrictEqual(predicate(1), false) + U.deepStrictEqual(predicate(0), true) + U.deepStrictEqual(predicate(-1), true) + }) + + it('getMonoidAny', () => { + const M = _.getMonoidAny() + const predicate = M.concat(isPositive, isNegative) + U.deepStrictEqual(predicate(0), false) + U.deepStrictEqual(predicate(-1), true) + U.deepStrictEqual(predicate(1), true) + }) + + it('getMonoidAll', () => { + const M = _.getMonoidAll() + const predicate = M.concat(isPositive, lt2) + U.deepStrictEqual(predicate(0), false) + U.deepStrictEqual(predicate(-2), false) + U.deepStrictEqual(predicate(1), true) + }) +}) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index 4b4a576de..b72638202 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -4,11 +4,12 @@ import { isDeepStrictEqual } from 'util' import * as B from '../src/boolean' import * as E from '../src/Either' import * as Eq from '../src/Eq' -import { identity, pipe, Predicate, tuple } from '../src/function' +import { identity, pipe, tuple } from '../src/function' import * as M from '../src/Monoid' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' +import { Predicate } from '../src/Predicate' import * as _ from '../src/ReadonlyArray' import { separated } from '../src/Separated' import * as S from '../src/string' diff --git a/test/function.ts b/test/function.ts index 6a58f3499..5d789ccf0 100644 --- a/test/function.ts +++ b/test/function.ts @@ -16,6 +16,7 @@ describe('function', () => { }) it('not', () => { + // tslint:disable-next-line: deprecation const n = _.not(Boolean) U.deepStrictEqual(n(false), true) U.deepStrictEqual(n(1), false) From 2776ee8464c1d920c6194deee875223164a45a8a Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 17:41:25 +0200 Subject: [PATCH 028/162] add `Refinement` module --- CHANGELOG.md | 2 ++ src/Array.ts | 3 ++- src/Either.ts | 3 ++- src/Filterable.ts | 3 ++- src/FromEither.ts | 3 ++- src/IOEither.ts | 3 ++- src/Map.ts | 3 ++- src/NonEmptyArray.ts | 3 ++- src/Option.ts | 3 ++- src/OptionT.ts | 3 ++- src/ReaderEither.ts | 3 ++- src/ReaderTaskEither.ts | 3 ++- src/ReadonlyArray.ts | 3 ++- src/ReadonlyMap.ts | 3 ++- src/ReadonlyNonEmptyArray.ts | 3 ++- src/ReadonlyRecord.ts | 3 ++- src/ReadonlySet.ts | 3 ++- src/Record.ts | 3 ++- src/Refinement.ts | 39 ++++++++++++++++++++++++++++++++++++ src/Set.ts | 3 ++- src/StateReaderTaskEither.ts | 3 ++- src/TaskEither.ts | 3 ++- src/TaskOption.ts | 3 ++- src/function.ts | 17 +++++++++------- src/index.ts | 5 +++++ test/Array.ts | 3 ++- test/Map.ts | 11 +++++----- test/ReadonlyMap.ts | 5 +++-- test/Refinement.ts | 39 ++++++++++++++++++++++++++++++++++++ 29 files changed, 148 insertions(+), 36 deletions(-) create mode 100644 src/Refinement.ts create mode 100644 test/Refinement.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c4e47c14a..45a42d9d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ high state of flux, you're at risk of it changing without notice. - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. - deprecate `Predicate`, use `Predicate` module instead. - deprecate `not`, use `Predicate` module instead. + - deprecate `Refinement`, use `Refinement` module instead. - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` @@ -40,6 +41,7 @@ high state of flux, you're at risk of it changing without notice. - **New Feature** - add `Endomorphism` module - add `Predicate` module + - add `Refinement` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/Array.ts b/src/Array.ts index af6fcb336..0c32a71b0 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -15,7 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Refinement } from './function' +import { identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -28,6 +28,7 @@ import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' import * as RA from './ReadonlyArray' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { separated, Separated } from './Separated' import { Show } from './Show' diff --git a/src/Either.ts b/src/Either.ts index e12d99396..99f71e3ef 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -31,7 +31,7 @@ import { Extend2 } from './Extend' import { Filterable2C } from './Filterable' import { Foldable2 } from './Foldable' import { FromEither2 } from './FromEither' -import { flow, identity, Lazy, pipe, Refinement } from './function' +import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import { HKT } from './HKT' import * as _ from './internal' @@ -41,6 +41,7 @@ import { Monoid } from './Monoid' import { Option } from './Option' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/Filterable.ts b/src/Filterable.ts index f215abd1f..3cbdfb4c8 100644 --- a/src/Filterable.ts +++ b/src/Filterable.ts @@ -24,7 +24,7 @@ import { separate } from './Compactable' import { Either } from './Either' -import { pipe, Refinement } from './function' +import { pipe } from './function' import { Functor, Functor1, @@ -45,6 +45,7 @@ import { import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { getLeft, getRight, Option } from './Option' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { separated, Separated } from './Separated' // ------------------------------------------------------------------------------------- diff --git a/src/FromEither.ts b/src/FromEither.ts index d2f617a63..f1e0cd679 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -6,11 +6,12 @@ import { Chain, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' import { Either } from './Either' -import { flow, Lazy, Refinement } from './function' +import { flow, Lazy } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' import * as _ from './internal' import { Option } from './Option' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' // ------------------------------------------------------------------------------------- // model diff --git a/src/IOEither.ts b/src/IOEither.ts index f16362e1c..7d6c4a760 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -37,7 +37,7 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_ } from './FromIO' -import { flow, identity, Lazy, pipe, Refinement } from './function' +import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import * as I from './IO' @@ -47,6 +47,7 @@ import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- diff --git a/src/Map.ts b/src/Map.ts index b97ac90c1..1a74f8708 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -9,7 +9,7 @@ import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' import { Foldable, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' -import { pipe, Refinement } from './function' +import { pipe } from './function' import { flap as flap_, Functor2 } from './Functor' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' import * as _ from './internal' @@ -19,6 +19,7 @@ import * as O from './Option' import { Ord } from './Ord' import { Predicate } from './Predicate' import * as RM from './ReadonlyMap' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { separated, Separated } from './Separated' import { Show } from './Show' diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index d035bb4e8..26fc2fa6a 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -22,7 +22,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Refinement } from './function' +import { identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -33,6 +33,7 @@ import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' import * as RNEA from './ReadonlyNonEmptyArray' +import { Refinement } from './Refinement' import * as Se from './Semigroup' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' diff --git a/src/Option.ts b/src/Option.ts index 2a526398a..400a295f2 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -29,7 +29,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Filterable1 } from './Filterable' import { Foldable1 } from './Foldable' -import { constNull, constUndefined, flow, identity, Lazy, pipe, Refinement } from './function' +import { constNull, constUndefined, flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { HKT } from './HKT' import * as _ from './internal' @@ -39,6 +39,7 @@ import { Monoid } from './Monoid' import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/OptionT.ts b/src/OptionT.ts index 4d409a791..cac264eef 100644 --- a/src/OptionT.ts +++ b/src/OptionT.ts @@ -10,13 +10,14 @@ import { import { ap as ap_, Apply, Apply1, Apply2, Apply2C, Apply3, Apply3C, Apply4 } from './Apply' import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' import { Either } from './Either' -import { constant, flow, Lazy, pipe, Refinement } from './function' +import { constant, flow, Lazy, pipe } from './function' import { Functor, Functor1, Functor2, Functor2C, Functor3, Functor3C, Functor4, map as map_ } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { Monad, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad' import * as O from './Option' import { Pointed, Pointed1, Pointed2, Pointed2C, Pointed3, Pointed3C, Pointed4 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import Option = O.Option diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index f0dbe223c..b20afd79e 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -33,7 +33,7 @@ import { fromOptionK as fromOptionK_, fromPredicate as fromPredicate_ } from './FromEither' -import { flow, identity, pipe, Refinement } from './function' +import { flow, identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { Monad3, Monad3C } from './Monad' @@ -42,6 +42,7 @@ import { Monoid } from './Monoid' import { Pointed3 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import Reader = R.Reader diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 49749ddc4..5efa60154 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -41,7 +41,7 @@ import { FromTask3, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, pipe, Refinement } from './function' +import { flow, identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -56,6 +56,7 @@ import { Predicate } from './Predicate' import * as R from './Reader' import { ReaderEither } from './ReaderEither' import * as RT from './ReaderTask' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import * as T from './Task' import * as TE from './TaskEither' diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 0155a4bf4..ae94c07cc 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -15,7 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Refinement } from './function' +import { identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -29,6 +29,7 @@ import { fromCompare, Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' import * as RNEA from './ReadonlyNonEmptyArray' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index bcd4a00c8..e8fac8203 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -9,7 +9,7 @@ import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' import { Foldable, Foldable1, Foldable2, Foldable2C, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' -import { pipe, Refinement, SK } from './function' +import { pipe, SK } from './function' import { flap as flap_, Functor2 } from './Functor' import { FunctorWithIndex2C } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -19,6 +19,7 @@ import { Monoid } from './Monoid' import * as O from './Option' import { Ord } from './Ord' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index a4e9a88bc..dc229b2ac 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -22,7 +22,7 @@ import { Eq, fromEquals } from './Eq' import { Extend1 } from './Extend' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, Lazy, pipe, Refinement, SK } from './function' +import { identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' @@ -34,6 +34,7 @@ import { getMonoid, Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' import { ReadonlyRecord } from './ReadonlyRecord' +import { Refinement } from './Refinement' import * as Se from './Semigroup' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 9edea410a..bfb259ad3 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, pipe, Refinement, SK } from './function' +import { identity, pipe, SK } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -19,6 +19,7 @@ import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 772fc56a5..5b3d1ea1c 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -3,11 +3,12 @@ */ import { Either } from './Either' import { Eq, fromEquals } from './Eq' -import { identity, Refinement } from './function' +import { identity } from './function' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' import { not, Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' diff --git a/src/Record.ts b/src/Record.ts index 6a501094e..2217c7a5f 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { pipe, Refinement } from './function' +import { pipe } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -20,6 +20,7 @@ import { Option } from './Option' import { Ord } from './Ord' import { Predicate } from './Predicate' import * as RR from './ReadonlyRecord' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated } from './Separated' import { Show } from './Show' diff --git a/src/Refinement.ts b/src/Refinement.ts new file mode 100644 index 000000000..e65e78c1a --- /dev/null +++ b/src/Refinement.ts @@ -0,0 +1,39 @@ +/** + * @since 2.11.0 + */ + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export interface Refinement { + (a: A): a is B +} + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const not = (refinement: Refinement): Refinement> => ( + a +): a is Exclude => !refinement(a) + +/** + * @since 2.11.0 + */ +export const or = (second: Refinement) => ( + first: Refinement +): Refinement => (a): a is B | C => first(a) || second(a) + +/** + * @since 2.11.0 + */ +export const and = (second: Refinement) => ( + first: Refinement +): Refinement => (a): a is B & C => first(a) && second(a) diff --git a/src/Set.ts b/src/Set.ts index f349f975a..3f6177efa 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -3,12 +3,13 @@ */ import { Either } from './Either' import { Eq } from './Eq' -import { identity, Refinement } from './function' +import { identity } from './function' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' import { Predicate } from './Predicate' import * as RS from './ReadonlySet' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { separated, Separated } from './Separated' import { Show } from './Show' diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 3219fbb1d..c2853b1c3 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -24,7 +24,7 @@ import { FromTask4, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Refinement } from './function' +import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor4 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -38,6 +38,7 @@ import { Predicate } from './Predicate' import { Reader } from './Reader' import { ReaderEither } from './ReaderEither' import * as RTE from './ReaderTaskEither' +import { Refinement } from './Refinement' import { State } from './State' import * as ST from './StateT' import { Task } from './Task' diff --git a/src/TaskEither.ts b/src/TaskEither.ts index aabe04e23..2d30d64dd 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -48,7 +48,7 @@ import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Refinement } from './function' +import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -60,6 +60,7 @@ import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import * as T from './Task' import { TaskOption } from './TaskOption' diff --git a/src/TaskOption.ts b/src/TaskOption.ts index d7151da12..68956d74e 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -22,7 +22,7 @@ import { FromTask1, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, Refinement } from './function' +import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import * as _ from './internal' import { Monad1 } from './Monad' @@ -32,6 +32,7 @@ import * as O from './Option' import * as OT from './OptionT' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' +import { Refinement } from './Refinement' import { Separated } from './Separated' import * as T from './Task' import { TaskEither } from './TaskEither' diff --git a/src/function.ts b/src/function.ts index 256e160b3..ced5a53a1 100644 --- a/src/function.ts +++ b/src/function.ts @@ -126,13 +126,6 @@ export interface Lazy { (): A } -/** - * @since 2.0.0 - */ -export interface Refinement { - (a: A): a is B -} - /** * @example * import { FunctionN } from 'fp-ts/function' @@ -731,6 +724,16 @@ export const SK = (_: A, b: B): B => b // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `Refinement` module instead. + * + * @since 2.0.0 + * @deprecated + */ +export interface Refinement { + (a: A): a is B +} + /** * Use `Predicate` module instead. * diff --git a/src/index.ts b/src/index.ts index fd7505ae5..3ee873571 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,6 +84,7 @@ import * as readonlyRecord from './ReadonlyRecord' import * as readonlySet from './ReadonlySet' import * as readonlyTuple from './ReadonlyTuple' import * as record from './Record' +import * as refinement from './Refinement' import * as ring from './Ring' import * as semigroup from './Semigroup' import * as semigroupoid from './Semigroupoid' @@ -439,6 +440,10 @@ export { * @since 2.0.0 */ record, + /** + * @since 2.11.0 + */ + refinement, /** * @since 2.0.0 */ diff --git a/test/Array.ts b/test/Array.ts index 34df1dcb4..46aca87bb 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -4,12 +4,13 @@ import * as _ from '../src/Array' import * as B from '../src/boolean' import * as E from '../src/Either' import * as Eq from '../src/Eq' -import { identity, pipe, Refinement, tuple } from '../src/function' +import { identity, pipe, tuple } from '../src/function' import * as M from '../src/Monoid' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' import { Predicate } from '../src/Predicate' +import { Refinement } from '../src/Refinement' import { separated } from '../src/Separated' import * as S from '../src/string' import * as T from '../src/Task' diff --git a/test/Map.ts b/test/Map.ts index df2d0bc7c..43b646418 100644 --- a/test/Map.ts +++ b/test/Map.ts @@ -1,18 +1,19 @@ -import * as U from './util' +import * as assert from 'assert' import { Either, left, right } from '../src/Either' import { Eq, fromEquals } from '../src/Eq' -import { identity, pipe, Refinement } from '../src/function' +import { identity, pipe } from '../src/function' import * as _ from '../src/Map' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' import * as RA from '../src/ReadonlyArray' +import { Refinement } from '../src/Refinement' import * as Se from '../src/Semigroup' -import { struct, Show } from '../src/Show' +import { separated } from '../src/Separated' +import { Show, struct } from '../src/Show' import * as S from '../src/string' import * as T from '../src/Task' -import * as assert from 'assert' -import { separated } from '../src/Separated' +import * as U from './util' interface User { readonly id: string diff --git a/test/ReadonlyMap.ts b/test/ReadonlyMap.ts index 6feb48f11..69e18ea9e 100644 --- a/test/ReadonlyMap.ts +++ b/test/ReadonlyMap.ts @@ -1,16 +1,17 @@ import * as assert from 'assert' import { Either, left, right } from '../src/Either' import { Eq, fromEquals } from '../src/Eq' -import { identity, pipe, Refinement } from '../src/function' +import { identity, pipe } from '../src/function' import * as IO from '../src/IO' import * as N from '../src/number' import * as O from '../src/Option' import * as Ord from '../src/Ord' import * as RA from '../src/ReadonlyArray' import * as _ from '../src/ReadonlyMap' +import { Refinement } from '../src/Refinement' import * as Se from '../src/Semigroup' import { separated } from '../src/Separated' -import { struct, Show } from '../src/Show' +import { Show, struct } from '../src/Show' import * as S from '../src/string' import * as T from '../src/Task' import * as U from './util' diff --git a/test/Refinement.ts b/test/Refinement.ts new file mode 100644 index 000000000..a1c9a2e59 --- /dev/null +++ b/test/Refinement.ts @@ -0,0 +1,39 @@ +import { pipe } from '../src/function' +import { ReadonlyRecord } from '../src/ReadonlyRecord' +import * as _ from '../src/Refinement' +import * as U from './util' + +export const string = (u: unknown): u is string => typeof u === 'string' + +export const number = (u: unknown): u is number => typeof u === 'number' + +export const boolean = (u: unknown): u is boolean => typeof u === 'boolean' + +describe('Refinement', () => { + it('not', () => { + const r1: _.Refinement = string + const r2 = _.not(r1) + U.deepStrictEqual(r2('a'), false) + U.deepStrictEqual(r2(1), true) + }) + + it('or', () => { + const r = pipe(string, _.or(number), _.or(boolean)) + U.deepStrictEqual(r({}), false) + U.deepStrictEqual(r('a'), true) + U.deepStrictEqual(r(1), true) + U.deepStrictEqual(r(true), true) + }) + + it('and', () => { + const ra = (r: ReadonlyRecord): r is { readonly a: string } => string(r['a']) + const rb = (r: ReadonlyRecord): r is { readonly b: number } => number(r['b']) + const r = pipe(ra, _.and(rb)) + U.deepStrictEqual(r({ a: 'a' }), false) + U.deepStrictEqual(r({ b: 1 }), false) + U.deepStrictEqual(r({}), false) + U.deepStrictEqual(r({ a: 'a', b: 'b' }), false) + U.deepStrictEqual(r({ a: 1, b: 2 }), false) + U.deepStrictEqual(r({ a: 'a', b: 1 }), true) + }) +}) From 07981945fdedc4f348b28c74eaaf2d67566e40dd Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 17:55:38 +0200 Subject: [PATCH 029/162] - add `FromState` module --- CHANGELOG.md | 4 + src/FromState.ts | 175 ++++++++++++++++++++++++++++++++++ src/StateReaderTaskEither.ts | 113 +++++++++++++++------- src/index.ts | 5 + test/StateReaderTaskEither.ts | 13 +++ 5 files changed, 276 insertions(+), 34 deletions(-) create mode 100644 src/FromState.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 45a42d9d1..db89413e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ high state of flux, you're at risk of it changing without notice. - add `Endomorphism` module - add `Predicate` module - add `Refinement` module + - add `FromState` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) @@ -79,6 +80,9 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - `StateReaderTaskEither` + - add `fromStateK` + - add `chainStateK` - `struct` - add `evolve` - `TaskEither` diff --git a/src/FromState.ts b/src/FromState.ts new file mode 100644 index 000000000..25eaf3e7b --- /dev/null +++ b/src/FromState.ts @@ -0,0 +1,175 @@ +/** + * Lift a computation from the `State` monad. + * + * @since 2.11.0 + */ +import type { Chain, Chain2, Chain3, Chain4 } from './Chain' +import type { Endomorphism } from './Endomorphism' +import { flow } from './function' +import type { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import * as S from './State' + +import State = S.State + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromState { + readonly URI: F + readonly fromState: (fa: State) => HKT2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromState2 { + readonly URI: F + readonly fromState: (fa: State) => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromState3 { + readonly URI: F + readonly fromState: (fa: State) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromState3C { + readonly URI: F + readonly _E: E + readonly fromState: (fa: State) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromState4 { + readonly URI: F + readonly fromState: (fa: State) => Kind4 +} + +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + +/** + * @category constructors + * @since 2.11.0 + */ +export function get(F: FromState4): () => Kind4 +export function get(F: FromState3): () => Kind3 +export function get(F: FromState3C): () => Kind3 +export function get(F: FromState2): () => Kind2 +export function get(F: FromState): () => HKT2 +export function get(F: FromState): () => HKT2 { + return () => F.fromState(S.get()) +} + +/** + * @category constructors + * @since 2.11.0 + */ +export function put(F: FromState4): (s: S) => Kind4 +export function put(F: FromState3): (s: S) => Kind3 +export function put(F: FromState3C): (s: S) => Kind3 +export function put(F: FromState2): (s: S) => Kind2 +export function put(F: FromState): (s: S) => HKT2 +export function put(F: FromState): (s: S) => HKT2 { + return (s) => F.fromState(S.put(s)) +} + +/** + * @category constructors + * @since 2.11.0 + */ +export function modify(F: FromState4): (f: Endomorphism) => Kind4 +export function modify(F: FromState3): (f: Endomorphism) => Kind3 +export function modify(F: FromState3C): (f: Endomorphism) => Kind3 +export function modify(F: FromState2): (f: Endomorphism) => Kind2 +export function modify(F: FromState): (f: Endomorphism) => HKT2 +export function modify(F: FromState): (f: Endomorphism) => HKT2 { + return flow(S.modify, F.fromState) +} + +/** + * @category constructors + * @since 2.11.0 + */ +export function gets(F: FromState4): (f: (s: S) => A) => Kind4 +export function gets(F: FromState3): (f: (s: S) => A) => Kind3 +export function gets(F: FromState3C): (f: (s: S) => A) => Kind3 +export function gets(F: FromState2): (f: (s: S) => A) => Kind2 +export function gets(F: FromState): (f: (s: S) => A) => HKT2 +export function gets(F: FromState): (f: (s: S) => A) => HKT2 { + return flow(S.gets, F.fromState) +} + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * @category combinators + * @since 2.11.0 + */ +export function fromStateK( + F: FromState4 +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind4 +export function fromStateK( + F: FromState3 +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind3 +export function fromStateK( + F: FromState3C +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind3 +export function fromStateK( + F: FromState2 +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind2 +export function fromStateK( + F: FromState +): , S, B>(f: (...a: A) => State) => (...a: A) => HKT2 +export function fromStateK( + F: FromState +): , S, B>(f: (...a: A) => State) => (...a: A) => HKT2 { + return (f) => flow(f, F.fromState) +} + +/** + * @category combinators + * @since 2.11.0 + */ +export function chainStateK( + F: FromState4, + M: Chain4 +): (f: (a: A) => State) => (ma: Kind4) => Kind4 +export function chainStateK( + F: FromState3, + M: Chain3 +): (f: (a: A) => State) => (ma: Kind3) => Kind3 +export function chainStateK( + F: FromState2, + M: Chain2 +): (f: (a: A) => State) => (ma: Kind2) => Kind2 +export function chainStateK( + F: FromState, + M: Chain +): (f: (a: A) => State) => (ma: HKT2) => HKT2 +export function chainStateK( + F: FromState2, + M: Chain2 +): (f: (a: A) => State) => (ma: Kind2) => Kind2 { + const fromStateKF = fromStateK(F) + return (f) => (ma) => M.chain(ma, fromStateKF(f)) +} diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index c2853b1c3..7a00f2567 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -43,6 +43,15 @@ import { State } from './State' import * as ST from './StateT' import { Task } from './Task' import { TaskEither } from './TaskEither' +import { + chainStateK as chainStateK_, + FromState4, + fromStateK as fromStateK_, + get as get_, + gets as gets_, + modify as modify_, + put as put_ +} from './FromState' // ------------------------------------------------------------------------------------- // model @@ -50,6 +59,7 @@ import { TaskEither } from './TaskEither' import ReaderTaskEither = RTE.ReaderTaskEither import Either = E.Either +import { Endomorphism } from './Endomorphism' /** * @category model @@ -172,40 +182,6 @@ export const fromReaderTaskEither: (ma: ReaderTaskEither) = /*#__PURE__*/ ST.fromF(RTE.Functor) -/** - * Get the current state - * - * @category constructors - * @since 2.0.0 - */ -export const get = (): StateReaderTaskEither => (s) => RTE.of([s, s]) - -/** - * Set the state - * - * @category constructors - * @since 2.0.0 - */ -export const put = (s: S): StateReaderTaskEither => () => RTE.of([undefined, s]) - -/** - * Modify the state by applying a function to the current state - * - * @category constructors - * @since 2.0.0 - */ -export const modify = (f: (s: S) => S): StateReaderTaskEither => (s) => - RTE.of([undefined, f(s)]) - -/** - * Get a value which depends on the current state - * - * @category constructors - * @since 2.0.0 - */ -export const gets = (f: (s: S) => A): StateReaderTaskEither => (s) => - RTE.of([f(s), s]) - /** * @category constructors * @since 2.0.0 @@ -592,6 +568,75 @@ export const Chain: Chain4 = { chain: _chain } +/** + * @category instances + * @since 2.11.0 + */ +export const FromState: FromState4 = { + URI, + fromState +} + +/** + * Get the current state + * + * @category constructors + * @since 2.0.0 + */ +export const get: () => StateReaderTaskEither = + /*#__PURE__*/ + get_(FromState) + +/** + * Set the state + * + * @category constructors + * @since 2.0.0 + */ +export const put: (s: S) => StateReaderTaskEither = + /*#__PURE__*/ + put_(FromState) + +/** + * Modify the state by applying a function to the current state + * + * @category constructors + * @since 2.0.0 + */ +export const modify: (f: Endomorphism) => StateReaderTaskEither = + /*#__PURE__*/ + modify_(FromState) + +/** + * Get a value which depends on the current state + * + * @category constructors + * @since 2.0.0 + */ +export const gets: (f: (s: S) => A) => StateReaderTaskEither = + /*#__PURE__*/ + gets_(FromState) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromStateK: , S, B>( + f: (...a: A) => State +) => (...a: A) => StateReaderTaskEither = + /*#__PURE__*/ + fromStateK_(FromState) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainStateK: ( + f: (a: A) => State +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = + /*#__PURE__*/ + chainStateK_(FromState, Chain) + /** * @category instances * @since 2.10.0 diff --git a/src/index.ts b/src/index.ts index 3ee873571..e2349533b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,7 @@ import * as foldable from './Foldable' import * as foldableWithIndex from './FoldableWithIndex' import * as fromEither from './FromEither' import * as fromIO from './FromIO' +import * as fromState from './FromState' import * as fromTask from './FromTask' import * as function_ from './function' import * as functor from './Functor' @@ -256,6 +257,10 @@ export { * @since 2.10.0 */ fromIO, + /** + * @since 2.11.0 + */ + fromState, /** * @since 2.10.0 */ diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 10ee59382..4b35041ab 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -355,4 +355,17 @@ describe('StateReaderTaskEither', () => { const e = await pipe(_.fromState(s), _.evaluate(state))({})() U.deepStrictEqual(e, E.right(1)) }) + + it('fromStateK', async () => { + const ma = _.fromStateK((n: number): State => (s) => [n * 2, s + 1]) + U.deepStrictEqual(await ma(3)(2)({})(), E.right([6, 3])) + }) + + it('chainStateK', async () => { + const f = _.chainStateK((n: number): State => (s) => [n * 2, s + 1]) + const right: _.StateReaderTaskEither = _.right(3) + U.deepStrictEqual(await pipe(right, f)(2)({})(), E.right([6, 3])) + const left: _.StateReaderTaskEither = _.left('a') + U.deepStrictEqual(await pipe(left, f)(2)({})(), E.left('a')) + }) }) From 6417829dbbac3c78442ceec052c8eccc00d76073 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 18:04:06 +0200 Subject: [PATCH 030/162] add `FromThese` module --- CHANGELOG.md | 1 + src/FromThese.ts | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/State.ts | 15 +++++-- src/TaskThese.ts | 24 +++++++++++ src/These.ts | 10 +++++ src/index.ts | 5 +++ test/TaskThese.ts | 7 ++++ 7 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/FromThese.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index db89413e1..85f74bddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ high state of flux, you're at risk of it changing without notice. - add `Predicate` module - add `Refinement` module - add `FromState` module + - add `FromThese` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/FromThese.ts b/src/FromThese.ts new file mode 100644 index 000000000..f104455f8 --- /dev/null +++ b/src/FromThese.ts @@ -0,0 +1,100 @@ +/** + * The `FromThese` type class represents those data types which support errors and warnings. + * + * @since 2.11.0 + */ +import { flow } from './function' +import type { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import type { These } from './These' + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese { + readonly URI: F + readonly fromThese: (e: These) => HKT2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese2 { + readonly URI: F + readonly fromThese: (e: These) => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese2C { + readonly URI: F + readonly _E: E + readonly fromThese: (e: These) => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese3 { + readonly URI: F + readonly fromThese: (e: These) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese3C { + readonly URI: F + readonly _E: E + readonly fromThese: (e: These) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromThese4 { + readonly URI: F + readonly fromThese: (e: These) => Kind4 +} + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * @category combinators + * @since 2.11.0 + */ +export function fromTheseK( + F: FromThese4 +): , E, B>(f: (...a: A) => These) => (...a: A) => Kind4 +export function fromTheseK( + F: FromThese3 +): , E, B>(f: (...a: A) => These) => (...a: A) => Kind3 +export function fromTheseK( + F: FromThese3C +): , B>(f: (...a: A) => These) => (...a: A) => Kind3 +export function fromTheseK( + F: FromThese2 +): , E, B>(f: (...a: A) => These) => (...a: A) => Kind2 +export function fromTheseK( + F: FromThese2C +): , B>(f: (...a: A) => These) => (...a: A) => Kind2 +export function fromTheseK( + F: FromThese +): , E, B>(f: (...a: A) => These) => (...a: A) => HKT2 +export function fromTheseK( + F: FromThese +): , E, B>(f: (...a: A) => These) => (...a: A) => HKT2 { + return (f) => flow(f, F.fromThese) +} diff --git a/src/State.ts b/src/State.ts index 38891ee9c..b9876fe43 100644 --- a/src/State.ts +++ b/src/State.ts @@ -2,12 +2,13 @@ * @since 2.0.0 */ import { Applicative2 } from './Applicative' -import { apFirst as apFirst_, Apply2, apSecond as apSecond_, apS as apS_ } from './Apply' +import { apFirst as apFirst_, Apply2, apS as apS_, apSecond as apSecond_ } from './Apply' +import { bind as bind_, Chain2, chainFirst as chainFirst_ } from './Chain' +import { FromState2 } from './FromState' import { identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' -import { bind as bind_, Chain2, chainFirst as chainFirst_ } from './Chain' -import { Pointed2 } from './Pointed' import { Monad2 } from './Monad' +import { Pointed2 } from './Pointed' // ------------------------------------------------------------------------------------- // model @@ -254,6 +255,14 @@ export const chainFirst: (f: (a: A) => State) => (ma: State /*#__PURE__*/ chainFirst_(Chain) +/** + * @category instances + * @since 2.11.0 + */ +export const FromState: FromState2 = { + URI, + fromState: identity +} // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- diff --git a/src/TaskThese.ts b/src/TaskThese.ts index e4d80a4b3..8ceef94b9 100644 --- a/src/TaskThese.ts +++ b/src/TaskThese.ts @@ -24,6 +24,7 @@ import { Semigroup } from './Semigroup' import * as T from './Task' import * as TH from './These' import * as TT from './TheseT' +import { FromThese2, fromTheseK as fromTheseK_ } from './FromThese' import These = TH.These import Task = T.Task @@ -120,6 +121,12 @@ export const fromTask: FromTask2['fromTask'] = rightTask */ export const fromEither: FromEither2['fromEither'] = T.of +/** + * @category constructors + * @since 2.11.0 + */ +export const fromThese: FromThese2['fromThese'] = T.of + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- @@ -408,6 +415,23 @@ export const fromPredicate = /*#__PURE__*/ fromPredicate_(FromEither) +/** + * @category instances + * @since 2.11.0 + */ +export const FromThese: FromThese2 = { + URI, + fromThese +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromTheseK = + /*#__PURE__*/ + fromTheseK_(FromThese) + /** * @category instances * @since 2.10.0 diff --git a/src/These.ts b/src/These.ts index 9d3719801..02225dc74 100644 --- a/src/These.ts +++ b/src/These.ts @@ -27,6 +27,7 @@ import { Either, Left, Right } from './Either' import { Eq, fromEquals } from './Eq' import { Foldable2 } from './Foldable' import { FromEither2, fromOption as fromOption_, fromOptionK as fromOptionK_ } from './FromEither' +import { FromThese2 } from './FromThese' import { identity, Lazy, pipe } from './function' import { flap as flap_, Functor2 } from './Functor' import { HKT } from './HKT' @@ -602,6 +603,15 @@ export const Bifunctor: Bifunctor2 = { mapLeft: _mapLeft } +/** + * @category instances + * @since 2.11.0 + */ +export const FromThese: FromThese2 = { + URI, + fromThese: identity +} + /** * @category instances * @since 2.7.0 diff --git a/src/index.ts b/src/index.ts index e2349533b..5922effab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,7 @@ import * as fromEither from './FromEither' import * as fromIO from './FromIO' import * as fromState from './FromState' import * as fromTask from './FromTask' +import * as fromThese from './FromThese' import * as function_ from './function' import * as functor from './Functor' import * as functorWithIndex from './FunctorWithIndex' @@ -265,6 +266,10 @@ export { * @since 2.10.0 */ fromTask, + /** + * @since 2.11.0 + */ + fromThese, /** * @since 2.0.0 */ diff --git a/test/TaskThese.ts b/test/TaskThese.ts index 3a89c545a..a59e8161e 100644 --- a/test/TaskThese.ts +++ b/test/TaskThese.ts @@ -172,4 +172,11 @@ describe('TaskThese', () => { U.deepStrictEqual(await pipe(_.left('a'), matchE)(), 'left a') U.deepStrictEqual(await pipe(_.both('a', 1), matchE)(), 'both a 1') }) + + it('fromTheseK', async () => { + const g = _.fromTheseK((n: number) => (n > 0 ? TH.right(n) : n === 0 ? TH.both('zero', n) : TH.left('negative'))) + U.deepStrictEqual(await g(-1)(), TH.left('negative')) + U.deepStrictEqual(await g(0)(), TH.both('zero', 0)) + U.deepStrictEqual(await g(1)(), TH.right(1)) + }) }) From 674bb8393c30961d3e68974789f219dfcbba153e Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 18:11:45 +0200 Subject: [PATCH 031/162] add `void` module --- CHANGELOG.md | 5 +++++ src/Monoid.ts | 22 +++++++++++++--------- src/Semigroup.ts | 15 +++++++++------ src/index.ts | 5 +++++ src/void.ts | 24 ++++++++++++++++++++++++ test/Semigroup.ts | 1 + 6 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 src/void.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 85f74bddc..19a15a575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Predicate`, use `Predicate` module instead. - deprecate `not`, use `Predicate` module instead. - deprecate `Refinement`, use `Refinement` module instead. + - `Monoid` + - deprecate `monoidVoid`, use `void` module instead. - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` @@ -38,12 +40,15 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Traversable` in favour of `getTraversable` (@anthonyjoeseph) - deprecate `TraversableWithIndex` in favour of `getTraversableWithIndex` (@anthonyjoeseph) - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) + - `Semigroup` + - deprecate `semigroupVoid`, use `void` module instead. - **New Feature** - add `Endomorphism` module - add `Predicate` module - add `Refinement` module - add `FromState` module - add `FromThese` module + - add `void` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/Monoid.ts b/src/Monoid.ts index 246f2db2c..c0ec1ec6e 100644 --- a/src/Monoid.ts +++ b/src/Monoid.ts @@ -178,15 +178,6 @@ export const tuple = >( empty: monoids.map((m) => m.empty) } as any) -/** - * @category instances - * @since 2.0.0 - */ -export const monoidVoid: Monoid = { - concat: Se.semigroupVoid.concat, - empty: undefined -} - // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- @@ -211,6 +202,19 @@ export const concatAll = (M: Monoid): ((as: ReadonlyArray) => A) => Se. // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `void` module instead. + * + * @category instances + * @since 2.0.0 + * @deprecated + */ +export const monoidVoid: Monoid = { + // tslint:disable-next-line: deprecation + concat: Se.semigroupVoid.concat, + empty: undefined +} + /** * Use `tuple` instead. * diff --git a/src/Semigroup.ts b/src/Semigroup.ts index 4bb79d9a7..ec46c8627 100644 --- a/src/Semigroup.ts +++ b/src/Semigroup.ts @@ -232,12 +232,6 @@ export const first = (): Semigroup => ({ concat: identity }) */ export const last = (): Semigroup => ({ concat: (_, y) => y }) -/** - * @category instances - * @since 2.0.0 - */ -export const semigroupVoid: Semigroup = constant(undefined) - // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- @@ -265,6 +259,15 @@ export const concatAll = (S: Semigroup) => (startWith: A) => (as: Readonly // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `void` module instead. + * + * @category instances + * @since 2.0.0 + * @deprecated + */ +export const semigroupVoid: Semigroup = constant(undefined) + /** * Use `object.getAssignSemigroup` instead. * diff --git a/src/index.ts b/src/index.ts index 5922effab..0fd9871ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,6 +114,7 @@ import * as tree from './Tree' import * as tuple from './Tuple' import * as unfoldable from './Unfoldable' import * as validationT from './ValidationT' +import * as void_ from './void' import * as witherable from './Witherable' import * as writer from './Writer' import * as writerT from './WriterT' @@ -566,6 +567,10 @@ export { * @since 2.0.0 */ validationT, + /** + * @since 2.11.0 + */ + void_ as void, /** * @since 2.0.0 */ diff --git a/src/void.ts b/src/void.ts new file mode 100644 index 000000000..02f0e3f88 --- /dev/null +++ b/src/void.ts @@ -0,0 +1,24 @@ +/** + * @since 2.11.0 + */ +import * as M from './Monoid' +import * as Se from './Semigroup' + +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- + +/** + * @category instances + * @since 2.11.0 + */ +export const Semigroup: Se.Semigroup = Se.constant(undefined) + +/** + * @category instances + * @since 2.11.0 + */ +export const Monoid: M.Monoid = { + concat: Semigroup.concat, + empty: undefined +} diff --git a/test/Semigroup.ts b/test/Semigroup.ts index d37ecc7ad..e1a2abccb 100644 --- a/test/Semigroup.ts +++ b/test/Semigroup.ts @@ -36,6 +36,7 @@ describe('Semigroup', () => { }) it('semigroupVoid', () => { + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.semigroupVoid.concat(undefined, undefined), undefined) }) From 5a56321e6de2678d59b9ba2087e18dd63b8b9ea4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 18:45:11 +0200 Subject: [PATCH 032/162] add `FromReader` module --- CHANGELOG.md | 1 + docs/modules/Array.ts.md | 32 ++-- docs/modules/EitherT.ts.md | 2 +- docs/modules/Endomorphism.ts.md | 83 +++++++++ docs/modules/Eq.ts.md | 2 +- docs/modules/Extend.ts.md | 2 +- docs/modules/Field.ts.md | 2 +- docs/modules/Filterable.ts.md | 2 +- docs/modules/FilterableWithIndex.ts.md | 2 +- docs/modules/Foldable.ts.md | 2 +- docs/modules/FoldableWithIndex.ts.md | 2 +- docs/modules/FromEither.ts.md | 2 +- docs/modules/FromIO.ts.md | 2 +- docs/modules/FromReader.ts.md | 183 +++++++++++++++++++ docs/modules/FromState.ts.md | 213 +++++++++++++++++++++++ docs/modules/FromTask.ts.md | 2 +- docs/modules/FromThese.ts.md | 138 +++++++++++++++ docs/modules/Functor.ts.md | 2 +- docs/modules/FunctorWithIndex.ts.md | 2 +- docs/modules/Group.ts.md | 2 +- docs/modules/HKT.ts.md | 2 +- docs/modules/HeytingAlgebra.ts.md | 2 +- docs/modules/IO.ts.md | 2 +- docs/modules/IOEither.ts.md | 2 +- docs/modules/IORef.ts.md | 2 +- docs/modules/Identity.ts.md | 2 +- docs/modules/Invariant.ts.md | 2 +- docs/modules/JoinSemilattice.ts.md | 2 +- docs/modules/Json.ts.md | 2 +- docs/modules/Lattice.ts.md | 2 +- docs/modules/Magma.ts.md | 2 +- docs/modules/Map.ts.md | 2 +- docs/modules/MeetSemilattice.ts.md | 2 +- docs/modules/Monad.ts.md | 2 +- docs/modules/MonadIO.ts.md | 2 +- docs/modules/MonadTask.ts.md | 2 +- docs/modules/MonadThrow.ts.md | 2 +- docs/modules/Monoid.ts.md | 30 ++-- docs/modules/NonEmptyArray.ts.md | 12 +- docs/modules/Option.ts.md | 2 +- docs/modules/OptionT.ts.md | 2 +- docs/modules/Ord.ts.md | 24 ++- docs/modules/Ordering.ts.md | 2 +- docs/modules/Pointed.ts.md | 2 +- docs/modules/Predicate.ts.md | 159 +++++++++++++++++ docs/modules/Profunctor.ts.md | 2 +- docs/modules/Random.ts.md | 2 +- docs/modules/Reader.ts.md | 2 +- docs/modules/ReaderEither.ts.md | 56 +++++- docs/modules/ReaderT.ts.md | 2 +- docs/modules/ReaderTask.ts.md | 41 ++++- docs/modules/ReaderTaskEither.ts.md | 56 +++++- docs/modules/ReadonlyArray.ts.md | 32 ++-- docs/modules/ReadonlyMap.ts.md | 2 +- docs/modules/ReadonlyNonEmptyArray.ts.md | 14 +- docs/modules/ReadonlyRecord.ts.md | 10 +- docs/modules/ReadonlySet.ts.md | 2 +- docs/modules/ReadonlyTuple.ts.md | 2 +- docs/modules/Record.ts.md | 97 ++++++++++- docs/modules/Refinement.ts.md | 69 ++++++++ docs/modules/Ring.ts.md | 2 +- docs/modules/Semigroup.ts.md | 26 +-- docs/modules/Semigroupoid.ts.md | 2 +- docs/modules/Semiring.ts.md | 2 +- docs/modules/Separated.ts.md | 2 +- docs/modules/Set.ts.md | 2 +- docs/modules/Show.ts.md | 2 +- docs/modules/State.ts.md | 13 +- docs/modules/StateReaderTaskEither.ts.md | 115 +++++++++++- docs/modules/StateT.ts.md | 2 +- docs/modules/Store.ts.md | 2 +- docs/modules/Strong.ts.md | 2 +- docs/modules/Task.ts.md | 2 +- docs/modules/TaskEither.ts.md | 2 +- docs/modules/TaskOption.ts.md | 2 +- docs/modules/TaskThese.ts.md | 35 +++- docs/modules/These.ts.md | 13 +- docs/modules/TheseT.ts.md | 2 +- docs/modules/Traced.ts.md | 2 +- docs/modules/Traversable.ts.md | 2 +- docs/modules/TraversableWithIndex.ts.md | 2 +- docs/modules/Tree.ts.md | 2 +- docs/modules/Tuple.ts.md | 2 +- docs/modules/Unfoldable.ts.md | 2 +- docs/modules/ValidationT.ts.md | 2 +- docs/modules/Witherable.ts.md | 2 +- docs/modules/Writer.ts.md | 2 +- docs/modules/WriterT.ts.md | 2 +- docs/modules/function.ts.md | 132 ++++++++------ docs/modules/index.ts.md | 79 ++++++++- docs/modules/internal.ts.md | 2 +- docs/modules/number.ts.md | 2 +- docs/modules/pipeable.ts.md | 2 +- docs/modules/string.ts.md | 2 +- docs/modules/struct.ts.md | 38 +++- docs/modules/void.ts.md | 41 +++++ src/FromReader.ts | 152 ++++++++++++++++ src/FromState.ts | 6 +- src/FromThese.ts | 4 +- src/ReaderEither.ts | 68 +++++++- src/ReaderTask.ts | 64 +++++-- src/ReaderTaskEither.ts | 67 ++++++- src/StateReaderTaskEither.ts | 82 +++++++-- src/function.ts | 3 +- src/index.ts | 5 + src/internal.ts | 8 +- test/ReaderEither.ts | 11 ++ 107 files changed, 2075 insertions(+), 275 deletions(-) create mode 100644 docs/modules/Endomorphism.ts.md create mode 100644 docs/modules/FromReader.ts.md create mode 100644 docs/modules/FromState.ts.md create mode 100644 docs/modules/FromThese.ts.md create mode 100644 docs/modules/Predicate.ts.md create mode 100644 docs/modules/Refinement.ts.md create mode 100644 docs/modules/void.ts.md create mode 100644 src/FromReader.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 19a15a575..4cd660235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ high state of flux, you're at risk of it changing without notice. - add `FromState` module - add `FromThese` module - add `void` module + - add `FromReader` module - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 8d04e5c8e..ee1734497 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -243,7 +243,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const compact: (fa: O.Option[]) => A[] +export declare const compact: (fa: Option[]) => A[] ``` Added in v2.0.0 @@ -290,7 +290,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const filterMap: (f: (a: A) => O.Option) => (fa: A[]) => B[] +export declare const filterMap: (f: (a: A) => Option) => (fa: A[]) => B[] ``` Added in v2.0.0 @@ -325,7 +325,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const filterMapWithIndex: (f: (i: number, a: A) => O.Option) => (fa: A[]) => B[] +export declare const filterMapWithIndex: (f: (i: number, a: A) => Option) => (fa: A[]) => B[] ``` Added in v2.0.0 @@ -526,7 +526,7 @@ Added in v2.6.3 **Signature** ```ts -export declare const unfold: (b: B, f: (b: B) => O.Option) => A[] +export declare const unfold: (b: B, f: (b: B) => Option) => A[] ``` Added in v2.6.6 @@ -1364,7 +1364,7 @@ Added in v2.11.0 **Signature** ```ts -export declare const fromOption: (ma: O.Option) => A[] +export declare const fromOption: (ma: Option) => A[] ``` Added in v2.11.0 @@ -1544,7 +1544,7 @@ Find the first element returned by an option based selector function **Signature** ```ts -export declare const findFirstMap: (f: (a: A) => O.Option) => (as: A[]) => O.Option +export declare const findFirstMap: (f: (a: A) => Option) => (as: A[]) => Option ``` **Example** @@ -1606,7 +1606,7 @@ Find the last element returned by an option based selector function **Signature** ```ts -export declare const findLastMap: (f: (a: A) => O.Option) => (as: A[]) => O.Option +export declare const findLastMap: (f: (a: A) => Option) => (as: A[]) => Option ``` **Example** @@ -1659,7 +1659,7 @@ Get the first element in an array, or `None` if the array is empty **Signature** ```ts -export declare const head: (as: A[]) => O.Option +export declare const head: (as: A[]) => Option ``` **Example** @@ -1681,7 +1681,7 @@ Get all but the last element of an array, creating a new array, or `None` if the **Signature** ```ts -export declare const init: (as: A[]) => O.Option +export declare const init: (as: A[]) => Option ``` **Example** @@ -1703,7 +1703,7 @@ Get the last element in an array, or `None` if the array is empty **Signature** ```ts -export declare const last: (as: A[]) => O.Option +export declare const last: (as: A[]) => Option ``` **Example** @@ -1785,7 +1785,7 @@ Get all but the first element of an array, creating a new array, or `None` if th **Signature** ```ts -export declare const tail: (as: A[]) => O.Option +export declare const tail: (as: A[]) => Option ``` **Example** @@ -2261,7 +2261,7 @@ Delete the element at the specified index, creating a new array, or returning `N **Signature** ```ts -export declare const deleteAt: (i: number) => (as: A[]) => O.Option +export declare const deleteAt: (i: number) => (as: A[]) => Option ``` **Example** @@ -2382,7 +2382,7 @@ Insert an element at the specified index, creating a new array, or returning `No **Signature** ```ts -export declare const insertAt: (i: number, a: A) => (as: A[]) => O.Option> +export declare const insertAt: (i: number, a: A) => (as: A[]) => Option> ``` **Example** @@ -2435,7 +2435,7 @@ This function provides a safe way to read a value at a particular index from an **Signature** ```ts -export declare const lookup: { (i: number): (as: A[]) => O.Option; (i: number, as: A[]): O.Option } +export declare const lookup: { (i: number): (as: A[]) => Option; (i: number, as: A[]): Option } ``` **Example** @@ -2459,7 +2459,7 @@ of bounds **Signature** ```ts -export declare const modifyAt: (i: number, f: (a: A) => A) => (as: A[]) => O.Option +export declare const modifyAt: (i: number, f: (a: A) => A) => (as: A[]) => Option ``` **Example** @@ -2534,7 +2534,7 @@ Change the element at the specified index, creating a new array, or returning `N **Signature** ```ts -export declare const updateAt: (i: number, a: A) => (as: A[]) => O.Option +export declare const updateAt: (i: number, a: A) => (as: A[]) => Option ``` **Example** diff --git a/docs/modules/EitherT.ts.md b/docs/modules/EitherT.ts.md index a8c785492..f64dd0081 100644 --- a/docs/modules/EitherT.ts.md +++ b/docs/modules/EitherT.ts.md @@ -401,7 +401,7 @@ export declare function match( ): (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: HKT>) => HKT ``` -Added in v3.0.0 +Added in v2.11.0 ## matchE diff --git a/docs/modules/Endomorphism.ts.md b/docs/modules/Endomorphism.ts.md new file mode 100644 index 000000000..9e15125bf --- /dev/null +++ b/docs/modules/Endomorphism.ts.md @@ -0,0 +1,83 @@ +--- +title: Endomorphism.ts +nav_order: 27 +parent: Modules +--- + +## Endomorphism overview + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [instances](#instances) + - [URI](#uri) + - [URI (type alias)](#uri-type-alias) + - [getMonoid](#getmonoid) + - [getSemigroup](#getsemigroup) +- [utils](#utils) + - [Endomorphism (interface)](#endomorphism-interface) + +--- + +# instances + +## URI + +**Signature** + +```ts +export declare const URI: 'Endomorphism' +``` + +Added in v2.11.0 + +## URI (type alias) + +**Signature** + +```ts +export type URI = typeof URI +``` + +Added in v2.11.0 + +## getMonoid + +Endomorphism form a `Monoid` where the `empty` value is the `identity` function. + +**Signature** + +```ts +export declare const getMonoid:
() => Monoid> +``` + +Added in v2.11.0 + +## getSemigroup + +Endomorphism form a `Semigroup` where the `concat` operation is the usual function composition. + +**Signature** + +```ts +export declare const getSemigroup: () => Semigroup> +``` + +Added in v2.11.0 + +# utils + +## Endomorphism (interface) + +**Signature** + +```ts +export interface Endomorphism { + (a: A): A +} +``` + +Added in v2.11.0 diff --git a/docs/modules/Eq.ts.md b/docs/modules/Eq.ts.md index 8270840aa..a21ada138 100644 --- a/docs/modules/Eq.ts.md +++ b/docs/modules/Eq.ts.md @@ -1,6 +1,6 @@ --- title: Eq.ts -nav_order: 27 +nav_order: 28 parent: Modules --- diff --git a/docs/modules/Extend.ts.md b/docs/modules/Extend.ts.md index 6a2e8e342..fd190142d 100644 --- a/docs/modules/Extend.ts.md +++ b/docs/modules/Extend.ts.md @@ -1,6 +1,6 @@ --- title: Extend.ts -nav_order: 28 +nav_order: 29 parent: Modules --- diff --git a/docs/modules/Field.ts.md b/docs/modules/Field.ts.md index 81ed9b166..7e177270d 100644 --- a/docs/modules/Field.ts.md +++ b/docs/modules/Field.ts.md @@ -1,6 +1,6 @@ --- title: Field.ts -nav_order: 29 +nav_order: 30 parent: Modules --- diff --git a/docs/modules/Filterable.ts.md b/docs/modules/Filterable.ts.md index 06c2f9936..b522bf10e 100644 --- a/docs/modules/Filterable.ts.md +++ b/docs/modules/Filterable.ts.md @@ -1,6 +1,6 @@ --- title: Filterable.ts -nav_order: 30 +nav_order: 31 parent: Modules --- diff --git a/docs/modules/FilterableWithIndex.ts.md b/docs/modules/FilterableWithIndex.ts.md index 47600e2a1..d22e3bae6 100644 --- a/docs/modules/FilterableWithIndex.ts.md +++ b/docs/modules/FilterableWithIndex.ts.md @@ -1,6 +1,6 @@ --- title: FilterableWithIndex.ts -nav_order: 31 +nav_order: 32 parent: Modules --- diff --git a/docs/modules/Foldable.ts.md b/docs/modules/Foldable.ts.md index e4c3bda95..ff63bf778 100644 --- a/docs/modules/Foldable.ts.md +++ b/docs/modules/Foldable.ts.md @@ -1,6 +1,6 @@ --- title: Foldable.ts -nav_order: 32 +nav_order: 33 parent: Modules --- diff --git a/docs/modules/FoldableWithIndex.ts.md b/docs/modules/FoldableWithIndex.ts.md index d768f1b23..a918428df 100644 --- a/docs/modules/FoldableWithIndex.ts.md +++ b/docs/modules/FoldableWithIndex.ts.md @@ -1,6 +1,6 @@ --- title: FoldableWithIndex.ts -nav_order: 33 +nav_order: 34 parent: Modules --- diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index 543fbf534..8950a8e7c 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -1,6 +1,6 @@ --- title: FromEither.ts -nav_order: 34 +nav_order: 35 parent: Modules --- diff --git a/docs/modules/FromIO.ts.md b/docs/modules/FromIO.ts.md index 5d20e3ed6..ac383a8bf 100644 --- a/docs/modules/FromIO.ts.md +++ b/docs/modules/FromIO.ts.md @@ -1,6 +1,6 @@ --- title: FromIO.ts -nav_order: 35 +nav_order: 36 parent: Modules --- diff --git a/docs/modules/FromReader.ts.md b/docs/modules/FromReader.ts.md new file mode 100644 index 000000000..a171d30ec --- /dev/null +++ b/docs/modules/FromReader.ts.md @@ -0,0 +1,183 @@ +--- +title: FromReader.ts +nav_order: 37 +parent: Modules +--- + +## FromReader overview + +Lift a computation from the `Reader` monad. + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [combinators](#combinators) + - [chainReaderK](#chainreaderk) + - [fromReaderK](#fromreaderk) +- [constructors](#constructors) + - [ask](#ask) + - [asks](#asks) +- [type classes](#type-classes) + - [FromReader (interface)](#fromreader-interface) + - [FromReader2 (interface)](#fromreader2-interface) + - [FromReader3 (interface)](#fromreader3-interface) + - [FromReader3C (interface)](#fromreader3c-interface) + - [FromReader4 (interface)](#fromreader4-interface) + +--- + +# combinators + +## chainReaderK + +**Signature** + +```ts +export declare function chainReaderK( + F: FromReader4, + M: Chain4 +): (f: (a: A) => Reader) => (ma: Kind4) => Kind4 +export declare function chainReaderK( + F: FromReader3, + M: Chain3 +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export declare function chainReaderK( + F: FromReader3C, + M: Chain3C +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export declare function chainReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 +export declare function chainReaderK( + F: FromReader, + M: Chain +): (f: (a: A) => Reader) => (ma: HKT2) => HKT2 +``` + +Added in v2.11.0 + +## fromReaderK + +**Signature** + +```ts +export declare function fromReaderK( + F: FromReader4 +):
, R, B>(f: (...a: A) => Reader) => (...a: A) => Kind4 +export declare function fromReaderK( + F: FromReader3 +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind3 +export declare function fromReaderK( + F: FromReader3C +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind3 +export declare function fromReaderK( + F: FromReader2 +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind2 +export declare function fromReaderK( + F: FromReader +): , R, B>(f: (...a: A) => Reader) => (...a: A) => HKT2 +``` + +Added in v2.11.0 + +# constructors + +## ask + +**Signature** + +```ts +export declare function ask(F: FromReader4): () => Kind4 +export declare function ask(F: FromReader3): () => Kind3 +export declare function ask(F: FromReader3C): () => Kind3 +export declare function ask(F: FromReader2): () => Kind2 +export declare function ask(F: FromReader): () => HKT2 +``` + +Added in v2.11.0 + +## asks + +**Signature** + +```ts +export declare function asks(F: FromReader4): (f: (r: R) => A) => Kind4 +export declare function asks(F: FromReader3): (f: (r: R) => A) => Kind3 +export declare function asks(F: FromReader3C): (f: (r: R) => A) => Kind3 +export declare function asks(F: FromReader2): (f: (r: R) => A) => Kind2 +export declare function asks(F: FromReader): (f: (r: R) => A) => HKT2 +``` + +Added in v2.11.0 + +# type classes + +## FromReader (interface) + +**Signature** + +```ts +export interface FromReader { + readonly URI: F + readonly fromReader: (fa: Reader) => HKT2 +} +``` + +Added in v2.11.0 + +## FromReader2 (interface) + +**Signature** + +```ts +export interface FromReader2 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind2 +} +``` + +Added in v2.11.0 + +## FromReader3 (interface) + +**Signature** + +```ts +export interface FromReader3 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind3 +} +``` + +Added in v2.11.0 + +## FromReader3C (interface) + +**Signature** + +```ts +export interface FromReader3C { + readonly URI: F + readonly _E: E + readonly fromReader: (fa: Reader) => Kind3 +} +``` + +Added in v2.11.0 + +## FromReader4 (interface) + +**Signature** + +```ts +export interface FromReader4 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind4 +} +``` + +Added in v2.11.0 diff --git a/docs/modules/FromState.ts.md b/docs/modules/FromState.ts.md new file mode 100644 index 000000000..fbe27e6e4 --- /dev/null +++ b/docs/modules/FromState.ts.md @@ -0,0 +1,213 @@ +--- +title: FromState.ts +nav_order: 38 +parent: Modules +--- + +## FromState overview + +Lift a computation from the `State` monad. + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [combinators](#combinators) + - [chainStateK](#chainstatek) + - [fromStateK](#fromstatek) +- [constructors](#constructors) + - [get](#get) + - [gets](#gets) + - [modify](#modify) + - [put](#put) +- [type classes](#type-classes) + - [FromState (interface)](#fromstate-interface) + - [FromState2 (interface)](#fromstate2-interface) + - [FromState3 (interface)](#fromstate3-interface) + - [FromState3C (interface)](#fromstate3c-interface) + - [FromState4 (interface)](#fromstate4-interface) + +--- + +# combinators + +## chainStateK + +**Signature** + +```ts +export declare function chainStateK( + F: FromState4, + M: Chain4 +): (f: (a: A) => State) => (ma: Kind4) => Kind4 +export declare function chainStateK( + F: FromState3, + M: Chain3 +): (f: (a: A) => State) => (ma: Kind3) => Kind3 +export declare function chainStateK( + F: FromState2, + M: Chain2 +): (f: (a: A) => State) => (ma: Kind2) => Kind2 +export declare function chainStateK( + F: FromState, + M: Chain +): (f: (a: A) => State) => (ma: HKT2) => HKT2 +``` + +Added in v2.11.0 + +## fromStateK + +**Signature** + +```ts +export declare function fromStateK( + F: FromState4 +):
, S, B>(f: (...a: A) => State) => (...a: A) => Kind4 +export declare function fromStateK( + F: FromState3 +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind3 +export declare function fromStateK( + F: FromState3C +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind3 +export declare function fromStateK( + F: FromState2 +): , S, B>(f: (...a: A) => State) => (...a: A) => Kind2 +export declare function fromStateK( + F: FromState +): , S, B>(f: (...a: A) => State) => (...a: A) => HKT2 +``` + +Added in v2.11.0 + +# constructors + +## get + +**Signature** + +```ts +export declare function get(F: FromState4): () => Kind4 +export declare function get(F: FromState3): () => Kind3 +export declare function get(F: FromState3C): () => Kind3 +export declare function get(F: FromState2): () => Kind2 +export declare function get(F: FromState): () => HKT2 +``` + +Added in v2.11.0 + +## gets + +**Signature** + +```ts +export declare function gets(F: FromState4): (f: (s: S) => A) => Kind4 +export declare function gets(F: FromState3): (f: (s: S) => A) => Kind3 +export declare function gets(F: FromState3C): (f: (s: S) => A) => Kind3 +export declare function gets(F: FromState2): (f: (s: S) => A) => Kind2 +export declare function gets(F: FromState): (f: (s: S) => A) => HKT2 +``` + +Added in v2.11.0 + +## modify + +**Signature** + +```ts +export declare function modify( + F: FromState4 +): (f: Endomorphism) => Kind4 +export declare function modify(F: FromState3): (f: Endomorphism) => Kind3 +export declare function modify( + F: FromState3C +): (f: Endomorphism) => Kind3 +export declare function modify(F: FromState2): (f: Endomorphism) => Kind2 +export declare function modify(F: FromState): (f: Endomorphism) => HKT2 +``` + +Added in v2.11.0 + +## put + +**Signature** + +```ts +export declare function put(F: FromState4): (s: S) => Kind4 +export declare function put(F: FromState3): (s: S) => Kind3 +export declare function put(F: FromState3C): (s: S) => Kind3 +export declare function put(F: FromState2): (s: S) => Kind2 +export declare function put(F: FromState): (s: S) => HKT2 +``` + +Added in v2.11.0 + +# type classes + +## FromState (interface) + +**Signature** + +```ts +export interface FromState { + readonly URI: F + readonly fromState: (fa: State) => HKT2 +} +``` + +Added in v2.11.0 + +## FromState2 (interface) + +**Signature** + +```ts +export interface FromState2 { + readonly URI: F + readonly fromState: (fa: State) => Kind2 +} +``` + +Added in v2.11.0 + +## FromState3 (interface) + +**Signature** + +```ts +export interface FromState3 { + readonly URI: F + readonly fromState: (fa: State) => Kind3 +} +``` + +Added in v2.11.0 + +## FromState3C (interface) + +**Signature** + +```ts +export interface FromState3C { + readonly URI: F + readonly _E: E + readonly fromState: (fa: State) => Kind3 +} +``` + +Added in v2.11.0 + +## FromState4 (interface) + +**Signature** + +```ts +export interface FromState4 { + readonly URI: F + readonly fromState: (fa: State) => Kind4 +} +``` + +Added in v2.11.0 diff --git a/docs/modules/FromTask.ts.md b/docs/modules/FromTask.ts.md index 8703453f1..c1f68f198 100644 --- a/docs/modules/FromTask.ts.md +++ b/docs/modules/FromTask.ts.md @@ -1,6 +1,6 @@ --- title: FromTask.ts -nav_order: 36 +nav_order: 39 parent: Modules --- diff --git a/docs/modules/FromThese.ts.md b/docs/modules/FromThese.ts.md new file mode 100644 index 000000000..69d434c0b --- /dev/null +++ b/docs/modules/FromThese.ts.md @@ -0,0 +1,138 @@ +--- +title: FromThese.ts +nav_order: 40 +parent: Modules +--- + +## FromThese overview + +The `FromThese` type class represents those data types which support errors and warnings. + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [combinators](#combinators) + - [fromTheseK](#fromthesek) +- [type classes](#type-classes) + - [FromThese (interface)](#fromthese-interface) + - [FromThese2 (interface)](#fromthese2-interface) + - [FromThese2C (interface)](#fromthese2c-interface) + - [FromThese3 (interface)](#fromthese3-interface) + - [FromThese3C (interface)](#fromthese3c-interface) + - [FromThese4 (interface)](#fromthese4-interface) + +--- + +# combinators + +## fromTheseK + +**Signature** + +```ts +export declare function fromTheseK( + F: FromThese4 +):
, E, B>(f: (...a: A) => These) => (...a: A) => Kind4 +export declare function fromTheseK( + F: FromThese3 +): , E, B>(f: (...a: A) => These) => (...a: A) => Kind3 +export declare function fromTheseK( + F: FromThese3C +): , B>(f: (...a: A) => These) => (...a: A) => Kind3 +export declare function fromTheseK( + F: FromThese2 +): , E, B>(f: (...a: A) => These) => (...a: A) => Kind2 +export declare function fromTheseK( + F: FromThese2C +): , B>(f: (...a: A) => These) => (...a: A) => Kind2 +export declare function fromTheseK( + F: FromThese +): , E, B>(f: (...a: A) => These) => (...a: A) => HKT2 +``` + +Added in v2.11.0 + +# type classes + +## FromThese (interface) + +**Signature** + +```ts +export interface FromThese { + readonly URI: F + readonly fromThese: (e: These) => HKT2 +} +``` + +Added in v2.11.0 + +## FromThese2 (interface) + +**Signature** + +```ts +export interface FromThese2 { + readonly URI: F + readonly fromThese: (e: These) => Kind2 +} +``` + +Added in v2.11.0 + +## FromThese2C (interface) + +**Signature** + +```ts +export interface FromThese2C { + readonly URI: F + readonly _E: E + readonly fromThese: (e: These) => Kind2 +} +``` + +Added in v2.11.0 + +## FromThese3 (interface) + +**Signature** + +```ts +export interface FromThese3 { + readonly URI: F + readonly fromThese: (e: These) => Kind3 +} +``` + +Added in v2.11.0 + +## FromThese3C (interface) + +**Signature** + +```ts +export interface FromThese3C { + readonly URI: F + readonly _E: E + readonly fromThese: (e: These) => Kind3 +} +``` + +Added in v2.11.0 + +## FromThese4 (interface) + +**Signature** + +```ts +export interface FromThese4 { + readonly URI: F + readonly fromThese: (e: These) => Kind4 +} +``` + +Added in v2.11.0 diff --git a/docs/modules/Functor.ts.md b/docs/modules/Functor.ts.md index daa86412e..d18877a71 100644 --- a/docs/modules/Functor.ts.md +++ b/docs/modules/Functor.ts.md @@ -1,6 +1,6 @@ --- title: Functor.ts -nav_order: 38 +nav_order: 42 parent: Modules --- diff --git a/docs/modules/FunctorWithIndex.ts.md b/docs/modules/FunctorWithIndex.ts.md index d554de303..d3c84004d 100644 --- a/docs/modules/FunctorWithIndex.ts.md +++ b/docs/modules/FunctorWithIndex.ts.md @@ -1,6 +1,6 @@ --- title: FunctorWithIndex.ts -nav_order: 39 +nav_order: 43 parent: Modules --- diff --git a/docs/modules/Group.ts.md b/docs/modules/Group.ts.md index fff59eb6d..8cf13c3a8 100644 --- a/docs/modules/Group.ts.md +++ b/docs/modules/Group.ts.md @@ -1,6 +1,6 @@ --- title: Group.ts -nav_order: 40 +nav_order: 44 parent: Modules --- diff --git a/docs/modules/HKT.ts.md b/docs/modules/HKT.ts.md index 8c53f62f5..ac7b0173b 100644 --- a/docs/modules/HKT.ts.md +++ b/docs/modules/HKT.ts.md @@ -1,6 +1,6 @@ --- title: HKT.ts -nav_order: 42 +nav_order: 46 parent: Modules --- diff --git a/docs/modules/HeytingAlgebra.ts.md b/docs/modules/HeytingAlgebra.ts.md index 8e972f030..43d64d111 100644 --- a/docs/modules/HeytingAlgebra.ts.md +++ b/docs/modules/HeytingAlgebra.ts.md @@ -1,6 +1,6 @@ --- title: HeytingAlgebra.ts -nav_order: 41 +nav_order: 45 parent: Modules --- diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index 55f338a2d..04f430830 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -1,6 +1,6 @@ --- title: IO.ts -nav_order: 47 +nav_order: 51 parent: Modules --- diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index df6a7566c..792fcfe95 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -1,6 +1,6 @@ --- title: IOEither.ts -nav_order: 48 +nav_order: 52 parent: Modules --- diff --git a/docs/modules/IORef.ts.md b/docs/modules/IORef.ts.md index 41971d360..91ce43431 100644 --- a/docs/modules/IORef.ts.md +++ b/docs/modules/IORef.ts.md @@ -1,6 +1,6 @@ --- title: IORef.ts -nav_order: 49 +nav_order: 53 parent: Modules --- diff --git a/docs/modules/Identity.ts.md b/docs/modules/Identity.ts.md index db85ff707..d8a84ad9b 100644 --- a/docs/modules/Identity.ts.md +++ b/docs/modules/Identity.ts.md @@ -1,6 +1,6 @@ --- title: Identity.ts -nav_order: 43 +nav_order: 47 parent: Modules --- diff --git a/docs/modules/Invariant.ts.md b/docs/modules/Invariant.ts.md index afe392670..343d99274 100644 --- a/docs/modules/Invariant.ts.md +++ b/docs/modules/Invariant.ts.md @@ -1,6 +1,6 @@ --- title: Invariant.ts -nav_order: 46 +nav_order: 50 parent: Modules --- diff --git a/docs/modules/JoinSemilattice.ts.md b/docs/modules/JoinSemilattice.ts.md index 5c277b169..9c808f292 100644 --- a/docs/modules/JoinSemilattice.ts.md +++ b/docs/modules/JoinSemilattice.ts.md @@ -1,6 +1,6 @@ --- title: JoinSemilattice.ts -nav_order: 50 +nav_order: 54 parent: Modules --- diff --git a/docs/modules/Json.ts.md b/docs/modules/Json.ts.md index 49f44417b..2ecbffff8 100644 --- a/docs/modules/Json.ts.md +++ b/docs/modules/Json.ts.md @@ -1,6 +1,6 @@ --- title: Json.ts -nav_order: 51 +nav_order: 55 parent: Modules --- diff --git a/docs/modules/Lattice.ts.md b/docs/modules/Lattice.ts.md index 41278efcd..0bd0479d3 100644 --- a/docs/modules/Lattice.ts.md +++ b/docs/modules/Lattice.ts.md @@ -1,6 +1,6 @@ --- title: Lattice.ts -nav_order: 52 +nav_order: 56 parent: Modules --- diff --git a/docs/modules/Magma.ts.md b/docs/modules/Magma.ts.md index 183e41898..a6118b15f 100644 --- a/docs/modules/Magma.ts.md +++ b/docs/modules/Magma.ts.md @@ -1,6 +1,6 @@ --- title: Magma.ts -nav_order: 53 +nav_order: 57 parent: Modules --- diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index 3601b0907..300303cc4 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -1,6 +1,6 @@ --- title: Map.ts -nav_order: 54 +nav_order: 58 parent: Modules --- diff --git a/docs/modules/MeetSemilattice.ts.md b/docs/modules/MeetSemilattice.ts.md index 2468e2070..e100614df 100644 --- a/docs/modules/MeetSemilattice.ts.md +++ b/docs/modules/MeetSemilattice.ts.md @@ -1,6 +1,6 @@ --- title: MeetSemilattice.ts -nav_order: 55 +nav_order: 59 parent: Modules --- diff --git a/docs/modules/Monad.ts.md b/docs/modules/Monad.ts.md index a3ad48c0b..8981f3fd8 100644 --- a/docs/modules/Monad.ts.md +++ b/docs/modules/Monad.ts.md @@ -1,6 +1,6 @@ --- title: Monad.ts -nav_order: 56 +nav_order: 60 parent: Modules --- diff --git a/docs/modules/MonadIO.ts.md b/docs/modules/MonadIO.ts.md index 5e82607d5..111cb30cc 100644 --- a/docs/modules/MonadIO.ts.md +++ b/docs/modules/MonadIO.ts.md @@ -1,6 +1,6 @@ --- title: MonadIO.ts -nav_order: 57 +nav_order: 61 parent: Modules --- diff --git a/docs/modules/MonadTask.ts.md b/docs/modules/MonadTask.ts.md index d23b022a0..14cb07bfc 100644 --- a/docs/modules/MonadTask.ts.md +++ b/docs/modules/MonadTask.ts.md @@ -1,6 +1,6 @@ --- title: MonadTask.ts -nav_order: 58 +nav_order: 62 parent: Modules --- diff --git a/docs/modules/MonadThrow.ts.md b/docs/modules/MonadThrow.ts.md index 8b6afa0ae..f0cd5aee4 100644 --- a/docs/modules/MonadThrow.ts.md +++ b/docs/modules/MonadThrow.ts.md @@ -1,6 +1,6 @@ --- title: MonadThrow.ts -nav_order: 59 +nav_order: 63 parent: Modules --- diff --git a/docs/modules/Monoid.ts.md b/docs/modules/Monoid.ts.md index 08d682feb..71abcd61e 100644 --- a/docs/modules/Monoid.ts.md +++ b/docs/modules/Monoid.ts.md @@ -1,6 +1,6 @@ --- title: Monoid.ts -nav_order: 60 +nav_order: 64 parent: Modules --- @@ -56,7 +56,6 @@ Added in v2.0.0 - [~~getJoinMonoid~~](#getjoinmonoid) - [~~getMeetMonoid~~](#getmeetmonoid) - [instances](#instances) - - [monoidVoid](#monoidvoid) - [~~getEndomorphismMonoid~~](#getendomorphismmonoid) - [~~getFunctionMonoid~~](#getfunctionmonoid) - [~~monoidAll~~](#monoidall) @@ -64,6 +63,7 @@ Added in v2.0.0 - [~~monoidProduct~~](#monoidproduct) - [~~monoidString~~](#monoidstring) - [~~monoidSum~~](#monoidsum) + - [~~monoidVoid~~](#monoidvoid) - [type classes](#type-classes) - [Monoid (interface)](#monoid-interface) - [utils](#utils) @@ -273,21 +273,9 @@ Added in v2.0.0 # instances -## monoidVoid - -**Signature** - -```ts -export declare const monoidVoid: Monoid -``` - -Added in v2.0.0 - ## ~~getEndomorphismMonoid~~ -Use `function.getEndomorphismMonoid` instead. - -**Note**. The execution order in `function.getEndomorphismMonoid` is reversed. +Use `Endomorphism` module instead. **Note**. The execution order in the `Endomorphism` module is reversed. **Signature** @@ -367,6 +355,18 @@ export declare const monoidSum: Monoid Added in v2.0.0 +## ~~monoidVoid~~ + +Use `void` module instead. + +**Signature** + +```ts +export declare const monoidVoid: Monoid +``` + +Added in v2.0.0 + # type classes ## Monoid (interface) diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index b7f6819fd..5a7b3598e 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -1,6 +1,6 @@ --- title: NonEmptyArray.ts -nav_order: 61 +nav_order: 65 parent: Modules --- @@ -518,7 +518,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const insertAt: (i: number, a: A) => (as: A[]) => O.Option> +export declare const insertAt: (i: number, a: A) => (as: A[]) => Option> ``` Added in v2.0.0 @@ -548,7 +548,7 @@ Added in v2.9.0 **Signature** ```ts -export declare const modifyAt: (i: number, f: (a: A) => A) => (as: NonEmptyArray) => O.Option> +export declare const modifyAt: (i: number, f: (a: A) => A) => (as: NonEmptyArray) => Option> ``` Added in v2.0.0 @@ -620,7 +620,7 @@ Added in v2.5.1 **Signature** ```ts -export declare const updateAt: (i: number, a: A) => (as: NonEmptyArray) => O.Option> +export declare const updateAt: (i: number, a: A) => (as: NonEmptyArray) => Option> ``` Added in v2.0.0 @@ -659,7 +659,7 @@ Use `Array`'s `filterWithIndex` instead. ```ts export declare const filterWithIndex: ( predicate: (i: number, a: A) => boolean -) => (as: NonEmptyArray) => O.Option> +) => (as: NonEmptyArray) => Option> ``` Added in v2.0.0 @@ -700,7 +700,7 @@ Builds a `NonEmptyArray` from an `Array` returning `none` if `as` is an empty ar **Signature** ```ts -export declare const fromArray: (as: A[]) => O.Option> +export declare const fromArray: (as: A[]) => Option> ``` Added in v2.0.0 diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 7115619c2..51a76870d 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1,6 +1,6 @@ --- title: Option.ts -nav_order: 63 +nav_order: 67 parent: Modules --- diff --git a/docs/modules/OptionT.ts.md b/docs/modules/OptionT.ts.md index ca4f62533..052e50d62 100644 --- a/docs/modules/OptionT.ts.md +++ b/docs/modules/OptionT.ts.md @@ -1,6 +1,6 @@ --- title: OptionT.ts -nav_order: 64 +nav_order: 68 parent: Modules --- diff --git a/docs/modules/Ord.ts.md b/docs/modules/Ord.ts.md index 1dda34782..91e097d02 100644 --- a/docs/modules/Ord.ts.md +++ b/docs/modules/Ord.ts.md @@ -1,6 +1,6 @@ --- title: Ord.ts -nav_order: 65 +nav_order: 69 parent: Modules --- @@ -47,12 +47,14 @@ Added in v2.0.0 - [utils](#utils) - [between](#between) - [clamp](#clamp) + - [equals](#equals) - [geq](#geq) - [gt](#gt) - [leq](#leq) - [lt](#lt) - [max](#max) - [min](#min) + - [trivial](#trivial) --- @@ -374,6 +376,16 @@ export declare const clamp: (O: Ord) => (low: A, hi: A) => (a: A) => A Added in v2.0.0 +## equals + +**Signature** + +```ts +export declare const equals: (O: Ord) => (second: A) => (first: A) => boolean +``` + +Added in v2.11.0 + ## geq Test whether one value is _non-strictly greater than_ another @@ -445,3 +457,13 @@ export declare const min: (O: Ord) => (first: A, second: A) => A ``` Added in v2.0.0 + +## trivial + +**Signature** + +```ts +export declare const trivial: Ord +``` + +Added in v2.11.0 diff --git a/docs/modules/Ordering.ts.md b/docs/modules/Ordering.ts.md index 66472aab8..bd94cd1e7 100644 --- a/docs/modules/Ordering.ts.md +++ b/docs/modules/Ordering.ts.md @@ -1,6 +1,6 @@ --- title: Ordering.ts -nav_order: 66 +nav_order: 70 parent: Modules --- diff --git a/docs/modules/Pointed.ts.md b/docs/modules/Pointed.ts.md index f5697a148..e8b6b0744 100644 --- a/docs/modules/Pointed.ts.md +++ b/docs/modules/Pointed.ts.md @@ -1,6 +1,6 @@ --- title: Pointed.ts -nav_order: 68 +nav_order: 72 parent: Modules --- diff --git a/docs/modules/Predicate.ts.md b/docs/modules/Predicate.ts.md new file mode 100644 index 000000000..8ece26016 --- /dev/null +++ b/docs/modules/Predicate.ts.md @@ -0,0 +1,159 @@ +--- +title: Predicate.ts +nav_order: 73 +parent: Modules +--- + +## Predicate overview + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [Contravariant](#contravariant) + - [contramap](#contramap) +- [instances](#instances) + - [Contravariant](#contravariant-1) + - [URI](#uri) + - [URI (type alias)](#uri-type-alias) + - [getMonoidAll](#getmonoidall) + - [getMonoidAny](#getmonoidany) + - [getSemigroupAll](#getsemigroupall) + - [getSemigroupAny](#getsemigroupany) +- [utils](#utils) + - [Predicate (interface)](#predicate-interface) + - [and](#and) + - [not](#not) + - [or](#or) + +--- + +# Contravariant + +## contramap + +**Signature** + +```ts +export declare const contramap: (f: (b: B) => A) => (predicate: Predicate
) => Predicate +``` + +Added in v2.11.0 + +# instances + +## Contravariant + +**Signature** + +```ts +export declare const Contravariant: Contravariant1<'Predicate'> +``` + +Added in v2.11.0 + +## URI + +**Signature** + +```ts +export declare const URI: 'Predicate' +``` + +Added in v2.11.0 + +## URI (type alias) + +**Signature** + +```ts +export type URI = typeof URI +``` + +Added in v2.11.0 + +## getMonoidAll + +**Signature** + +```ts +export declare const getMonoidAll: () => Monoid> +``` + +Added in v2.11.0 + +## getMonoidAny + +**Signature** + +```ts +export declare const getMonoidAny: () => Monoid> +``` + +Added in v2.11.0 + +## getSemigroupAll + +**Signature** + +```ts +export declare const getSemigroupAll: () => Semigroup> +``` + +Added in v2.11.0 + +## getSemigroupAny + +**Signature** + +```ts +export declare const getSemigroupAny: () => Semigroup> +``` + +Added in v2.11.0 + +# utils + +## Predicate (interface) + +**Signature** + +```ts +export interface Predicate { + (a: A): boolean +} +``` + +Added in v2.11.0 + +## and + +**Signature** + +```ts +export declare const and: (second: Predicate) => (first: Predicate) => Predicate +``` + +Added in v2.11.0 + +## not + +**Signature** + +```ts +export declare const not: (predicate: Predicate) => Predicate +``` + +Added in v2.11.0 + +## or + +**Signature** + +```ts +export declare const or: (second: Predicate) => (first: Predicate) => Predicate +``` + +Added in v2.11.0 diff --git a/docs/modules/Profunctor.ts.md b/docs/modules/Profunctor.ts.md index 65e5bbb89..2a854bbdc 100644 --- a/docs/modules/Profunctor.ts.md +++ b/docs/modules/Profunctor.ts.md @@ -1,6 +1,6 @@ --- title: Profunctor.ts -nav_order: 69 +nav_order: 74 parent: Modules --- diff --git a/docs/modules/Random.ts.md b/docs/modules/Random.ts.md index 6cf875b75..7d7ff3534 100644 --- a/docs/modules/Random.ts.md +++ b/docs/modules/Random.ts.md @@ -1,6 +1,6 @@ --- title: Random.ts -nav_order: 70 +nav_order: 75 parent: Modules --- diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 2647ca91c..b36c8edc4 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -1,6 +1,6 @@ --- title: Reader.ts -nav_order: 71 +nav_order: 76 parent: Modules --- diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 4b3695901..4c52e8c8d 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderEither.ts -nav_order: 72 +nav_order: 77 parent: Modules --- @@ -38,12 +38,14 @@ Added in v2.0.0 - [chainFirst](#chainfirst) - [chainFirstW](#chainfirstw) - [chainOptionK](#chainoptionk) + - [chainReaderK](#chainreaderk) - [filterOrElse](#filterorelse) - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) - [fromEitherK](#fromeitherk) - [fromOptionK](#fromoptionk) + - [fromReaderK](#fromreaderk) - [orElse](#orelse) - [orElseW](#orelsew) - [swap](#swap) @@ -54,6 +56,7 @@ Added in v2.0.0 - [fromEither](#fromeither) - [fromOption](#fromoption) - [fromPredicate](#frompredicate) + - [fromReader](#fromreader) - [left](#left) - [leftReader](#leftreader) - [right](#right) @@ -74,6 +77,7 @@ Added in v2.0.0 - [Bifunctor](#bifunctor-1) - [Chain](#chain) - [FromEither](#fromeither) + - [FromReader](#fromreader) - [Functor](#functor-1) - [Monad](#monad-1) - [MonadThrow](#monadthrow-1) @@ -370,6 +374,18 @@ export declare const chainOptionK: ( Added in v2.10.0 +## chainReaderK + +**Signature** + +```ts +export declare const chainReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + ## filterOrElse **Signature** @@ -448,6 +464,18 @@ export declare const fromOptionK: ( Added in v2.10.0 +## fromReaderK + +**Signature** + +```ts +export declare const fromReaderK: ( + f: (...a: A) => R.Reader +) => (...a: A) => ReaderEither +``` + +Added in v2.11.0 + ## orElse **Signature** @@ -500,6 +528,8 @@ Added in v2.0.0 ## ask +Reads the current context. + **Signature** ```ts @@ -510,10 +540,12 @@ Added in v2.0.0 ## asks +Projects a value from the global context in a `ReaderEither`. + **Signature** ```ts -export declare const asks: (f: (r: R) => A) => ReaderEither +export declare const asks: (f: (r: R) => A) => ReaderEither ``` Added in v2.0.0 @@ -551,6 +583,16 @@ export declare const fromPredicate: { Added in v2.0.0 +## fromReader + +**Signature** + +```ts +export declare const fromReader: (ma: R.Reader) => ReaderEither +``` + +Added in v2.11.0 + ## left **Signature** @@ -767,6 +809,16 @@ export declare const FromEither: FromEither3<'ReaderEither'> Added in v2.10.0 +## FromReader + +**Signature** + +```ts +export declare const FromReader: FromReader3<'ReaderEither'> +``` + +Added in v2.11.0 + ## Functor **Signature** diff --git a/docs/modules/ReaderT.ts.md b/docs/modules/ReaderT.ts.md index c387f5ea8..cbd4df628 100644 --- a/docs/modules/ReaderT.ts.md +++ b/docs/modules/ReaderT.ts.md @@ -1,6 +1,6 @@ --- title: ReaderT.ts -nav_order: 73 +nav_order: 78 parent: Modules --- diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 2ac8d5fb2..42d01c4b5 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTask.ts -nav_order: 74 +nav_order: 79 parent: Modules --- @@ -29,10 +29,12 @@ Added in v2.3.0 - [chainFirstIOK](#chainfirstiok) - [chainFirstTaskK](#chainfirsttaskk) - [chainIOK](#chainiok) + - [chainReaderK](#chainreaderk) - [chainTaskK](#chaintaskk) - [flap](#flap) - [flatten](#flatten) - [fromIOK](#fromiok) + - [fromReaderK](#fromreaderk) - [fromTaskK](#fromtaskk) - [~~local~~](#local) - [constructors](#constructors) @@ -48,6 +50,7 @@ Added in v2.3.0 - [ApplySeq](#applyseq) - [Chain](#chain) - [FromIO](#fromio) + - [FromReader](#fromreader) - [FromTask](#fromtask) - [Functor](#functor-1) - [Monad](#monad-1) @@ -239,6 +242,16 @@ export declare const chainIOK: (f: (a: A) => IO) => (first: ReaderTa Added in v2.4.0 +## chainReaderK + +**Signature** + +```ts +export declare const chainReaderK: (f: (a: A) => R.Reader) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainTaskK **Signature** @@ -283,6 +296,16 @@ export declare const fromIOK: (f: (...a: A) => IO) => (...a: A) => R Added in v2.4.0 +## fromReaderK + +**Signature** + +```ts +export declare const fromReaderK: (f: (...a: A) => R.Reader) => (...a: A) => ReaderTask +``` + +Added in v2.11.0 + ## fromTaskK **Signature** @@ -309,6 +332,8 @@ Added in v2.3.0 ## ask +Reads the current context. + **Signature** ```ts @@ -319,10 +344,12 @@ Added in v2.3.0 ## asks +Projects a value from the global context in a `ReaderTask`. + **Signature** ```ts -export declare const asks: (f: (r: R) => A) => ReaderTask +export declare const asks: (f: (r: R) => A) => ReaderTask ``` Added in v2.3.0 @@ -419,6 +446,16 @@ export declare const FromIO: FromIO2<'ReaderTask'> Added in v2.10.0 +## FromReader + +**Signature** + +```ts +export declare const FromReader: FromReader2<'ReaderTask'> +``` + +Added in v2.11.0 + ## FromTask **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 4b0605dab..9e22c2ebd 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTaskEither.ts -nav_order: 75 +nav_order: 80 parent: Modules --- @@ -43,6 +43,7 @@ Added in v2.0.0 - [chainIOEitherKW](#chainioeitherkw) - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) + - [chainReaderK](#chainreaderk) - [chainTaskEitherK](#chaintaskeitherk) - [chainTaskEitherKW](#chaintaskeitherkw) - [chainTaskK](#chaintaskk) @@ -54,6 +55,7 @@ Added in v2.0.0 - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) + - [fromReaderK](#fromreaderk) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) - [orElse](#orelse) @@ -68,6 +70,7 @@ Added in v2.0.0 - [fromIOEither](#fromioeither) - [fromOption](#fromoption) - [fromPredicate](#frompredicate) + - [fromReader](#fromreader) - [fromReaderEither](#fromreadereither) - [fromTask](#fromtask) - [fromTaskEither](#fromtaskeither) @@ -100,6 +103,7 @@ Added in v2.0.0 - [Chain](#chain) - [FromEither](#fromeither) - [FromIO](#fromio) + - [FromReader](#fromreader) - [FromTask](#fromtask) - [Functor](#functor-1) - [Monad](#monad-1) @@ -469,6 +473,18 @@ export declare const chainOptionK: ( Added in v2.10.0 +## chainReaderK + +**Signature** + +```ts +export declare const chainReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainTaskEitherK **Signature** @@ -611,6 +627,18 @@ export declare const fromOptionK: ( Added in v2.10.0 +## fromReaderK + +**Signature** + +```ts +export declare const fromReaderK: ( + f: (...a: A) => R.Reader +) => (...a: A) => ReaderTaskEither +``` + +Added in v2.11.0 + ## fromTaskEitherK **Signature** @@ -687,6 +715,8 @@ Added in v2.0.0 ## ask +Reads the current context. + **Signature** ```ts @@ -697,10 +727,12 @@ Added in v2.0.0 ## asks +Projects a value from the global context in a `ReaderEither`. + **Signature** ```ts -export declare const asks: (f: (r: R) => A) => ReaderTaskEither +export declare const asks: (f: (r: R) => A) => ReaderTaskEither ``` Added in v2.0.0 @@ -758,6 +790,16 @@ export declare const fromPredicate: { Added in v2.0.0 +## fromReader + +**Signature** + +```ts +export declare const fromReader: (fa: R.Reader) => ReaderTaskEither +``` + +Added in v2.11.0 + ## fromReaderEither **Signature** @@ -1094,6 +1136,16 @@ export declare const FromIO: FromIO3<'ReaderTaskEither'> Added in v2.10.0 +## FromReader + +**Signature** + +```ts +export declare const FromReader: FromReader3<'ReaderTaskEither'> +``` + +Added in v2.11.0 + ## FromTask **Signature** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index e9f7d47a8..980bc9723 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyArray.ts -nav_order: 76 +nav_order: 81 parent: Modules --- @@ -243,7 +243,7 @@ Added in v2.5.0 **Signature** ```ts -export declare const compact: (fa: readonly O.Option[]) => readonly A[] +export declare const compact: (fa: readonly Option[]) => readonly A[] ``` Added in v2.5.0 @@ -290,7 +290,7 @@ Added in v2.5.0 **Signature** ```ts -export declare const filterMap: (f: (a: A) => O.Option) => (fa: readonly A[]) => readonly B[] +export declare const filterMap: (f: (a: A) => Option) => (fa: readonly A[]) => readonly B[] ``` Added in v2.5.0 @@ -327,9 +327,7 @@ Added in v2.5.0 **Signature** ```ts -export declare const filterMapWithIndex: ( - f: (i: number, a: A) => O.Option -) => (fa: readonly A[]) => readonly B[] +export declare const filterMapWithIndex: (f: (i: number, a: A) => Option) => (fa: readonly A[]) => readonly B[] ``` Added in v2.5.0 @@ -532,7 +530,7 @@ Added in v2.6.3 **Signature** ```ts -export declare const unfold: (b: B, f: (b: B) => O.Option) => readonly A[] +export declare const unfold: (b: B, f: (b: B) => Option) => readonly A[] ``` Added in v2.6.6 @@ -2044,7 +2042,7 @@ Delete the element at the specified index, creating a new array, or returning `N **Signature** ```ts -export declare const deleteAt: (i: number) => (as: readonly A[]) => O.Option +export declare const deleteAt: (i: number) => (as: readonly A[]) => Option ``` **Example** @@ -2197,7 +2195,7 @@ Find the first element returned by an option based selector function **Signature** ```ts -export declare const findFirstMap: (f: (a: A) => O.Option) => (as: readonly A[]) => O.Option +export declare const findFirstMap: (f: (a: A) => Option) => (as: readonly A[]) => Option ``` **Example** @@ -2311,7 +2309,7 @@ Find the last element returned by an option based selector function **Signature** ```ts -export declare const findLastMap: (f: (a: A) => O.Option) => (as: readonly A[]) => O.Option +export declare const findLastMap: (f: (a: A) => Option) => (as: readonly A[]) => Option ``` **Example** @@ -2340,7 +2338,7 @@ Get the first element in an array, or `None` if the array is empty **Signature** ```ts -export declare const head: (as: readonly A[]) => O.Option +export declare const head: (as: readonly A[]) => Option ``` **Example** @@ -2362,7 +2360,7 @@ Get all but the last element of an array, creating a new array, or `None` if the **Signature** ```ts -export declare const init: (as: readonly A[]) => O.Option +export declare const init: (as: readonly A[]) => Option ``` **Example** @@ -2384,7 +2382,7 @@ Insert an element at the specified index, creating a new array, or returning `No **Signature** ```ts -export declare const insertAt: (i: number, a: A) => (as: readonly A[]) => O.Option> +export declare const insertAt: (i: number, a: A) => (as: readonly A[]) => Option> ``` **Example** @@ -2437,7 +2435,7 @@ Get the last element in an array, or `None` if the array is empty **Signature** ```ts -export declare const last: (as: readonly A[]) => O.Option +export declare const last: (as: readonly A[]) => Option ``` **Example** @@ -2505,7 +2503,7 @@ of bounds **Signature** ```ts -export declare const modifyAt: (i: number, f: (a: A) => A) => (as: readonly A[]) => O.Option +export declare const modifyAt: (i: number, f: (a: A) => A) => (as: readonly A[]) => Option ``` **Example** @@ -2588,7 +2586,7 @@ Get all but the first element of an array, creating a new array, or `None` if th **Signature** ```ts -export declare const tail: (as: readonly A[]) => O.Option +export declare const tail: (as: readonly A[]) => Option ``` **Example** @@ -2638,7 +2636,7 @@ Change the element at the specified index, creating a new array, or returning `N **Signature** ```ts -export declare const updateAt: (i: number, a: A) => (as: readonly A[]) => O.Option +export declare const updateAt: (i: number, a: A) => (as: readonly A[]) => Option ``` **Example** diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index 0fd6a94c6..ae3c53313 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyMap.ts -nav_order: 77 +nav_order: 82 parent: Modules --- diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index adf9a3aed..2529eae69 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyNonEmptyArray.ts -nav_order: 78 +nav_order: 83 parent: Modules --- @@ -619,7 +619,7 @@ Added in v2.9.0 export declare const modifyAt: ( i: number, f: (a: A) => A -) => (as: ReadonlyNonEmptyArray) => O.Option> +) => (as: ReadonlyNonEmptyArray) => Option> ``` Added in v2.5.0 @@ -698,7 +698,7 @@ Added in v2.5.1 export declare const updateAt: ( i: number, a: A -) => (as: ReadonlyNonEmptyArray) => O.Option> +) => (as: ReadonlyNonEmptyArray) => Option> ``` Added in v2.5.0 @@ -742,7 +742,7 @@ Use `ReadonlyArray`'s `filterWithIndex` instead. ```ts export declare const filterWithIndex: ( predicate: (i: number, a: A) => boolean -) => (as: ReadonlyNonEmptyArray) => O.Option> +) => (as: ReadonlyNonEmptyArray) => Option> ``` Added in v2.5.0 @@ -771,7 +771,7 @@ Use `ReadonlyArray`'s `insertAt` instead. **Signature** ```ts -export declare const insertAt: (i: number, a: A) => (as: readonly A[]) => O.Option> +export declare const insertAt: (i: number, a: A) => (as: readonly A[]) => Option> ``` Added in v2.5.0 @@ -797,7 +797,7 @@ Return a `ReadonlyNonEmptyArray` from a `ReadonlyArray` returning `none` if the **Signature** ```ts -export declare const fromReadonlyArray: (as: readonly A[]) => O.Option> +export declare const fromReadonlyArray: (as: readonly A[]) => Option> ``` Added in v2.5.0 @@ -1136,7 +1136,7 @@ Added in v2.5.0 **Signature** ```ts -export declare const fromArray: (as: A[]) => O.Option> +export declare const fromArray: (as: A[]) => Option> ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 837022278..83e421bbe 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyRecord.ts -nav_order: 79 +nav_order: 84 parent: Modules --- @@ -111,7 +111,7 @@ Added in v2.5.0 **Signature** ```ts -export declare const compact: (r: Readonly>>) => Readonly> +export declare const compact: (r: Readonly>>) => Readonly> ``` Added in v2.5.0 @@ -149,7 +149,7 @@ Added in v2.5.0 ```ts export declare const filterMap: ( - f: (a: A) => O.Option + f: (a: A) => Option ) => (fa: Readonly>) => Readonly> ``` @@ -973,7 +973,7 @@ Added in v2.5.0 export declare const modifyAt: ( k: string, f: (a: A) => A -) => (r: Readonly>) => O.Option>> +) => (r: Readonly>) => Option>> ``` Added in v2.5.0 @@ -1206,7 +1206,7 @@ Added in v2.11.0 export declare const updateAt: ( k: string, a: A -) => (r: Readonly>) => O.Option>> +) => (r: Readonly>) => Option>> ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 0878364e7..15dc0bad0 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlySet.ts -nav_order: 80 +nav_order: 85 parent: Modules --- diff --git a/docs/modules/ReadonlyTuple.ts.md b/docs/modules/ReadonlyTuple.ts.md index 30ae39420..9aced14ee 100644 --- a/docs/modules/ReadonlyTuple.ts.md +++ b/docs/modules/ReadonlyTuple.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyTuple.ts -nav_order: 81 +nav_order: 86 parent: Modules --- diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 0a46ed255..5080d44bb 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -1,6 +1,6 @@ --- title: Record.ts -nav_order: 82 +nav_order: 87 parent: Modules --- @@ -38,13 +38,17 @@ Added in v2.0.0 - [FunctorWithIndex](#functorwithindex) - [URI](#uri) - [URI (type alias)](#uri-type-alias) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getFoldable](#getfoldable) - [getFoldableWithIndex](#getfoldablewithindex) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getShow](#getshow) - [getTraversable](#gettraversable) - [getTraversableWithIndex](#gettraversablewithindex) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [getWitherable](#getwitherable) - [~~FoldableWithIndex~~](#foldablewithindex) - [~~Foldable~~](#foldable) @@ -55,6 +59,7 @@ Added in v2.0.0 - [utils](#utils) - [collect](#collect) - [deleteAt](#deleteat) + - [difference](#difference) - [elem](#elem) - [every](#every) - [filterMapWithIndex](#filtermapwithindex) @@ -63,6 +68,7 @@ Added in v2.0.0 - [fromFoldable](#fromfoldable) - [fromFoldableMap](#fromfoldablemap) - [has](#has) + - [intersection](#intersection) - [isEmpty](#isempty) - [isSubrecord](#issubrecord) - [keys](#keys) @@ -83,6 +89,7 @@ Added in v2.0.0 - [toUnfoldable](#tounfoldable) - [traverse](#traverse) - [traverseWithIndex](#traversewithindex) + - [union](#union) - [updateAt](#updateat) - [~~empty~~](#empty) - [~~hasOwnProperty (function)~~](#hasownproperty-function) @@ -97,7 +104,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const compact: (fa: Record>) => Record +export declare const compact: (fa: Record>) => Record ``` Added in v2.0.0 @@ -134,7 +141,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const filterMap: (f: (a: A) => O.Option) => (fa: Record) => Record +export declare const filterMap: (f: (a: A) => Option) => (fa: Record) => Record ``` Added in v2.0.0 @@ -323,6 +330,16 @@ export type URI = typeof URI Added in v2.0.0 +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: () => Magma> +``` + +Added in v2.11.0 + ## getEq **Signature** @@ -353,6 +370,16 @@ export declare const getFoldableWithIndex: (O: Ord) => FoldableWithIndex Added in v2.11.0 +## getIntersectionSemigroup + +**Signature** + +```ts +export declare const getIntersectionSemigroup: (S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getMonoid Returns a `Monoid` instance for `Record`s given a `Semigroup` instance for their values. @@ -406,6 +433,26 @@ export declare const getTraversableWithIndex: (O: Ord) => TraversableWit Added in v2.11.0 +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (S: Semigroup) => Monoid> +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getWitherable **Signature** @@ -536,6 +583,16 @@ export declare function deleteAt( Added in v2.0.0 +## difference + +**Signature** + +```ts +export declare const difference: (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + ## elem **Signature** @@ -564,7 +621,7 @@ Added in v2.0.0 ```ts export declare const filterMapWithIndex: ( - f: (key: K, a: A) => O.Option + f: (key: K, a: A) => Option ) => (fa: Record) => Record ``` @@ -705,6 +762,18 @@ export declare const has: (k: string, r: Record) = Added in v2.10.0 +## intersection + +**Signature** + +```ts +export declare const intersection: ( + M: Magma +) => (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + ## isEmpty Test whether a `Record` is empty. @@ -752,8 +821,8 @@ Lookup the value for a key in a `Record`. ```ts export declare const lookup: { - (k: string): (r: Record) => O.Option - (k: string, r: Record): O.Option + (k: string): (r: Record) => Option + (k: string, r: Record): Option } ``` @@ -791,7 +860,7 @@ Added in v2.0.0 export declare const modifyAt: ( k: string, f: (a: A) => A -) => (r: Record) => O.Option> +) => (r: Record) => Option> ``` Added in v2.0.0 @@ -1015,12 +1084,24 @@ export declare function traverseWithIndex( Added in v2.0.0 +## union + +**Signature** + +```ts +export declare const union: ( + M: Magma +) => (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + ## updateAt **Signature** ```ts -export declare const updateAt: (k: string, a: A) => (r: Record) => O.Option> +export declare const updateAt: (k: string, a: A) => (r: Record) => Option> ``` Added in v2.0.0 diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md new file mode 100644 index 000000000..e41544adb --- /dev/null +++ b/docs/modules/Refinement.ts.md @@ -0,0 +1,69 @@ +--- +title: Refinement.ts +nav_order: 88 +parent: Modules +--- + +## Refinement overview + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [utils](#utils) + - [Refinement (interface)](#refinement-interface) + - [and](#and) + - [not](#not) + - [or](#or) + +--- + +# utils + +## Refinement (interface) + +**Signature** + +```ts +export interface Refinement { + (a: A): a is B +} +``` + +Added in v2.11.0 + +## and + +**Signature** + +```ts +export declare const and: ( + second: Refinement +) => (first: Refinement) => Refinement +``` + +Added in v2.11.0 + +## not + +**Signature** + +```ts +export declare const not: (refinement: Refinement) => Refinement> +``` + +Added in v2.11.0 + +## or + +**Signature** + +```ts +export declare const or: ( + second: Refinement +) => (first: Refinement) => Refinement +``` + +Added in v2.11.0 diff --git a/docs/modules/Ring.ts.md b/docs/modules/Ring.ts.md index 5d44a01c9..b86be39ee 100644 --- a/docs/modules/Ring.ts.md +++ b/docs/modules/Ring.ts.md @@ -1,6 +1,6 @@ --- title: Ring.ts -nav_order: 83 +nav_order: 89 parent: Modules --- diff --git a/docs/modules/Semigroup.ts.md b/docs/modules/Semigroup.ts.md index 80c1d755d..ffc945d03 100644 --- a/docs/modules/Semigroup.ts.md +++ b/docs/modules/Semigroup.ts.md @@ -1,6 +1,6 @@ --- title: Semigroup.ts -nav_order: 84 +nav_order: 90 parent: Modules --- @@ -66,7 +66,6 @@ Added in v2.0.0 - [instances](#instances) - [first](#first) - [last](#last) - - [semigroupVoid](#semigroupvoid) - [~~getFirstSemigroup~~](#getfirstsemigroup) - [~~getFunctionSemigroup~~](#getfunctionsemigroup) - [~~getLastSemigroup~~](#getlastsemigroup) @@ -76,6 +75,7 @@ Added in v2.0.0 - [~~semigroupProduct~~](#semigroupproduct) - [~~semigroupString~~](#semigroupstring) - [~~semigroupSum~~](#semigroupsum) + - [~~semigroupVoid~~](#semigroupvoid) - [type classes](#type-classes) - [Semigroup (interface)](#semigroup-interface) - [utils](#utils) @@ -369,16 +369,6 @@ assert.deepStrictEqual(S.last().concat(1, 2), 2) Added in v2.10.0 -## semigroupVoid - -**Signature** - -```ts -export declare const semigroupVoid: Semigroup -``` - -Added in v2.0.0 - ## ~~getFirstSemigroup~~ Use `first` instead. @@ -487,6 +477,18 @@ export declare const semigroupSum: Semigroup Added in v2.0.0 +## ~~semigroupVoid~~ + +Use `void` module instead. + +**Signature** + +```ts +export declare const semigroupVoid: Semigroup +``` + +Added in v2.0.0 + # type classes ## Semigroup (interface) diff --git a/docs/modules/Semigroupoid.ts.md b/docs/modules/Semigroupoid.ts.md index f5da9f31e..6737fb0c8 100644 --- a/docs/modules/Semigroupoid.ts.md +++ b/docs/modules/Semigroupoid.ts.md @@ -1,6 +1,6 @@ --- title: Semigroupoid.ts -nav_order: 85 +nav_order: 91 parent: Modules --- diff --git a/docs/modules/Semiring.ts.md b/docs/modules/Semiring.ts.md index 2dc848d74..a825d1c84 100644 --- a/docs/modules/Semiring.ts.md +++ b/docs/modules/Semiring.ts.md @@ -1,6 +1,6 @@ --- title: Semiring.ts -nav_order: 86 +nav_order: 92 parent: Modules --- diff --git a/docs/modules/Separated.ts.md b/docs/modules/Separated.ts.md index a327b4a61..40697b649 100644 --- a/docs/modules/Separated.ts.md +++ b/docs/modules/Separated.ts.md @@ -1,6 +1,6 @@ --- title: Separated.ts -nav_order: 87 +nav_order: 93 parent: Modules --- diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 2861fd36d..81308fe7c 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -1,6 +1,6 @@ --- title: Set.ts -nav_order: 88 +nav_order: 94 parent: Modules --- diff --git a/docs/modules/Show.ts.md b/docs/modules/Show.ts.md index a21ec9435..db26860e2 100644 --- a/docs/modules/Show.ts.md +++ b/docs/modules/Show.ts.md @@ -1,6 +1,6 @@ --- title: Show.ts -nav_order: 89 +nav_order: 95 parent: Modules --- diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index a44624301..cdd2f4c2a 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -1,6 +1,6 @@ --- title: State.ts -nav_order: 90 +nav_order: 96 parent: Modules --- @@ -35,6 +35,7 @@ Added in v2.0.0 - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) + - [FromState](#fromstate) - [Functor](#functor-1) - [Monad](#monad-1) - [Pointed](#pointed-1) @@ -263,6 +264,16 @@ export declare const Chain: Chain2<'State'> Added in v2.10.0 +## FromState + +**Signature** + +```ts +export declare const FromState: FromState2<'State'> +``` + +Added in v2.11.0 + ## Functor **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index ea079662c..157bc490d 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: StateReaderTaskEither.ts -nav_order: 91 +nav_order: 97 parent: Modules --- @@ -43,8 +43,10 @@ Added in v2.0.0 - [chainIOEitherKW](#chainioeitherkw) - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) + - [chainReaderK](#chainreaderk) - [chainReaderTaskEitherK](#chainreadertaskeitherk) - [chainReaderTaskEitherKW](#chainreadertaskeitherkw) + - [chainStateK](#chainstatek) - [chainTaskEitherK](#chaintaskeitherk) - [chainTaskEitherKW](#chaintaskeitherkw) - [chainTaskK](#chaintaskk) @@ -56,15 +58,20 @@ Added in v2.0.0 - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) + - [fromReaderK](#fromreaderk) - [fromReaderTaskEitherK](#fromreadertaskeitherk) + - [fromStateK](#fromstatek) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) - [constructors](#constructors) + - [ask](#ask) + - [asks](#asks) - [fromEither](#fromeither) - [fromIO](#fromio) - [fromIOEither](#fromioeither) - [fromOption](#fromoption) - [fromPredicate](#frompredicate) + - [fromReader](#fromreader) - [fromReaderEither](#fromreadereither) - [fromReaderTaskEither](#fromreadertaskeither) - [fromState](#fromstate) @@ -92,6 +99,8 @@ Added in v2.0.0 - [Chain](#chain) - [FromEither](#fromeither) - [FromIO](#fromio) + - [FromReader](#fromreader) + - [FromState](#fromstate) - [FromTask](#fromtask) - [Functor](#functor-1) - [Monad](#monad-1) @@ -454,6 +463,18 @@ export declare const chainOptionK: ( Added in v2.10.0 +## chainReaderK + +**Signature** + +```ts +export declare const chainReaderK: ( + f: (a: A) => Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainReaderTaskEitherK **Signature** @@ -480,6 +501,18 @@ export declare const chainReaderTaskEitherKW: ( Added in v2.6.1 +## chainStateK + +**Signature** + +```ts +export declare const chainStateK: ( + f: (a: A) => State +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainTaskEitherK **Signature** @@ -628,6 +661,18 @@ export declare const fromOptionK: ( Added in v2.10.0 +## fromReaderK + +**Signature** + +```ts +export declare const fromReaderK:
( + f: (...a: A) => Reader +) => (...a: A) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## fromReaderTaskEitherK **Signature** @@ -640,6 +685,18 @@ export declare function fromReaderTaskEitherK( + f: (...a: A) => State +) => (...a: A) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## fromTaskEitherK **Signature** @@ -666,6 +723,30 @@ Added in v2.10.0 # constructors +## ask + +Reads the current context. + +**Signature** + +```ts +export declare const ask: () => StateReaderTaskEither +``` + +Added in v2.11.0 + +## asks + +Projects a value from the global context in a `ReaderEither`. + +**Signature** + +```ts +export declare const asks: (f: (r: R) => A) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## fromEither **Signature** @@ -719,6 +800,16 @@ export declare const fromPredicate: { Added in v2.4.4 +## fromReader + +**Signature** + +```ts +export declare const fromReader: (ma: Reader) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## fromReaderEither **Signature** @@ -852,7 +943,7 @@ Modify the state by applying a function to the current state **Signature** ```ts -export declare const modify: (f: (s: S) => S) => StateReaderTaskEither +export declare const modify: (f: Endomorphism) => StateReaderTaskEither ``` Added in v2.0.0 @@ -991,6 +1082,26 @@ export declare const FromIO: FromIO4<'StateReaderTaskEither'> Added in v2.10.0 +## FromReader + +**Signature** + +```ts +export declare const FromReader: FromReader4<'StateReaderTaskEither'> +``` + +Added in v2.11.0 + +## FromState + +**Signature** + +```ts +export declare const FromState: FromState4<'StateReaderTaskEither'> +``` + +Added in v2.11.0 + ## FromTask **Signature** diff --git a/docs/modules/StateT.ts.md b/docs/modules/StateT.ts.md index 83182aad9..20c3ae813 100644 --- a/docs/modules/StateT.ts.md +++ b/docs/modules/StateT.ts.md @@ -1,6 +1,6 @@ --- title: StateT.ts -nav_order: 92 +nav_order: 98 parent: Modules --- diff --git a/docs/modules/Store.ts.md b/docs/modules/Store.ts.md index 90df33d43..af00114f4 100644 --- a/docs/modules/Store.ts.md +++ b/docs/modules/Store.ts.md @@ -1,6 +1,6 @@ --- title: Store.ts -nav_order: 93 +nav_order: 99 parent: Modules --- diff --git a/docs/modules/Strong.ts.md b/docs/modules/Strong.ts.md index b4ea62010..0a2996b6c 100644 --- a/docs/modules/Strong.ts.md +++ b/docs/modules/Strong.ts.md @@ -1,6 +1,6 @@ --- title: Strong.ts -nav_order: 95 +nav_order: 101 parent: Modules --- diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index 317656294..d4b778bd5 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -1,6 +1,6 @@ --- title: Task.ts -nav_order: 97 +nav_order: 103 parent: Modules --- diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 729a70b5e..c91b3f840 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -1,6 +1,6 @@ --- title: TaskEither.ts -nav_order: 98 +nav_order: 104 parent: Modules --- diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 21d9641db..063c2c2e3 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -1,6 +1,6 @@ --- title: TaskOption.ts -nav_order: 99 +nav_order: 105 parent: Modules --- diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index bdcb0a4e1..767946907 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -1,6 +1,6 @@ --- title: TaskThese.ts -nav_order: 100 +nav_order: 106 parent: Modules --- @@ -24,6 +24,7 @@ Added in v2.4.0 - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) - [fromTaskK](#fromtaskk) + - [fromTheseK](#fromthesek) - [swap](#swap) - [constructors](#constructors) - [both](#both) @@ -33,6 +34,7 @@ Added in v2.4.0 - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [fromTask](#fromtask) + - [fromThese](#fromthese) - [left](#left) - [leftIO](#leftio) - [leftTask](#lefttask) @@ -51,6 +53,7 @@ Added in v2.4.0 - [FromEither](#fromeither) - [FromIO](#fromio) - [FromTask](#fromtask) + - [FromThese](#fromthese) - [Functor](#functor-1) - [Pointed](#pointed-1) - [URI](#uri) @@ -170,6 +173,16 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a: A Added in v2.10.0 +## fromTheseK + +**Signature** + +```ts +export declare const fromTheseK: (f: (...a: A) => TH.These) => (...a: A) => TaskThese +``` + +Added in v2.11.0 + ## swap **Signature** @@ -255,6 +268,16 @@ export declare const fromTask: (fa: T.Task) => TaskThese Added in v2.7.0 +## fromThese + +**Signature** + +```ts +export declare const fromThese: (e: TH.These) => TaskThese +``` + +Added in v2.11.0 + ## left **Signature** @@ -451,6 +474,16 @@ export declare const FromTask: FromTask2<'TaskThese'> Added in v2.10.0 +## FromThese + +**Signature** + +```ts +export declare const FromThese: FromThese2<'TaskThese'> +``` + +Added in v2.11.0 + ## Functor **Signature** diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index e10978f00..59b0b010b 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -1,6 +1,6 @@ --- title: These.ts -nav_order: 101 +nav_order: 107 parent: Modules --- @@ -70,6 +70,7 @@ Added in v2.0.0 - [Bifunctor](#bifunctor-1) - [Foldable](#foldable-1) - [FromEither](#fromeither) + - [FromThese](#fromthese) - [Functor](#functor-1) - [Pointed](#pointed-1) - [Traversable](#traversable) @@ -545,6 +546,16 @@ export declare const FromEither: FromEither2<'These'> Added in v2.10.0 +## FromThese + +**Signature** + +```ts +export declare const FromThese: FromThese2<'These'> +``` + +Added in v2.11.0 + ## Functor **Signature** diff --git a/docs/modules/TheseT.ts.md b/docs/modules/TheseT.ts.md index dbeeacdda..bd2699be6 100644 --- a/docs/modules/TheseT.ts.md +++ b/docs/modules/TheseT.ts.md @@ -1,6 +1,6 @@ --- title: TheseT.ts -nav_order: 102 +nav_order: 108 parent: Modules --- diff --git a/docs/modules/Traced.ts.md b/docs/modules/Traced.ts.md index 16ce9fc4e..47961c83e 100644 --- a/docs/modules/Traced.ts.md +++ b/docs/modules/Traced.ts.md @@ -1,6 +1,6 @@ --- title: Traced.ts -nav_order: 103 +nav_order: 109 parent: Modules --- diff --git a/docs/modules/Traversable.ts.md b/docs/modules/Traversable.ts.md index 5ff28ba51..d93c531bb 100644 --- a/docs/modules/Traversable.ts.md +++ b/docs/modules/Traversable.ts.md @@ -1,6 +1,6 @@ --- title: Traversable.ts -nav_order: 104 +nav_order: 110 parent: Modules --- diff --git a/docs/modules/TraversableWithIndex.ts.md b/docs/modules/TraversableWithIndex.ts.md index 53011ff6a..4929c6bca 100644 --- a/docs/modules/TraversableWithIndex.ts.md +++ b/docs/modules/TraversableWithIndex.ts.md @@ -1,6 +1,6 @@ --- title: TraversableWithIndex.ts -nav_order: 105 +nav_order: 111 parent: Modules --- diff --git a/docs/modules/Tree.ts.md b/docs/modules/Tree.ts.md index 927beda0e..3c7d620cf 100644 --- a/docs/modules/Tree.ts.md +++ b/docs/modules/Tree.ts.md @@ -1,6 +1,6 @@ --- title: Tree.ts -nav_order: 106 +nav_order: 112 parent: Modules --- diff --git a/docs/modules/Tuple.ts.md b/docs/modules/Tuple.ts.md index dafc724c0..e504f519b 100644 --- a/docs/modules/Tuple.ts.md +++ b/docs/modules/Tuple.ts.md @@ -1,6 +1,6 @@ --- title: Tuple.ts -nav_order: 107 +nav_order: 113 parent: Modules --- diff --git a/docs/modules/Unfoldable.ts.md b/docs/modules/Unfoldable.ts.md index 0673a6ad5..235667351 100644 --- a/docs/modules/Unfoldable.ts.md +++ b/docs/modules/Unfoldable.ts.md @@ -1,6 +1,6 @@ --- title: Unfoldable.ts -nav_order: 108 +nav_order: 114 parent: Modules --- diff --git a/docs/modules/ValidationT.ts.md b/docs/modules/ValidationT.ts.md index 370f3f5d1..561871a5f 100644 --- a/docs/modules/ValidationT.ts.md +++ b/docs/modules/ValidationT.ts.md @@ -1,6 +1,6 @@ --- title: ValidationT.ts -nav_order: 109 +nav_order: 115 parent: Modules --- diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index da5642473..b77e34196 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -1,6 +1,6 @@ --- title: Witherable.ts -nav_order: 110 +nav_order: 117 parent: Modules --- diff --git a/docs/modules/Writer.ts.md b/docs/modules/Writer.ts.md index d15d11bbf..6b670a9a6 100644 --- a/docs/modules/Writer.ts.md +++ b/docs/modules/Writer.ts.md @@ -1,6 +1,6 @@ --- title: Writer.ts -nav_order: 111 +nav_order: 118 parent: Modules --- diff --git a/docs/modules/WriterT.ts.md b/docs/modules/WriterT.ts.md index e9ff3cf29..f0d9981bb 100644 --- a/docs/modules/WriterT.ts.md +++ b/docs/modules/WriterT.ts.md @@ -1,6 +1,6 @@ --- title: WriterT.ts -nav_order: 112 +nav_order: 119 parent: Modules --- diff --git a/docs/modules/function.ts.md b/docs/modules/function.ts.md index 914ca72f6..6a72a3f40 100644 --- a/docs/modules/function.ts.md +++ b/docs/modules/function.ts.md @@ -1,6 +1,6 @@ --- title: function.ts -nav_order: 37 +nav_order: 41 parent: Modules --- @@ -14,19 +14,17 @@ Added in v2.0.0 - [instances](#instances) - [getBooleanAlgebra](#getbooleanalgebra) - - [getEndomorphismMonoid](#getendomorphismmonoid) - [getMonoid](#getmonoid) - [getRing](#getring) - [getSemigroup](#getsemigroup) - [getSemiring](#getsemiring) + - [~~getEndomorphismMonoid~~](#getendomorphismmonoid) - [utils](#utils) - - [Endomorphism (interface)](#endomorphism-interface) - [FunctionN (interface)](#functionn-interface) - [Lazy (interface)](#lazy-interface) - - [Predicate (interface)](#predicate-interface) - - [Refinement (interface)](#refinement-interface) - [SK](#sk) - [absurd](#absurd) + - [apply](#apply) - [constFalse](#constfalse) - [constNull](#constnull) - [constTrue](#consttrue) @@ -39,12 +37,15 @@ Added in v2.0.0 - [hole](#hole) - [identity](#identity) - [increment](#increment) - - [not](#not) - [pipe](#pipe) - [tuple](#tuple) - [tupled](#tupled) - [unsafeCoerce](#unsafecoerce) - [untupled](#untupled) + - [~~Endomorphism~~ (interface)](#endomorphism-interface) + - [~~Predicate~~ (interface)](#predicate-interface) + - [~~Refinement~~ (interface)](#refinement-interface) + - [~~not~~](#not) --- @@ -60,18 +61,6 @@ export declare const getBooleanAlgebra: (B: BooleanAlgebra) => Added in v2.10.0 -## getEndomorphismMonoid - -Endomorphism form a monoid where the `empty` value is the identity function. - -**Signature** - -```ts -export declare const getEndomorphismMonoid: () => Monoid> -``` - -Added in v2.10.0 - ## getMonoid Unary functions form a monoid as long as you can provide a monoid for the codomain. @@ -85,7 +74,8 @@ export declare const getMonoid: (M: Monoid) => () => Monoid<(a: **Example** ```ts -import { Predicate, getMonoid } from 'fp-ts/function' +import { Predicate } from 'fp-ts/Predicate' +import { getMonoid } from 'fp-ts/function' import * as B from 'fp-ts/boolean' const f: Predicate = (n) => n <= 2 @@ -156,19 +146,19 @@ export declare const getSemiring: (S: Semiring) => Semiring<(a: A) => B Added in v2.10.0 -# utils +## ~~getEndomorphismMonoid~~ -## Endomorphism (interface) +Use `Endomorphism` module instead. **Signature** ```ts -export interface Endomorphism { - (a: A): A -} +export declare const getEndomorphismMonoid: () => Monoid> ``` -Added in v2.0.0 +Added in v2.10.0 + +# utils ## FunctionN (interface) @@ -204,50 +194,36 @@ export interface Lazy { Added in v2.0.0 -## Predicate (interface) +## SK **Signature** ```ts -export interface Predicate { - (a: A): boolean -} +export declare const SK: (_: A, b: B) => B ``` -Added in v2.0.0 +Added in v2.11.0 -## Refinement (interface) +## absurd **Signature** ```ts -export interface Refinement { - (a: A): a is B -} +export declare function absurd(_: never): A ``` Added in v2.0.0 -## SK +## apply **Signature** ```ts -export declare const SK: (_: A, b: B) => B +export declare const apply: (a: A) => (f: (a: A) => B) => B ``` Added in v2.11.0 -## absurd - -**Signature** - -```ts -export declare function absurd(_: never): A -``` - -Added in v2.0.0 - ## constFalse A thunk that returns always `false`. @@ -459,16 +435,6 @@ export declare function increment(n: number): number Added in v2.0.0 -## not - -**Signature** - -```ts -export declare function not(predicate: Predicate): Predicate -``` - -Added in v2.0.0 - ## pipe Pipes the value of an expression into a pipeline of functions. @@ -780,3 +746,57 @@ export declare function untupled, B>(f: (a: A) ``` Added in v2.4.0 + +## ~~Endomorphism~~ (interface) + +Use `Endomorphism` module instead. + +**Signature** + +```ts +export interface Endomorphism { + (a: A): A +} +``` + +Added in v2.0.0 + +## ~~Predicate~~ (interface) + +Use `Predicate` module instead. + +**Signature** + +```ts +export interface Predicate { + (a: A): boolean +} +``` + +Added in v2.0.0 + +## ~~Refinement~~ (interface) + +Use `Refinement` module instead. + +**Signature** + +```ts +export interface Refinement { + (a: A): a is B +} +``` + +Added in v2.0.0 + +## ~~not~~ + +Use `Predicate` module instead. + +**Signature** + +```ts +export declare function not(predicate: Predicate): Predicate +``` + +Added in v2.0.0 diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index ac9dc8a58..26dcc748a 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -1,6 +1,6 @@ --- title: index.ts -nav_order: 44 +nav_order: 48 parent: Modules --- @@ -39,6 +39,7 @@ Added in v2.0.0 - [distributiveLattice](#distributivelattice) - [either](#either) - [eitherT](#eithert) + - [endomorphism](#endomorphism) - [eq](#eq) - [extend](#extend) - [field](#field) @@ -48,7 +49,10 @@ Added in v2.0.0 - [foldableWithIndex](#foldablewithindex) - [fromEither](#fromeither) - [fromIO](#fromio) + - [fromReader](#fromreader) + - [fromState](#fromstate) - [fromTask](#fromtask) + - [fromThese](#fromthese) - [function](#function) - [functor](#functor) - [functorWithIndex](#functorwithindex) @@ -79,6 +83,7 @@ Added in v2.0.0 - [ordering](#ordering) - [pipeable](#pipeable) - [pointed](#pointed) + - [predicate](#predicate) - [profunctor](#profunctor) - [random](#random) - [reader](#reader) @@ -93,6 +98,7 @@ Added in v2.0.0 - [readonlySet](#readonlyset) - [readonlyTuple](#readonlytuple) - [record](#record) + - [refinement](#refinement) - [ring](#ring) - [semigroup](#semigroup) - [semigroupoid](#semigroupoid) @@ -120,6 +126,7 @@ Added in v2.0.0 - [tuple](#tuple) - [unfoldable](#unfoldable) - [validationT](#validationt) + - [void](#void) - [witherable](#witherable) - [writer](#writer) - [writerT](#writert) @@ -388,6 +395,16 @@ export declare const eitherT: typeof eitherT Added in v2.0.0 +## endomorphism + +**Signature** + +```ts +export declare const endomorphism: typeof endomorphism +``` + +Added in v2.11.0 + ## eq **Signature** @@ -478,6 +495,26 @@ export declare const fromIO: typeof fromIO Added in v2.10.0 +## fromReader + +**Signature** + +```ts +export declare const fromReader: typeof fromReader +``` + +Added in v2.11.0 + +## fromState + +**Signature** + +```ts +export declare const fromState: typeof fromState +``` + +Added in v2.11.0 + ## fromTask **Signature** @@ -488,6 +525,16 @@ export declare const fromTask: typeof fromTask Added in v2.10.0 +## fromThese + +**Signature** + +```ts +export declare const fromThese: typeof fromThese +``` + +Added in v2.11.0 + ## function **Signature** @@ -788,6 +835,16 @@ export declare const pointed: typeof pointed Added in v2.10.0 +## predicate + +**Signature** + +```ts +export declare const predicate: typeof predicate +``` + +Added in v2.11.0 + ## profunctor **Signature** @@ -928,6 +985,16 @@ export declare const record: typeof record Added in v2.0.0 +## refinement + +**Signature** + +```ts +export declare const refinement: typeof refinement +``` + +Added in v2.11.0 + ## ring **Signature** @@ -1198,6 +1265,16 @@ export declare const validationT: typeof validationT Added in v2.0.0 +## void + +**Signature** + +```ts +export declare const void: typeof void_ +``` + +Added in v2.11.0 + ## witherable **Signature** diff --git a/docs/modules/internal.ts.md b/docs/modules/internal.ts.md index ebc7bbb05..ffa8a1ade 100644 --- a/docs/modules/internal.ts.md +++ b/docs/modules/internal.ts.md @@ -1,6 +1,6 @@ --- title: internal.ts -nav_order: 45 +nav_order: 49 parent: Modules --- diff --git a/docs/modules/number.ts.md b/docs/modules/number.ts.md index 28902f1e8..e982b83a4 100644 --- a/docs/modules/number.ts.md +++ b/docs/modules/number.ts.md @@ -1,6 +1,6 @@ --- title: number.ts -nav_order: 62 +nav_order: 66 parent: Modules --- diff --git a/docs/modules/pipeable.ts.md b/docs/modules/pipeable.ts.md index 4d3646173..94dd2bb61 100644 --- a/docs/modules/pipeable.ts.md +++ b/docs/modules/pipeable.ts.md @@ -1,6 +1,6 @@ --- title: pipeable.ts -nav_order: 67 +nav_order: 71 parent: Modules --- diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index 0447861e1..71604c698 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -1,6 +1,6 @@ --- title: string.ts -nav_order: 94 +nav_order: 100 parent: Modules --- diff --git a/docs/modules/struct.ts.md b/docs/modules/struct.ts.md index 87660a046..171a55cd6 100644 --- a/docs/modules/struct.ts.md +++ b/docs/modules/struct.ts.md @@ -1,6 +1,6 @@ --- title: struct.ts -nav_order: 96 +nav_order: 102 parent: Modules --- @@ -14,6 +14,8 @@ Added in v2.10.0 - [instances](#instances) - [getAssignSemigroup](#getassignsemigroup) +- [utils](#utils) + - [evolve](#evolve) --- @@ -44,3 +46,37 @@ assert.deepStrictEqual(S.concat({ name: 'name', age: 23 }, { name: 'name', age: ``` Added in v2.10.0 + +# utils + +## evolve + +Creates a new object by recursively evolving a shallow copy of `a`, according to the `transformation` functions. + +**Signature** + +```ts +export declare const evolve: unknown }>( + transformations: F +) => (a: A) => { [K in keyof F]: ReturnType } +``` + +**Example** + +```ts +import { pipe } from 'fp-ts/function' +import { evolve } from 'fp-ts/struct' + +assert.deepStrictEqual( + pipe( + { a: 'a', b: 1 }, + evolve({ + a: (a) => a.length, + b: (b) => b * 2, + }) + ), + { a: 1, b: 2 } +) +``` + +Added in v2.11.0 diff --git a/docs/modules/void.ts.md b/docs/modules/void.ts.md new file mode 100644 index 000000000..c9a58f4ab --- /dev/null +++ b/docs/modules/void.ts.md @@ -0,0 +1,41 @@ +--- +title: void.ts +nav_order: 116 +parent: Modules +--- + +## void overview + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [instances](#instances) + - [Monoid](#monoid) + - [Semigroup](#semigroup) + +--- + +# instances + +## Monoid + +**Signature** + +```ts +export declare const Monoid: M.Monoid +``` + +Added in v2.11.0 + +## Semigroup + +**Signature** + +```ts +export declare const Semigroup: Se.Semigroup +``` + +Added in v2.11.0 diff --git a/src/FromReader.ts b/src/FromReader.ts new file mode 100644 index 000000000..cb4d14349 --- /dev/null +++ b/src/FromReader.ts @@ -0,0 +1,152 @@ +/** + * Lift a computation from the `Reader` monad. + * + * @since 2.11.0 + */ +import { Chain, Chain2, Chain3, Chain3C, Chain4 } from './Chain' +import { flow } from './function' +import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import * as R from './Reader' + +import Reader = R.Reader + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromReader { + readonly URI: F + readonly fromReader: (fa: Reader) => HKT2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromReader2 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromReader3 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromReader3C { + readonly URI: F + readonly _E: E + readonly fromReader: (fa: Reader) => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromReader4 { + readonly URI: F + readonly fromReader: (fa: Reader) => Kind4 +} + +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + +/** + * @category constructors + * @since 2.11.0 + */ +export function ask(F: FromReader4): () => Kind4 +export function ask(F: FromReader3): () => Kind3 +export function ask(F: FromReader3C): () => Kind3 +export function ask(F: FromReader2): () => Kind2 +export function ask(F: FromReader): () => HKT2 +export function ask(F: FromReader): () => HKT2 { + return () => F.fromReader(R.ask()) +} + +/** + * @category constructors + * @since 2.11.0 + */ +export function asks(F: FromReader4): (f: (r: R) => A) => Kind4 +export function asks(F: FromReader3): (f: (r: R) => A) => Kind3 +export function asks(F: FromReader3C): (f: (r: R) => A) => Kind3 +export function asks(F: FromReader2): (f: (r: R) => A) => Kind2 +export function asks(F: FromReader): (f: (r: R) => A) => HKT2 +export function asks(F: FromReader): (f: (r: R) => A) => HKT2 { + return F.fromReader +} + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * @category combinators + * @since 2.11.0 + */ +export function fromReaderK( + F: FromReader4 +):
, R, B>(f: (...a: A) => Reader) => (...a: A) => Kind4 +export function fromReaderK( + F: FromReader3 +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind3 +export function fromReaderK( + F: FromReader3C +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind3 +export function fromReaderK( + F: FromReader2 +): , R, B>(f: (...a: A) => Reader) => (...a: A) => Kind2 +export function fromReaderK( + F: FromReader +): , R, B>(f: (...a: A) => Reader) => (...a: A) => HKT2 +export function fromReaderK( + F: FromReader +): , R, B>(f: (...a: A) => Reader) => (...a: A) => HKT2 { + return (f) => flow(f, F.fromReader) +} + +/** + * @category combinators + * @since 2.11.0 + */ +export function chainReaderK( + F: FromReader4, + M: Chain4 +): (f: (a: A) => Reader) => (ma: Kind4) => Kind4 +export function chainReaderK( + F: FromReader3, + M: Chain3 +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export function chainReaderK( + F: FromReader3C, + M: Chain3C +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export function chainReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 +export function chainReaderK( + F: FromReader, + M: Chain +): (f: (a: A) => Reader) => (ma: HKT2) => HKT2 +export function chainReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 { + const fromReaderKF = fromReaderK(F) + return (f) => (ma) => M.chain(ma, fromReaderKF(f)) +} diff --git a/src/FromState.ts b/src/FromState.ts index 25eaf3e7b..5fa12b924 100644 --- a/src/FromState.ts +++ b/src/FromState.ts @@ -3,10 +3,10 @@ * * @since 2.11.0 */ -import type { Chain, Chain2, Chain3, Chain4 } from './Chain' -import type { Endomorphism } from './Endomorphism' +import { Chain, Chain2, Chain3, Chain4 } from './Chain' +import { Endomorphism } from './Endomorphism' import { flow } from './function' -import type { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' import * as S from './State' import State = S.State diff --git a/src/FromThese.ts b/src/FromThese.ts index f104455f8..80245bc06 100644 --- a/src/FromThese.ts +++ b/src/FromThese.ts @@ -4,8 +4,8 @@ * @since 2.11.0 */ import { flow } from './function' -import type { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' -import type { These } from './These' +import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import { These } from './These' // ------------------------------------------------------------------------------------- // model diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index b20afd79e..6d88cd235 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -33,6 +33,13 @@ import { fromOptionK as fromOptionK_, fromPredicate as fromPredicate_ } from './FromEither' +import { + ask as ask_, + asks as asks_, + chainReaderK as chainReaderK_, + FromReader3, + fromReaderK as fromReaderK_ +} from './FromReader' import { flow, identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' @@ -98,19 +105,13 @@ export const leftReader: (me: Reader) => ReaderEi * @category constructors * @since 2.0.0 */ -export const ask: () => ReaderEither = () => _.right - -/** - * @category constructors - * @since 2.0.0 - */ -export const asks: (f: (r: R) => A) => ReaderEither = (f) => flow(f, _.right) +export const fromEither: FromEither3['fromEither'] = R.of /** * @category constructors - * @since 2.0.0 + * @since 2.11.0 */ -export const fromEither: FromEither3['fromEither'] = R.of +export const fromReader: (ma: Reader) => ReaderEither = rightReader // ------------------------------------------------------------------------------------- // destructors @@ -613,6 +614,55 @@ export const Alt: Alt3 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader3 = { + URI, + fromReader +} + +/** + * Reads the current context. + * + * @category constructors + * @since 2.0.0 + */ +export const ask: () => ReaderEither = + /*#__PURE__*/ + ask_(FromReader) + +/** + * Projects a value from the global context in a `ReaderEither`. + * + * @category constructors + * @since 2.0.0 + */ +export const asks: (f: (r: R) => A) => ReaderEither = + /*#__PURE__*/ + asks_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderK: , R, B>( + f: (...a: A) => Reader +) => (...a: A) => ReaderEither = + /*#__PURE__*/ + fromReaderK_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderK: ( + f: (a: A) => Reader +) => (ma: ReaderEither) => ReaderEither = + /*#__PURE__*/ + chainReaderK_(FromReader, Chain) + /** * @category instances * @since 2.7.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index d43f09a5e..5cc54a2a7 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -11,6 +11,13 @@ import { } from './Apply' import { bind as bind_, Chain2, chainFirst as chainFirst_ } from './Chain' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_ } from './FromIO' +import { + ask as ask_, + asks as asks_, + chainReaderK as chainReaderK_, + FromReader2, + fromReaderK as fromReaderK_ +} from './FromReader' import { chainFirstTaskK as chainFirstTaskK_, chainTaskK as chainTaskK_, @@ -57,18 +64,6 @@ export const fromReader: (ma: Reader) => ReaderTask = /*#__PURE__*/ RT.fromReader(T.Pointed) -/** - * @category constructors - * @since 2.3.0 - */ -export const ask = (): ReaderTask => T.of - -/** - * @category constructors - * @since 2.3.0 - */ -export const asks = (f: (r: R) => A): ReaderTask => flow(f, T.of) - /** * @category constructors * @since 2.3.0 @@ -383,6 +378,51 @@ export const chainFirstIOK = /*#__PURE__*/ chainFirstIOK_(FromIO, Chain) +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader2 = { + URI, + fromReader +} + +/** + * Reads the current context. + * + * @category constructors + * @since 2.3.0 + */ +export const ask = + /*#__PURE__*/ + ask_(FromReader) + +/** + * Projects a value from the global context in a `ReaderTask`. + * + * @category constructors + * @since 2.3.0 + */ +export const asks = + /*#__PURE__*/ + asks_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderK = + /*#__PURE__*/ + fromReaderK_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderK = + /*#__PURE__*/ + chainReaderK_(FromReader, Chain) + /** * @category instances * @since 2.10.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 5efa60154..5dadee89f 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -35,6 +35,13 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO3, fromIOK as fromIOK_ } from './FromIO' +import { + ask as ask_, + asks as asks_, + chainReaderK as chainReaderK_, + FromReader3, + fromReaderK as fromReaderK_ +} from './FromReader' import { chainFirstTaskK as chainFirstTaskK_, chainTaskK as chainTaskK_, @@ -198,16 +205,9 @@ export const leftIO: (me: IO) => ReaderTaskEither() => ReaderTaskEither = () => TE.right - -/** - * @category constructors - * @since 2.0.0 + * @since 2.11.0 */ -export const asks: (f: (r: R) => A) => ReaderTaskEither = (f) => - flow(TE.right, TE.map(f)) +export const fromReader: FromReader3['fromReader'] = rightReader /** * @category constructors @@ -836,6 +836,55 @@ export const Alt: Alt3 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader3 = { + URI, + fromReader +} + +/** + * Reads the current context. + * + * @category constructors + * @since 2.0.0 + */ +export const ask: () => ReaderTaskEither = + /*#__PURE__*/ + ask_(FromReader) + +/** + * Projects a value from the global context in a `ReaderEither`. + * + * @category constructors + * @since 2.0.0 + */ +export const asks: (f: (r: R) => A) => ReaderTaskEither = + /*#__PURE__*/ + asks_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderK: , R, B>( + f: (...a: A) => R.Reader +) => (...a: A) => ReaderTaskEither = + /*#__PURE__*/ + fromReaderK_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither = + /*#__PURE__*/ + chainReaderK_(FromReader, Chain) + /** * @category instances * @since 2.10.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 7a00f2567..28b5e30d6 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -7,6 +7,7 @@ import { apFirst as apFirst_, Apply4, apS as apS_, apSecond as apSecond_ } from import { Bifunctor4 } from './Bifunctor' import { bind as bind_, Chain4, chainFirst as chainFirst_ } from './Chain' import * as E from './Either' +import { Endomorphism } from './Endomorphism' import { chainEitherK as chainEitherK_, chainOptionK as chainOptionK_, @@ -18,6 +19,22 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO4, fromIOK as fromIOK_ } from './FromIO' +import { + ask as ask_, + asks as asks_, + chainReaderK as chainReaderK_, + FromReader4, + fromReaderK as fromReaderK_ +} from './FromReader' +import { + chainStateK as chainStateK_, + FromState4, + fromStateK as fromStateK_, + get as get_, + gets as gets_, + modify as modify_, + put as put_ +} from './FromState' import { chainFirstTaskK as chainFirstTaskK_, chainTaskK as chainTaskK_, @@ -43,15 +60,6 @@ import { State } from './State' import * as ST from './StateT' import { Task } from './Task' import { TaskEither } from './TaskEither' -import { - chainStateK as chainStateK_, - FromState4, - fromStateK as fromStateK_, - get as get_, - gets as gets_, - modify as modify_, - put as put_ -} from './FromState' // ------------------------------------------------------------------------------------- // model @@ -59,7 +67,6 @@ import { import ReaderTaskEither = RTE.ReaderTaskEither import Either = E.Either -import { Endomorphism } from './Endomorphism' /** * @category model @@ -182,6 +189,12 @@ export const fromReaderTaskEither: (ma: ReaderTaskEither) = /*#__PURE__*/ ST.fromF(RTE.Functor) +/** + * @category constructors + * @since 2.11.0 + */ +export const fromReader: (ma: Reader) => StateReaderTaskEither = rightReader + /** * @category constructors * @since 2.0.0 @@ -738,6 +751,55 @@ export const Alt: Alt4 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader4 = { + URI, + fromReader +} + +/** + * Reads the current context. + * + * @category constructors + * @since 2.11.0 + */ +export const ask: () => StateReaderTaskEither = + /*#__PURE__*/ + ask_(FromReader) + +/** + * Projects a value from the global context in a `ReaderEither`. + * + * @category constructors + * @since 2.11.0 + */ +export const asks: (f: (r: R) => A) => StateReaderTaskEither = + /*#__PURE__*/ + asks_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderK: , R, B>( + f: (...a: A) => Reader +) => (...a: A) => StateReaderTaskEither = + /*#__PURE__*/ + fromReaderK_(FromReader) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderK: ( + f: (a: A) => Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = + /*#__PURE__*/ + chainReaderK_(FromReader, Chain) + /** * @category instances * @since 2.10.0 diff --git a/src/function.ts b/src/function.ts index ced5a53a1..7a29140ae 100644 --- a/src/function.ts +++ b/src/function.ts @@ -55,7 +55,8 @@ export const getSemigroup = (S: Semigroup) => (): Semigroup<(a: * Unary functions form a monoid as long as you can provide a monoid for the codomain. * * @example - * import { Predicate, getMonoid } from 'fp-ts/Predicate' + * import { Predicate } from 'fp-ts/Predicate' + * import { getMonoid } from 'fp-ts/function' * import * as B from 'fp-ts/boolean' * * const f: Predicate = (n) => n <= 2 diff --git a/src/index.ts b/src/index.ts index 0fd9871ad..50d924046 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,7 @@ import * as foldable from './Foldable' import * as foldableWithIndex from './FoldableWithIndex' import * as fromEither from './FromEither' import * as fromIO from './FromIO' +import * as fromReader from './FromReader' import * as fromState from './FromState' import * as fromTask from './FromTask' import * as fromThese from './FromThese' @@ -259,6 +260,10 @@ export { * @since 2.10.0 */ fromIO, + /** + * @since 2.11.0 + */ + fromReader, /** * @since 2.11.0 */ diff --git a/src/internal.ts b/src/internal.ts index 52cc20aaf..7f6c464a5 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -1,10 +1,10 @@ /** * @since 2.10.0 */ -import type { Either, Left, Right } from './Either' -import type { NonEmptyArray } from './NonEmptyArray' -import type { None, Option, Some } from './Option' -import type { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' +import { Either, Left, Right } from './Either' +import { NonEmptyArray } from './NonEmptyArray' +import { None, Option, Some } from './Option' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' // ------------------------------------------------------------------------------------- // Option diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 0cd1b8faf..edb0853b6 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -272,4 +272,15 @@ describe('ReaderEither', () => { U.deepStrictEqual(f(_.right(1))({}), 'right') U.deepStrictEqual(f(_.left('a'))({}), 'left') }) + + it('fromReaderK', () => { + const ma = _.fromReaderK((n: number): R.Reader => (c) => n * c) + U.deepStrictEqual(ma(3)(2), E.right(6)) + }) + + it('chainReaderK', () => { + const f = _.chainReaderK((n: number): R.Reader => (c) => n * c) + U.deepStrictEqual(pipe(_.right(3), f)(2), E.right(6)) + U.deepStrictEqual(pipe(_.left('a'), f)(2), E.left('a')) + }) }) From ce12a07a37706a3247f8f58e8a5a7c175152ed9b Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 31 Mar 2021 19:55:50 +0200 Subject: [PATCH 033/162] Witherable: add wiltDefault, witherDefault --- CHANGELOG.md | 2 ++ src/Array.ts | 34 +++++++++++------------- src/Either.ts | 25 +++-------------- src/Map.ts | 14 +++------- src/Option.ts | 42 ++++++++++++++--------------- src/ReadonlyArray.ts | 37 +++++++++++--------------- src/ReadonlyMap.ts | 19 +++---------- src/ReadonlyRecord.ts | 61 ++++++++++++++++++------------------------ src/Record.ts | 61 ++++++++++++++++++------------------------ src/Witherable.ts | 62 +++++++++++++++++++++++++++++++++++++++---- 10 files changed, 171 insertions(+), 186 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cd660235..e33da73d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,8 @@ high state of flux, you're at risk of it changing without notice. - add `fromTaskEither` (@thewilkybarkid) - `Witherable` - add `filterE`, #1458 (@vinassefranche) + - add `wiltDefault` + - add `witherDefault` # 2.10.0-rc.7 diff --git a/src/Array.ts b/src/Array.ts index 0c32a71b0..50626abc0 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -35,7 +35,14 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { filterE as filterE_, PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { + filterE as filterE_, + PipeableWilt1, + PipeableWither1, + Witherable1, + wiltDefault, + witherDefault +} from './Witherable' import NonEmptyArray = NEA.NonEmptyArray @@ -1263,20 +1270,6 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' return (ta, f) => pipe(ta, traverseWithIndexF(f)) } /* istanbul ignore next */ -const _wither: Witherable1['wither'] = ( - F: ApplicativeHKT -): ((ta: Array, f: (a: A) => HKT>) => HKT>) => { - const witherF = wither(F) - return (fa, f) => pipe(fa, witherF(f)) -} -/* istanbul ignore next */ -const _wilt: Witherable1['wilt'] = ( - F: ApplicativeHKT -): ((fa: Array, f: (a: A) => HKT>) => HKT, Array>>) => { - const wiltF = wilt(F) - return (fa, f) => pipe(fa, wiltF(f)) -} -/* istanbul ignore next */ const _chainRecDepthFirst: ChainRec1['chainRec'] = RA.ChainRecDepthFirst.chainRec as any /* istanbul ignore next */ const _chainRecBreadthFirst: ChainRec1['chainRec'] = RA.ChainRecBreadthFirst.chainRec as any @@ -1589,8 +1582,8 @@ export const traverseWithIndex: PipeableTraverseWithIndex1 = (F: export const wither: PipeableWither1 = ( F: ApplicativeHKT ): ((f: (a: A) => HKT>) => (fa: Array) => HKT>) => { - const traverseF = traverse(F) - return (f) => (fa) => F.map(pipe(fa, traverseF(f)), compact) + const _witherF = _wither(F) + return (f) => (fa) => _witherF(fa, f) } /** @@ -1600,8 +1593,8 @@ export const wither: PipeableWither1 = ( export const wilt: PipeableWilt1 = ( F: ApplicativeHKT ): ((f: (a: A) => HKT>) => (fa: Array) => HKT, Array>>) => { - const traverseF = traverse(F) - return (f) => (fa) => F.map(pipe(fa, traverseF(f)), separate) + const _wiltF = _wilt(F) + return (f) => (fa) => _wiltF(fa, f) } /** @@ -1979,6 +1972,9 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex } +const _wither: Witherable1['wither'] = witherDefault(Traversable, Compactable) +const _wilt: Witherable1['wilt'] = wiltDefault(Traversable, Compactable) + /** * @category instances * @since 2.7.0 diff --git a/src/Either.ts b/src/Either.ts index 99f71e3ef..4e2d6262e 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -46,7 +46,7 @@ import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' import { PipeableTraverse2, Traversable2 } from './Traversable' -import { Witherable2C } from './Witherable' +import { wiltDefault, Witherable2C, witherDefault } from './Witherable' // ------------------------------------------------------------------------------------- // model @@ -932,24 +932,7 @@ export function getFilterable(M: Monoid): Filterable2C { */ export function getWitherable(M: Monoid): Witherable2C { const F_ = getFilterable(M) - - const wither = ( - F: ApplicativeHKT - ): ((ma: Either, f: (a: A) => HKT>) => HKT>) => { - const traverseF = _traverse(F) - return (ma, f) => F.map(traverseF(ma, f), F_.compact) - } - - const wilt = ( - F: ApplicativeHKT - ): (( - ma: Either, - f: (a: A) => HKT> - ) => HKT, Either>>) => { - const traverseF = _traverse(F) - return (ma, f) => F.map(traverseF(ma, f), F_.separate) - } - + const C = getCompactable(M) return { URI, _E: undefined as any, @@ -965,8 +948,8 @@ export function getWitherable(M: Monoid): Witherable2C { reduce: _reduce, foldMap: _foldMap, reduceRight: _reduceRight, - wither, - wilt + wither: witherDefault(Traversable, C), + wilt: wiltDefault(Traversable, C) } } diff --git a/src/Map.ts b/src/Map.ts index 1a74f8708..8bf1d1dbc 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -25,7 +25,7 @@ import { separated, Separated } from './Separated' import { Show } from './Show' import { TraversableWithIndex2C } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { Witherable2C } from './Witherable' +import { wiltDefault, Witherable2C, witherDefault } from './Witherable' import Option = O.Option @@ -644,16 +644,8 @@ export function getWitherable(O: Ord): Witherable2C & TraversableW foldMapWithIndex: TWI.foldMapWithIndex, reduceRightWithIndex: TWI.reduceRightWithIndex, traverseWithIndex: TWI.traverseWithIndex, - wilt: ( - F: Applicative - ): ((wa: Map, f: (a: A) => HKT>) => HKT, Map>>) => { - const traverseF = TWI.traverse(F) - return (wa, f) => F.map(traverseF(wa, f), separate) - }, - wither: (F: Applicative): ((wa: Map, f: (a: A) => HKT>) => HKT>) => { - const traverseF = TWI.traverse(F) - return (wa, f) => F.map(traverseF(wa, f), compact) - } + wilt: wiltDefault(TWI, Compactable), + wither: witherDefault(TWI, Compactable) } } diff --git a/src/Option.ts b/src/Option.ts index 400a295f2..f83f24fe5 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -44,7 +44,7 @@ import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable' // ------------------------------------------------------------------------------------- // model @@ -498,20 +498,6 @@ const _partition: Filterable1['partition'] = (fa: Option, predicate: pipe(fa, partition(predicate)) /* istanbul ignore next */ const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) -/* istanbul ignore next */ -const _wither: Witherable1['wither'] = ( - F: ApplicativeHKT -): ((fa: Option, f: (a: A) => HKT>) => HKT>) => { - const witherF = wither(F) - return (fa, f) => pipe(fa, witherF(f)) -} -/* istanbul ignore next */ -const _wilt: Witherable1['wilt'] = ( - F: ApplicativeHKT -): ((fa: Option, f: (a: A) => HKT>) => HKT, Option>>) => { - const wiltF = wilt(F) - return (fa, f) => pipe(fa, wiltF(f)) -} // ------------------------------------------------------------------------------------- // type class members @@ -725,18 +711,22 @@ export const sequence: Traversable1['sequence'] = (F: ApplicativeHKT) * @category Witherable * @since 2.6.5 */ -export const wither: PipeableWither1 = (F: ApplicativeHKT) => (f: (a: A) => HKT>) => ( - fa: Option -): HKT> => (isNone(fa) ? F.of(none) : f(fa.value)) +export const wither: PipeableWither1 = ( + F: ApplicativeHKT +): ((f: (a: A) => HKT>) => (fa: Option) => HKT>) => { + const _witherF = _wither(F) + return (f) => (fa) => _witherF(fa, f) +} /** * @category Witherable * @since 2.6.5 */ -export const wilt: PipeableWilt1 = (F: ApplicativeHKT) => (f: (a: A) => HKT>) => ( - fa: Option -): HKT, Option>> => { - return isNone(fa) ? F.of(defaultSeparated) : F.map(f(fa.value), (e) => separated(getLeft(e), getRight(e))) +export const wilt: PipeableWilt1 = ( + F: ApplicativeHKT +): ((f: (a: A) => HKT>) => (fa: Option) => HKT, Option>>) => { + const _wiltF = _wilt(F) + return (f) => (fa) => _wiltF(fa, f) } // ------------------------------------------------------------------------------------- @@ -1101,6 +1091,14 @@ export const Traversable: Traversable1 = { sequence } +const _wither: Witherable1['wither'] = + /*#__PURE__*/ + witherDefault(Traversable, Compactable) + +const _wilt: Witherable1['wilt'] = + /*#__PURE__*/ + wiltDefault(Traversable, Compactable) + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index ae94c07cc..2aa5deb95 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -36,7 +36,14 @@ import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableTraverseWithIndex1, TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable1 } from './Unfoldable' -import { filterE as filterE_, PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { + filterE as filterE_, + PipeableWilt1, + PipeableWither1, + Witherable1, + wiltDefault, + witherDefault +} from './Witherable' import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray @@ -1323,23 +1330,6 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' const traverseWithIndexF = traverseWithIndex(F) return (ta, f) => pipe(ta, traverseWithIndexF(f)) } -/* istanbul ignore next */ -const _wither: Witherable1['wither'] = ( - F: ApplicativeHKT -): ((ta: ReadonlyArray, f: (a: A) => HKT>) => HKT>) => { - const witherF = wither(F) - return (fa, f) => pipe(fa, witherF(f)) -} -/* istanbul ignore next */ -const _wilt: Witherable1['wilt'] = ( - F: ApplicativeHKT -): (( - fa: ReadonlyArray, - f: (a: A) => HKT> -) => HKT, ReadonlyArray>>) => { - const wiltF = wilt(F) - return (fa, f) => pipe(fa, wiltF(f)) -} const _chainRecDepthFirst: ChainRec1['chainRec'] = ( a: A, f: (a: A) => ReadonlyArray> @@ -1727,8 +1717,8 @@ export const traverseWithIndex: PipeableTraverseWithIndex1 = (F: export const wither: PipeableWither1 = ( F: ApplicativeHKT ): ((f: (a: A) => HKT>) => (fa: ReadonlyArray) => HKT>) => { - const traverseF = traverse(F) - return (f) => (fa) => F.map(pipe(fa, traverseF(f)), compact) + const _witherF = _wither(F) + return (f) => (fa) => _witherF(fa, f) } /** @@ -1740,8 +1730,8 @@ export const wilt: PipeableWilt1 = ( ): (( f: (a: A) => HKT> ) => (fa: ReadonlyArray) => HKT, ReadonlyArray>>) => { - const traverseF = traverse(F) - return (f) => (fa) => F.map(pipe(fa, traverseF(f)), separate) + const _wiltF = _wilt(F) + return (f) => (fa) => _wiltF(fa, f) } /** @@ -2159,6 +2149,9 @@ export const ChainRecBreadthFirst: ChainRec1 = { chainRec: _chainRecBreadthFirst } +const _wither: Witherable1['wither'] = witherDefault(Traversable, Compactable) +const _wilt: Witherable1['wilt'] = wiltDefault(Traversable, Compactable) + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index e8fac8203..f45abd7dd 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -26,7 +26,7 @@ import { Show } from './Show' import { Traversable2C } from './Traversable' import { TraversableWithIndex2C } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { Witherable2C } from './Witherable' +import { wiltDefault, Witherable2C, witherDefault } from './Witherable' import Option = O.Option @@ -985,21 +985,8 @@ export function getWitherable(O: Ord): Witherable2C & TraversableW foldMapWithIndex: TWI.foldMapWithIndex, reduceRightWithIndex: TWI.reduceRightWithIndex, traverseWithIndex: TWI.traverseWithIndex, - wilt: ( - F: Applicative - ): (( - wa: ReadonlyMap, - f: (a: A) => HKT> - ) => HKT, ReadonlyMap>>) => { - const traverseF = TWI.traverse(F) - return (wa, f) => F.map(traverseF(wa, f), separate) - }, - wither: ( - F: Applicative - ): ((wa: ReadonlyMap, f: (a: A) => HKT>) => HKT>) => { - const traverseF = TWI.traverse(F) - return (wa, f) => F.map(traverseF(wa, f), compact) - } + wilt: wiltDefault(TWI, Compactable), + wither: witherDefault(TWI, Compactable) } } diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index bfb259ad3..623b93f3c 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -27,7 +27,7 @@ import * as S from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable' // ------------------------------------------------------------------------------------- // model @@ -1043,23 +1043,6 @@ const _traverseWithIndex = ( const traverseWithIndexF = traverseWithIndex(F) return (ta, f) => pipe(ta, traverseWithIndexF(f)) } -/* istanbul ignore next */ -const _wither = ( - F: Applicative -): ((fa: ReadonlyRecord, f: (a: A) => HKT>) => HKT>) => { - const witherF = wither(F) - return (fa, f) => pipe(fa, witherF(f)) -} -/* istanbul ignore next */ -const _wilt = ( - F: Applicative -): (( - fa: ReadonlyRecord, - f: (a: A) => HKT> -) => HKT, ReadonlyRecord>>) => { - const wiltF = wilt(F) - return (fa, f) => pipe(fa, wiltF(f)) -} // ------------------------------------------------------------------------------------- // type class members @@ -1459,23 +1442,26 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1) => Witherable1 = (O) => ({ - URI, - map: _map, - reduce: _reduce(O), - foldMap: _foldMap(O), - reduceRight: _reduceRight(O), - traverse: _traverse, - sequence, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap, - wither: _wither, - wilt: _wilt -}) +export const getWitherable = (O: Ord): Witherable1 => { + const T = getTraversable(O) + return { + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: T.traverse, + sequence: T.sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: witherDefault(T, Compactable), + wilt: wiltDefault(T, Compactable) + } +} /** * @category instances @@ -1590,6 +1576,11 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex } +// tslint:disable-next-line: deprecation +const _wither = witherDefault(Traversable, Compactable) +// tslint:disable-next-line: deprecation +const _wilt = wiltDefault(Traversable, Compactable) + /** * Use `getWitherable` instead. * diff --git a/src/Record.ts b/src/Record.ts index 2217c7a5f..75b515a63 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -28,7 +28,7 @@ import * as S from './string' import { Traversable1 } from './Traversable' import { TraversableWithIndex1 } from './TraversableWithIndex' import { Unfoldable, Unfoldable1 } from './Unfoldable' -import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable' +import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable' // ------------------------------------------------------------------------------------- // model @@ -682,23 +682,6 @@ const _traverseWithIndex = ( const traverseWithIndexF = traverseWithIndex(F) return (ta, f) => pipe(ta, traverseWithIndexF(f)) } -/* istanbul ignore next */ -const _wither = ( - F: Applicative -): ((fa: Record, f: (a: A) => HKT>) => HKT>) => { - const witherF = wither(F) - return (fa, f) => pipe(fa, witherF(f)) -} -/* istanbul ignore next */ -const _wilt = ( - F: Applicative -): (( - fa: Record, - f: (a: A) => HKT> -) => HKT, Record>>) => { - const wiltF = wilt(F) - return (fa, f) => pipe(fa, wiltF(f)) -} // ------------------------------------------------------------------------------------- // type class members @@ -1021,23 +1004,26 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1): Witherable1 => ({ - URI, - map: _map, - reduce: _reduce(O), - foldMap: _foldMap(O), - reduceRight: _reduceRight(O), - traverse: _traverse, - sequence, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap, - wither: _wither, - wilt: _wilt -}) +export const getWitherable = (O: Ord): Witherable1 => { + const T = getTraversable(O) + return { + URI, + map: _map, + reduce: _reduce(O), + foldMap: _foldMap(O), + reduceRight: _reduceRight(O), + traverse: T.traverse, + sequence: T.sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: witherDefault(T, Compactable), + wilt: wiltDefault(T, Compactable) + } +} /** * @category instances @@ -1152,6 +1138,11 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex } +// tslint:disable-next-line: deprecation +const _wither = witherDefault(Traversable, Compactable) +// tslint:disable-next-line: deprecation +const _wilt = wiltDefault(Traversable, Compactable) + /** * Use `getWitherable` instead. * diff --git a/src/Witherable.ts b/src/Witherable.ts index 92df723ba..897a475dc 100644 --- a/src/Witherable.ts +++ b/src/Witherable.ts @@ -5,13 +5,15 @@ * * @since 2.0.0 */ -import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' -import { none, some, Option } from './Option' -import { Traversable, Traversable1, Traversable2, Traversable2C, Traversable3 } from './Traversable' import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3, Applicative3C } from './Applicative' -import { Filterable, Filterable1, Filterable2, Filterable2C, Filterable3 } from './Filterable' +import { Compactable, Compactable1, Compactable2, Compactable2C } from './Compactable' import { Either } from './Either' +import { Filterable, Filterable1, Filterable2, Filterable2C, Filterable3 } from './Filterable' +import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' +import * as _ from './internal' +import { Option } from './Option' import { Separated } from './Separated' +import { Traversable, Traversable1, Traversable2, Traversable2C, Traversable3 } from './Traversable' // ------------------------------------------------------------------------------------- // model @@ -547,6 +549,56 @@ export interface PipeableWilt3 { ) => (wa: Kind3) => HKT, Kind3>> } +// ------------------------------------------------------------------------------------- +// defaults +// ------------------------------------------------------------------------------------- + +/** + * Return a `wilt` implementation from `Traversable` and `Compactable`. + * + * @category defaults + * @since 3.0.0 + */ +export function wiltDefault(T: Traversable2C, C: Compactable2): Witherable2C['wilt'] +export function wiltDefault(T: Traversable2, C: Compactable2C): Witherable2C['wilt'] +export function wiltDefault(T: Traversable1, C: Compactable1): Witherable1['wilt'] +export function wiltDefault(T: Traversable, C: Compactable): Witherable['wilt'] +export function wiltDefault(T: Traversable, C: Compactable): Witherable['wilt'] { + return ( + F: Applicative + ): ((wa: HKT, f: (a: A) => HKT>) => HKT, HKT>>) => { + const traverseF = T.traverse(F) + return (wa, f) => F.map(traverseF(wa, f), C.separate) + } +} + +/** + * Return a `wither` implementation from `Traversable` and `Compactable`. + * + * @category defaults + * @since 3.0.0 + */ +export function witherDefault( + T: Traversable2C, + C: Compactable2 +): Witherable2C['wither'] +export function witherDefault( + T: Traversable2, + C: Compactable2C +): Witherable2C['wither'] +export function witherDefault(T: Traversable1, C: Compactable1): Witherable1['wither'] +export function witherDefault(T: Traversable, C: Compactable): Witherable['wither'] +export function witherDefault(T: Traversable, C: Compactable): Witherable['wither'] { + return (F: Applicative): ((wa: HKT, f: (a: A) => HKT>) => HKT>) => { + const traverseF = T.traverse(F) + return (wa, f) => F.map(traverseF(wa, f), C.compact) + } +} + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + /** * @since 2.11.0 */ @@ -585,6 +637,6 @@ export function filterE( ): (F: Applicative) => (predicate: (a: A) => HKT) => (ga: HKT) => HKT> { return (F) => { const witherF = W.wither(F) - return (predicate) => (ga) => witherF(ga, (a) => F.map(predicate(a), (b) => (b ? some(a) : none))) + return (predicate) => (ga) => witherF(ga, (a) => F.map(predicate(a), (b) => (b ? _.some(a) : _.none))) } } From c4a5d291f0bde3a00f212c28164b30c891a59b53 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 09:41:25 +0200 Subject: [PATCH 034/162] Array / ReadonlyArray: - add `getUnionSemigroup` - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` --- CHANGELOG.md | 8 ++++++++ src/Array.ts | 45 ++++++++++++++++++++++++++++++++++++++++++- src/ReadonlyArray.ts | 45 ++++++++++++++++++++++++++++++++++++++++++- test/Array.ts | 26 +++++++++++++++++++++++++ test/ReadonlyArray.ts | 26 +++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e33da73d0..b51225aac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ high state of flux, you're at risk of it changing without notice. - add `filterE` - add `ChainRecDepthFirst` instance (@qlonik) - add `ChainRecBreadthFirst` instance (@qlonik) + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` - `function` - add `SK` (@cdimitroulas) - add `apply` @@ -69,6 +73,10 @@ high state of flux, you're at risk of it changing without notice. - add `filterE` - add `ChainRecDepthFirst` instance (@qlonik) - add `ChainRecBreadthFirst` instance (@qlonik) + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - `ReadonlyRecord` diff --git a/src/Array.ts b/src/Array.ts index 50626abc0..73ea0158e 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -20,6 +20,7 @@ import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' import * as _ from './internal' +import { Magma } from './Magma' import { Monad1 } from './Monad' import { Monoid } from './Monoid' import * as NEA from './NonEmptyArray' @@ -39,8 +40,8 @@ import { filterE as filterE_, PipeableWilt1, PipeableWither1, - Witherable1, wiltDefault, + Witherable1, witherDefault } from './Witherable' @@ -1708,6 +1709,48 @@ export const getEq: (E: Eq) => Eq> = RA.getEq */ export const getOrd: (O: Ord) => Ord> = RA.getOrd +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => { + const unionE = union(E) + return { + concat: (first, second) => unionE(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (E: Eq): Monoid> => ({ + concat: getUnionSemigroup(E).concat, + empty: [] +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (E: Eq): Semigroup> => { + const intersectionE = intersection(E) + return { + concat: (first, second) => intersectionE(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq): Magma> => { + const differenceE = difference(E) + return { + concat: (first, second) => differenceE(second)(first) + } +} + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 2aa5deb95..4585198ea 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -20,6 +20,7 @@ import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT } from './HKT' import * as _ from './internal' +import { Magma } from './Magma' import { Monad1 } from './Monad' import { Monoid } from './Monoid' import { NonEmptyArray } from './NonEmptyArray' @@ -40,8 +41,8 @@ import { filterE as filterE_, PipeableWilt1, PipeableWither1, - Witherable1, wiltDefault, + Witherable1, witherDefault } from './Witherable' @@ -1861,6 +1862,48 @@ export const getOrd = (O: Ord): Ord> => return N.Ord.compare(aLen, bLen) }) +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => { + const unionE = union(E) + return { + concat: (first, second) => unionE(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (E: Eq): Monoid> => ({ + concat: getUnionSemigroup(E).concat, + empty +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (E: Eq): Semigroup> => { + const intersectionE = intersection(E) + return { + concat: (first, second) => intersectionE(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq): Magma> => { + const differenceE = difference(E) + return { + concat: (first, second) => differenceE(second)(first) + } +} + /** * @category instances * @since 2.7.0 diff --git a/test/Array.ts b/test/Array.ts index 46aca87bb..dcf388a79 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -1081,6 +1081,32 @@ describe('Array', () => { U.deepStrictEqual(pipe([1, 2], _.difference(N.Eq)([1, 2])), []) }) + it('getUnionMonoid', () => { + const concat = _.getUnionMonoid(N.Eq).concat + const two: Array = [1, 2] + U.deepStrictEqual(concat(two, [3, 4]), [1, 2, 3, 4]) + U.deepStrictEqual(concat(two, [2, 3]), [1, 2, 3]) + U.deepStrictEqual(concat(two, [1, 2]), [1, 2]) + + U.deepStrictEqual(concat(two, []), two) + U.deepStrictEqual(concat([], two), two) + U.deepStrictEqual(concat([], []), []) + }) + + it('getIntersectionSemigroup', () => { + const concat = _.getIntersectionSemigroup(N.Eq).concat + U.deepStrictEqual(concat([1, 2], [3, 4]), []) + U.deepStrictEqual(concat([1, 2], [2, 3]), [2]) + U.deepStrictEqual(concat([1, 2], [1, 2]), [1, 2]) + }) + + it('getDifferenceMagma', () => { + const concat = _.getDifferenceMagma(N.Eq).concat + U.deepStrictEqual(concat([1, 2], [3, 4]), [1, 2]) + U.deepStrictEqual(concat([1, 2], [2, 3]), [1]) + U.deepStrictEqual(concat([1, 2], [1, 2]), []) + }) + it('should be safe when calling map with a binary function', () => { interface Foo { readonly bar: () => number diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index b72638202..dfcca389e 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1150,6 +1150,32 @@ describe('ReadonlyArray', () => { U.deepStrictEqual(pipe([1, 2], _.difference(N.Eq)([1, 2])), []) }) + it('getUnionMonoid', () => { + const M = _.getUnionMonoid(N.Eq) + const two: ReadonlyArray = [1, 2] + U.deepStrictEqual(M.concat(two, [3, 4]), [1, 2, 3, 4]) + U.deepStrictEqual(M.concat(two, [2, 3]), [1, 2, 3]) + U.deepStrictEqual(M.concat(two, [1, 2]), [1, 2]) + + U.strictEqual(M.concat(two, M.empty), two) + U.strictEqual(M.concat(M.empty, two), two) + U.strictEqual(M.concat(M.empty, M.empty), M.empty) + }) + + it('getIntersectionSemigroup', () => { + const concat = _.getIntersectionSemigroup(N.Eq).concat + U.deepStrictEqual(concat([1, 2], [3, 4]), []) + U.deepStrictEqual(concat([1, 2], [2, 3]), [2]) + U.deepStrictEqual(concat([1, 2], [1, 2]), [1, 2]) + }) + + it('getDifferenceMagma', () => { + const concat = _.getDifferenceMagma(N.Eq).concat + U.deepStrictEqual(concat([1, 2], [3, 4]), [1, 2]) + U.deepStrictEqual(concat([1, 2], [2, 3]), [1]) + U.deepStrictEqual(concat([1, 2], [1, 2]), []) + }) + it('should be safe when calling map with a binary function', () => { interface Foo { readonly bar: () => number From 17a41ca594f14784971a9e093d51f52b74aff484 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 10:06:09 +0200 Subject: [PATCH 035/162] Set / ReadonlySet - add `getUnionSemigroup` - add `getDifferenceMagma` --- CHANGELOG.md | 6 ++++++ src/ReadonlySet.ts | 35 ++++++++++++++++++++++++----------- src/Set.ts | 35 ++++++++++++++++++++++++----------- test/ReadonlySet.ts | 5 +++++ test/Set.ts | 5 +++++ 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51225aac..f7bf0d34d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,9 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` (@anthonyjoeseph) - add `getIntersectionSemigroup` (@anthonyjoeseph) - add `getDifferenceMagma` (@anthonyjoeseph) + - `ReadonlySet` + - add `getUnionSemigroup` + - add `getDifferenceMagma` - `Record` - add `union` - add `intersection` @@ -95,6 +98,9 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - `Set` + - add `getUnionSemigroup` + - add `getDifferenceMagma` - `StateReaderTaskEither` - add `fromStateK` - add `chainStateK` diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 5b3d1ea1c..8dfec64f2 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -4,6 +4,7 @@ import { Either } from './Either' import { Eq, fromEquals } from './Eq' import { identity } from './function' +import { Magma } from './Magma' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' @@ -290,26 +291,38 @@ export function difference( } } +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => ({ + concat: union(E) +}) + /** * @category instances * @since 2.5.0 */ -export function getUnionMonoid(E: Eq): Monoid> { - return { - concat: union(E), - empty - } -} +export const getUnionMonoid = (E: Eq): Monoid> => ({ + concat: getUnionSemigroup(E).concat, + empty +}) /** * @category instances * @since 2.5.0 */ -export function getIntersectionSemigroup(E: Eq): Semigroup> { - return { - concat: intersection(E) - } -} +export const getIntersectionSemigroup = (E: Eq): Semigroup> => ({ + concat: intersection(E) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq): Magma> => ({ + concat: difference(E) +}) /** * @since 2.5.0 diff --git a/src/Set.ts b/src/Set.ts index 3f6177efa..007d8d5e5 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -4,6 +4,7 @@ import { Either } from './Either' import { Eq } from './Eq' import { identity } from './function' +import { Magma } from './Magma' import { Monoid } from './Monoid' import { Option } from './Option' import { Ord } from './Ord' @@ -253,26 +254,38 @@ export function difference(E: Eq): (me: Set, that?: Set) => Set | } } +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => ({ + concat: union(E) +}) + /** * @category instances * @since 2.0.0 */ -export function getUnionMonoid(E: Eq): Monoid> { - return { - concat: union(E), - empty: new Set() - } -} +export const getUnionMonoid = (E: Eq): Monoid> => ({ + concat: getUnionSemigroup(E).concat, + empty: new Set() +}) /** * @category instances * @since 2.0.0 */ -export function getIntersectionSemigroup(E: Eq): Semigroup> { - return { - concat: intersection(E) - } -} +export const getIntersectionSemigroup = (E: Eq): Semigroup> => ({ + concat: intersection(E) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq): Magma> => ({ + concat: difference(E) +}) /** * @since 2.0.0 diff --git a/test/ReadonlySet.ts b/test/ReadonlySet.ts index c531ae9ae..2cd3d9c95 100644 --- a/test/ReadonlySet.ts +++ b/test/ReadonlySet.ts @@ -140,6 +140,11 @@ describe('ReadonlySet', () => { U.deepStrictEqual(IS.concat(_.empty, new Set([1, 3])), _.empty) }) + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma(N.Eq) + U.deepStrictEqual(M.concat(new Set([1, 2]), new Set([1, 3])), new Set([2])) + }) + it('difference', () => { U.deepStrictEqual(_.difference(N.Eq)(new Set([1, 2]), new Set([1, 3])), new Set([2])) diff --git a/test/Set.ts b/test/Set.ts index 0ea49ec9a..5cf34122e 100644 --- a/test/Set.ts +++ b/test/Set.ts @@ -136,6 +136,11 @@ describe('Set', () => { U.deepStrictEqual(IS.concat(_.empty, new Set([1, 3])), _.empty) }) + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma(N.Eq) + U.deepStrictEqual(M.concat(new Set([1, 2]), new Set([1, 3])), new Set([2])) + }) + it('difference', () => { U.deepStrictEqual(_.difference(N.Eq)(new Set([1, 2]), new Set([1, 3])), new Set([2])) From cac84f75e4f223a8d6a6dcd76ca542984b36bac4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 10:32:28 +0200 Subject: [PATCH 036/162] NonEmptyArray / ReadonlyNonEmptyArray - add `union` - add `getUnionSemigroup` --- CHANGELOG.md | 4 ++++ src/Array.ts | 4 ++-- src/NonEmptyArray.ts | 18 +++++++++++++++--- src/ReadonlyArray.ts | 4 ++-- src/ReadonlyNonEmptyArray.ts | 20 +++++++++++++++++--- src/ReadonlyRecord.ts | 3 +++ src/Record.ts | 3 +++ test/NonEmptyArray.ts | 7 +++++++ test/ReadonlyNonEmptyArray.ts | 2 +- 9 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7bf0d34d..7af518029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,8 @@ high state of flux, you're at risk of it changing without notice. - add `apply` - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - add `union` + - add `getUnionSemigroup` - `Ord` - add `trivial` instance - add `equals` @@ -79,6 +81,8 @@ high state of flux, you're at risk of it changing without notice. - add `getDifferenceMagma` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - add `union` + - add `getUnionSemigroup` - `ReadonlyRecord` - add `union` (@anthonyjoeseph) - add `intersection` (@anthonyjoeseph) diff --git a/src/Array.ts b/src/Array.ts index 73ea0158e..5298bd9c5 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -1121,10 +1121,10 @@ export function union(E: Eq): (xs: Array, ys?: Array) => Array | return (first, second?) => { if (second === undefined) { const unionE = union(E) - return (ys) => unionE(ys, first) + return (second) => unionE(second, first) } return isNonEmpty(first) && isNonEmpty(second) - ? unionE(first, second) + ? unionE(second)(first) : isNonEmpty(first) ? copy(first) : copy(second) diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 26fc2fa6a..8d832f9df 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -139,11 +139,12 @@ export const sortBy = (ords: Array>): ((as: NonEmptyArray } /** - * @internal + * @category combinators + * @since 2.11.0 */ -export const union = (E: Eq): Semigroup>['concat'] => { +export const union = (E: Eq): ((second: NonEmptyArray) => (first: NonEmptyArray) => NonEmptyArray) => { const uniqE = uniq(E) - return (first, second) => uniqE(concat(first, second)) + return (second) => (first) => uniqE(concat(first, second)) } /** @@ -787,6 +788,17 @@ export const getSemigroup = (): Semigroup> => ({ */ export const getEq: (E: Eq) => Eq> = RNEA.getEq +/** + * @category combinators + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => { + const unionE = union(E) + return { + concat: (first, second) => unionE(second)(first) + } +} + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 4585198ea..b38cff4bf 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -1198,9 +1198,9 @@ export function union( return (first, second?) => { if (second === undefined) { const unionE = union(E) - return (ys) => unionE(ys, first) + return (second) => unionE(second, first) } - return isNonEmpty(first) && isNonEmpty(second) ? unionE(first, second) : isNonEmpty(first) ? first : second + return isNonEmpty(first) && isNonEmpty(second) ? unionE(second)(first) : isNonEmpty(first) ? first : second } } diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index dc229b2ac..105851692 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -149,11 +149,14 @@ export const sortBy = ( } /** - * @internal + * @category combinators + * @since 2.11.0 */ -export const union = (E: Eq): Semigroup>['concat'] => { +export const union = ( + E: Eq +): ((second: ReadonlyNonEmptyArray) => (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { const uniqE = uniq(E) - return (first, second) => uniqE(concat(first, second)) + return (second) => (first) => uniqE(concat(first, second)) } /** @@ -832,6 +835,17 @@ export const getSemigroup = (): Semigroup> = export const getEq = (E: Eq): Eq> => fromEquals((xs, ys) => xs.length === ys.length && xs.every((x, i) => E.equals(x, ys[i]))) +/** + * @category combinators + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => { + const unionE = union(E) + return { + concat: (first, second) => unionE(second)(first) + } +} + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 623b93f3c..0702b572b 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -900,6 +900,7 @@ export function elem( } /** + * @category combinators * @since 2.11.0 */ export const union = (M: Magma) => (second: ReadonlyRecord) => ( @@ -928,6 +929,7 @@ export const union = (M: Magma) => (second: ReadonlyRecord) => } /** + * @category combinators * @since 2.11.0 */ export const intersection = (M: Magma) => (second: ReadonlyRecord) => ( @@ -946,6 +948,7 @@ export const intersection = (M: Magma) => (second: ReadonlyRecord(second: ReadonlyRecord) => ( diff --git a/src/Record.ts b/src/Record.ts index 75b515a63..aa16c1f54 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -573,6 +573,7 @@ export const elem: ( } = RR.elem /** + * @category combinators * @since 2.11.0 */ export const union = ( @@ -591,6 +592,7 @@ export const union = ( } /** + * @category combinators * @since 2.11.0 */ export const intersection = (M: Magma) => (second: Record) => ( @@ -603,6 +605,7 @@ export const intersection = (M: Magma) => (second: Record) => ( } /** + * @category combinators * @since 2.11.0 */ export const difference = (second: Record) => (first: Record): Record => { diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 85c2eec1d..fb069e031 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -198,6 +198,13 @@ describe('NonEmptyArray', () => { }) }) + it('union', () => { + const concat = _.getUnionSemigroup(N.Eq).concat + U.deepStrictEqual(concat([1, 2], [3, 4]), [1, 2, 3, 4]) + U.deepStrictEqual(concat([1, 2], [2, 3]), [1, 2, 3]) + U.deepStrictEqual(concat([1, 2], [1, 2]), [1, 2]) + }) + it('insertAt', () => { const make = (x: number) => ({ x }) const a1 = make(1) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index bb09d21a6..71bd704d1 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -609,7 +609,7 @@ describe('ReadonlyNonEmptyArray', () => { }) it('union', () => { - const concat = _.union(N.Eq) + const concat = _.getUnionSemigroup(N.Eq).concat U.deepStrictEqual(concat([1, 2], [3, 4]), [1, 2, 3, 4]) U.deepStrictEqual(concat([1, 2], [2, 3]), [1, 2, 3]) U.deepStrictEqual(concat([1, 2], [1, 2]), [1, 2]) From 459cd1c063313def3f9112d34ab439b12a57e1d6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 10:36:13 +0200 Subject: [PATCH 037/162] ReadonlyArray: add fromOption --- CHANGELOG.md | 1 + src/Array.ts | 4 +--- src/ReadonlyArray.ts | 6 ++++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7af518029..08719f23b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - add `fromOption` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` diff --git a/src/Array.ts b/src/Array.ts index 5298bd9c5..7b0312b2c 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -154,9 +154,7 @@ export function fromPredicate(predicate: Predicate): (a: A) => Array { * @category constructors * @since 2.11.0 */ -export const fromOption = (ma: Option): Array => { - return _.isSome(ma) ? [ma.value] : [] -} +export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? [] : [ma.value]) // ------------------------------------------------------------------------------------- // destructors diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index b38cff4bf..6a65aabca 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -141,6 +141,12 @@ export const range = (start: number, end: number): ReadonlyArray => */ export const replicate = (n: number, a: A): ReadonlyArray => makeBy(n, () => a) +/** + * @category constructors + * @since 2.11.0 + */ +export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? empty : [ma.value]) + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- From adbbb3ee8b2f899b30eb4ea6c98e062bfb4603b6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 10:49:36 +0200 Subject: [PATCH 038/162] FromEither: add FromEither1 --- src/FromEither.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/FromEither.ts b/src/FromEither.ts index f1e0cd679..bd236e204 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -4,10 +4,10 @@ * @since 2.10.0 */ -import { Chain, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' +import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' import { Either } from './Either' import { flow, Lazy } from './function' -import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import { HKT2, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import * as _ from './internal' import { Option } from './Option' import { Predicate } from './Predicate' @@ -26,6 +26,15 @@ export interface FromEither { readonly fromEither: (e: Either) => HKT2 } +/** + * @category type classes + * @since 2.11.0 + */ +export interface FromEither1 { + readonly URI: F + readonly fromEither: (e: Either) => Kind +} + /** * @category type classes * @since 2.10.0 @@ -252,6 +261,9 @@ export function fromEitherK( export function fromEitherK( F: FromEither2C ): , B>(f: (...a: A) => Either) => (...a: A) => Kind2 +export function fromEitherK( + F: FromEither1 +): , B>(f: (...a: A) => Either) => (...a: A) => Kind export function fromEitherK( F: FromEither ): , B>(f: (...a: A) => Either) => (...a: A) => HKT2 @@ -284,6 +296,10 @@ export function chainEitherK( F: FromEither2C, M: Chain2C ): (f: (a: A) => Either) => (ma: Kind2) => Kind2 +export function chainEitherK( + F: FromEither1, + M: Chain1 +): (f: (a: A) => Either) => (ma: Kind) => Kind export function chainEitherK( F: FromEither, M: Chain From a937b73ec676a3b7582968598edcb0dc19abea5f Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 10:55:37 +0200 Subject: [PATCH 039/162] ReadonlyArray: add fromPredicate --- CHANGELOG.md | 1 + src/ReadonlyArray.ts | 10 ++++++++++ test/ReadonlyArray.ts | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08719f23b..d3d96a525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ high state of flux, you're at risk of it changing without notice. - add `getIntersectionSemigroup` - add `getDifferenceMagma` - add `fromOption` + - add `fromPredicate` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 6a65aabca..69acdb1af 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -141,6 +141,16 @@ export const range = (start: number, end: number): ReadonlyArray => */ export const replicate = (n: number, a: A): ReadonlyArray => makeBy(n, () => a) +/** + * @category constructors + * @since 2.11.0 + */ +export function fromPredicate(refinement: Refinement): (a: A) => ReadonlyArray +export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray +export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray { + return (a) => (predicate(a) ? [a] : empty) +} + /** * @category constructors * @since 2.11.0 diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index dfcca389e..7dc5208fa 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -11,6 +11,7 @@ import * as O from '../src/Option' import * as Ord from '../src/Ord' import { Predicate } from '../src/Predicate' import * as _ from '../src/ReadonlyArray' +import { Refinement } from '../src/Refinement' import { separated } from '../src/Separated' import * as S from '../src/string' import * as T from '../src/Task' @@ -1378,4 +1379,23 @@ describe('ReadonlyArray', () => { ) }) }) + + describe('fromPredicate', () => { + it('can create an array from a Refinement', () => { + const refinement: Refinement = (a): a is string => typeof a === 'string' + U.deepStrictEqual(_.fromPredicate(refinement)('hello'), ['hello']) + U.deepStrictEqual(_.fromPredicate(refinement)(null), []) + }) + + it('can create an array from a Predicate', () => { + const predicate = (a: string) => a.length > 0 + U.deepStrictEqual(_.fromPredicate(predicate)('hi'), ['hi']) + U.deepStrictEqual(_.fromPredicate(predicate)(''), []) + }) + }) + + it('fromOption', () => { + U.deepStrictEqual(_.fromOption(O.some('hello')), ['hello']) + U.deepStrictEqual(_.fromOption(O.none), []) + }) }) From 6898b03722cddd8ed9c12469059d0f3007b8d7a4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 11:06:05 +0200 Subject: [PATCH 040/162] Either: add `chainOptionK` --- CHANGELOG.md | 2 + src/Either.ts | 278 ++++++++++++++++++++++------------------------ src/FromEither.ts | 7 ++ 3 files changed, 144 insertions(+), 143 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3d96a525..6f6111565 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - `Either` + - add `chainOptionK` - `function` - add `SK` (@cdimitroulas) - add `apply` diff --git a/src/Either.ts b/src/Either.ts index 4e2d6262e..a1b6b12ef 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -30,7 +30,14 @@ import { Eq } from './Eq' import { Extend2 } from './Extend' import { Filterable2C } from './Filterable' import { Foldable2 } from './Foldable' -import { FromEither2 } from './FromEither' +import { + chainOptionK as chainOptionK_, + filterOrElse as filterOrElse_, + FromEither2, + fromOption as fromOption_, + fromOptionK as fromOptionK_, + fromPredicate as fromPredicate_ +} from './FromEither' import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import { HKT } from './HKT' @@ -38,7 +45,6 @@ import * as _ from './internal' import { Monad2, Monad2C } from './Monad' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' -import { Option } from './Option' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' import { Refinement } from './Refinement' @@ -118,67 +124,6 @@ export const left: (e: E) => Either = _.left */ export const right: (a: A) => Either = _.right -/** - * @example - * import { fromOption, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * import { none, some } from 'fp-ts/Option' - * - * assert.deepStrictEqual( - * pipe( - * some(1), - * fromOption(() => 'error') - * ), - * right(1) - * ) - * assert.deepStrictEqual( - * pipe( - * none, - * fromOption(() => 'error') - * ), - * left('error') - * ) - * - * @category constructors - * @since 2.0.0 - */ -export const fromOption: (onNone: Lazy) => (ma: Option) => Either = (onNone) => (ma) => - ma._tag === 'None' ? left(onNone()) : right(ma.value) - -/** - * @example - * import { fromPredicate, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * - * assert.deepStrictEqual( - * pipe( - * 1, - * fromPredicate( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * right(1) - * ) - * assert.deepStrictEqual( - * pipe( - * -1, - * fromPredicate( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * left('error') - * ) - * - * @category constructors - * @since 2.0.0 - */ -export const fromPredicate: { - (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either -} = (predicate: Predicate, onFalse: (a: A) => E) => (a: A) => (predicate(a) ? right(a) : left(onFalse(a))) - // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- @@ -383,28 +328,6 @@ export const toUnion: (fa: Either) => E | A = // combinators // ------------------------------------------------------------------------------------- -/** - * @category combinators - * @since 2.10.0 - */ -export const fromOptionK = ( - onNone: Lazy -): (, B>(f: (...a: A) => Option) => (...a: A) => Either) => { - const from = fromOption(onNone) - return (f) => flow(f, from) -} - -/** - * @category combinators - * @since 2.10.0 - */ -export const chainOptionK = ( - onNone: Lazy -): ((f: (a: A) => Option) => (ma: Either) => Either) => { - const from = fromOptionK(onNone) - return (f) => chain(from(f)) -} - /** * Returns a `Right` if is a `Left` (and vice versa). * @@ -432,64 +355,6 @@ export const orElseW = (onLeft: (e: E1) => Either) => (ma: */ export const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either = orElseW -/** - * Less strict version of [`filterOrElse`](#filterOrElse). - * - * @category combinators - * @since 2.9.0 - */ -export const filterOrElseW: { - (refinement: Refinement, onFalse: (a: A) => E2): ( - ma: Either - ) => Either - (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either -} = (predicate: Predicate, onFalse: (a: A) => E2): ((ma: Either) => Either) => - chainW((a) => (predicate(a) ? right(a) : left(onFalse(a)))) - -/** - * @example - * import { filterOrElse as filterOrElse, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * - * assert.deepStrictEqual( - * pipe( - * right(1), - * filterOrElse( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * right(1) - * ) - * assert.deepStrictEqual( - * pipe( - * right(-1), - * filterOrElse( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * left('error') - * ) - * assert.deepStrictEqual( - * pipe( - * left('a'), - * filterOrElse( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * left('a') - * ) - * - * @category combinators - * @since 2.0.0 - */ -export const filterOrElse: { - (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either - (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either -} = filterOrElseW - // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- @@ -1203,6 +1068,133 @@ export const FromEither: FromEither2 = { fromEither: identity } +/** + * @example + * import * as E from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * import * as O from 'fp-ts/Option' + * + * assert.deepStrictEqual( + * pipe( + * O.some(1), + * E.fromOption(() => 'error') + * ), + * E.right(1) + * ) + * assert.deepStrictEqual( + * pipe( + * O.none, + * E.fromOption(() => 'error') + * ), + * E.left('error') + * ) + * + * @category constructors + * @since 2.0.0 + */ +export const fromOption = + /*#__PURE__*/ + fromOption_(FromEither) + +/** + * @example + * import * as E from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual( + * pipe( + * 1, + * E.fromPredicate((n) => n > 0) + * ), + * E.right(1) + * ) + * assert.deepStrictEqual( + * pipe( + * -1, + * E.fromPredicate((n) => n > 0) + * ), + * E.left(-1) + * ) + * + * @category constructors + * @since 2.0.0 + */ +export const fromPredicate = + /*#__PURE__*/ + fromPredicate_(FromEither) + +/** + * @category combinators + * @since 2.10.0 + */ +export const fromOptionK = + /*#__PURE__*/ + fromOptionK_(FromEither) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainOptionK = + /*#__PURE__*/ + chainOptionK_(FromEither, Chain) + +/** + * @example + * import * as E from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual( + * pipe( + * E.right(1), + * E.filterOrElse( + * (n) => n > 0, + * () => 'error' + * ) + * ), + * E.right(1) + * ) + * assert.deepStrictEqual( + * pipe( + * E.right(-1), + * E.filterOrElse( + * (n) => n > 0, + * () => 'error' + * ) + * ), + * E.left('error') + * ) + * assert.deepStrictEqual( + * pipe( + * E.left('a'), + * E.filterOrElse( + * (n) => n > 0, + * () => 'error' + * ) + * ), + * E.left('a') + * ) + * + * @category combinators + * @since 2.0.0 + */ +export const filterOrElse = + /*#__PURE__*/ + filterOrElse_(FromEither, Chain) + +/** + * Less strict version of [`filterOrElse`](#filterOrElse). + * + * @category combinators + * @since 2.9.0 + */ +export const filterOrElseW: { + (refinement: Refinement, onFalse: (a: A) => E2): ( + ma: Either + ) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either +} = filterOrElse + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- diff --git a/src/FromEither.ts b/src/FromEither.ts index bd236e204..8c8dbb7a0 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -87,6 +87,7 @@ export interface FromEither4 { // ------------------------------------------------------------------------------------- /** + * @category constructors * @since 2.10.0 */ export function fromOption( @@ -110,6 +111,7 @@ export function fromOption(F: FromEither): (onNone: Lazy) => (ma: } /** + * @category constructors * @since 2.10.0 */ export function fromPredicate( @@ -163,6 +165,7 @@ export function fromPredicate( // ------------------------------------------------------------------------------------- /** + * @category combinators * @since 2.10.0 */ export function fromOptionK( @@ -206,6 +209,7 @@ export function fromOptionK( } /** + * @category combinators * @since 2.10.0 */ export function chainOptionK( @@ -244,6 +248,7 @@ export function chainOptionK( } /** + * @category combinators * @since 2.10.0 */ export function fromEitherK( @@ -274,6 +279,7 @@ export function fromEitherK( } /** + * @category combinators * @since 2.10.0 */ export function chainEitherK( @@ -313,6 +319,7 @@ export function chainEitherK( } /** + * @category combinators * @since 2.10.0 */ export function filterOrElse( From 9734b0c3707f02e18c9fccb819f55e25129d0f60 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 11:13:11 +0200 Subject: [PATCH 041/162] - `Option` - add `FromEither` instance - add `fromEitherK` - add `chainEitherK` --- CHANGELOG.md | 4 ++++ src/Option.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f6111565..bb3aa93f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,10 @@ high state of flux, you're at risk of it changing without notice. - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` - add `getUnionSemigroup` + - `Option` + - add `FromEither` instance + - add `fromEitherK` + - add `chainEitherK` - `Ord` - add `trivial` instance - add `equals` diff --git a/src/Option.ts b/src/Option.ts index f83f24fe5..96d6ee064 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -29,6 +29,7 @@ import { Eq } from './Eq' import { Extend1 } from './Extend' import { Filterable1 } from './Filterable' import { Foldable1 } from './Foldable' +import { chainEitherK as chainEitherK_, FromEither1, fromEitherK as fromEitherK_ } from './FromEither' import { constNull, constUndefined, flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { HKT } from './HKT' @@ -1134,6 +1135,31 @@ export const MonadThrow: MonadThrow1 = { throwError } +/** + * @category instances + * @since 2.11.0 + */ +export const FromEither: FromEither1 = { + URI, + fromEither +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromEitherK = + /*#__PURE__*/ + fromEitherK_(FromEither) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainEitherK = + /*#__PURE__*/ + chainEitherK_(FromEither, Chain) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- From 696fc619f01b0df1a28a855e04a330dca234633a Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 11:31:37 +0200 Subject: [PATCH 042/162] - `Array` / `ReadonlyArray` - add `fromEither` - add `FromEither` instance - add `fromEitherK` --- CHANGELOG.md | 6 ++++++ src/Array.ts | 26 ++++++++++++++++++++++++++ src/ReadonlyArray.ts | 26 ++++++++++++++++++++++++++ test/Array.ts | 5 +++++ test/ReadonlyArray.ts | 5 +++++ 5 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb3aa93f4..d949ff437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,9 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - add `fromEither` + - add `FromEither` instance + - add `fromEitherK` - `Either` - add `chainOptionK` - `function` @@ -87,6 +90,9 @@ high state of flux, you're at risk of it changing without notice. - add `getDifferenceMagma` - add `fromOption` - add `fromPredicate` + - add `fromEither` + - add `FromEither` instance + - add `fromEitherK` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` diff --git a/src/Array.ts b/src/Array.ts index 7b0312b2c..f5b97da1e 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -15,6 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' +import { FromEither1, fromEitherK as fromEitherK_ } from './FromEither' import { identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' @@ -156,6 +157,14 @@ export function fromPredicate(predicate: Predicate): (a: A) => Array { */ export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? [] : [ma.value]) +/** + * Transforms an `Either` to a `ReadonlyArray`. + * + * @category constructors + * @since 2.11.0 + */ +export const fromEither = (e: Either): Array => (_.isLeft(e) ? [] : [e.right]) + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- @@ -2071,6 +2080,23 @@ export const filterE = /*#__PURE__*/ filterE_(Witherable) +/** + * @category instances + * @since 2.11.0 + */ +export const FromEither: FromEither1 = { + URI, + fromEither +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromEitherK = + /*#__PURE__*/ + fromEitherK_(FromEither) + // ------------------------------------------------------------------------------------- // unsafe // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 69acdb1af..1c7c7790c 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -15,6 +15,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable1 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' +import { FromEither1, fromEitherK as fromEitherK_ } from './FromEither' import { identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' @@ -157,6 +158,14 @@ export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArr */ export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? empty : [ma.value]) +/** + * Transforms an `Either` to a `ReadonlyArray`. + * + * @category constructors + * @since 2.11.0 + */ +export const fromEither = (e: Either): ReadonlyArray => (_.isLeft(e) ? empty : [e.right]) + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- @@ -2259,6 +2268,23 @@ export const filterE = /*#__PURE__*/ filterE_(Witherable) +/** + * @category instances + * @since 2.11.0 + */ +export const FromEither: FromEither1 = { + URI, + fromEither +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromEitherK = + /*#__PURE__*/ + fromEitherK_(FromEither) + // ------------------------------------------------------------------------------------- // unsafe // ------------------------------------------------------------------------------------- diff --git a/test/Array.ts b/test/Array.ts index dcf388a79..8734c7145 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -1162,4 +1162,9 @@ describe('Array', () => { U.deepStrictEqual(_.fromOption(O.some('hello')), ['hello']) U.deepStrictEqual(_.fromOption(O.none), []) }) + + it('fromEither', () => { + U.deepStrictEqual(_.fromEither(E.right(1)), [1]) + U.deepStrictEqual(_.fromEither(E.left('a')), []) + }) }) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index 7dc5208fa..27505671b 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1398,4 +1398,9 @@ describe('ReadonlyArray', () => { U.deepStrictEqual(_.fromOption(O.some('hello')), ['hello']) U.deepStrictEqual(_.fromOption(O.none), []) }) + + it('fromEither', () => { + U.deepStrictEqual(_.fromEither(E.right(1)), [1]) + U.strictEqual(_.fromEither(E.left('a')), _.empty) + }) }) From 498dba1e69bf055ecc2e68cc405f5a690eb414b0 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 12:04:07 +0200 Subject: [PATCH 043/162] - `Map` / `ReadonlyMap` - add `union` - add `intersection` - add `difference` - add `getUnionSemigroup` - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` --- CHANGELOG.md | 16 +++++ src/Map.ts | 93 ++++++++++++++++++++++++++++ src/ReadonlyMap.ts | 145 ++++++++++++++++++++++++++++++++++++++++++++ test/Map.ts | 73 ++++++++++++++++++++++ test/ReadonlyMap.ts | 77 +++++++++++++++++++++++ tslint.json | 2 +- 6 files changed, 405 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d949ff437..26eb71bf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,14 @@ high state of flux, you're at risk of it changing without notice. - `function` - add `SK` (@cdimitroulas) - add `apply` + - `Map` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` @@ -93,6 +101,14 @@ high state of flux, you're at risk of it changing without notice. - add `fromEither` - add `FromEither` instance - add `fromEitherK` + - `ReadonlyMap` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` diff --git a/src/Map.ts b/src/Map.ts index 8bf1d1dbc..3d1e871cb 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -595,6 +595,48 @@ declare module './HKT' { } } +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq, S: Semigroup): Semigroup> => { + const unionES = union(E, S) + return { + concat: (first, second) => unionES(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (E: Eq, S: Semigroup): Monoid> => ({ + concat: getUnionSemigroup(E, S).concat, + empty: new Map() +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (E: Eq, S: Semigroup): Semigroup> => { + const intersectionES = intersection(E, S) + return { + concat: (first, second) => intersectionES(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq) => (): Magma> => { + const differenceE = difference(E) + return { + concat: (first, second) => differenceE(second)(first) + } +} + /** * @category instances * @since 2.0.0 @@ -799,6 +841,57 @@ export const Filterable: Filterable2 = { partitionMap: _partitionMap } +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +const copy = (m: Map): Map => new Map(m) + +/** + * @since 2.11.0 + */ +export const union = (E: Eq, M: Magma): ((second: Map) => (first: Map) => Map) => { + const unionEM = RM.union(E, M) + return (second) => (first) => { + if (isEmpty(first)) { + return copy(second) + } + if (isEmpty(second)) { + return copy(first) + } + return unionEM(second)(first) as any + } +} + +/** + * @since 2.11.0 + */ +export const intersection = (E: Eq, M: Magma): ((second: Map) => (first: Map) => Map) => { + const intersectionEM = RM.intersection(E, M) + return (second) => (first) => { + if (isEmpty(first) || isEmpty(second)) { + return new Map() + } + return intersectionEM(second)(first) as any + } +} + +/** + * @since 2.11.0 + */ +export const difference = (E: Eq): ((_second: Map) => (first: Map) => Map) => { + const differenceE = RM.difference(E) + return (second: Map) => (first: Map) => { + if (isEmpty(first)) { + return copy(second) + } + if (isEmpty(second)) { + return copy(first) + } + return differenceE(second)(first) as any + } +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index f45abd7dd..7eddb8bb6 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -739,6 +739,48 @@ declare module './HKT' { } } +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq, S: Semigroup): Semigroup> => { + const unionES = union(E, S) + return { + concat: (first, second) => unionES(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionMonoid = (E: Eq, S: Semigroup): Monoid> => ({ + concat: getUnionSemigroup(E, S).concat, + empty +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getIntersectionSemigroup = (E: Eq, S: Semigroup): Semigroup> => { + const intersectionES = intersection(E, S) + return { + concat: (first, second) => intersectionES(second)(first) + } +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq) => (): Magma> => { + const differenceE = difference(E) + return { + concat: (first, second) => differenceE(second)(first) + } +} + /** * @category instances * @since 2.5.0 @@ -990,6 +1032,109 @@ export function getWitherable(O: Ord): Witherable2C & TraversableW } } +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const union = ( + E: Eq, + M: Magma +): ((second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap) => { + const lookupE = lookup(E) + return (second) => (first) => { + if (isEmpty(first)) { + return second + } + if (isEmpty(second)) { + return first + } + const out: Map = new Map() + const firstEntries = first.entries() + let e: Next + while (!(e = firstEntries.next()).done) { + const [k, a] = e.value + const oka = lookupE(k)(second) + if (_.isSome(oka)) { + out.set(k, M.concat(a, oka.value)) + } else { + out.set(k, a) + } + } + const secondEntries = second.entries() + while (!(e = secondEntries.next()).done) { + const [k, a] = e.value + const oka = lookupE(k)(out) + if (_.isNone(oka)) { + out.set(k, a) + } + } + return out + } +} + +/** + * @since 2.11.0 + */ +export const intersection = ( + E: Eq, + M: Magma +): ((second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap) => { + const lookupE = lookup(E) + return (second) => (first) => { + if (isEmpty(first) || isEmpty(second)) { + return empty + } + const out: Map = new Map() + const entries = first.entries() + let e: Next + while (!(e = entries.next()).done) { + const [k, a] = e.value + const oka = lookupE(k)(second) + if (_.isSome(oka)) { + out.set(k, M.concat(a, oka.value)) + } + } + return out + } +} + +/** + * @since 2.11.0 + */ +export const difference = ( + E: Eq +): ((_second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap) => { + const memberE = member(E) + return (second: ReadonlyMap) => (first: ReadonlyMap) => { + if (isEmpty(first)) { + return second + } + if (isEmpty(second)) { + return first + } + const out: Map = new Map() + const firstEntries = first.entries() + let e: Next + while (!(e = firstEntries.next()).done) { + const [k, a] = e.value + if (!memberE(k)(second)) { + out.set(k, a) + } + } + const secondEntries = second.entries() + while (!(e = secondEntries.next()).done) { + const [k, a] = e.value + if (!memberE(k)(first)) { + out.set(k, a) + } + } + return out + } +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/Map.ts b/test/Map.ts index 43b646418..5c0a58f17 100644 --- a/test/Map.ts +++ b/test/Map.ts @@ -1089,4 +1089,77 @@ describe('Map', () => { aa3 ) }) + + it('getUnionMonoid', () => { + const M = _.getUnionMonoid(eqUser, S.Semigroup) + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.deepStrictEqual(M.concat(x, M.empty), x) + U.deepStrictEqual(M.concat(M.empty, x), x) + U.deepStrictEqual(M.concat(x, new Map()), x) + U.deepStrictEqual(M.concat(new Map(), x), x) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1b2'], + [{ id: 'c' }, 'c1c2'], + [{ id: 'd' }, 'd2'] + ]) + ) + }) + + it('getIntersectionSemigroup', () => { + const M = _.getIntersectionSemigroup(eqUser, S.Semigroup) + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.deepStrictEqual(M.concat(x, new Map()), new Map()) + U.deepStrictEqual(M.concat(new Map(), x), new Map()) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'b' }, 'b1b2'], + [{ id: 'c' }, 'c1c2'] + ]) + ) + }) + + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma(eqUser)() + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.deepStrictEqual(M.concat(x, new Map()), x) + U.deepStrictEqual(M.concat(new Map(), x), x) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'd' }, 'd2'] + ]) + ) + }) }) diff --git a/test/ReadonlyMap.ts b/test/ReadonlyMap.ts index 69e18ea9e..ea6a24388 100644 --- a/test/ReadonlyMap.ts +++ b/test/ReadonlyMap.ts @@ -1157,4 +1157,81 @@ describe('ReadonlyMap', () => { aa3 ) }) + + it('getUnionMonoid', () => { + const M = _.getUnionMonoid(eqUser, S.Semigroup) + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.strictEqual(M.concat(x, M.empty), x) + U.strictEqual(M.concat(M.empty, x), x) + U.strictEqual(M.concat(x, new Map()), x) + U.strictEqual(M.concat(new Map(), x), x) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1b2'], + [{ id: 'c' }, 'c1c2'], + [{ id: 'd' }, 'd2'] + ]) + ) + }) + + it('getIntersectionSemigroup', () => { + const M = _.getIntersectionSemigroup(eqUser, S.Semigroup) + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.strictEqual(M.concat(x, _.empty), _.empty) + U.strictEqual(M.concat(_.empty, x), _.empty) + U.strictEqual(M.concat(x, new Map()), _.empty) + U.strictEqual(M.concat(new Map(), x), _.empty) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'b' }, 'b1b2'], + [{ id: 'c' }, 'c1c2'] + ]) + ) + }) + + it('getDifferenceMagma', () => { + const M = _.getDifferenceMagma(eqUser)() + const x = new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'b' }, 'b1'], + [{ id: 'c' }, 'c1'] + ]) + const y = new Map([ + [{ id: 'b' }, 'b2'], + [{ id: 'c' }, 'c2'], + [{ id: 'd' }, 'd2'] + ]) + U.strictEqual(M.concat(x, _.empty), x) + U.strictEqual(M.concat(_.empty, x), x) + U.strictEqual(M.concat(x, new Map()), x) + U.strictEqual(M.concat(new Map(), x), x) + U.deepStrictEqual( + M.concat(x, y), + new Map([ + [{ id: 'a' }, 'a1'], + [{ id: 'd' }, 'd2'] + ]) + ) + }) }) diff --git a/tslint.json b/tslint.json index 1e8759af7..c4e921dba 100644 --- a/tslint.json +++ b/tslint.json @@ -6,7 +6,7 @@ "variable-name": false, "quotemark": [true, "single", "jsx-double"], "ter-indent": false, - "strict-boolean-expressions": true, + "strict-boolean-expressions": false, "forin": true, "no-console": true, "array-type": [true, "generic"], From 8aec31fd5fb7114c270f08edc1f0c87fd89088ec Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 13:05:14 +0200 Subject: [PATCH 044/162] Either: fix fromPredicate example and update docs --- docs/modules/Array.ts.md | 81 ++++++- docs/modules/Either.ts.md | 46 ++-- docs/modules/FromEither.ts.md | 256 +++++++++++++---------- docs/modules/Map.ts.md | 80 +++++++ docs/modules/NonEmptyArray.ts.md | 22 ++ docs/modules/Option.ts.md | 33 +++ docs/modules/ReadonlyArray.ts.md | 102 +++++++++ docs/modules/ReadonlyMap.ts.md | 85 ++++++++ docs/modules/ReadonlyNonEmptyArray.ts.md | 24 +++ docs/modules/ReadonlyRecord.ts.md | 78 +++---- docs/modules/ReadonlySet.ts.md | 26 ++- docs/modules/Record.ts.md | 74 +++---- docs/modules/Set.ts.md | 26 ++- docs/modules/Witherable.ts.md | 47 +++++ src/Either.ts | 16 +- 15 files changed, 770 insertions(+), 226 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index ee1734497..8c89a840f 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -76,6 +76,7 @@ Added in v2.0.0 - [duplicate](#duplicate) - [flap](#flap) - [flatten](#flatten) + - [fromEitherK](#fromeitherk) - [intersection](#intersection) - [intersperse](#intersperse) - [lefts](#lefts) @@ -99,6 +100,7 @@ Added in v2.0.0 - [constructors](#constructors) - [append](#append) - [appendW](#appendw) + - [fromEither](#fromeither) - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [makeBy](#makeby) @@ -138,6 +140,7 @@ Added in v2.0.0 - [FilterableWithIndex](#filterablewithindex-1) - [Foldable](#foldable-1) - [FoldableWithIndex](#foldablewithindex-1) + - [FromEither](#fromeither) - [Functor](#functor-1) - [FunctorWithIndex](#functorwithindex-1) - [Monad](#monad-1) @@ -148,11 +151,15 @@ Added in v2.0.0 - [URI (type alias)](#uri-type-alias) - [Unfoldable](#unfoldable-1) - [Witherable](#witherable-1) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getOrd](#getord) - [getSemigroup](#getsemigroup) - [getShow](#getshow) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [~~array~~](#array) - [unsafe](#unsafe) - [unsafeDeleteAt](#unsafedeleteat) @@ -863,6 +870,16 @@ export declare const flatten: (mma: A[][]) => A[] Added in v2.5.0 +## fromEitherK + +**Signature** + +```ts +export declare const fromEitherK: (f: (...a: A) => Either) => (...a: A) => B[] +``` + +Added in v2.11.0 + ## intersection Creates an array of unique values that are included in all given arrays using a `Eq` for equality @@ -1359,12 +1376,24 @@ export declare const appendW: (end: B) => (init: A[]) => NEA.NonEmptyArray Added in v2.11.0 +## fromEither + +Transforms an `Either` to a `ReadonlyArray`. + +**Signature** + +```ts +export declare const fromEither: (e: Either) => A[] +``` + +Added in v2.11.0 + ## fromOption **Signature** ```ts -export declare const fromOption: (ma: Option) => A[] +export declare const fromOption: (ma: Option) => readonly A[] ``` Added in v2.11.0 @@ -1946,6 +1975,16 @@ export declare const FoldableWithIndex: FoldableWithIndex1<'Array', number> Added in v2.7.0 +## FromEither + +**Signature** + +```ts +export declare const FromEither: FromEither1<'Array'> +``` + +Added in v2.11.0 + ## Functor **Signature** @@ -2046,6 +2085,16 @@ export declare const Witherable: Witherable1<'Array'> Added in v2.7.0 +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => Magma +``` + +Added in v2.11.0 + ## getEq Derives an `Eq` over the `Array` of a given element type from the `Eq` of that type. The derived `Eq` defines two @@ -2071,6 +2120,16 @@ assert.strictEqual(E.equals(['a'], []), false) Added in v2.0.0 +## getIntersectionSemigroup + +**Signature** + +```ts +export declare const getIntersectionSemigroup: (E: Eq) => Semigroup +``` + +Added in v2.11.0 + ## getMonoid Returns a `Monoid` for `Array` @@ -2139,6 +2198,26 @@ export declare const getShow: (S: Show) => Show Added in v2.0.0 +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (E: Eq) => Monoid +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq) => Semigroup +``` + +Added in v2.11.0 + ## ~~array~~ Use small, specific instances instead. diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 0e317bcfc..9774afb54 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -502,7 +502,7 @@ export declare const chainOptionK: ( ) => (f: (a: A) => Option) => (ma: Either) => Either ``` -Added in v2.10.0 +Added in v2.11.0 ## duplicate @@ -522,7 +522,7 @@ Added in v2.0.0 ```ts export declare const filterOrElse: { - (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either + (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either } ``` @@ -530,38 +530,38 @@ export declare const filterOrElse: { **Example** ```ts -import { filterOrElse as filterOrElse, left, right } from 'fp-ts/Either' +import * as E from 'fp-ts/Either' import { pipe } from 'fp-ts/function' assert.deepStrictEqual( pipe( - right(1), - filterOrElse( + E.right(1), + E.filterOrElse( (n) => n > 0, () => 'error' ) ), - right(1) + E.right(1) ) assert.deepStrictEqual( pipe( - right(-1), - filterOrElse( + E.right(-1), + E.filterOrElse( (n) => n > 0, () => 'error' ) ), - left('error') + E.left('error') ) assert.deepStrictEqual( pipe( - left('a'), - filterOrElse( + E.left('a'), + E.filterOrElse( (n) => n > 0, () => 'error' ) ), - left('a') + E.left('a') ) ``` @@ -625,9 +625,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const fromOptionK: ( - onNone: Lazy -) => (f: (...a: A) => Option) => (...a: A) => Either +export declare const fromOptionK: (onNone: Lazy) => (f: (...a: A) => Option) => (...a: A) => Either ``` Added in v2.10.0 @@ -683,23 +681,23 @@ export declare const fromOption: (onNone: Lazy) => (ma: Option) => E **Example** ```ts -import { fromOption, left, right } from 'fp-ts/Either' +import * as E from 'fp-ts/Either' import { pipe } from 'fp-ts/function' -import { none, some } from 'fp-ts/Option' +import * as O from 'fp-ts/Option' assert.deepStrictEqual( pipe( - some(1), - fromOption(() => 'error') + O.some(1), + E.fromOption(() => 'error') ), - right(1) + E.right(1) ) assert.deepStrictEqual( pipe( - none, - fromOption(() => 'error') + O.none, + E.fromOption(() => 'error') ), - left('error') + E.left('error') ) ``` @@ -711,7 +709,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { - (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either + (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either } ``` diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index 8950a8e7c..33525349d 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -14,107 +14,27 @@ Added in v2.10.0

Table of contents

+- [combinators](#combinators) + - [chainEitherK](#chaineitherk) + - [chainOptionK](#chainoptionk) + - [filterOrElse](#filterorelse) + - [fromEitherK](#fromeitherk) + - [fromOptionK](#fromoptionk) +- [constructors](#constructors) + - [fromOption](#fromoption) + - [fromPredicate](#frompredicate) - [type classes](#type-classes) - [FromEither (interface)](#fromeither-interface) + - [FromEither1 (interface)](#fromeither1-interface) - [FromEither2 (interface)](#fromeither2-interface) - [FromEither2C (interface)](#fromeither2c-interface) - [FromEither3 (interface)](#fromeither3-interface) - [FromEither3C (interface)](#fromeither3c-interface) - [FromEither4 (interface)](#fromeither4-interface) -- [utils](#utils) - - [chainEitherK](#chaineitherk) - - [chainOptionK](#chainoptionk) - - [filterOrElse](#filterorelse) - - [fromEitherK](#fromeitherk) - - [fromOption](#fromoption) - - [fromOptionK](#fromoptionk) - - [fromPredicate](#frompredicate) --- -# type classes - -## FromEither (interface) - -**Signature** - -```ts -export interface FromEither { - readonly URI: F - readonly fromEither: (e: Either) => HKT2 -} -``` - -Added in v2.10.0 - -## FromEither2 (interface) - -**Signature** - -```ts -export interface FromEither2 { - readonly URI: F - readonly fromEither: (e: Either) => Kind2 -} -``` - -Added in v2.10.0 - -## FromEither2C (interface) - -**Signature** - -```ts -export interface FromEither2C { - readonly URI: F - readonly _E: E - readonly fromEither:
(e: Either) => Kind2 -} -``` - -Added in v2.10.0 - -## FromEither3 (interface) - -**Signature** - -```ts -export interface FromEither3 { - readonly URI: F - readonly fromEither: (e: Either) => Kind3 -} -``` - -Added in v2.10.0 - -## FromEither3C (interface) - -**Signature** - -```ts -export interface FromEither3C { - readonly URI: F - readonly _E: E - readonly fromEither: (e: Either) => Kind3 -} -``` - -Added in v2.10.0 - -## FromEither4 (interface) - -**Signature** - -```ts -export interface FromEither4 { - readonly URI: F - readonly fromEither: (e: Either) => Kind4 -} -``` - -Added in v2.10.0 - -# utils +# combinators ## chainEitherK @@ -141,6 +61,10 @@ export declare function chainEitherK( F: FromEither2C, M: Chain2C ): (f: (a: A) => Either) => (ma: Kind2) => Kind2 +export declare function chainEitherK( + F: FromEither1, + M: Chain1 +): (f: (a: A) => Either) => (ma: Kind) => Kind export declare function chainEitherK( F: FromEither, M: Chain @@ -257,6 +181,9 @@ export declare function fromEitherK( export declare function fromEitherK( F: FromEither2C ): , B>(f: (...a: A) => Either) => (...a: A) => Kind2 +export declare function fromEitherK( + F: FromEither1 +): , B>(f: (...a: A) => Either) => (...a: A) => Kind export declare function fromEitherK( F: FromEither ): , B>(f: (...a: A) => Either) => (...a: A) => HKT2 @@ -264,31 +191,6 @@ export declare function fromEitherK( Added in v2.10.0 -## fromOption - -**Signature** - -```ts -export declare function fromOption( - F: FromEither4 -): (onNone: Lazy) => (ma: Option) => Kind4 -export declare function fromOption( - F: FromEither3 -): (onNone: Lazy) => (ma: Option) => Kind3 -export declare function fromOption( - F: FromEither3C -): (onNone: Lazy) => (ma: Option) => Kind3 -export declare function fromOption( - F: FromEither2 -): (onNone: Lazy) => (ma: Option) => Kind2 -export declare function fromOption( - F: FromEither2C -): (onNone: Lazy) => (ma: Option) => Kind2 -export declare function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 -``` - -Added in v2.10.0 - ## fromOptionK **Signature** @@ -326,6 +228,33 @@ export declare function fromOptionK( Added in v2.10.0 +# constructors + +## fromOption + +**Signature** + +```ts +export declare function fromOption( + F: FromEither4 +): (onNone: Lazy) => (ma: Option) => Kind4 +export declare function fromOption( + F: FromEither3 +): (onNone: Lazy) => (ma: Option) => Kind3 +export declare function fromOption( + F: FromEither3C +): (onNone: Lazy) => (ma: Option) => Kind3 +export declare function fromOption( + F: FromEither2 +): (onNone: Lazy) => (ma: Option) => Kind2 +export declare function fromOption( + F: FromEither2C +): (onNone: Lazy) => (ma: Option) => Kind2 +export declare function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 +``` + +Added in v2.10.0 + ## fromPredicate **Signature** @@ -370,3 +299,98 @@ export declare function fromPredicate( ``` Added in v2.10.0 + +# type classes + +## FromEither (interface) + +**Signature** + +```ts +export interface FromEither { + readonly URI: F + readonly fromEither: (e: Either) => HKT2 +} +``` + +Added in v2.10.0 + +## FromEither1 (interface) + +**Signature** + +```ts +export interface FromEither1 { + readonly URI: F + readonly fromEither: (e: Either) => Kind +} +``` + +Added in v2.11.0 + +## FromEither2 (interface) + +**Signature** + +```ts +export interface FromEither2 { + readonly URI: F + readonly fromEither: (e: Either) => Kind2 +} +``` + +Added in v2.10.0 + +## FromEither2C (interface) + +**Signature** + +```ts +export interface FromEither2C { + readonly URI: F + readonly _E: E + readonly fromEither: (e: Either) => Kind2 +} +``` + +Added in v2.10.0 + +## FromEither3 (interface) + +**Signature** + +```ts +export interface FromEither3 { + readonly URI: F + readonly fromEither: (e: Either) => Kind3 +} +``` + +Added in v2.10.0 + +## FromEither3C (interface) + +**Signature** + +```ts +export interface FromEither3C { + readonly URI: F + readonly _E: E + readonly fromEither: (e: Either) => Kind3 +} +``` + +Added in v2.10.0 + +## FromEither4 (interface) + +**Signature** + +```ts +export interface FromEither4 { + readonly URI: F + readonly fromEither: (e: Either) => Kind4 +} +``` + +Added in v2.10.0 diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index 300303cc4..e2457bf8c 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -41,17 +41,23 @@ Added in v2.0.0 - [Functor](#functor-1) - [URI](#uri) - [URI (type alias)](#uri-type-alias) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getFilterableWithIndex](#getfilterablewithindex) - [getFoldableWithIndex](#getfoldablewithindex) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getShow](#getshow) - [getTraversableWithIndex](#gettraversablewithindex) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [getWitherable](#getwitherable) - [~~map\_~~](#map_) - [utils](#utils) - [collect](#collect) + - [difference](#difference) - [elem](#elem) + - [intersection](#intersection) - [isEmpty](#isempty) - [isSubmap](#issubmap) - [keys](#keys) @@ -64,6 +70,7 @@ Added in v2.0.0 - [size](#size) - [toArray](#toarray) - [toUnfoldable](#tounfoldable) + - [union](#union) - [updateAt](#updateat) - [values](#values) - [~~empty~~](#empty) @@ -345,6 +352,16 @@ export type URI = typeof URI Added in v2.0.0 +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => () => Magma> +``` + +Added in v2.11.0 + ## getEq **Signature** @@ -375,6 +392,16 @@ export declare const getFoldableWithIndex: (O: Ord) => FoldableWithIndex2C Added in v2.10.0 +## getIntersectionSemigroup + +**Signature** + +```ts +export declare const getIntersectionSemigroup: (E: Eq, S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getMonoid Gets `Monoid` instance for Maps given `Semigroup` instance for their values @@ -407,6 +434,26 @@ export declare const getTraversableWithIndex: (O: Ord) => TraversableWithI Added in v2.10.0 +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (E: Eq, S: Semigroup) => Monoid> +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq, S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getWitherable **Signature** @@ -441,6 +488,16 @@ export declare function collect(O: Ord): (f: (k: K, a: A) => B) => ( Added in v2.0.0 +## difference + +**Signature** + +```ts +export declare const difference: (E: Eq) => (_second: Map) => (first: Map) => Map +``` + +Added in v2.11.0 + ## elem Test whether or not a value is a member of a map @@ -453,6 +510,19 @@ export declare const elem: (E: Eq) => { (a: A): (m: Map) => boole Added in v2.0.0 +## intersection + +**Signature** + +```ts +export declare const intersection: ( + E: Eq, + M: Magma +) => (second: Map) => (first: Map) => Map +``` + +Added in v2.11.0 + ## isEmpty Test whether or not a map is empty @@ -610,6 +680,16 @@ export declare function toUnfoldable(ord: Ord, U: Unfoldable): (d Added in v2.0.0 +## union + +**Signature** + +```ts +export declare const union: (E: Eq, M: Magma) => (second: Map) => (first: Map) => Map +``` + +Added in v2.11.0 + ## updateAt **Signature** diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 5a7b3598e..d983864fa 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -58,6 +58,7 @@ Added in v2.0.0 - [flatten](#flatten) - [foldMap](#foldmap) - [foldMapWithIndex](#foldmapwithindex) + - [getUnionSemigroup](#getunionsemigroup) - [group](#group) - [groupBy](#groupby) - [groupSort](#groupsort) @@ -68,6 +69,7 @@ Added in v2.0.0 - [reverse](#reverse) - [sort](#sort) - [splitAt](#splitat) + - [union](#union) - [unzip](#unzip) - [updateAt](#updateat) - [zip](#zip) @@ -437,6 +439,16 @@ export declare const foldMapWithIndex: ( Added in v2.0.0 +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq) => Se.Semigroup> +``` + +Added in v2.11.0 + ## group Group equal, consecutive elements of an array into non empty arrays. @@ -605,6 +617,16 @@ export declare const splitAt: (n: number) => (as: NonEmptyArray) => [NonEm Added in v2.10.0 +## union + +**Signature** + +```ts +export declare const union: (E: Eq) => (second: NonEmptyArray) => (first: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + ## unzip **Signature** diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 51a76870d..e0f366f61 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -61,10 +61,12 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [chainEitherK](#chaineitherk) - [chainFirst](#chainfirst) - [duplicate](#duplicate) - [flap](#flap) - [flatten](#flatten) + - [fromEitherK](#fromeitherk) - [~~mapNullable~~](#mapnullable) - [constructors](#constructors) - [fromEither](#fromeither) @@ -93,6 +95,7 @@ Added in v2.0.0 - [Extend](#extend-1) - [Filterable](#filterable-1) - [Foldable](#foldable-1) + - [FromEither](#fromeither) - [Functor](#functor-1) - [Monad](#monad-1) - [MonadThrow](#monadthrow-1) @@ -456,6 +459,16 @@ export declare const apSecond: (second: Option) => (first: Option) = Added in v2.0.0 +## chainEitherK + +**Signature** + +```ts +export declare const chainEitherK: (f: (a: A) => Either) => (ma: Option) => Option +``` + +Added in v2.11.0 + ## chainFirst Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -507,6 +520,16 @@ export declare const flatten: (mma: Option>) => Option Added in v2.0.0 +## fromEitherK + +**Signature** + +```ts +export declare const fromEitherK: (f: (...a: A) => Either) => (...a: A) => Option +``` + +Added in v2.11.0 + ## ~~mapNullable~~ **Signature** @@ -887,6 +910,16 @@ export declare const Foldable: Foldable1<'Option'> Added in v2.7.0 +## FromEither + +**Signature** + +```ts +export declare const FromEither: FromEither1<'Option'> +``` + +Added in v2.11.0 + ## Functor **Signature** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 980bc9723..382a3debf 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -75,6 +75,7 @@ Added in v2.5.0 - [duplicate](#duplicate) - [flap](#flap) - [flatten](#flatten) + - [fromEitherK](#fromeitherk) - [intersection](#intersection) - [intersperse](#intersperse) - [prependAll](#prependall) @@ -97,6 +98,9 @@ Added in v2.5.0 - [constructors](#constructors) - [append](#append) - [appendW](#appendw) + - [fromEither](#fromeither) + - [fromOption](#fromoption) + - [fromPredicate](#frompredicate) - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) @@ -125,6 +129,7 @@ Added in v2.5.0 - [FilterableWithIndex](#filterablewithindex-1) - [Foldable](#foldable-1) - [FoldableWithIndex](#foldablewithindex-1) + - [FromEither](#fromeither) - [Functor](#functor-1) - [FunctorWithIndex](#functorwithindex-1) - [Monad](#monad-1) @@ -135,11 +140,15 @@ Added in v2.5.0 - [URI (type alias)](#uri-type-alias) - [Unfoldable](#unfoldable-1) - [Witherable](#witherable-1) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getOrd](#getord) - [getSemigroup](#getsemigroup) - [getShow](#getshow) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [~~readonlyArray~~](#readonlyarray) - [interop](#interop) - [fromArray](#fromarray) @@ -871,6 +880,16 @@ export declare const flatten: (mma: readonly (readonly A[])[]) => readonly A[ Added in v2.5.0 +## fromEitherK + +**Signature** + +```ts +export declare const fromEitherK: (f: (...a: A) => Either) => (...a: A) => readonly B[] +``` + +Added in v2.11.0 + ## intersection Creates an array of unique values that are included in all given arrays using a `Eq` for equality @@ -1365,6 +1384,39 @@ export declare const appendW: (end: B) => (init: readonly A[]) => RNEA.Rea Added in v2.11.0 +## fromEither + +Transforms an `Either` to a `ReadonlyArray`. + +**Signature** + +```ts +export declare const fromEither: (e: Either) => readonly A[] +``` + +Added in v2.11.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (ma: Option) => readonly A[] +``` + +Added in v2.11.0 + +## fromPredicate + +**Signature** + +```ts +export declare function fromPredicate(refinement: Refinement): (a: A) => ReadonlyArray +export declare function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray +``` + +Added in v2.11.0 + ## makeBy Return a `ReadonlyArray` of length `n` with element `i` initialized with `f(i)`. @@ -1707,6 +1759,16 @@ export declare const FoldableWithIndex: FoldableWithIndex1<'ReadonlyArray', numb Added in v2.7.0 +## FromEither + +**Signature** + +```ts +export declare const FromEither: FromEither1<'ReadonlyArray'> +``` + +Added in v2.11.0 + ## Functor **Signature** @@ -1807,6 +1869,16 @@ export declare const Witherable: Witherable1<'ReadonlyArray'> Added in v2.7.0 +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => Magma +``` + +Added in v2.11.0 + ## getEq Derives an `Eq` over the `ReadonlyArray` of a given element type from the `Eq` of that type. The derived `Eq` defines two @@ -1832,6 +1904,16 @@ assert.strictEqual(E.equals(['a'], []), false) Added in v2.5.0 +## getIntersectionSemigroup + +**Signature** + +```ts +export declare const getIntersectionSemigroup: (E: Eq) => Semigroup +``` + +Added in v2.11.0 + ## getMonoid Returns a `Monoid` for `ReadonlyArray`. @@ -1900,6 +1982,26 @@ export declare const getShow: (S: Show) => Show Added in v2.5.0 +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (E: Eq) => Monoid +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq) => Semigroup +``` + +Added in v2.11.0 + ## ~~readonlyArray~~ Use small, specific instances instead. diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index ae3c53313..5948d197f 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -46,21 +46,27 @@ Added in v2.5.0 - [Functor](#functor-1) - [URI](#uri) - [URI (type alias)](#uri-type-alias) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getFilterableWithIndex](#getfilterablewithindex) - [getFoldable](#getfoldable) - [getFoldableWithIndex](#getfoldablewithindex) - [getFunctorWithIndex](#getfunctorwithindex) + - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) - [getShow](#getshow) - [getTraversable](#gettraversable) - [getTraversableWithIndex](#gettraversablewithindex) + - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [getWitherable](#getwitherable) - [~~readonlyMap~~](#readonlymap) - [utils](#utils) - [collect](#collect) + - [difference](#difference) - [elem](#elem) - [empty](#empty) + - [intersection](#intersection) - [isEmpty](#isempty) - [isSubmap](#issubmap) - [keys](#keys) @@ -71,6 +77,7 @@ Added in v2.5.0 - [pop](#pop) - [size](#size) - [toReadonlyArray](#toreadonlyarray) + - [union](#union) - [updateAt](#updateat) - [values](#values) @@ -414,6 +421,16 @@ export type URI = typeof URI Added in v2.5.0 +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => () => Magma> +``` + +Added in v2.11.0 + ## getEq **Signature** @@ -464,6 +481,16 @@ export declare const getFunctorWithIndex: () => FunctorWithIndex2C<'R Added in v2.10.0 +## getIntersectionSemigroup + +**Signature** + +```ts +export declare const getIntersectionSemigroup: (E: Eq, S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getMonoid Gets `Monoid` instance for Maps given `Semigroup` instance for their values @@ -506,6 +533,26 @@ export declare const getTraversableWithIndex: (O: Ord) => TraversableWithI Added in v2.10.0 +## getUnionMonoid + +**Signature** + +```ts +export declare const getUnionMonoid: (E: Eq, S: Semigroup) => Monoid> +``` + +Added in v2.11.0 + +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq, S: Semigroup) => Semigroup> +``` + +Added in v2.11.0 + ## getWitherable **Signature** @@ -542,6 +589,18 @@ export declare function collect( Added in v2.5.0 +## difference + +**Signature** + +```ts +export declare const difference: ( + E: Eq +) => (_second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap +``` + +Added in v2.11.0 + ## elem Test whether or not a value is a member of a map @@ -569,6 +628,19 @@ export declare const empty: ReadonlyMap Added in v2.5.0 +## intersection + +**Signature** + +```ts +export declare const intersection: ( + E: Eq, + M: Magma +) => (second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap +``` + +Added in v2.11.0 + ## isEmpty Test whether or not a map is empty @@ -711,6 +783,19 @@ export declare const toReadonlyArray: (O: Ord) => (m: ReadonlyMap Added in v2.5.0 +## union + +**Signature** + +```ts +export declare const union: ( + E: Eq, + M: Magma +) => (second: ReadonlyMap) => (first: ReadonlyMap) => ReadonlyMap +``` + +Added in v2.11.0 + ## updateAt **Signature** diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 2529eae69..2c18cf30d 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -64,6 +64,7 @@ Added in v2.5.0 - [duplicate](#duplicate) - [flap](#flap) - [flatten](#flatten) + - [getUnionSemigroup](#getunionsemigroup) - [group](#group) - [groupBy](#groupby) - [groupSort](#groupsort) @@ -73,6 +74,7 @@ Added in v2.5.0 - [reverse](#reverse) - [sort](#sort) - [splitAt](#splitat) + - [union](#union) - [unzip](#unzip) - [updateAt](#updateat) - [zip](#zip) @@ -513,6 +515,16 @@ export declare const flatten: (mma: ReadonlyNonEmptyArray(E: Eq) => Se.Semigroup> +``` + +Added in v2.11.0 + ## group Group equal, consecutive elements of a `ReadonlyArray` into `ReadonlyNonEmptyArray`s. @@ -678,6 +690,18 @@ export declare const splitAt: ( Added in v2.10.0 +## union + +**Signature** + +```ts +export declare const union: ( + E: Eq +) => (second: ReadonlyNonEmptyArray) => (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## unzip **Signature** diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 83e421bbe..37a3a656e 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -29,10 +29,13 @@ Added in v2.5.0 - [wither](#wither) - [combinators](#combinators) - [deleteAt](#deleteat) + - [difference](#difference) - [filterMapWithIndex](#filtermapwithindex) - [flap](#flap) + - [intersection](#intersection) - [map](#map) - [mapWithIndex](#mapwithindex) + - [union](#union) - [upsertAt](#upsertat) - [~~insertAt~~](#insertat) - [constructors](#constructors) @@ -72,7 +75,6 @@ Added in v2.5.0 - [ReadonlyRecord (type alias)](#readonlyrecord-type-alias) - [utils](#utils) - [collect](#collect) - - [difference](#difference) - [elem](#elem) - [empty](#empty) - [every](#every) @@ -81,7 +83,6 @@ Added in v2.5.0 - [fromFoldable](#fromfoldable) - [fromFoldableMap](#fromfoldablemap) - [has](#has) - - [intersection](#intersection) - [isEmpty](#isempty) - [isSubrecord](#issubrecord) - [keys](#keys) @@ -98,7 +99,6 @@ Added in v2.5.0 - [toReadonlyArray](#toreadonlyarray) - [traverse](#traverse) - [traverseWithIndex](#traversewithindex) - - [union](#union) - [updateAt](#updateat) - [~~hasOwnProperty (function)~~](#hasownproperty-function) @@ -263,6 +263,18 @@ export declare function deleteAt( Added in v2.5.0 +## difference + +**Signature** + +```ts +export declare const difference: ( + second: Readonly> +) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 + ## filterMapWithIndex **Signature** @@ -287,6 +299,18 @@ export declare const flap: (a: A) => (fab: Readonly( + M: Magma +) => (second: Readonly>) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 + ## map Map a `ReadonlyRecord` passing the values to the iterating function. @@ -313,6 +337,18 @@ export declare function mapWithIndex( Added in v2.5.0 +## union + +**Signature** + +```ts +export declare const union: ( + M: Magma +) => (second: Readonly>) => (first: Readonly>) => Readonly> +``` + +Added in v2.11.0 + ## upsertAt Insert or replace a key/value pair in a `ReadonlyRecord`. @@ -717,18 +753,6 @@ assert.deepStrictEqual(collect(Ord)((key, val) => ({ key: key, value: val }))(x) Added in v2.5.0 -## difference - -**Signature** - -```ts -export declare const difference: ( - second: Readonly> -) => (first: Readonly>) => Readonly> -``` - -Added in v2.11.0 - ## elem **Signature** @@ -901,18 +925,6 @@ export declare const has: (k: string, r: Readonly( - M: Magma -) => (second: Readonly>) => (first: Readonly>) => Readonly> -``` - -Added in v2.11.0 - ## isEmpty Test whether a `ReadonlyRecord` is empty. @@ -1186,18 +1198,6 @@ export declare function traverseWithIndex( Added in v2.5.0 -## union - -**Signature** - -```ts -export declare const union: ( - M: Magma -) => (second: Readonly>) => (first: Readonly>) => Readonly> -``` - -Added in v2.11.0 - ## updateAt **Signature** diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 15dc0bad0..d40e7b063 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -32,10 +32,12 @@ Added in v2.5.0 - [destructors](#destructors) - [toSet](#toset) - [instances](#instances) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getIntersectionSemigroup](#getintersectionsemigroup) - [getShow](#getshow) - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [utils](#utils) - [elem](#elem) - [empty](#empty) @@ -269,6 +271,16 @@ Added in v2.5.0 # instances +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => Magma> +``` + +Added in v2.11.0 + ## getEq **Signature** @@ -284,7 +296,7 @@ Added in v2.5.0 **Signature** ```ts -export declare function getIntersectionSemigroup(E: Eq): Semigroup> +export declare const getIntersectionSemigroup: (E: Eq) => Semigroup> ``` Added in v2.5.0 @@ -304,11 +316,21 @@ Added in v2.5.0 **Signature** ```ts -export declare function getUnionMonoid(E: Eq): Monoid> +export declare const getUnionMonoid: (E: Eq) => Monoid> ``` Added in v2.5.0 +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq) => Semigroup> +``` + +Added in v2.11.0 + # utils ## elem diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 5080d44bb..ab90b5a6f 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -28,7 +28,10 @@ Added in v2.0.0 - [wilt](#wilt) - [wither](#wither) - [combinators](#combinators) + - [difference](#difference) - [flap](#flap) + - [intersection](#intersection) + - [union](#union) - [upsertAt](#upsertat) - [instances](#instances) - [Compactable](#compactable-1) @@ -59,7 +62,6 @@ Added in v2.0.0 - [utils](#utils) - [collect](#collect) - [deleteAt](#deleteat) - - [difference](#difference) - [elem](#elem) - [every](#every) - [filterMapWithIndex](#filtermapwithindex) @@ -68,7 +70,6 @@ Added in v2.0.0 - [fromFoldable](#fromfoldable) - [fromFoldableMap](#fromfoldablemap) - [has](#has) - - [intersection](#intersection) - [isEmpty](#isempty) - [isSubrecord](#issubrecord) - [keys](#keys) @@ -89,7 +90,6 @@ Added in v2.0.0 - [toUnfoldable](#tounfoldable) - [traverse](#traverse) - [traverseWithIndex](#traversewithindex) - - [union](#union) - [updateAt](#updateat) - [~~empty~~](#empty) - [~~hasOwnProperty (function)~~](#hasownproperty-function) @@ -234,6 +234,16 @@ Added in v2.6.5 # combinators +## difference + +**Signature** + +```ts +export declare const difference: (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + ## flap Derivable from `Functor`. @@ -246,6 +256,30 @@ export declare const flap: (a: A) => (fab: Record B>) => Added in v2.10.0 +## intersection + +**Signature** + +```ts +export declare const intersection: ( + M: Magma +) => (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + +## union + +**Signature** + +```ts +export declare const union: ( + M: Magma +) => (second: Record) => (first: Record) => Record +``` + +Added in v2.11.0 + ## upsertAt Insert or replace a key/value pair in a `Record`. @@ -583,16 +617,6 @@ export declare function deleteAt( Added in v2.0.0 -## difference - -**Signature** - -```ts -export declare const difference: (second: Record) => (first: Record) => Record -``` - -Added in v2.11.0 - ## elem **Signature** @@ -762,18 +786,6 @@ export declare const has: (k: string, r: Record) = Added in v2.10.0 -## intersection - -**Signature** - -```ts -export declare const intersection: ( - M: Magma -) => (second: Record) => (first: Record) => Record -``` - -Added in v2.11.0 - ## isEmpty Test whether a `Record` is empty. @@ -1084,18 +1096,6 @@ export declare function traverseWithIndex( Added in v2.0.0 -## union - -**Signature** - -```ts -export declare const union: ( - M: Magma -) => (second: Record) => (first: Record) => Record -``` - -Added in v2.11.0 - ## updateAt **Signature** diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 81308fe7c..1ac5b6d1e 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -28,10 +28,12 @@ Added in v2.0.0 - [fromArray](#fromarray) - [singleton](#singleton) - [instances](#instances) + - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getIntersectionSemigroup](#getintersectionsemigroup) - [getShow](#getshow) - [getUnionMonoid](#getunionmonoid) + - [getUnionSemigroup](#getunionsemigroup) - [utils](#utils) - [elem](#elem) - [empty](#empty) @@ -235,6 +237,16 @@ Added in v2.0.0 # instances +## getDifferenceMagma + +**Signature** + +```ts +export declare const getDifferenceMagma: (E: Eq) => Magma> +``` + +Added in v2.11.0 + ## getEq **Signature** @@ -250,7 +262,7 @@ Added in v2.0.0 **Signature** ```ts -export declare function getIntersectionSemigroup(E: Eq): Semigroup> +export declare const getIntersectionSemigroup: (E: Eq) => Semigroup> ``` Added in v2.0.0 @@ -270,11 +282,21 @@ Added in v2.0.0 **Signature** ```ts -export declare function getUnionMonoid(E: Eq): Monoid> +export declare const getUnionMonoid: (E: Eq) => Monoid> ``` Added in v2.0.0 +## getUnionSemigroup + +**Signature** + +```ts +export declare const getUnionSemigroup: (E: Eq) => Semigroup> +``` + +Added in v2.11.0 + # utils ## elem diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index b77e34196..6e79573e1 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -16,6 +16,9 @@ Added in v2.0.0

Table of contents

+- [defaults](#defaults) + - [wiltDefault](#wiltdefault) + - [witherDefault](#witherdefault) - [type classes](#type-classes) - [Witherable (interface)](#witherable-interface) - [Witherable1 (interface)](#witherable1-interface) @@ -48,6 +51,50 @@ Added in v2.0.0 --- +# defaults + +## wiltDefault + +Return a `wilt` implementation from `Traversable` and `Compactable`. + +**Signature** + +```ts +export declare function wiltDefault( + T: Traversable2C, + C: Compactable2 +): Witherable2C['wilt'] +export declare function wiltDefault( + T: Traversable2, + C: Compactable2C +): Witherable2C['wilt'] +export declare function wiltDefault(T: Traversable1, C: Compactable1): Witherable1['wilt'] +export declare function wiltDefault(T: Traversable, C: Compactable): Witherable['wilt'] +``` + +Added in v3.0.0 + +## witherDefault + +Return a `wither` implementation from `Traversable` and `Compactable`. + +**Signature** + +```ts +export declare function witherDefault( + T: Traversable2C, + C: Compactable2 +): Witherable2C['wither'] +export declare function witherDefault( + T: Traversable2, + C: Compactable2C +): Witherable2C['wither'] +export declare function witherDefault(T: Traversable1, C: Compactable1): Witherable1['wither'] +export declare function witherDefault(T: Traversable, C: Compactable): Witherable['wither'] +``` + +Added in v3.0.0 + # type classes ## Witherable (interface) diff --git a/src/Either.ts b/src/Either.ts index a1b6b12ef..0534f15ff 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1098,22 +1098,28 @@ export const fromOption = /** * @example - * import * as E from 'fp-ts/Either' + * import { fromPredicate, left, right } from 'fp-ts/Either' * import { pipe } from 'fp-ts/function' * * assert.deepStrictEqual( * pipe( * 1, - * E.fromPredicate((n) => n > 0) + * fromPredicate( + * (n) => n > 0, + * () => 'error' + * ) * ), - * E.right(1) + * right(1) * ) * assert.deepStrictEqual( * pipe( * -1, - * E.fromPredicate((n) => n > 0) + * fromPredicate( + * (n) => n > 0, + * () => 'error' + * ) * ), - * E.left(-1) + * left('error') * ) * * @category constructors From 0e64713955f02799a4216710dbf18927f28d5fe8 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 11:51:26 +0100 Subject: [PATCH 045/162] Add orElseFirst and orLeft to IOEither --- src/EitherT.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ src/IOEither.ts | 24 +++++++++++++++ test/IOEither.ts | 13 +++++++++ 3 files changed, 113 insertions(+) diff --git a/src/EitherT.ts b/src/EitherT.ts index 72b82a6cc..ee50350e4 100644 --- a/src/EitherT.ts +++ b/src/EitherT.ts @@ -466,6 +466,82 @@ export function orElse( return (onLeft) => (ma) => M.chain(ma, (e) => (E.isLeft(e) ? onLeft(e.left) : M.of(e))) } +/** + * @since 2.11.0 + */ +export function orElseFirst( + M: Monad3 +): ( + onLeft: (e: E) => Kind3> +) =>
(ma: Kind3>) => Kind3> +export function orElseFirst( + M: Monad3C +): ( + onLeft: (e: E) => Kind3> +) => (ma: Kind3>) => Kind3> +export function orElseFirst( + M: Monad2 +): ( + onLeft: (e: E) => Kind2> +) => (ma: Kind2>) => Kind2> +export function orElseFirst( + M: Monad2C +): ( + onLeft: (e: E) => Kind2> +) => (ma: Kind2>) => Kind2> +export function orElseFirst( + M: Monad1 +): (onLeft: (e: E) => Kind>) => (ma: Kind>) => Kind> +export function orElseFirst( + M: Monad +): (onLeft: (e: E) => HKT>) => (ma: HKT>) => HKT> +export function orElseFirst( + M: Monad +): (onLeft: (e: E) => HKT>) => (ma: HKT>) => HKT> { + const orElseM = orElse(M) + return (onLeft) => orElseM((e) => M.map(onLeft(e), (eb) => (E.isLeft(eb) ? eb : E.left(e)))) +} + +/** + * @since 2.11.0 + */ +export function orLeft( + M: Monad3 +): ( + onLeft: (e: E1) => Kind3 +) => (fa: Kind3>) => Kind3> +export function orLeft( + M: Monad3C +): ( + onLeft: (e: E1) => Kind3 +) => (fa: Kind3>) => Kind3> +export function orLeft( + M: Monad2 +): ( + onLeft: (e: E1) => Kind2 +) => (fa: Kind2>) => Kind2> +export function orLeft( + M: Monad2C +): (onLeft: (e: E1) => Kind2) => (fa: Kind2>) => Kind2> +export function orLeft( + M: Monad1 +): (onLeft: (e: E1) => Kind) => (fa: Kind>) => Kind> +export function orLeft( + M: Monad +): (onLeft: (e: E1) => HKT) => (fa: HKT>) => HKT> +export function orLeft( + M: Monad +): (onLeft: (e: E1) => HKT) => (fa: HKT>) => HKT> { + return (onLeft) => (ma) => + M.chain( + ma, + E.match( + (e) => M.map(onLeft(e), E.left), + (a) => M.of(E.right(a)) + ) + ) +} + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- diff --git a/src/IOEither.ts b/src/IOEither.ts index 7d6c4a760..f0a6b80aa 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -241,6 +241,30 @@ export const orElseW: ( onLeft: (e: E1) => IOEither ) => (ma: IOEither) => IOEither = orElse as any +/** + * @category combinators + * @since 2.11.0 + */ +export const orElseFirst: (onLeft: (e: E) => IOEither) => (ma: IOEither) => IOEither = + /*#__PURE__*/ + ET.orElseFirst(I.Monad) + +/** + * @category combinators + * @since 2.11.0 + */ +export const orElseFirstW: ( + onLeft: (e: E1) => IOEither +) => (ma: IOEither) => IOEither = orElseFirst as any + +/** + * @category combinators + * @since 2.11.0 + */ +export const orLeft: (onLeft: (e: E1) => IO) => (fa: IOEither) => IOEither = + /*#__PURE__*/ + ET.orLeft(I.Monad) + /** * @category combinators * @since 2.0.0 diff --git a/test/IOEither.ts b/test/IOEither.ts index 2ab221ad1..ca011b82e 100644 --- a/test/IOEither.ts +++ b/test/IOEither.ts @@ -251,6 +251,19 @@ describe('IOEither', () => { U.deepStrictEqual(_.orElse(() => _.right(2))(_.right(1))(), E.right(1)) }) + it('orElseFirst', () => { + const f = _.orElseFirst((e: string) => (e.length <= 1 ? _.right(true) : _.left(e + '!'))) + U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)(), E.left('a')) + U.deepStrictEqual(pipe(_.left('aa'), f)(), E.left('aa!')) + }) + + it('orLeft', () => { + const f = _.orLeft((e: string) => I.of(e + '!')) + U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)(), E.left('a!')) + }) + it('tryCatch', () => { U.deepStrictEqual(_.tryCatch(() => 1, E.toError)(), E.right(1)) U.deepStrictEqual( From 3f310c7ace72f90a30075f65f33f0699bdb7a1bf Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 13:14:53 +0100 Subject: [PATCH 046/162] Add docs --- docs/modules/EitherT.ts.md | 70 +++++++++++++++++++++++++++++++++++++ docs/modules/IOEither.ts.md | 35 +++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/docs/modules/EitherT.ts.md b/docs/modules/EitherT.ts.md index f64dd0081..e1fcfcf1d 100644 --- a/docs/modules/EitherT.ts.md +++ b/docs/modules/EitherT.ts.md @@ -30,6 +30,8 @@ Added in v2.0.0 - [match](#match) - [matchE](#matche) - [orElse](#orelse) + - [orElseFirst](#orelsefirst) + - [orLeft](#orleft) - [right](#right) - [rightF](#rightf) - [swap](#swap) @@ -477,6 +479,74 @@ export declare function orElse( Added in v2.10.0 +## orElseFirst + +**Signature** + +```ts +export declare function orElseFirst( + M: Monad3 +): ( + onLeft: (e: E) => Kind3> +) => (ma: Kind3>) => Kind3> +export declare function orElseFirst( + M: Monad3C +): ( + onLeft: (e: E) => Kind3> +) => (ma: Kind3>) => Kind3> +export declare function orElseFirst( + M: Monad2 +): ( + onLeft: (e: E) => Kind2> +) => (ma: Kind2>) => Kind2> +export declare function orElseFirst( + M: Monad2C +): ( + onLeft: (e: E) => Kind2> +) => (ma: Kind2>) => Kind2> +export declare function orElseFirst( + M: Monad1 +): (onLeft: (e: E) => Kind>) => (ma: Kind>) => Kind> +export declare function orElseFirst( + M: Monad +): (onLeft: (e: E) => HKT>) => (ma: HKT>) => HKT> +``` + +Added in v2.11.0 + +## orLeft + +**Signature** + +```ts +export declare function orLeft( + M: Monad3 +): ( + onLeft: (e: E1) => Kind3 +) => (fa: Kind3>) => Kind3> +export declare function orLeft( + M: Monad3C +): ( + onLeft: (e: E1) => Kind3 +) => (fa: Kind3>) => Kind3> +export declare function orLeft( + M: Monad2 +): ( + onLeft: (e: E1) => Kind2 +) => (fa: Kind2>) => Kind2> +export declare function orLeft( + M: Monad2C +): (onLeft: (e: E1) => Kind2) => (fa: Kind2>) => Kind2> +export declare function orLeft( + M: Monad1 +): (onLeft: (e: E1) => Kind) => (fa: Kind>) => Kind> +export declare function orLeft( + M: Monad +): (onLeft: (e: E1) => HKT) => (fa: HKT>) => HKT> +``` + +Added in v2.11.0 + ## right **Signature** diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 792fcfe95..527f87516 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -51,7 +51,10 @@ Added in v2.0.0 - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) - [orElse](#orelse) + - [orElseFirst](#orelsefirst) + - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) + - [orLeft](#orleft) - [swap](#swap) - [constructors](#constructors) - [fromEither](#fromeither) @@ -483,6 +486,28 @@ export declare const orElse: (onLeft: (e: E1) => IOEither) => Added in v2.0.0 +## orElseFirst + +**Signature** + +```ts +export declare const orElseFirst: (onLeft: (e: E) => IOEither) => (ma: IOEither) => IOEither +``` + +Added in v2.11.0 + +## orElseFirstW + +**Signature** + +```ts +export declare const orElseFirstW: ( + onLeft: (e: E1) => IOEither +) => (ma: IOEither) => IOEither +``` + +Added in v2.11.0 + ## orElseW Less strict version of [`orElse`](#orElse). @@ -497,6 +522,16 @@ export declare const orElseW: ( Added in v2.10.0 +## orLeft + +**Signature** + +```ts +export declare const orLeft: (onLeft: (e: E1) => I.IO) => (fa: IOEither) => IOEither +``` + +Added in v2.11.0 + ## swap **Signature** From 34dd0bacdb36031834ff91a651d5d768b27a1720 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 13:31:30 +0100 Subject: [PATCH 047/162] Add coverage --- dtslint/ts3.5/IOEither.ts | 50 +++++++++++++++++++++++++++++++++++++++ test/IOEither.ts | 11 +++++++++ 2 files changed, 61 insertions(+) diff --git a/dtslint/ts3.5/IOEither.ts b/dtslint/ts3.5/IOEither.ts index 88825ebfe..8831b5260 100644 --- a/dtslint/ts3.5/IOEither.ts +++ b/dtslint/ts3.5/IOEither.ts @@ -13,6 +13,56 @@ pipe( _.getOrElseW(() => IO.of(null)) ) +// +// orElse +// + +// $ExpectType IOEither +pipe( + _.left('a'), + _.orElse((a) => _.left(a.length)) +) + +// +// orElseW +// + +// $ExpectType IOEither +pipe( + _.left('a'), + _.orElseW((a) => _.right(a.length)) +) + +// +// orElseFirst +// + +// $ExpectType IOEither +pipe( + _.left('a'), + _.orElseFirst((a) => _.right(a.length)) +) + +// +// orElseFirstW +// + +// $ExpectType IOEither +pipe( + _.left('a'), + _.orElseFirstW((a) => _.left(a.length)) +) + +// +// orLeft +// + +// $ExpectType IOEither +pipe( + _.left('a'), + _.orLeft((a) => IO.of(a.length)) +) + // // chainW // diff --git a/test/IOEither.ts b/test/IOEither.ts index ca011b82e..0211c4cae 100644 --- a/test/IOEither.ts +++ b/test/IOEither.ts @@ -251,6 +251,10 @@ describe('IOEither', () => { U.deepStrictEqual(_.orElse(() => _.right(2))(_.right(1))(), E.right(1)) }) + it('orElseW', () => { + U.deepStrictEqual(_.orElseW(() => _.right(2))(_.right(1))(), E.right(1)) + }) + it('orElseFirst', () => { const f = _.orElseFirst((e: string) => (e.length <= 1 ? _.right(true) : _.left(e + '!'))) U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1)) @@ -258,6 +262,13 @@ describe('IOEither', () => { U.deepStrictEqual(pipe(_.left('aa'), f)(), E.left('aa!')) }) + it('orElseFirstW', () => { + const f = _.orElseFirstW((e: string) => (e.length <= 1 ? _.right(true) : _.left(e + '!'))) + U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)(), E.left('a')) + U.deepStrictEqual(pipe(_.left('aa'), f)(), E.left('aa!')) + }) + it('orLeft', () => { const f = _.orLeft((e: string) => I.of(e + '!')) U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1)) From eb13ce631f95dedf9ec5c3ff42bc1fa6c94bf059 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 13:46:31 +0100 Subject: [PATCH 048/162] Add to TaskEither --- docs/modules/TaskEither.ts.md | 37 ++++++++++++++++++++++++++ dtslint/ts3.5/TaskEither.ts | 50 +++++++++++++++++++++++++++++++++++ src/TaskEither.ts | 24 +++++++++++++++++ test/TaskEither.ts | 37 ++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index c91b3f840..a32427802 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -61,7 +61,10 @@ Added in v2.0.0 - [fromOptionK](#fromoptionk) - [fromTaskK](#fromtaskk) - [orElse](#orelse) + - [orElseFirst](#orelsefirst) + - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) + - [orLeft](#orleft) - [swap](#swap) - [constructors](#constructors) - [fromEither](#fromeither) @@ -630,6 +633,30 @@ test() Added in v2.0.0 +## orElseFirst + +**Signature** + +```ts +export declare const orElseFirst: ( + onLeft: (e: E) => TaskEither +) => (ma: TaskEither) => TaskEither +``` + +Added in v2.11.0 + +## orElseFirstW + +**Signature** + +```ts +export declare const orElseFirstW: ( + onLeft: (e: E1) => TaskEither +) => (ma: TaskEither) => TaskEither +``` + +Added in v2.11.0 + ## orElseW Less strict version of [`orElse`](#orElse). @@ -644,6 +671,16 @@ export declare const orElseW: ( Added in v2.10.0 +## orLeft + +**Signature** + +```ts +export declare const orLeft: (onLeft: (e: E1) => T.Task) => (fa: TaskEither) => TaskEither +``` + +Added in v2.11.0 + ## swap **Signature** diff --git a/dtslint/ts3.5/TaskEither.ts b/dtslint/ts3.5/TaskEither.ts index b1678307c..84d375344 100644 --- a/dtslint/ts3.5/TaskEither.ts +++ b/dtslint/ts3.5/TaskEither.ts @@ -15,6 +15,56 @@ pipe( _.getOrElseW(() => T.of(null)) ) +// +// orElse +// + +// $ExpectType TaskEither +pipe( + _.left('a'), + _.orElse((a) => _.left(a.length)) +) + +// +// orElseW +// + +// $ExpectType TaskEither +pipe( + _.left('a'), + _.orElseW((a) => _.right(a.length)) +) + +// +// orElseFirst +// + +// $ExpectType TaskEither +pipe( + _.left('a'), + _.orElseFirst((a) => _.right(a.length)) +) + +// +// orElseFirstW +// + +// $ExpectType TaskEither +pipe( + _.left('a'), + _.orElseFirstW((a) => _.left(a.length)) +) + +// +// orLeft +// + +// $ExpectType TaskEither +pipe( + _.left('a'), + _.orLeft((a) => T.of(a.length)) +) + // // chainW // diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 2d30d64dd..27276bf58 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -319,6 +319,30 @@ export const orElseW: ( onLeft: (e: E1) => TaskEither ) => (ma: TaskEither) => TaskEither = orElse as any +/** + * @category combinators + * @since 2.11.0 + */ +export const orElseFirst: (onLeft: (e: E) => TaskEither) => (ma: TaskEither) => TaskEither = + /*#__PURE__*/ + ET.orElseFirst(T.Monad) + +/** + * @category combinators + * @since 2.11.0 + */ +export const orElseFirstW: ( + onLeft: (e: E1) => TaskEither +) => (ma: TaskEither) => TaskEither = orElseFirst as any + +/** + * @category combinators + * @since 2.11.0 + */ +export const orLeft: (onLeft: (e: E1) => Task) => (fa: TaskEither) => TaskEither = + /*#__PURE__*/ + ET.orLeft(T.Monad) + /** * @category combinators * @since 2.0.0 diff --git a/test/TaskEither.ts b/test/TaskEither.ts index 9a619f7a9..e98d22f98 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -392,6 +392,43 @@ describe('TaskEither', () => { ) }) + it('orElseW', async () => { + U.deepStrictEqual( + await pipe( + _.left('foo'), + _.orElse((l) => _.right(l.length)) + )(), + E.right(3) + ) + U.deepStrictEqual( + await pipe( + _.right(1), + _.orElse(() => _.right(2)) + )(), + E.right(1) + ) + }) + + it('orElseFirst', async () => { + const f = _.orElseFirst((e: string) => (e.length <= 1 ? _.right(true) : _.left(e + '!'))) + U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)(), E.left('a')) + U.deepStrictEqual(await pipe(_.left('aa'), f)(), E.left('aa!')) + }) + + it('orElseFirstW', async () => { + const f = _.orElseFirstW((e: string) => (e.length <= 1 ? _.right(true) : _.left(e + '!'))) + U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)(), E.left('a')) + U.deepStrictEqual(await pipe(_.left('aa'), f)(), E.left('aa!')) + }) + + it('orLeft', async () => { + const f = _.orLeft((e: string) => T.of(e + '!')) + U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)(), E.left('a!')) + }) + it('swap', async () => { U.deepStrictEqual(await _.swap(_.right(1))(), E.left(1)) U.deepStrictEqual(await _.swap(_.left('a'))(), E.right('a')) From 78c66c783120f279b885cdebc81a849ea3dd1c78 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 14:07:41 +0100 Subject: [PATCH 049/162] Add to ReaderEither --- docs/modules/ReaderEither.ts.md | 39 +++++++++++++++++++++++++ dtslint/ts3.5/ReaderEither.ts | 50 +++++++++++++++++++++++++++++++++ src/ReaderEither.ts | 28 ++++++++++++++++++ test/ReaderEither.ts | 25 +++++++++++++++++ 4 files changed, 142 insertions(+) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 4c52e8c8d..46e89bc01 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -47,7 +47,10 @@ Added in v2.0.0 - [fromOptionK](#fromoptionk) - [fromReaderK](#fromreaderk) - [orElse](#orelse) + - [orElseFirst](#orelsefirst) + - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) + - [orLeft](#orleft) - [swap](#swap) - [~~local~~](#local) - [constructors](#constructors) @@ -488,6 +491,30 @@ export declare const orElse: ( Added in v2.0.0 +## orElseFirst + +**Signature** + +```ts +export declare const orElseFirst: ( + onLeft: (e: E) => ReaderEither +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v3.0.0 + +## orElseFirstW + +**Signature** + +```ts +export declare const orElseFirstW: ( + onLeft: (e: E1) => ReaderEither +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v3.0.0 + ## orElseW Less strict version of [`orElse`](#orElse). @@ -502,6 +529,18 @@ export declare const orElseW: ( Added in v2.10.0 +## orLeft + +**Signature** + +```ts +export declare const orLeft: ( + onLeft: (e: E1) => R.Reader +) => (fa: ReaderEither) => ReaderEither +``` + +Added in v3.0.0 + ## swap **Signature** diff --git a/dtslint/ts3.5/ReaderEither.ts b/dtslint/ts3.5/ReaderEither.ts index 88760a704..c00e7a012 100644 --- a/dtslint/ts3.5/ReaderEither.ts +++ b/dtslint/ts3.5/ReaderEither.ts @@ -13,6 +13,56 @@ pipe( _.getOrElseW(() => R.of<{ b: number }, null>(null)) ) +// +// orElse +// + +// $ExpectType ReaderEither<{ a: string; }, number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElse((a) => _.left(a.length)) +) + +// +// orElseW +// + +// $ExpectType ReaderEither<{ b: number; } & { a: string; }, never, string | number> +pipe( + _.left<{ a: string }, string, string>('a'), + _.orElseW((a) => _.right<{ b: number }, never, string | number>(a.length)) +) + +// +// orElseFirst +// + +// $ExpectType ReaderEither<{ a: string; }, string, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElseFirst((a) => _.right(a.length)) +) + +// +// orElseFirstW +// + +// $ExpectType ReaderEither<{ a: string; }, string | number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElseFirstW((a) => _.left(a.length)) +) + +// +// orLeft +// + +// $ExpectType ReaderEither<{ a: string; }, number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orLeft((a) => R.of(a.length)) +) + // // chainW // diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 6d88cd235..0e0548ffe 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -231,6 +231,34 @@ export const orElseW: ( onLeft: (e: E1) => ReaderEither ) => (ma: ReaderEither) => ReaderEither = orElse as any +/** + * @category combinators + * @since 3.0.0 + */ +export const orElseFirst: ( + onLeft: (e: E) => ReaderEither +) => (ma: ReaderEither) => ReaderEither = + /*#__PURE__*/ + ET.orElseFirst(R.Monad) + +/** + * @category combinators + * @since 3.0.0 + */ +export const orElseFirstW: ( + onLeft: (e: E1) => ReaderEither +) => (ma: ReaderEither) => ReaderEither = orElseFirst as any + +/** + * @category combinators + * @since 3.0.0 + */ +export const orLeft: ( + onLeft: (e: E1) => Reader +) => (fa: ReaderEither) => ReaderEither = + /*#__PURE__*/ + ET.orLeft(R.Monad) + /** * @category combinators * @since 2.0.0 diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index edb0853b6..c4685a797 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -134,6 +134,31 @@ describe('ReaderEither', () => { U.deepStrictEqual(orElse(_.right(1))({}), E.right(1)) }) + it('orElseW', () => { + const orElse = _.orElse((s: string) => (s.length > 2 ? _.right(1) : _.left(2))) + U.deepStrictEqual(orElse(_.right(1))({}), E.right(1)) + }) + + it('orElseFirst', () => { + const f = _.orElseFirst((s: string) => (s.length <= 1 ? _.right(true) : _.left(s + '!'))) + U.deepStrictEqual(pipe(_.right(1), f)({}), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)({}), E.left('a')) + U.deepStrictEqual(pipe(_.left('aa'), f)({}), E.left('aa!')) + }) + + it('orElseFirstW', () => { + const f = _.orElseFirstW((s: string) => (s.length <= 1 ? _.right(true) : _.left(s + '!'))) + U.deepStrictEqual(pipe(_.right(1), f)({}), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)({}), E.left('a')) + U.deepStrictEqual(pipe(_.left('aa'), f)({}), E.left('aa!')) + }) + + it('orLeft', () => { + const f = _.orLeft((s: string) => R.of(s + '!')) + U.deepStrictEqual(pipe(_.right(1), f)({}), E.right(1)) + U.deepStrictEqual(pipe(_.left('a'), f)({}), E.left('a!')) + }) + describe('getSemigroup', () => { it('concat', () => { // tslint:disable-next-line: deprecation From 45b7f653619c5997dd2e685652f7371d8a143392 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 14:23:56 +0100 Subject: [PATCH 050/162] Add to ReaderTaskEither --- docs/modules/ReaderTaskEither.ts.md | 39 ++++++++++++++++++++++ dtslint/ts3.5/ReaderTaskEither.ts | 50 +++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 28 ++++++++++++++++ test/ReaderTaskEither.ts | 37 +++++++++++++++++++++ 4 files changed, 154 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 9e22c2ebd..4e1595ce1 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -59,7 +59,10 @@ Added in v2.0.0 - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) - [orElse](#orelse) + - [orElseFirst](#orelsefirst) + - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) + - [orLeft](#orleft) - [swap](#swap) - [~~local~~](#local) - [constructors](#constructors) @@ -673,6 +676,30 @@ export declare const orElse: ( Added in v2.0.0 +## orElseFirst + +**Signature** + +```ts +export declare const orElseFirst: ( + onLeft: (e: E) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v3.0.0 + +## orElseFirstW + +**Signature** + +```ts +export declare const orElseFirstW: ( + onLeft: (e: E1) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v3.0.0 + ## orElseW Less strict version of [`orElse`](#orElse). @@ -687,6 +714,18 @@ export declare const orElseW: ( Added in v2.10.0 +## orLeft + +**Signature** + +```ts +export declare const orLeft: ( + onLeft: (e: E1) => RT.ReaderTask +) => (fa: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v3.0.0 + ## swap **Signature** diff --git a/dtslint/ts3.5/ReaderTaskEither.ts b/dtslint/ts3.5/ReaderTaskEither.ts index 17877cd28..3d47ab58b 100644 --- a/dtslint/ts3.5/ReaderTaskEither.ts +++ b/dtslint/ts3.5/ReaderTaskEither.ts @@ -15,6 +15,56 @@ pipe( _.getOrElseW(() => RT.of<{ b: number }, null>(null)) ) +// +// orElse +// + +// $ExpectType ReaderTaskEither<{ a: string; }, number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElse((a) => _.left(a.length)) +) + +// +// orElseW +// + +// $ExpectType ReaderTaskEither<{ b: number; } & { a: string; }, never, string | number> +pipe( + _.left<{ a: string }, string, string>('a'), + _.orElseW((a) => _.right<{ b: number }, never, string | number>(a.length)) +) + +// +// orElseFirst +// + +// $ExpectType ReaderTaskEither<{ a: string; }, string, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElseFirst((a) => _.right(a.length)) +) + +// +// orElseFirstW +// + +// $ExpectType ReaderTaskEither<{ a: string; }, string | number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orElseFirstW((a) => _.left(a.length)) +) + +// +// orLeft +// + +// $ExpectType ReaderTaskEither<{ a: string; }, number, never> +pipe( + _.left<{ a: string }, string, never>('a'), + _.orLeft((a) => RT.of(a.length)) +) + // // chainW // diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 5dadee89f..c76ceddf2 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -335,6 +335,34 @@ export const orElseW: ( onLeft: (e: E1) => ReaderTaskEither ) => (ma: ReaderTaskEither) => ReaderTaskEither = orElse as any +/** + * @category combinators + * @since 3.0.0 + */ +export const orElseFirst: ( + onLeft: (e: E) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = + /*#__PURE__*/ + ET.orElseFirst(RT.Monad) + +/** + * @category combinators + * @since 3.0.0 + */ +export const orElseFirstW: ( + onLeft: (e: E1) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = orElseFirst as any + +/** + * @category combinators + * @since 3.0.0 + */ +export const orLeft: ( + onLeft: (e: E1) => ReaderTask +) => (fa: ReaderTaskEither) => ReaderTaskEither = + /*#__PURE__*/ + ET.orLeft(RT.Monad) + /** * @category combinators * @since 2.0.0 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 1267bf984..b99ea1335 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -250,6 +250,43 @@ describe('ReaderTaskEither', () => { ) }) + it('orElseW', async () => { + U.deepStrictEqual( + await pipe( + _.right(1), + _.orElseW((s: string) => _.right(s.length)) + )({})(), + E.right(1) + ) + U.deepStrictEqual( + await pipe( + _.left('error'), + _.orElseW((s) => _.right(s.length)) + )({})(), + E.right(5) + ) + }) + + it('orElseFirst', async () => { + const f = _.orElseFirst((s: string) => (s.length <= 1 ? _.right(true) : _.left(s + '!'))) + U.deepStrictEqual(await pipe(_.right(1), f)({})(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)({})(), E.left('a')) + U.deepStrictEqual(await pipe(_.left('aa'), f)({})(), E.left('aa!')) + }) + + it('orElseFirstW', async () => { + const f = _.orElseFirstW((s: string) => (s.length <= 1 ? _.right(true) : _.left(s + '!'))) + U.deepStrictEqual(await pipe(_.right(1), f)({})(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)({})(), E.left('a')) + U.deepStrictEqual(await pipe(_.left('aa'), f)({})(), E.left('aa!')) + }) + + it('orLeft', async () => { + const f = _.orLeft((s: string) => RT.of(s + '!')) + U.deepStrictEqual(await pipe(_.right(1), f)({})(), E.right(1)) + U.deepStrictEqual(await pipe(_.left('a'), f)({})(), E.left('a!')) + }) + describe('MonadIO', () => { it('fromIO', async () => { U.deepStrictEqual(await _.fromIO(() => 1)({})(), E.right(1)) From 11232db96e75b916af2add36988c826674482810 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 14:29:16 +0100 Subject: [PATCH 051/162] Fix versions --- docs/modules/ReaderEither.ts.md | 6 +++--- docs/modules/ReaderTaskEither.ts.md | 6 +++--- src/ReaderEither.ts | 6 +++--- src/ReaderTaskEither.ts | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 46e89bc01..3e611a4f3 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -501,7 +501,7 @@ export declare const orElseFirst: ( ) => (ma: ReaderEither) => ReaderEither ``` -Added in v3.0.0 +Added in v2.11.0 ## orElseFirstW @@ -513,7 +513,7 @@ export declare const orElseFirstW: ( ) => (ma: ReaderEither) => ReaderEither ``` -Added in v3.0.0 +Added in v2.11.0 ## orElseW @@ -539,7 +539,7 @@ export declare const orLeft: ( ) => (fa: ReaderEither) => ReaderEither ``` -Added in v3.0.0 +Added in v2.11.0 ## swap diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 4e1595ce1..73f8ed73d 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -686,7 +686,7 @@ export declare const orElseFirst: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v3.0.0 +Added in v2.11.0 ## orElseFirstW @@ -698,7 +698,7 @@ export declare const orElseFirstW: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v3.0.0 +Added in v2.11.0 ## orElseW @@ -724,7 +724,7 @@ export declare const orLeft: ( ) => (fa: ReaderTaskEither) => ReaderTaskEither ``` -Added in v3.0.0 +Added in v2.11.0 ## swap diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 0e0548ffe..2f17074dc 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -233,7 +233,7 @@ export const orElseW: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orElseFirst: ( onLeft: (e: E) => ReaderEither @@ -243,7 +243,7 @@ export const orElseFirst: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orElseFirstW: ( onLeft: (e: E1) => ReaderEither @@ -251,7 +251,7 @@ export const orElseFirstW: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orLeft: ( onLeft: (e: E1) => Reader diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c76ceddf2..24a73590d 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -337,7 +337,7 @@ export const orElseW: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orElseFirst: ( onLeft: (e: E) => ReaderTaskEither @@ -347,7 +347,7 @@ export const orElseFirst: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orElseFirstW: ( onLeft: (e: E1) => ReaderTaskEither @@ -355,7 +355,7 @@ export const orElseFirstW: ( /** * @category combinators - * @since 3.0.0 + * @since 2.11.0 */ export const orLeft: ( onLeft: (e: E1) => ReaderTask From 63838965f05cf249faffa0556e0ac7371d7ea431 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 1 Apr 2021 14:36:20 +0100 Subject: [PATCH 052/162] Correct tests --- test/ReaderEither.ts | 2 +- test/TaskEither.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index c4685a797..fe882ba63 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -135,7 +135,7 @@ describe('ReaderEither', () => { }) it('orElseW', () => { - const orElse = _.orElse((s: string) => (s.length > 2 ? _.right(1) : _.left(2))) + const orElse = _.orElseW((s: string) => (s.length > 2 ? _.right(1) : _.left(2))) U.deepStrictEqual(orElse(_.right(1))({}), E.right(1)) }) diff --git a/test/TaskEither.ts b/test/TaskEither.ts index e98d22f98..73079aeb0 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -396,14 +396,14 @@ describe('TaskEither', () => { U.deepStrictEqual( await pipe( _.left('foo'), - _.orElse((l) => _.right(l.length)) + _.orElseW((l) => _.right(l.length)) )(), E.right(3) ) U.deepStrictEqual( await pipe( _.right(1), - _.orElse(() => _.right(2)) + _.orElseW(() => _.right(2)) )(), E.right(1) ) From ba806877d02977cfa0ccde82bd8ce8f18ec11dc2 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:05:35 +0200 Subject: [PATCH 053/162] number: add MagmaSub --- CHANGELOG.md | 2 ++ src/number.ts | 9 +++++++++ test/number.ts | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26eb71bf3..a0d46f850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,8 @@ high state of flux, you're at risk of it changing without notice. - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` - add `getUnionSemigroup` + - `number` + - add `MagmaSub` - `Option` - add `FromEither` instance - add `fromEitherK` diff --git a/src/number.ts b/src/number.ts index c920e9991..87d2f2532 100644 --- a/src/number.ts +++ b/src/number.ts @@ -8,6 +8,7 @@ import * as O from './Ord' import * as S from './Show' import { Semigroup, semigroupProduct, semigroupSum } from './Semigroup' import { Monoid, monoidProduct, monoidSum } from './Monoid' +import { Magma } from './Magma' /** * @category instances @@ -44,6 +45,14 @@ export const Field: F.Field = F.fieldNumber // tslint:disable-next-line: deprecation export const Show: S.Show = S.showNumber +/** + * @category instances + * @since 2.11.0 + */ +export const MagmaSub: Magma = { + concat: Field.sub +} + /** * `number` semigroup under addition. * diff --git a/test/number.ts b/test/number.ts index 57652c887..d9eb86cd3 100644 --- a/test/number.ts +++ b/test/number.ts @@ -21,4 +21,8 @@ describe('string', () => { it('SemigroupProduct', () => { U.deepStrictEqual(_.SemigroupProduct.concat(2, 3), 6) }) + + it('MagmaSub', () => { + U.deepStrictEqual(_.MagmaSub.concat(2, 3), -1) + }) }) From e8818dbb4022225847f17ebd3623aca8348d3e77 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:07:46 +0200 Subject: [PATCH 054/162] string: add `toUpperCase` --- CHANGELOG.md | 2 ++ src/string.ts | 5 +++++ test/string.ts | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0d46f850..7457bfa5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,6 +140,8 @@ high state of flux, you're at risk of it changing without notice. - `StateReaderTaskEither` - add `fromStateK` - add `chainStateK` + - `string` + - add `toUpperCase` - `struct` - add `evolve` - `TaskEither` diff --git a/src/string.ts b/src/string.ts index eb3f08cb7..65ef1d79d 100644 --- a/src/string.ts +++ b/src/string.ts @@ -86,3 +86,8 @@ export const isEmpty = (s: string): boolean => s.length === 0 * @since 2.10.0 */ export const size = (s: string): number => s.length + +/** + * @since 2.11.0 + */ +export const toUpperCase = (s: string): string => s.toUpperCase() diff --git a/test/string.ts b/test/string.ts index 0a09ecbf2..464f0ee32 100644 --- a/test/string.ts +++ b/test/string.ts @@ -29,4 +29,8 @@ describe('string', () => { U.deepStrictEqual(_.size(''), 0) U.deepStrictEqual(_.size('a'), 1) }) + + it('toUpperCase', () => { + U.deepStrictEqual(_.toUpperCase('a'), 'A') + }) }) From 205a9078ffd9f1729be75b42c192898ea45df5c1 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:13:41 +0200 Subject: [PATCH 055/162] - `Magma` - add `reverse` - add `filterFirst` - add `filterSecond` - add `endo` - add `concatAll` --- CHANGELOG.md | 6 ++++ src/Magma.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Semigroup.ts | 10 +++---- test/Magma.ts | 42 ++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 test/Magma.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 7457bfa5b..3f573cc11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,12 @@ high state of flux, you're at risk of it changing without notice. - `function` - add `SK` (@cdimitroulas) - add `apply` + - `Magma` + - add `reverse` + - add `filterFirst` + - add `filterSecond` + - add `endo` + - add `concatAll` - `Map` - add `union` - add `intersection` diff --git a/src/Magma.ts b/src/Magma.ts index c00cae5ae..83e3f7abd 100644 --- a/src/Magma.ts +++ b/src/Magma.ts @@ -6,6 +6,9 @@ * @since 2.0.0 */ +import { Endomorphism } from './Endomorphism' +import { Predicate } from './Predicate' + // ------------------------------------------------------------------------------------- // model // ------------------------------------------------------------------------------------- @@ -17,3 +20,71 @@ export interface Magma { readonly concat: (x: A, y: A) => A } + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * The dual of a `Magma`, obtained by swapping the arguments of `concat`. + * + * @example + * import { reverse, concatAll } from 'fp-ts/Magma' + * import * as N from 'fp-ts/number' + * + * const subAll = concatAll(reverse(N.MagmaSub))(0) + * + * assert.deepStrictEqual(subAll([1, 2, 3]), 2) + * + * @category combinators + * @since 2.11.0 + */ +export const reverse = (M: Magma): Magma => ({ + concat: (first, second) => M.concat(second, first) +}) + +/** + * @category combinators + * @since 2.11.0 + */ +export const filterFirst = (predicate: Predicate) => (M: Magma): Magma => ({ + concat: (first, second) => (predicate(first) ? M.concat(first, second) : second) +}) + +/** + * @category combinators + * @since 2.11.0 + */ +export const filterSecond = (predicate: Predicate) => (M: Magma): Magma => ({ + concat: (first, second) => (predicate(second) ? M.concat(first, second) : first) +}) + +/** + * @category combinators + * @since 2.11.0 + */ +export const endo = (f: Endomorphism) => (M: Magma): Magma => ({ + concat: (first, second) => M.concat(f(first), f(second)) +}) + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * Given a sequence of `as`, concat them and return the total. + * + * If `as` is empty, return the provided `startWith` value. + * + * @example + * import { concatAll } from 'fp-ts/Magma' + * import * as N from 'fp-ts/number' + * + * const subAll = concatAll(N.MagmaSub)(0) + * + * assert.deepStrictEqual(subAll([1, 2, 3]), -6) + * + * @since 2.11.0 + */ +export const concatAll = (M: Magma) => (startWith: A) => (as: ReadonlyArray): A => + as.reduce((a, acc) => M.concat(a, acc), startWith) diff --git a/src/Semigroup.ts b/src/Semigroup.ts index ec46c8627..d69c08ac7 100644 --- a/src/Semigroup.ts +++ b/src/Semigroup.ts @@ -39,11 +39,12 @@ */ import { getSemigroup, identity } from './function' import * as _ from './internal' -import { Magma } from './Magma' +import * as M from './Magma' import * as Or from './Ord' import { ReadonlyRecord } from './ReadonlyRecord' import Ord = Or.Ord +import Magma = M.Magma // ------------------------------------------------------------------------------------- // model @@ -119,9 +120,7 @@ export const constant = (a: A): Semigroup => ({ * @category combinators * @since 2.10.0 */ -export const reverse = (S: Semigroup): Semigroup => ({ - concat: (x, y) => S.concat(y, x) -}) +export const reverse: (S: Semigroup) => Semigroup = M.reverse /** * Given a struct of semigroups returns a semigroup for the struct. @@ -252,8 +251,7 @@ export const last = (): Semigroup => ({ concat: (_, y) => y }) * * @since 2.10.0 */ -export const concatAll = (S: Semigroup) => (startWith: A) => (as: ReadonlyArray): A => - as.reduce(S.concat, startWith) +export const concatAll: (S: Semigroup) => (startWith: A) => (as: ReadonlyArray) => A = M.concatAll // ------------------------------------------------------------------------------------- // deprecated diff --git a/test/Magma.ts b/test/Magma.ts new file mode 100644 index 000000000..9a0f9a673 --- /dev/null +++ b/test/Magma.ts @@ -0,0 +1,42 @@ +import { increment, pipe } from '../src/function' +import * as _ from '../src/Magma' +import * as N from '../src/number' +import * as U from './util' + +describe('Magma', () => { + it('reverse', () => { + const subAll = _.concatAll(_.reverse(N.MagmaSub))(0) + U.deepStrictEqual(subAll([1, 2, 3]), 2) + }) + + it('filterFirst', () => { + const M = pipe( + N.SemigroupSum, + _.filterFirst((n) => n >= 0) + ) + // sum ignoring negative partials + const sum = _.concatAll(M)(0) + U.deepStrictEqual(sum([1, -2, 3]), 3) + }) + + it('filterSecond', () => { + const M = pipe( + N.SemigroupSum, + _.filterSecond((n) => n >= 0) + ) + // sum ignoring negative elements + const sum = _.concatAll(M)(0) + U.deepStrictEqual(sum([1, -2, 3]), 4) + }) + + it('endo', () => { + const M = pipe(N.SemigroupSum, _.endo(increment)) + const sum = _.concatAll(M)(0) + U.deepStrictEqual(sum([1, -2, 3]), 8) + }) + + it('concatAll', () => { + const subAll = _.concatAll(N.MagmaSub)(0) + U.deepStrictEqual(subAll([1, 2, 3]), -6) + }) +}) From 7f819d4789e7c9b0b6bef966855e98fcc86ec4a6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:17:17 +0200 Subject: [PATCH 056/162] - `Set` / `ReadonlySet` - add `reduceRight` --- src/ReadonlySet.ts | 8 ++++++++ src/Set.ts | 5 +++++ test/ReadonlySet.ts | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 8dfec64f2..af9c55090 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -340,6 +340,14 @@ export function foldMap(O: Ord, M: Monoid): (f: (a: A) => M) => (fa: return (f) => (fa) => toReadonlyArrayO(fa).reduce((b, a) => M.concat(b, f(a)), M.empty) } +/** + * @since 2.11.0 + */ +export const reduceRight = (O: Ord): ((b: B, f: (a: A, b: B) => B) => (fa: ReadonlySet) => B) => { + const toReadonlyArrayO = toReadonlyArray(O) + return (b, f) => (fa) => toReadonlyArrayO(fa).reduceRight((b, a) => f(a, b), b) +} + /** * Create a set with one element * diff --git a/src/Set.ts b/src/Set.ts index 007d8d5e5..28bead4ae 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -297,6 +297,11 @@ export const reduce: (O: Ord) => (b: B, f: (b: B, a: A) => B) => (fa: S */ export const foldMap: (O: Ord, M: Monoid) => (f: (a: A) => M) => (fa: Set) => M = RS.foldMap +/** + * @since 2.11.0 + */ +export const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (fa: Set) => B = RS.reduceRight + /** * Create a set with one element * diff --git a/test/ReadonlySet.ts b/test/ReadonlySet.ts index 2cd3d9c95..33e2bf434 100644 --- a/test/ReadonlySet.ts +++ b/test/ReadonlySet.ts @@ -161,6 +161,12 @@ describe('ReadonlySet', () => { U.deepStrictEqual(_.foldMap(N.Ord, getMonoid())((a) => [a])(new Set([3, 2, 1])), [1, 2, 3]) }) + it('reduceRight', () => { + const f = _.reduceRight(N.Ord)('', (a, b) => b + a) + U.deepStrictEqual(f(new Set([1, 2, 3])), '321') + U.deepStrictEqual(f(new Set([3, 2, 1])), '321') + }) + it('singleton', () => { U.deepStrictEqual(_.singleton(1), new Set([1])) }) From 97cc7a73ad3244ef62a87d685e6c9622c7c39a9e Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:21:23 +0200 Subject: [PATCH 057/162] - `TaskEither` - add `fromTaskOptionK` - add `chainTaskOptionK` --- CHANGELOG.md | 2 ++ src/TaskEither.ts | 20 ++++++++++++++++++++ test/TaskEither.ts | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f573cc11..d76a7620f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,6 +152,8 @@ high state of flux, you're at risk of it changing without notice. - add `evolve` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) + - add `fromTaskOptionK` + - add `chainTaskOptionK` - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) - `Witherable` diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 27276bf58..53bdbaaa7 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -351,6 +351,26 @@ export const swap: (ma: TaskEither) => TaskEither = /*#__PURE__*/ ET.swap(T.Functor) +/** + * @category combinators + * @since 2.11.0 + */ +export const fromTaskOptionK = ( + onNone: Lazy +): (, B>(f: (...a: A) => TaskOption) => (...a: A) => TaskEither) => { + const from = fromTaskOption(onNone) + return (f) => flow(f, from) +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainTaskOptionK = ( + onNone: Lazy +): ((f: (a: A) => TaskOption) => (ma: TaskEither) => TaskEither) => + flow(fromTaskOptionK(onNone), chain) + /** * @category combinators * @since 2.4.0 diff --git a/test/TaskEither.ts b/test/TaskEither.ts index 73079aeb0..ef024e71b 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -611,4 +611,11 @@ describe('TaskEither', () => { U.deepStrictEqual(await f(_.right(1))(), 'right') U.deepStrictEqual(await f(_.left(''))(), 'left') }) + + it('chainTaskOptionK', async () => { + const f = _.chainTaskOptionK(() => 'a')((n: number) => (n > 0 ? TO.some(n * 2) : TO.none)) + U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(2)) + U.deepStrictEqual(await pipe(_.right(-1), f)(), E.left('a')) + U.deepStrictEqual(await pipe(_.left('b'), f)(), E.left('b')) + }) }) From c3d0eb6d62cb04f3a8ef49c73e0de64f75155635 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:24:22 +0200 Subject: [PATCH 058/162] - `Alt` - add `altAll` - `Alternative` - add `altAll` --- CHANGELOG.md | 4 ++++ src/Alt.ts | 30 ++++++++++++++++++++++++++++++ src/Alternative.ts | 28 +++++++++++++++++++++++++++- src/Witherable.ts | 4 ++-- test/Alternative.ts | 12 ++++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 test/Alternative.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d76a7620f..2e782fec0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,10 @@ high state of flux, you're at risk of it changing without notice. - add `FromThese` module - add `void` module - add `FromReader` module + - `Alt` + - add `altAll` + - `Alternative` + - add `altAll` - `Array` - add `prependW`, `appendW` (@thewilkybarkid) - add `fromOption`, `fromPredicate` (@cdimitroulas) diff --git a/src/Alt.ts b/src/Alt.ts index 3d0bb5eaa..1f0a4ae1c 100644 --- a/src/Alt.ts +++ b/src/Alt.ts @@ -73,3 +73,33 @@ export interface Alt3C extends Functor3C { export interface Alt4 extends Functor4 { readonly alt: (fa: Kind4, that: Lazy>) => Kind4 } + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export function altAll( + F: Alt4 +): (startWith: Kind4) => (as: ReadonlyArray>) => Kind4 +export function altAll( + F: Alt3 +): (startWith: Kind3) => (as: ReadonlyArray>) => Kind3 +export function altAll( + F: Alt3C +): (startWith: Kind3) => (as: ReadonlyArray>) => Kind3 +export function altAll( + F: Alt2 +): (startWith: Kind2) => (as: ReadonlyArray>) => Kind2 +export function altAll( + F: Alt2C +): (startWith: Kind2) => (as: ReadonlyArray>) => Kind2 +export function altAll( + F: Alt1 +): (startWith: Kind) => (as: ReadonlyArray>) => Kind +export function altAll(F: Alt): (startWith: HKT) => (as: ReadonlyArray>) => HKT +export function altAll(F: Alt): (startWith: HKT) => (as: ReadonlyArray>) => HKT { + return (startWith) => (as) => as.reduce((acc, a) => F.alt(acc, () => a), startWith) +} diff --git a/src/Alternative.ts b/src/Alternative.ts index 434302646..b15e13a50 100644 --- a/src/Alternative.ts +++ b/src/Alternative.ts @@ -14,7 +14,7 @@ * * @since 2.0.0 */ -import { Alt, Alt1, Alt2, Alt2C, Alt3, Alt3C, Alt4 } from './Alt' +import { Alt, Alt1, Alt2, Alt2C, Alt3, Alt3C, Alt4, altAll as altAll_ } from './Alt' import { Applicative, Applicative1, @@ -85,3 +85,29 @@ export interface Alternative3C extends Applicative3C, export interface Alternative4 extends Applicative4, Alt4 { readonly zero: () => Kind4 } + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export function altAll( + F: Alternative4 +): (as: ReadonlyArray>) => Kind4 +export function altAll( + F: Alternative3 +): (as: ReadonlyArray>) => Kind3 +export function altAll( + F: Alternative3C +): (as: ReadonlyArray>) => Kind3 +export function altAll(F: Alternative2): (as: ReadonlyArray>) => Kind2 +export function altAll( + F: Alternative2C +): (as: ReadonlyArray>) => Kind2 +export function altAll(F: Alternative1): (as: ReadonlyArray>) => Kind +export function altAll(F: Alternative): (as: ReadonlyArray>) => HKT +export function altAll(F: Alternative): (as: ReadonlyArray>) => HKT { + return altAll_(F)(F.zero()) +} diff --git a/src/Witherable.ts b/src/Witherable.ts index 897a475dc..3584a9dea 100644 --- a/src/Witherable.ts +++ b/src/Witherable.ts @@ -557,7 +557,7 @@ export interface PipeableWilt3 { * Return a `wilt` implementation from `Traversable` and `Compactable`. * * @category defaults - * @since 3.0.0 + * @since 2.11.0 */ export function wiltDefault(T: Traversable2C, C: Compactable2): Witherable2C['wilt'] export function wiltDefault(T: Traversable2, C: Compactable2C): Witherable2C['wilt'] @@ -576,7 +576,7 @@ export function wiltDefault(T: Traversable, C: Compactable): Witherable * Return a `wither` implementation from `Traversable` and `Compactable`. * * @category defaults - * @since 3.0.0 + * @since 2.11.0 */ export function witherDefault( T: Traversable2C, diff --git a/test/Alternative.ts b/test/Alternative.ts new file mode 100644 index 000000000..6548595f8 --- /dev/null +++ b/test/Alternative.ts @@ -0,0 +1,12 @@ +import * as _ from '../src/Alternative' +import * as O from '../src/Option' +import * as U from './util' + +describe('Alternative', () => { + it('altAll', () => { + const altAll = _.altAll(O.Alternative) + U.deepStrictEqual(altAll([]), O.none) + U.deepStrictEqual(altAll([O.none]), O.none) + U.deepStrictEqual(altAll([O.none, O.some(1)]), O.some(1)) + }) +}) From a5da88785218119b9ed80837bb0a33dfeba2919d Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 14:48:57 +0200 Subject: [PATCH 059/162] - `NonEmptyArray` / `ReadonlyNonEmptyArray` - add `makeBy` - add `range` --- CHANGELOG.md | 8 +++ docs/modules/Alt.ts.md | 32 +++++++++ docs/modules/Alternative.ts.md | 30 ++++++++ docs/modules/Array.ts.md | 34 ++++----- docs/modules/Magma.ts.md | 89 ++++++++++++++++++++++++ docs/modules/NonEmptyArray.ts.md | 46 ++++++++++++ docs/modules/ReadonlyArray.ts.md | 34 ++++----- docs/modules/ReadonlyNonEmptyArray.ts.md | 46 ++++++++++++ docs/modules/ReadonlySet.ts.md | 11 +++ docs/modules/Set.ts.md | 11 +++ docs/modules/TaskEither.ts.md | 26 +++++++ docs/modules/Witherable.ts.md | 4 +- docs/modules/number.ts.md | 11 +++ docs/modules/string.ts.md | 11 +++ src/Array.ts | 25 +++---- src/NonEmptyArray.ts | 50 +++++++++---- src/ReadonlyArray.ts | 25 +++---- src/ReadonlyNonEmptyArray.ts | 46 +++++++++--- test/Array.ts | 8 +++ test/ReadonlyArray.ts | 8 +++ test/ReadonlyNonEmptyArray.ts | 27 +++++-- 21 files changed, 479 insertions(+), 103 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e782fec0..6b8ab2aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ high state of flux, you're at risk of it changing without notice. # 2.11 - **Deprecation** + - `Array` + - deprecate `range`, use `NonEmptyArray` module instead. - `function` - deprecate `Endomorphism`, use `Endomorphism` module instead. - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. @@ -26,6 +28,8 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Refinement`, use `Refinement` module instead. - `Monoid` - deprecate `monoidVoid`, use `void` module instead. + - `ReadonlyArray` + - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` @@ -90,6 +94,8 @@ high state of flux, you're at risk of it changing without notice. - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` - add `getUnionSemigroup` + - add `makeBy` + - add `range` - `number` - add `MagmaSub` - `Option` @@ -125,6 +131,8 @@ high state of flux, you're at risk of it changing without notice. - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` - add `getUnionSemigroup` + - add `makeBy` + - add `range` - `ReadonlyRecord` - add `union` (@anthonyjoeseph) - add `intersection` (@anthonyjoeseph) diff --git a/docs/modules/Alt.ts.md b/docs/modules/Alt.ts.md index 4a5fcc4bb..81f0b784e 100644 --- a/docs/modules/Alt.ts.md +++ b/docs/modules/Alt.ts.md @@ -29,6 +29,8 @@ Added in v2.0.0 - [Alt3 (interface)](#alt3-interface) - [Alt3C (interface)](#alt3c-interface) - [Alt4 (interface)](#alt4-interface) +- [utils](#utils) + - [altAll](#altall) --- @@ -117,3 +119,33 @@ export interface Alt4 extends Functor4 { ``` Added in v2.0.0 + +# utils + +## altAll + +**Signature** + +```ts +export declare function altAll( + F: Alt4 +): (startWith: Kind4) => (as: ReadonlyArray>) => Kind4 +export declare function altAll( + F: Alt3 +): (startWith: Kind3) => (as: ReadonlyArray>) => Kind3 +export declare function altAll( + F: Alt3C +): (startWith: Kind3) => (as: ReadonlyArray>) => Kind3 +export declare function altAll( + F: Alt2 +): (startWith: Kind2) => (as: ReadonlyArray>) => Kind2 +export declare function altAll( + F: Alt2C +): (startWith: Kind2) => (as: ReadonlyArray>) => Kind2 +export declare function altAll( + F: Alt1 +): (startWith: Kind) => (as: ReadonlyArray>) => Kind +export declare function altAll(F: Alt): (startWith: HKT) => (as: ReadonlyArray>) => HKT +``` + +Added in v2.11.0 diff --git a/docs/modules/Alternative.ts.md b/docs/modules/Alternative.ts.md index 5f24bb2a5..1c9378b10 100644 --- a/docs/modules/Alternative.ts.md +++ b/docs/modules/Alternative.ts.md @@ -33,6 +33,8 @@ Added in v2.0.0 - [Alternative3 (interface)](#alternative3-interface) - [Alternative3C (interface)](#alternative3c-interface) - [Alternative4 (interface)](#alternative4-interface) +- [utils](#utils) + - [altAll](#altall) --- @@ -121,3 +123,31 @@ export interface Alternative4 extends Applicative4, Alt4 ``` Added in v2.10.0 + +# utils + +## altAll + +**Signature** + +```ts +export declare function altAll( + F: Alternative4 +): (as: ReadonlyArray>) => Kind4 +export declare function altAll( + F: Alternative3 +): (as: ReadonlyArray>) => Kind3 +export declare function altAll( + F: Alternative3C +): (as: ReadonlyArray>) => Kind3 +export declare function altAll( + F: Alternative2 +): (as: ReadonlyArray>) => Kind2 +export declare function altAll( + F: Alternative2C +): (as: ReadonlyArray>) => Kind2 +export declare function altAll(F: Alternative1): (as: ReadonlyArray>) => Kind +export declare function altAll(F: Alternative): (as: ReadonlyArray>) => HKT +``` + +Added in v2.11.0 diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 8c89a840f..5e8b3657c 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -106,9 +106,9 @@ Added in v2.0.0 - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) - - [range](#range) - [replicate](#replicate) - [~~cons~~](#cons) + - [~~range~~](#range) - [~~snoc~~](#snoc) - [destructors](#destructors) - [findFirst](#findfirst) @@ -1465,26 +1465,6 @@ export declare const prependW: (head: B) => (tail: A[]) => NEA.NonEmptyArr Added in v2.11.0 -## range - -Create an `Array` containing a range of integers, including both endpoints. - -**Signature** - -```ts -export declare const range: (start: number, end: number) => Array -``` - -**Example** - -```ts -import { range } from 'fp-ts/Array' - -assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) -``` - -Added in v2.0.0 - ## replicate Create a `Array` containing a value repeated the specified number of times. @@ -1519,6 +1499,18 @@ export declare const cons: typeof NEA.cons Added in v2.0.0 +## ~~range~~ + +Use `NonEmptyArray` module instead. + +**Signature** + +```ts +export declare const range: (start: number, end: number) => NEA.NonEmptyArray +``` + +Added in v2.0.0 + ## ~~snoc~~ Use `append` instead. diff --git a/docs/modules/Magma.ts.md b/docs/modules/Magma.ts.md index a6118b15f..8664a12a1 100644 --- a/docs/modules/Magma.ts.md +++ b/docs/modules/Magma.ts.md @@ -16,11 +16,73 @@ Added in v2.0.0

Table of contents

+- [combinators](#combinators) + - [endo](#endo) + - [filterFirst](#filterfirst) + - [filterSecond](#filtersecond) + - [reverse](#reverse) - [type classes](#type-classes) - [Magma (interface)](#magma-interface) +- [utils](#utils) + - [concatAll](#concatall) --- +# combinators + +## endo + +**Signature** + +```ts +export declare const endo:
(f: Endomorphism) => (M: Magma) => Magma +``` + +Added in v2.11.0 + +## filterFirst + +**Signature** + +```ts +export declare const filterFirst: (predicate: Predicate) => (M: Magma) => Magma +``` + +Added in v2.11.0 + +## filterSecond + +**Signature** + +```ts +export declare const filterSecond: (predicate: Predicate) => (M: Magma) => Magma +``` + +Added in v2.11.0 + +## reverse + +The dual of a `Magma`, obtained by swapping the arguments of `concat`. + +**Signature** + +```ts +export declare const reverse: (M: Magma) => Magma +``` + +**Example** + +```ts +import { reverse, concatAll } from 'fp-ts/Magma' +import * as N from 'fp-ts/number' + +const subAll = concatAll(reverse(N.MagmaSub))(0) + +assert.deepStrictEqual(subAll([1, 2, 3]), 2) +``` + +Added in v2.11.0 + # type classes ## Magma (interface) @@ -34,3 +96,30 @@ export interface Magma { ``` Added in v2.0.0 + +# utils + +## concatAll + +Given a sequence of `as`, concat them and return the total. + +If `as` is empty, return the provided `startWith` value. + +**Signature** + +```ts +export declare const concatAll: (M: Magma) => (startWith: A) => (as: readonly A[]) => A +``` + +**Example** + +```ts +import { concatAll } from 'fp-ts/Magma' +import * as N from 'fp-ts/number' + +const subAll = concatAll(N.MagmaSub)(0) + +assert.deepStrictEqual(subAll([1, 2, 3]), -6) +``` + +Added in v2.11.0 diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index d983864fa..b56693ebe 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -80,6 +80,8 @@ Added in v2.0.0 - [constructors](#constructors) - [fromArray](#fromarray) - [fromReadonlyNonEmptyArray](#fromreadonlynonemptyarray) + - [makeBy](#makeby) + - [range](#range) - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) @@ -737,6 +739,50 @@ export declare const fromReadonlyNonEmptyArray: (as: RNEA.ReadonlyNonEmptyArr Added in v2.10.0 +## makeBy + +Return a `NonEmptyArray` of length `n` with element `i` initialized with `f(i)`. + +**Note**. `n` is normalized to a natural number. + +**Signature** + +```ts +export declare const makeBy: (f: (i: number) => A) => (n: number) => NonEmptyArray +``` + +**Example** + +```ts +import { makeBy } from 'fp-ts/NonEmptyArray' +import { pipe } from 'fp-ts/function' + +const double = (n: number): number => n * 2 +assert.deepStrictEqual(pipe(5, makeBy(double)), [0, 2, 4, 6, 8]) +``` + +Added in v2.11.0 + +## range + +Create a `NonEmptyArray` containing a range of integers, including both endpoints. + +**Signature** + +```ts +export declare const range: (start: number, end: number) => NonEmptyArray +``` + +**Example** + +```ts +import { range } from 'fp-ts/NonEmptyArray' + +assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) +``` + +Added in v2.11.0 + ## ~~cons~~ Use `Array`'s `prepend` instead. diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 382a3debf..6ed4a782e 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -104,9 +104,9 @@ Added in v2.5.0 - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) - - [range](#range) - [replicate](#replicate) - [~~cons~~](#cons) + - [~~range~~](#range) - [~~snoc~~](#snoc) - [destructors](#destructors) - [foldLeft](#foldleft) @@ -1473,26 +1473,6 @@ export declare const prependW: (head: B) => (tail: readonly A[]) => RNEA.R Added in v2.11.0 -## range - -Create a `ReadonlyArray` containing a range of integers, including both endpoints. - -**Signature** - -```ts -export declare const range: (start: number, end: number) => ReadonlyArray -``` - -**Example** - -```ts -import { range } from 'fp-ts/ReadonlyArray' - -assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) -``` - -Added in v2.5.0 - ## replicate Create a `ReadonlyArray` containing a value repeated the specified number of times. @@ -1527,6 +1507,18 @@ export declare const cons: typeof RNEA.cons Added in v2.5.0 +## ~~range~~ + +Use `ReadonlyNonEmptyArray` module instead. + +**Signature** + +```ts +export declare const range: (start: number, end: number) => RNEA.ReadonlyNonEmptyArray +``` + +Added in v2.5.0 + ## ~~snoc~~ Use `append` instead. diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 2c18cf30d..d764c969b 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -85,6 +85,8 @@ Added in v2.5.0 - [~~prependToAll~~](#prependtoall) - [constructors](#constructors) - [fromReadonlyArray](#fromreadonlyarray) + - [makeBy](#makeby) + - [range](#range) - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) @@ -826,6 +828,50 @@ export declare const fromReadonlyArray: (as: readonly A[]) => Option(f: (i: number) => A) => (n: number) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { makeBy } from 'fp-ts/ReadonlyNonEmptyArray' +import { pipe } from 'fp-ts/function' + +const double = (n: number): number => n * 2 +assert.deepStrictEqual(pipe(5, makeBy(double)), [0, 2, 4, 6, 8]) +``` + +Added in v2.11.0 + +## range + +Create a `ReadonlyNonEmptyArray` containing a range of integers, including both endpoints. + +**Signature** + +```ts +export declare const range: (start: number, end: number) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { range } from 'fp-ts/ReadonlyNonEmptyArray' + +assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) +``` + +Added in v2.11.0 + ## ~~cons~~ Use `ReadonlyArray`'s `prepend` instead. diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index d40e7b063..42683287e 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -48,6 +48,7 @@ Added in v2.5.0 - [partition](#partition) - [partitionMap](#partitionmap) - [reduce](#reduce) + - [reduceRight](#reduceright) - [separate](#separate) - [size](#size) - [some](#some) @@ -447,6 +448,16 @@ export declare function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => Added in v2.5.0 +## reduceRight + +**Signature** + +```ts +export declare const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (fa: ReadonlySet) => B +``` + +Added in v2.11.0 + ## separate **Signature** diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 1ac5b6d1e..8e2178de5 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -44,6 +44,7 @@ Added in v2.0.0 - [partition](#partition) - [partitionMap](#partitionmap) - [reduce](#reduce) + - [reduceRight](#reduceright) - [separate](#separate) - [size](#size) - [some](#some) @@ -399,6 +400,16 @@ export declare const reduce: (O: Ord) => (b: B, f: (b: B, a: A) => B) = Added in v2.0.0 +## reduceRight + +**Signature** + +```ts +export declare const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (fa: Set) => B +``` + +Added in v2.11.0 + ## separate **Signature** diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index a32427802..4e8a4fcd7 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -51,6 +51,7 @@ Added in v2.0.0 - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) - [chainTaskK](#chaintaskk) + - [chainTaskOptionK](#chaintaskoptionk) - [filterOrElse](#filterorelse) - [filterOrElseW](#filterorelsew) - [flap](#flap) @@ -60,6 +61,7 @@ Added in v2.0.0 - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) - [fromTaskK](#fromtaskk) + - [fromTaskOptionK](#fromtaskoptionk) - [orElse](#orelse) - [orElseFirst](#orelsefirst) - [orElseFirstW](#orelsefirstw) @@ -493,6 +495,18 @@ export declare const chainTaskK: (f: (a: A) => T.Task) => (first: Ta Added in v2.10.0 +## chainTaskOptionK + +**Signature** + +```ts +export declare const chainTaskOptionK: ( + onNone: Lazy +) => (f: (a: A) => TaskOption) => (ma: TaskEither) => TaskEither +``` + +Added in v2.11.0 + ## filterOrElse **Signature** @@ -601,6 +615,18 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a: A Added in v2.10.0 +## fromTaskOptionK + +**Signature** + +```ts +export declare const fromTaskOptionK: ( + onNone: Lazy +) => (f: (...a: A) => TaskOption) => (...a: A) => TaskEither +``` + +Added in v2.11.0 + ## orElse Returns `ma` if is a `Right` or the value returned by `onLeft` otherwise. diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index 6e79573e1..d7ab94a16 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -72,7 +72,7 @@ export declare function wiltDefault(T: Traversable1, C: Compa export declare function wiltDefault(T: Traversable, C: Compactable): Witherable['wilt'] ``` -Added in v3.0.0 +Added in v2.11.0 ## witherDefault @@ -93,7 +93,7 @@ export declare function witherDefault(T: Traversable1, C: Com export declare function witherDefault(T: Traversable, C: Compactable): Witherable['wither'] ``` -Added in v3.0.0 +Added in v2.11.0 # type classes diff --git a/docs/modules/number.ts.md b/docs/modules/number.ts.md index e982b83a4..418f1b9c7 100644 --- a/docs/modules/number.ts.md +++ b/docs/modules/number.ts.md @@ -16,6 +16,7 @@ Added in v2.10.0 - [Bounded](#bounded) - [Eq](#eq) - [Field](#field) + - [MagmaSub](#magmasub) - [MonoidProduct](#monoidproduct) - [MonoidSum](#monoidsum) - [Ord](#ord) @@ -57,6 +58,16 @@ export declare const Field: F.Field Added in v2.10.0 +## MagmaSub + +**Signature** + +```ts +export declare const MagmaSub: Magma +``` + +Added in v2.11.0 + ## MonoidProduct `number` monoid under multiplication. diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index 71604c698..908a7ea51 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -22,6 +22,7 @@ Added in v2.10.0 - [empty](#empty) - [isEmpty](#isempty) - [size](#size) + - [toUpperCase](#touppercase) --- @@ -136,3 +137,13 @@ export declare const size: (s: string) => number ``` Added in v2.10.0 + +## toUpperCase + +**Signature** + +```ts +export declare const toUpperCase: (s: string) => string +``` + +Added in v2.11.0 diff --git a/src/Array.ts b/src/Array.ts index f5b97da1e..2e95b03c9 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -110,21 +110,7 @@ export const appendW: (end: B) => (init: Array) => NEA.NonEmptyArray(n: number, f: (i: number) => A): Array => (n <= 0 ? [] : NEA.makeBy(n, f)) - -/** - * Create an `Array` containing a range of integers, including both endpoints. - * - * @example - * import { range } from 'fp-ts/Array' - * - * assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) - * - * @category constructors - * @since 2.0.0 - */ -export const range = (start: number, end: number): Array => - start <= end ? makeBy(end - start + 1, (i) => start + i) : [start] +export const makeBy = (n: number, f: (i: number) => A): Array => (n <= 0 ? [] : NEA.makeBy(f)(n)) /** * Create a `Array` containing a value repeated the specified number of times. @@ -2178,6 +2164,15 @@ export const apS = // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `NonEmptyArray` module instead. + * + * @category constructors + * @since 2.0.0 + * @deprecated + */ +export const range = NEA.range + /** * Use a new `[]` instead. * diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 8d832f9df..0cc3ee610 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -164,18 +164,6 @@ export const rotate = (n: number) => (as: NonEmptyArray): NonEmptyArray } } -/** - * @internal - */ -export const makeBy = (n: number, f: (i: number) => A): NonEmptyArray => { - const j = Math.max(0, Math.floor(n)) - const out: NonEmptyArray = [f(0)] - for (let i = 1; i < j; i++) { - out.push(f(i)) - } - return out -} - // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- @@ -195,6 +183,44 @@ export const fromReadonlyNonEmptyArray: (as: ReadonlyNonEmptyArray) => Non */ export const fromArray = (as: Array): Option> => (isNonEmpty(as) ? _.some(as) : _.none) +/** + * Return a `NonEmptyArray` of length `n` with element `i` initialized with `f(i)`. + * + * **Note**. `n` is normalized to a natural number. + * + * @example + * import { makeBy } from 'fp-ts/NonEmptyArray' + * import { pipe } from 'fp-ts/function' + * + * const double = (n: number): number => n * 2 + * assert.deepStrictEqual(pipe(5, makeBy(double)), [0, 2, 4, 6, 8]) + * + * @category constructors + * @since 2.11.0 + */ +export const makeBy = (f: (i: number) => A) => (n: number): NonEmptyArray => { + const j = Math.max(0, Math.floor(n)) + const out: NonEmptyArray = [f(0)] + for (let i = 1; i < j; i++) { + out.push(f(i)) + } + return out +} + +/** + * Create a `NonEmptyArray` containing a range of integers, including both endpoints. + * + * @example + * import { range } from 'fp-ts/NonEmptyArray' + * + * assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) + * + * @category constructors + * @since 2.11.0 + */ +export const range = (start: number, end: number): NonEmptyArray => + start <= end ? makeBy((i) => start + i)(end - start + 1) : [start] + // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 1c7c7790c..4c326981b 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -111,21 +111,7 @@ export const appendW = RNEA.appendW * @category constructors * @since 2.5.0 */ -export const makeBy = (n: number, f: (i: number) => A): ReadonlyArray => (n <= 0 ? empty : RNEA.makeBy(n, f)) - -/** - * Create a `ReadonlyArray` containing a range of integers, including both endpoints. - * - * @example - * import { range } from 'fp-ts/ReadonlyArray' - * - * assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) - * - * @category constructors - * @since 2.5.0 - */ -export const range = (start: number, end: number): ReadonlyArray => - start <= end ? makeBy(end - start + 1, (i) => start + i) : [start] +export const makeBy = (n: number, f: (i: number) => A): ReadonlyArray => (n <= 0 ? empty : RNEA.makeBy(f)(n)) /** * Create a `ReadonlyArray` containing a value repeated the specified number of times. @@ -2413,6 +2399,15 @@ export const apS = // deprecated // ------------------------------------------------------------------------------------- +/** + * Use `ReadonlyNonEmptyArray` module instead. + * + * @category constructors + * @since 2.5.0 + * @deprecated + */ +export const range = RNEA.range + /** * Use `prepend` instead. * diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 105851692..5eb32cfc8 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -176,10 +176,35 @@ export const rotate = (n: number) => (as: ReadonlyNonEmptyArray): Readonly } } +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + /** - * @internal + * Return a `ReadonlyNonEmptyArray` from a `ReadonlyArray` returning `none` if the input is empty. + * + * @category constructors + * @since 2.5.0 */ -export const makeBy = (n: number, f: (i: number) => A): ReadonlyNonEmptyArray => { +export const fromReadonlyArray = (as: ReadonlyArray): Option> => + isNonEmpty(as) ? _.some(as) : _.none + +/** + * Return a `ReadonlyNonEmptyArray` of length `n` with element `i` initialized with `f(i)`. + * + * **Note**. `n` is normalized to a natural number. + * + * @example + * import { makeBy } from 'fp-ts/ReadonlyNonEmptyArray' + * import { pipe } from 'fp-ts/function' + * + * const double = (n: number): number => n * 2 + * assert.deepStrictEqual(pipe(5, makeBy(double)), [0, 2, 4, 6, 8]) + * + * @category constructors + * @since 2.11.0 + */ +export const makeBy = (f: (i: number) => A) => (n: number): ReadonlyNonEmptyArray => { const j = Math.max(0, Math.floor(n)) const out: NonEmptyArray = [f(0)] for (let i = 1; i < j; i++) { @@ -188,18 +213,19 @@ export const makeBy = (n: number, f: (i: number) => A): ReadonlyNonEmptyArray return out } -// ------------------------------------------------------------------------------------- -// constructors -// ------------------------------------------------------------------------------------- - /** - * Return a `ReadonlyNonEmptyArray` from a `ReadonlyArray` returning `none` if the input is empty. + * Create a `ReadonlyNonEmptyArray` containing a range of integers, including both endpoints. + * + * @example + * import { range } from 'fp-ts/ReadonlyNonEmptyArray' + * + * assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) * * @category constructors - * @since 2.5.0 + * @since 2.11.0 */ -export const fromReadonlyArray = (as: ReadonlyArray): Option> => - isNonEmpty(as) ? _.some(as) : _.none +export const range = (start: number, end: number): ReadonlyNonEmptyArray => + start <= end ? makeBy((i) => start + i)(end - start + 1) : [start] // ------------------------------------------------------------------------------------- // destructors diff --git a/test/Array.ts b/test/Array.ts index 8734c7145..db0958c6d 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -992,14 +992,22 @@ describe('Array', () => { }) it('range', () => { + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(0, 0), [0]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(0, 1), [0, 1]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(1, 5), [1, 2, 3, 4, 5]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(10, 15), [10, 11, 12, 13, 14, 15]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-1, 0), [-1, 0]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-5, -1), [-5, -4, -3, -2, -1]) // out of bound + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(2, 1), [2]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-1, -2), [-1]) }) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index 27505671b..b5a9d05cd 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1067,14 +1067,22 @@ describe('ReadonlyArray', () => { }) it('range', () => { + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(0, 0), [0]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(0, 1), [0, 1]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(1, 5), [1, 2, 3, 4, 5]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(10, 15), [10, 11, 12, 13, 14, 15]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-1, 0), [-1, 0]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-5, -1), [-5, -4, -3, -2, -1]) // out of bound + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(2, 1), [2]) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.range(-1, -2), [-1]) }) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index 71bd704d1..c752cca8f 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -615,13 +615,6 @@ describe('ReadonlyNonEmptyArray', () => { U.deepStrictEqual(concat([1, 2], [1, 2]), [1, 2]) }) - it('makeBy', () => { - U.deepStrictEqual(_.makeBy(5, U.double), [0, 2, 4, 6, 8]) - // If `n` (must be a natural number) is non positive return `[f(0)]`. - U.deepStrictEqual(_.makeBy(0, U.double), [0]) - U.deepStrictEqual(_.makeBy(-1, U.double), [0]) - }) - it('matchLeft', () => { U.deepStrictEqual( pipe( @@ -655,4 +648,24 @@ describe('ReadonlyNonEmptyArray', () => { U.deepStrictEqual(pipe(['a', 'b'], _.modifyLast(f)), ['a', 'b!']) U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyLast(f)), ['a', 'b', 'c!']) }) + + it('makeBy', () => { + const f = _.makeBy(U.double) + U.deepStrictEqual(f(5), [0, 2, 4, 6, 8]) + // If `n` (must be a natural number) is non positive return `[f(0)]`. + U.deepStrictEqual(f(0), [0]) + U.deepStrictEqual(f(-1), [0]) + }) + + it('range', () => { + U.deepStrictEqual(_.range(0, 0), [0]) + U.deepStrictEqual(_.range(0, 1), [0, 1]) + U.deepStrictEqual(_.range(1, 5), [1, 2, 3, 4, 5]) + U.deepStrictEqual(_.range(10, 15), [10, 11, 12, 13, 14, 15]) + U.deepStrictEqual(_.range(-1, 0), [-1, 0]) + U.deepStrictEqual(_.range(-5, -1), [-5, -4, -3, -2, -1]) + // out of bound + U.deepStrictEqual(_.range(2, 1), [2]) + U.deepStrictEqual(_.range(-1, -2), [-1]) + }) }) From 9dfc556fcc754a498a448495695fd49a5267f968 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 16:06:08 +0200 Subject: [PATCH 060/162] - `NonEmptyArray` / `ReadonlyNonEmptyArray` - add `concatW` - add `updateHead` - add `updateLast` - add `replicate` - add `rotate` - add `sortBy` - add `uniq` --- docs/modules/NonEmptyArray.ts.md | 161 ++++++++++++++++++++++ docs/modules/ReadonlyNonEmptyArray.ts.md | 167 +++++++++++++++++++++++ src/NonEmptyArray.ts | 102 +++++++++++++- src/ReadonlyNonEmptyArray.ts | 106 +++++++++++++- test/NonEmptyArray.ts | 23 ++++ test/ReadonlyNonEmptyArray.ts | 23 ++++ 6 files changed, 576 insertions(+), 6 deletions(-) diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index b56693ebe..9b014d3b8 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -52,6 +52,7 @@ Added in v2.0.0 - [chop](#chop) - [chunksOf](#chunksof) - [concat](#concat) + - [concatW](#concatw) - [copy](#copy) - [duplicate](#duplicate) - [flap](#flap) @@ -67,11 +68,16 @@ Added in v2.0.0 - [modifyAt](#modifyat) - [prependAll](#prependall) - [reverse](#reverse) + - [rotate](#rotate) - [sort](#sort) + - [sortBy](#sortby) - [splitAt](#splitat) - [union](#union) + - [uniq](#uniq) - [unzip](#unzip) - [updateAt](#updateat) + - [updateHead](#updatehead) + - [updateLast](#updatelast) - [zip](#zip) - [zipWith](#zipwith) - [~~filterWithIndex~~](#filterwithindex) @@ -82,6 +88,7 @@ Added in v2.0.0 - [fromReadonlyNonEmptyArray](#fromreadonlynonemptyarray) - [makeBy](#makeby) - [range](#range) + - [replicate](#replicate) - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) @@ -373,6 +380,17 @@ export declare function concat(first: NonEmptyArray, second: Array): No Added in v2.2.0 +## concatW + +**Signature** + +```ts +export declare function concatW(second: NonEmptyArray): (first: Array) => NonEmptyArray +export declare function concatW(second: Array): (first: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + ## copy **Signature** @@ -597,6 +615,27 @@ export declare const reverse: (as: NonEmptyArray) => NonEmptyArray Added in v2.0.0 +## rotate + +Rotate a `NonEmptyArray` by `n` steps. + +**Signature** + +```ts +export declare const rotate: (n: number) => (as: NonEmptyArray) => NonEmptyArray +``` + +**Example** + +```ts +import { rotate } from 'fp-ts/NonEmptyArray' + +assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3]) +assert.deepStrictEqual(rotate(-2)([1, 2, 3, 4, 5]), [3, 4, 5, 1, 2]) +``` + +Added in v2.11.0 + ## sort **Signature** @@ -607,6 +646,60 @@ export declare const sort: (O: Ord) => (as: NonEmptyArray) Added in v2.0.0 +## sortBy + +Sort the elements of a `NonEmptyArray` in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, +etc... + +**Signature** + +```ts +export declare const sortBy: (ords: Ord[]) => (as: NonEmptyArray) => NonEmptyArray +``` + +**Example** + +```ts +import * as NEA from 'fp-ts/NonEmptyArray' +import { contramap } from 'fp-ts/Ord' +import * as S from 'fp-ts/string' +import * as N from 'fp-ts/number' +import { pipe } from 'fp-ts/function' + +interface Person { + name: string + age: number +} + +const byName = pipe( + S.Ord, + contramap((p: Person) => p.name) +) + +const byAge = pipe( + N.Ord, + contramap((p: Person) => p.age) +) + +const sortByNameByAge = NEA.sortBy([byName, byAge]) + +const persons: NEA.NonEmptyArray = [ + { name: 'a', age: 1 }, + { name: 'b', age: 3 }, + { name: 'c', age: 2 }, + { name: 'b', age: 2 }, +] + +assert.deepStrictEqual(sortByNameByAge(persons), [ + { name: 'a', age: 1 }, + { name: 'b', age: 2 }, + { name: 'b', age: 3 }, + { name: 'c', age: 2 }, +]) +``` + +Added in v2.11.0 + ## splitAt Splits a `NonEmptyArray` into two pieces, the first piece has max `n` elements. @@ -629,6 +722,27 @@ export declare const union: (E: Eq) => (second: NonEmptyArray) => (firs Added in v2.11.0 +## uniq + +Remove duplicates from a `NonEmptyArray`, keeping the first occurrence of an element. + +**Signature** + +```ts +export declare const uniq: (E: Eq) => (as: NonEmptyArray) => NonEmptyArray +``` + +**Example** + +```ts +import { uniq } from 'fp-ts/NonEmptyArray' +import * as N from 'fp-ts/number' + +assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2]) +``` + +Added in v2.11.0 + ## unzip **Signature** @@ -649,6 +763,30 @@ export declare const updateAt: (i: number, a: A) => (as: NonEmptyArray) => Added in v2.0.0 +## updateHead + +Change the head, creating a new `NonEmptyArray`. + +**Signature** + +```ts +export declare const updateHead: (a: A) => (as: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + +## updateLast + +Change the last element, creating a new `NonEmptyArray`. + +**Signature** + +```ts +export declare const updateLast: (a: A) => (as: NonEmptyArray) => NonEmptyArray +``` + +Added in v2.11.0 + ## zip **Signature** @@ -783,6 +921,29 @@ assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) Added in v2.11.0 +## replicate + +Create a `NonEmptyArray` containing a value repeated the specified number of times. + +**Note**. `n` is normalized to a natural number. + +**Signature** + +```ts +export declare const replicate: (a: A) => (n: number) => RNEA.ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { replicate } from 'fp-ts/NonEmptyArray' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe(3, replicate('a')), ['a', 'a', 'a']) +``` + +Added in v2.11.0 + ## ~~cons~~ Use `Array`'s `prepend` instead. diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index d764c969b..f0cb0f4ca 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -61,6 +61,7 @@ Added in v2.5.0 - [chop](#chop) - [chunksOf](#chunksof) - [concat](#concat) + - [concatW](#concatw) - [duplicate](#duplicate) - [flap](#flap) - [flatten](#flatten) @@ -72,11 +73,16 @@ Added in v2.5.0 - [modifyAt](#modifyat) - [prependAll](#prependall) - [reverse](#reverse) + - [rotate](#rotate) - [sort](#sort) + - [sortBy](#sortby) - [splitAt](#splitat) - [union](#union) + - [uniq](#uniq) - [unzip](#unzip) - [updateAt](#updateat) + - [updateHead](#updatehead) + - [updateLast](#updatelast) - [zip](#zip) - [zipWith](#zipwith) - [~~filterWithIndex~~](#filterwithindex) @@ -87,6 +93,7 @@ Added in v2.5.0 - [fromReadonlyArray](#fromreadonlyarray) - [makeBy](#makeby) - [range](#range) + - [replicate](#replicate) - [~~cons~~](#cons) - [~~snoc~~](#snoc) - [destructors](#destructors) @@ -481,6 +488,21 @@ export declare function concat(first: ReadonlyNonEmptyArray, second: Reado Added in v2.5.0 +## concatW + +**Signature** + +```ts +export declare function concatW( + second: ReadonlyNonEmptyArray +): (first: ReadonlyArray) => ReadonlyNonEmptyArray +export declare function concatW( + second: ReadonlyArray +): (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## duplicate Derivable from `Extend`. @@ -668,6 +690,27 @@ export declare const reverse: (as: ReadonlyNonEmptyArray) => ReadonlyNonEm Added in v2.5.0 +## rotate + +Rotate a `ReadonlyNonEmptyArray` by `n` steps. + +**Signature** + +```ts +export declare const rotate: (n: number) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { rotate } from 'fp-ts/ReadonlyNonEmptyArray' + +assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3]) +assert.deepStrictEqual(rotate(-2)([1, 2, 3, 4, 5]), [3, 4, 5, 1, 2]) +``` + +Added in v2.11.0 + ## sort **Signature** @@ -678,6 +721,62 @@ export declare const sort: (O: Ord) => (as: ReadonlyNonEmptyA Added in v2.5.0 +## sortBy + +Sort the elements of a `ReadonlyNonEmptyArray` in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, +etc... + +**Signature** + +```ts +export declare const sortBy: ( + ords: readonly Ord[] +) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import * as RNEA from 'fp-ts/ReadonlyNonEmptyArray' +import { contramap } from 'fp-ts/Ord' +import * as S from 'fp-ts/string' +import * as N from 'fp-ts/number' +import { pipe } from 'fp-ts/function' + +interface Person { + name: string + age: number +} + +const byName = pipe( + S.Ord, + contramap((p: Person) => p.name) +) + +const byAge = pipe( + N.Ord, + contramap((p: Person) => p.age) +) + +const sortByNameByAge = RNEA.sortBy([byName, byAge]) + +const persons: RNEA.ReadonlyNonEmptyArray = [ + { name: 'a', age: 1 }, + { name: 'b', age: 3 }, + { name: 'c', age: 2 }, + { name: 'b', age: 2 }, +] + +assert.deepStrictEqual(sortByNameByAge(persons), [ + { name: 'a', age: 1 }, + { name: 'b', age: 2 }, + { name: 'b', age: 3 }, + { name: 'c', age: 2 }, +]) +``` + +Added in v2.11.0 + ## splitAt Splits a `ReadonlyNonEmptyArray` into two pieces, the first piece has max `n` elements. @@ -704,6 +803,27 @@ export declare const union: ( Added in v2.11.0 +## uniq + +Remove duplicates from a `ReadonlyNonEmptyArray`, keeping the first occurrence of an element. + +**Signature** + +```ts +export declare const uniq: (E: Eq) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { uniq } from 'fp-ts/ReadonlyNonEmptyArray' +import * as N from 'fp-ts/number' + +assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2]) +``` + +Added in v2.11.0 + ## unzip **Signature** @@ -729,6 +849,30 @@ export declare const updateAt: ( Added in v2.5.0 +## updateHead + +Change the head, creating a new `ReadonlyNonEmptyArray`. + +**Signature** + +```ts +export declare const updateHead: (a: A) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + +## updateLast + +Change the last element, creating a new `ReadonlyNonEmptyArray`. + +**Signature** + +```ts +export declare const updateLast: (a: A) => (as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## zip **Signature** @@ -872,6 +1016,29 @@ assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5]) Added in v2.11.0 +## replicate + +Create a `ReadonlyNonEmptyArray` containing a value repeated the specified number of times. + +**Note**. `n` is normalized to a natural number. + +**Signature** + +```ts +export declare const replicate: (a: A) => (n: number) => ReadonlyNonEmptyArray +``` + +**Example** + +```ts +import { replicate } from 'fp-ts/ReadonlyNonEmptyArray' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe(3, replicate('a')), ['a', 'a', 'a']) +``` + +Added in v2.11.0 + ## ~~cons~~ Use `ReadonlyArray`'s `prepend` instead. diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 0cc3ee610..d3fb3d9d7 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -111,7 +111,16 @@ export const unsafeUpdateAt = (i: number, a: A, as: NonEmptyArray): NonEmp } /** - * @internal + * Remove duplicates from a `NonEmptyArray`, keeping the first occurrence of an element. + * + * @example + * import { uniq } from 'fp-ts/NonEmptyArray' + * import * as N from 'fp-ts/number' + * + * assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2]) + * + * @category combinators + * @since 2.11.0 */ export const uniq = (E: Eq) => (as: NonEmptyArray): NonEmptyArray => { if (as.length === 1) { @@ -128,7 +137,43 @@ export const uniq = (E: Eq) => (as: NonEmptyArray): NonEmptyArray => } /** - * @internal + * Sort the elements of a `NonEmptyArray` in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, + * etc... + * + * @example + * import * as NEA from 'fp-ts/NonEmptyArray' + * import { contramap } from 'fp-ts/Ord' + * import * as S from 'fp-ts/string' + * import * as N from 'fp-ts/number' + * import { pipe } from 'fp-ts/function' + * + * interface Person { + * name: string + * age: number + * } + * + * const byName = pipe(S.Ord, contramap((p: Person) => p.name)) + * + * const byAge = pipe(N.Ord, contramap((p: Person) => p.age)) + * + * const sortByNameByAge = NEA.sortBy([byName, byAge]) + * + * const persons: NEA.NonEmptyArray = [ + * { name: 'a', age: 1 }, + * { name: 'b', age: 3 }, + * { name: 'c', age: 2 }, + * { name: 'b', age: 2 } + * ] + * + * assert.deepStrictEqual(sortByNameByAge(persons), [ + * { name: 'a', age: 1 }, + * { name: 'b', age: 2 }, + * { name: 'b', age: 3 }, + * { name: 'c', age: 2 } + * ]) + * + * @category combinators + * @since 2.11.0 */ export const sortBy = (ords: Array>): ((as: NonEmptyArray) => NonEmptyArray) => { if (isNonEmpty(ords)) { @@ -148,7 +193,16 @@ export const union = (E: Eq): ((second: NonEmptyArray) => (first: NonEm } /** - * @internal + * Rotate a `NonEmptyArray` by `n` steps. + * + * @example + * import { rotate } from 'fp-ts/NonEmptyArray' + * + * assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3]) + * assert.deepStrictEqual(rotate(-2)([1, 2, 3, 4, 5]), [3, 4, 5, 1, 2]) + * + * @category combinators + * @since 2.11.0 */ export const rotate = (n: number) => (as: NonEmptyArray): NonEmptyArray => { const len = as.length @@ -207,6 +261,22 @@ export const makeBy = (f: (i: number) => A) => (n: number): NonEmptyArray return out } +/** + * Create a `NonEmptyArray` containing a value repeated the specified number of times. + * + * **Note**. `n` is normalized to a natural number. + * + * @example + * import { replicate } from 'fp-ts/NonEmptyArray' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe(3, replicate('a')), ['a', 'a', 'a']) + * + * @category constructors + * @since 2.11.0 + */ +export const replicate = (a: A): ((n: number) => ReadonlyNonEmptyArray) => makeBy(() => a) + /** * Create a `NonEmptyArray` containing a range of integers, including both endpoints. * @@ -255,6 +325,16 @@ export const unappend = (as: NonEmptyArray): [Array, A] => [init(as), l // combinators // ------------------------------------------------------------------------------------- +/** + * @category combinators + * @since 2.11.0 + */ +export function concatW(second: NonEmptyArray): (first: Array) => NonEmptyArray +export function concatW(second: Array): (first: NonEmptyArray) => NonEmptyArray +export function concatW(second: Array): (first: NonEmptyArray) => Array { + return (first: NonEmptyArray) => first.concat(second) +} + // TODO: curry in v3 /** * @category combinators @@ -1133,6 +1213,14 @@ export const modifyHead = (f: Endomorphism) => (as: NonEmptyArray): Non ...tail(as) ] +/** + * Change the head, creating a new `NonEmptyArray`. + * + * @category combinators + * @since 2.11.0 + */ +export const updateHead = (a: A): ((as: NonEmptyArray) => NonEmptyArray) => modifyHead(() => a) + /** * Apply a function to the last element, creating a new `NonEmptyArray`. * @@ -1141,6 +1229,14 @@ export const modifyHead = (f: Endomorphism) => (as: NonEmptyArray): Non export const modifyLast = (f: Endomorphism) => (as: NonEmptyArray): NonEmptyArray => pipe(init(as), append(f(last(as)))) +/** + * Change the last element, creating a new `NonEmptyArray`. + * + * @category combinators + * @since 2.11.0 + */ +export const updateLast = (a: A): ((as: NonEmptyArray) => NonEmptyArray) => modifyLast(() => a) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 5eb32cfc8..14af534bf 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -119,7 +119,16 @@ export const unsafeUpdateAt = (i: number, a: A, as: ReadonlyNonEmptyArray) } /** - * @internal + * Remove duplicates from a `ReadonlyNonEmptyArray`, keeping the first occurrence of an element. + * + * @example + * import { uniq } from 'fp-ts/ReadonlyNonEmptyArray' + * import * as N from 'fp-ts/number' + * + * assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2]) + * + * @category combinators + * @since 2.11.0 */ export const uniq = (E: Eq) => (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray => { if (as.length === 1) { @@ -136,7 +145,43 @@ export const uniq = (E: Eq) => (as: ReadonlyNonEmptyArray): ReadonlyNon } /** - * @internal + * Sort the elements of a `ReadonlyNonEmptyArray` in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, + * etc... + * + * @example + * import * as RNEA from 'fp-ts/ReadonlyNonEmptyArray' + * import { contramap } from 'fp-ts/Ord' + * import * as S from 'fp-ts/string' + * import * as N from 'fp-ts/number' + * import { pipe } from 'fp-ts/function' + * + * interface Person { + * name: string + * age: number + * } + * + * const byName = pipe(S.Ord, contramap((p: Person) => p.name)) + * + * const byAge = pipe(N.Ord, contramap((p: Person) => p.age)) + * + * const sortByNameByAge = RNEA.sortBy([byName, byAge]) + * + * const persons: RNEA.ReadonlyNonEmptyArray = [ + * { name: 'a', age: 1 }, + * { name: 'b', age: 3 }, + * { name: 'c', age: 2 }, + * { name: 'b', age: 2 } + * ] + * + * assert.deepStrictEqual(sortByNameByAge(persons), [ + * { name: 'a', age: 1 }, + * { name: 'b', age: 2 }, + * { name: 'b', age: 3 }, + * { name: 'c', age: 2 } + * ]) + * + * @category combinators + * @since 2.11.0 */ export const sortBy = ( ords: ReadonlyArray> @@ -160,7 +205,16 @@ export const union = ( } /** - * @internal + * Rotate a `ReadonlyNonEmptyArray` by `n` steps. + * + * @example + * import { rotate } from 'fp-ts/ReadonlyNonEmptyArray' + * + * assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3]) + * assert.deepStrictEqual(rotate(-2)([1, 2, 3, 4, 5]), [3, 4, 5, 1, 2]) + * + * @category combinators + * @since 2.11.0 */ export const rotate = (n: number) => (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray => { const len = as.length @@ -213,6 +267,22 @@ export const makeBy = (f: (i: number) => A) => (n: number): ReadonlyNonEmptyA return out } +/** + * Create a `ReadonlyNonEmptyArray` containing a value repeated the specified number of times. + * + * **Note**. `n` is normalized to a natural number. + * + * @example + * import { replicate } from 'fp-ts/ReadonlyNonEmptyArray' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe(3, replicate('a')), ['a', 'a', 'a']) + * + * @category constructors + * @since 2.11.0 + */ +export const replicate = (a: A): ((n: number) => ReadonlyNonEmptyArray) => makeBy(() => a) + /** * Create a `ReadonlyNonEmptyArray` containing a range of integers, including both endpoints. * @@ -271,6 +341,20 @@ export const fromArray = (as: Array): Option> => // combinators // ------------------------------------------------------------------------------------- +/** + * @category combinators + * @since 2.11.0 + */ +export function concatW( + second: ReadonlyNonEmptyArray +): (first: ReadonlyArray) => ReadonlyNonEmptyArray +export function concatW( + second: ReadonlyArray +): (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +export function concatW(second: ReadonlyArray): (first: ReadonlyNonEmptyArray) => ReadonlyArray { + return (first: ReadonlyNonEmptyArray) => first.concat(second) +} + /** * @category combinators * @since 2.5.0 @@ -1187,6 +1271,14 @@ export const modifyHead = (f: Endomorphism) => (as: ReadonlyNonEmptyArray< ...tail(as) ] +/** + * Change the head, creating a new `ReadonlyNonEmptyArray`. + * + * @category combinators + * @since 2.11.0 + */ +export const updateHead = (a: A): ((as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => modifyHead(() => a) + /** * Apply a function to the last element, creating a new `ReadonlyNonEmptyArray`. * @@ -1195,6 +1287,14 @@ export const modifyHead = (f: Endomorphism) => (as: ReadonlyNonEmptyArray< export const modifyLast = (f: Endomorphism) => (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray => pipe(init(as), append(f(last(as)))) +/** + * Change the last element, creating a new `ReadonlyNonEmptyArray`. + * + * @category combinators + * @since 2.11.0 + */ +export const updateLast = (a: A): ((as: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => modifyLast(() => a) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index fb069e031..3706edc49 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -525,4 +525,27 @@ describe('NonEmptyArray', () => { U.deepStrictEqual(pipe(['a', 'b'], _.modifyLast(f)), ['a', 'b!']) U.deepStrictEqual(pipe(['a', 'b', 'c'], _.modifyLast(f)), ['a', 'b', 'c!']) }) + + it('replicate', () => { + const f = _.replicate('a') + U.deepStrictEqual(pipe(0, f), ['a']) + U.deepStrictEqual(pipe(1, f), ['a']) + U.deepStrictEqual(pipe(2, f), ['a', 'a']) + }) + + it('updateHead', () => { + U.deepStrictEqual(pipe(['a'], _.updateHead('d')), ['d']) + U.deepStrictEqual(pipe(['a', 'b'], _.updateHead('d')), ['d', 'b']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.updateHead('d')), ['d', 'b', 'c']) + }) + + it('updateLast', () => { + U.deepStrictEqual(pipe(['a'], _.updateLast('d')), ['d']) + U.deepStrictEqual(pipe(['a', 'b'], _.updateLast('d')), ['a', 'd']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.updateLast('d')), ['a', 'b', 'd']) + }) + + it('concatW', () => { + U.deepStrictEqual(pipe(['a'], _.concatW(['b'])), ['a', 'b']) + }) }) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index c752cca8f..e4012578d 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -668,4 +668,27 @@ describe('ReadonlyNonEmptyArray', () => { U.deepStrictEqual(_.range(2, 1), [2]) U.deepStrictEqual(_.range(-1, -2), [-1]) }) + + it('replicate', () => { + const f = _.replicate('a') + U.deepStrictEqual(pipe(0, f), ['a']) + U.deepStrictEqual(pipe(1, f), ['a']) + U.deepStrictEqual(pipe(2, f), ['a', 'a']) + }) + + it('updateHead', () => { + U.deepStrictEqual(pipe(['a'], _.updateHead('d')), ['d']) + U.deepStrictEqual(pipe(['a', 'b'], _.updateHead('d')), ['d', 'b']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.updateHead('d')), ['d', 'b', 'c']) + }) + + it('updateLast', () => { + U.deepStrictEqual(pipe(['a'], _.updateLast('d')), ['d']) + U.deepStrictEqual(pipe(['a', 'b'], _.updateLast('d')), ['a', 'd']) + U.deepStrictEqual(pipe(['a', 'b', 'c'], _.updateLast('d')), ['a', 'b', 'd']) + }) + + it('concatW', () => { + U.deepStrictEqual(pipe(['a'], _.concatW(['b'])), ['a', 'b']) + }) }) From 85cdc47203a1da3770bd5f7db37f8623bb5d5e7f Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 16:59:58 +0200 Subject: [PATCH 061/162] fix range deprecations in perf --- perf/Either/sequenceArray.ts | 6 +++--- perf/IO/sequenceArray.ts | 6 +++--- perf/Option/sequenceArray.ts | 6 +++--- perf/ReaderTask/traverseSeqArrayWithIndex.ts | 6 +++--- perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts | 6 +++--- perf/StateReaderTaskEither/sequenceArray.ts | 6 +++--- perf/Task/sequenceArray.ts | 6 +++--- perf/Task/stack.ts | 4 ++-- perf/Task/traverseSeqArrayWithIndex.ts | 6 +++--- perf/TaskEither/traverseSeqArrayWithIndex.ts | 6 +++--- perf/TaskOption.ts/traverseSeqArrayWithIndex.ts | 6 +++--- perf/TaskOption/traverseSeqArrayWithIndex.ts | 6 +++--- 12 files changed, 35 insertions(+), 35 deletions(-) diff --git a/perf/Either/sequenceArray.ts b/perf/Either/sequenceArray.ts index be97170a3..710825ec0 100644 --- a/perf/Either/sequenceArray.ts +++ b/perf/Either/sequenceArray.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/Either' import { pipe } from '../../src/function' @@ -11,11 +11,11 @@ Fastest is _.sequenceArray const suite = new Benchmark.Suite() -const as = pipe(A.range(0, 1000), A.map(_.of)) +const as = pipe(RNEA.range(0, 1000), RNEA.map(_.of)) suite .add('A.sequence(_.Applicative)', function () { - pipe(as, A.sequence(_.Applicative)) + pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { pipe(as, _.sequenceArray) diff --git a/perf/IO/sequenceArray.ts b/perf/IO/sequenceArray.ts index d1d50f710..7cdaf6915 100644 --- a/perf/IO/sequenceArray.ts +++ b/perf/IO/sequenceArray.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/IO' import { pipe } from '../../src/function' @@ -11,11 +11,11 @@ Fastest is _.sequenceArray const suite = new Benchmark.Suite() -const as = pipe(A.range(0, 1000), A.map(_.of)) +const as = pipe(RNEA.range(0, 1000), RNEA.map(_.of)) suite .add('A.sequence(_.Applicative)', function () { - pipe(as, A.sequence(_.Applicative)) + pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { pipe(as, _.sequenceArray) diff --git a/perf/Option/sequenceArray.ts b/perf/Option/sequenceArray.ts index 74b24d635..7e15baa0d 100644 --- a/perf/Option/sequenceArray.ts +++ b/perf/Option/sequenceArray.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/Option' import { pipe } from '../../src/function' @@ -11,11 +11,11 @@ Fastest is _.sequenceArray const suite = new Benchmark.Suite() -const as = pipe(A.range(0, 1000), A.map(_.of)) +const as = pipe(RNEA.range(0, 1000), RNEA.map(_.of)) suite .add('A.sequence(_.Applicative)', function () { - return pipe(as, A.sequence(_.Applicative)) + return pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { return pipe(as, _.sequenceArray) diff --git a/perf/ReaderTask/traverseSeqArrayWithIndex.ts b/perf/ReaderTask/traverseSeqArrayWithIndex.ts index bbbc6816e..99c1798d0 100644 --- a/perf/ReaderTask/traverseSeqArrayWithIndex.ts +++ b/perf/ReaderTask/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/ReaderTask' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )(undefined)() }) .add('_.traverseSeqArrayWithIndex', function () { diff --git a/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts b/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts index 78237b8cd..3e03076d3 100644 --- a/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts +++ b/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/ReaderTaskEither' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )(undefined)() }) .add('_.traverseSeqArrayWithIndex', function () { diff --git a/perf/StateReaderTaskEither/sequenceArray.ts b/perf/StateReaderTaskEither/sequenceArray.ts index a9e7eaa81..6b5b976d6 100644 --- a/perf/StateReaderTaskEither/sequenceArray.ts +++ b/perf/StateReaderTaskEither/sequenceArray.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/StateReaderTaskEither' import { pipe } from '../../src/function' @@ -11,11 +11,11 @@ Fastest is _.sequenceArray const suite = new Benchmark.Suite() -const as = pipe(A.range(0, 1000), A.map(_.of)) +const as = pipe(RNEA.range(0, 1000), RNEA.map(_.of)) suite .add('A.sequence(_.Applicative)', function () { - pipe(as, A.sequence(_.Applicative)) + pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { pipe(as, _.sequenceArray) diff --git a/perf/Task/sequenceArray.ts b/perf/Task/sequenceArray.ts index 69d40c6f5..c97d0f68a 100644 --- a/perf/Task/sequenceArray.ts +++ b/perf/Task/sequenceArray.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/Task' import { pipe } from '../../src/function' @@ -11,11 +11,11 @@ Fastest is _.sequenceArray const suite = new Benchmark.Suite() -const as = pipe(A.range(0, 1000), A.map(_.of)) +const as = pipe(RNEA.range(0, 1000), RNEA.map(_.of)) suite .add('A.sequence(_.ApplicativePar)', function () { - return pipe(as, A.sequence(_.ApplicativePar))() + return pipe(as, RNEA.sequence(_.ApplicativePar))() }) .add('_.sequenceArray', function () { return pipe(as, _.sequenceArray)() diff --git a/perf/Task/stack.ts b/perf/Task/stack.ts index e6f2ce100..86be2da38 100644 --- a/perf/Task/stack.ts +++ b/perf/Task/stack.ts @@ -1,8 +1,8 @@ -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/Task' import { pipe } from '../../src/function' -const as = A.range(0, 100000) +const as = RNEA.range(0, 100000) // tslint:disable-next-line: no-floating-promises pipe( diff --git a/perf/Task/traverseSeqArrayWithIndex.ts b/perf/Task/traverseSeqArrayWithIndex.ts index 54df0d198..3795930d6 100644 --- a/perf/Task/traverseSeqArrayWithIndex.ts +++ b/perf/Task/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/Task' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) .add('_.traverseSeqArrayWithIndex', function () { diff --git a/perf/TaskEither/traverseSeqArrayWithIndex.ts b/perf/TaskEither/traverseSeqArrayWithIndex.ts index b3fd4a21d..7e1832509 100644 --- a/perf/TaskEither/traverseSeqArrayWithIndex.ts +++ b/perf/TaskEither/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/TaskEither' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) .add('_.traverseSeqArrayWithIndex', function () { diff --git a/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts b/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts index 0d4bcf2b2..e997da572 100644 --- a/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts +++ b/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/TaskOption' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) .add('_.traverseSeqArrayWithIndex', function () { diff --git a/perf/TaskOption/traverseSeqArrayWithIndex.ts b/perf/TaskOption/traverseSeqArrayWithIndex.ts index 0d4bcf2b2..e997da572 100644 --- a/perf/TaskOption/traverseSeqArrayWithIndex.ts +++ b/perf/TaskOption/traverseSeqArrayWithIndex.ts @@ -1,5 +1,5 @@ import * as Benchmark from 'benchmark' -import * as A from '../../src/Array' +import * as RNEA from '../../src/ReadonlyNonEmptyArray' import * as _ from '../../src/TaskOption' import { pipe } from '../../src/function' @@ -11,13 +11,13 @@ Fastest is _.traverseSeqArrayWithIndex const suite = new Benchmark.Suite() -const as = A.range(0, 1000) +const as = pipe(RNEA.range(0, 1000)) suite .add('A.traverseWithIndex(_.ApplicativeSeq)', function () { return pipe( as, - A.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) + RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) .add('_.traverseSeqArrayWithIndex', function () { From a214689d32f5ea2cee30d29982e9cf2e43162d14 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 2 Apr 2021 16:46:47 +0200 Subject: [PATCH 062/162] update docs --- docs/modules/Either.ts.md | 8 +-- docs/modules/FromEither.ts.md | 119 ++-------------------------------- 2 files changed, 10 insertions(+), 117 deletions(-) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 612d905f4..90422de90 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -522,8 +522,8 @@ Added in v2.0.0 ```ts export declare const filterOrElse: { - (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either - (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either + (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either } ``` @@ -709,8 +709,8 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { - (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either + (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either } ``` diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index 48677e61b..61a13015f 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -34,89 +34,7 @@ Added in v2.10.0 --- -# type classes - -## FromEither (interface) - -**Signature** - -```ts -export interface FromEither { - readonly URI: F - readonly fromEither: (e: Either) => HKT2 -} -``` - -Added in v2.10.0 - -## FromEither2 (interface) - -**Signature** - -```ts -export interface FromEither2 { - readonly URI: F - readonly fromEither: (e: Either) => Kind2 -} -``` - -Added in v2.10.0 - -## FromEither2C (interface) - -**Signature** - -```ts -export interface FromEither2C { - readonly URI: F - readonly _E: E - readonly fromEither: (e: Either) => Kind2 -} -``` - -Added in v2.10.0 - -## FromEither3 (interface) - -**Signature** - -```ts -export interface FromEither3 { - readonly URI: F - readonly fromEither: (e: Either) => Kind3 -} -``` - -Added in v2.10.0 - -## FromEither3C (interface) - -**Signature** - -```ts -export interface FromEither3C { - readonly URI: F - readonly _E: E - readonly fromEither: (e: Either) => Kind3 -} -``` - -Added in v2.10.0 - -## FromEither4 (interface) - -**Signature** - -```ts -export interface FromEither4 { - readonly URI: F - readonly fromEither: (e: Either) => Kind4 -} -``` - -Added in v2.10.0 - -# utils +# combinators ## chainEitherK @@ -273,31 +191,6 @@ export declare function fromEitherK( Added in v2.10.0 -## fromOption - -**Signature** - -```ts -export declare function fromOption( - F: FromEither4 -): (onNone: Lazy) => (ma: Option) => Kind4 -export declare function fromOption( - F: FromEither3 -): (onNone: Lazy) => (ma: Option) => Kind3 -export declare function fromOption( - F: FromEither3C -): (onNone: Lazy) => (ma: Option) => Kind3 -export declare function fromOption( - F: FromEither2 -): (onNone: Lazy) => (ma: Option) => Kind2 -export declare function fromOption( - F: FromEither2C -): (onNone: Lazy) => (ma: Option) => Kind2 -export declare function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 -``` - -Added in v2.10.0 - ## fromOptionK **Signature** @@ -344,10 +237,10 @@ Added in v2.10.0 ```ts export declare function fromOption( F: FromEither4 -): (onNone: Lazy) => (ma: Option) => Kind4 +): (onNone: Lazy) => (ma: Option) => Kind4 export declare function fromOption( F: FromEither3 -): (onNone: Lazy) => (ma: Option) => Kind3 +): (onNone: Lazy) => (ma: Option) => Kind3 export declare function fromOption( F: FromEither3C ): (onNone: Lazy) => (ma: Option) => Kind3 @@ -469,7 +362,7 @@ Added in v2.10.0 ```ts export interface FromEither3 { readonly URI: F - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: (e: Either) => Kind3 } ``` @@ -483,7 +376,7 @@ Added in v2.10.0 export interface FromEither3C { readonly URI: F readonly _E: E - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: (e: Either) => Kind3 } ``` @@ -496,7 +389,7 @@ Added in v2.10.0 ```ts export interface FromEither4 { readonly URI: F - readonly fromEither: (e: Either) => Kind4 + readonly fromEither: (e: Either) => Kind4 } ``` From 410a48e5d4692da9d1afcb5af1ef61d4a4912a32 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 1 Apr 2021 17:37:15 +0200 Subject: [PATCH 063/162] - `Array` / `ReadonlyArray` - add `concat` / `concatW` - add `match`, `matchW`, `matchLeftW`, `matchRightW` - add `fromOptionK` --- CHANGELOG.md | 8 +++ docs/modules/Array.ts.md | 104 +++++++++++++++++++++++++++++-- docs/modules/ReadonlyArray.ts.md | 103 +++++++++++++++++++++++++++++- src/Array.ts | 79 ++++++++++++++++++++--- src/ReadonlyArray.ts | 80 +++++++++++++++++++++--- test/Array.ts | 24 +++++++ test/ReadonlyArray.ts | 26 ++++++++ 7 files changed, 398 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68e8aa063..6cc526382 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,10 @@ high state of flux, you're at risk of it changing without notice. - add `fromEither` - add `FromEither` instance - add `fromEitherK` + - make `isEmpty` a user defined guard + - add `concat` / `concatW` + - add `match`, `matchW`, `matchLeftW`, `matchRightW` + - add `fromOptionK` - `Either` - add `chainOptionK` - `function` @@ -119,6 +123,10 @@ high state of flux, you're at risk of it changing without notice. - add `fromEither` - add `FromEither` instance - add `fromEitherK` + - make `isEmpty` a user defined guard + - add `concat` / `concatW` + - add `match`, `matchW`, `matchLeftW`, `matchRightW` + - add `fromOptionK` - `ReadonlyMap` - add `union` - add `intersection` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 19b699182..f804bc6ae 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -68,6 +68,8 @@ Added in v2.0.0 - [chop](#chop) - [chunksOf](#chunksof) - [comprehension](#comprehension) + - [concat](#concat) + - [concatW](#concatw) - [copy](#copy) - [difference](#difference) - [dropLeft](#dropleft) @@ -77,6 +79,7 @@ Added in v2.0.0 - [flap](#flap) - [flatten](#flatten) - [fromEitherK](#fromeitherk) + - [fromOptionK](#fromoptionk) - [intersection](#intersection) - [intersperse](#intersperse) - [lefts](#lefts) @@ -120,8 +123,12 @@ Added in v2.0.0 - [head](#head) - [init](#init) - [last](#last) + - [match](#match) - [matchLeft](#matchleft) + - [matchLeftW](#matchleftw) - [matchRight](#matchright) + - [matchRightW](#matchrightw) + - [matchW](#matchw) - [spanLeft](#spanleft) - [tail](#tail) - [guards](#guards) @@ -732,6 +739,26 @@ assert.deepStrictEqual( Added in v2.0.0 +## concat + +**Signature** + +```ts +export declare const concat: (second: A[]) => (first: A[]) => A[] +``` + +Added in v2.11.0 + +## concatW + +**Signature** + +```ts +export declare const concatW: (second: B[]) => (first: A[]) => (B | A)[] +``` + +Added in v2.11.0 + ## copy **Signature** @@ -880,6 +907,16 @@ export declare const fromEitherK: (f: (...a: A) => Either) => (.. Added in v2.11.0 +## fromOptionK + +**Signature** + +```ts +export declare const fromOptionK: (f: (...a: A) => Option) => (...a: A) => B[] +``` + +Added in v2.11.0 + ## intersection Creates an array of unique values that are included in all given arrays using a `Eq` for equality @@ -1378,7 +1415,7 @@ Added in v2.11.0 ## fromEither -Transforms an `Either` to a `ReadonlyArray`. +Transforms an `Either` to a `Array`. **Signature** @@ -1393,7 +1430,7 @@ Added in v2.11.0 **Signature** ```ts -export declare const fromOption: (ma: Option) => readonly A[] +export declare const fromOption: (ma: Option) => A[] ``` Added in v2.11.0 @@ -1739,9 +1776,21 @@ assert.deepStrictEqual(last([]), none) Added in v2.0.0 +## match + +Less strict version of [`match`](#match). + +**Signature** + +```ts +export declare const match: (onEmpty: Lazy, onNonEmpty: (as: NEA.NonEmptyArray) => B) => (as: A[]) => B +``` + +Added in v2.11.0 + ## matchLeft -Break an array into its first element and remaining elements +Break an `Array` into its first element and remaining elements. **Signature** @@ -1763,9 +1812,24 @@ assert.strictEqual(len([1, 2, 3]), 3) Added in v2.10.0 +## matchLeftW + +Less strict version of [`matchLeft`](#matchLeft). + +**Signature** + +```ts +export declare const matchLeftW: ( + onEmpty: Lazy, + onNonEmpty: (head: A, tail: A[]) => C +) => (as: A[]) => B | C +``` + +Added in v3.0.0 + ## matchRight -Break an array into its initial elements and the last element +Break an `Array` into its initial elements and the last element. **Signature** @@ -1775,6 +1839,36 @@ export declare const matchRight: (onEmpty: Lazy, onNonEmpty: (init: A[] Added in v2.10.0 +## matchRightW + +Less strict version of [`matchRight`](#matchRight). + +**Signature** + +```ts +export declare const matchRightW: ( + onEmpty: Lazy, + onNonEmpty: (init: A[], last: A) => C +) => (as: A[]) => B | C +``` + +Added in v2.11.0 + +## matchW + +Less strict version of [`match`](#match). + +**Signature** + +```ts +export declare const matchW: ( + onEmpty: Lazy, + onNonEmpty: (as: NEA.NonEmptyArray) => C +) => (as: A[]) => B | C +``` + +Added in v2.11.0 + ## spanLeft Split an array into two parts: @@ -2474,7 +2568,7 @@ Test whether an array is empty **Signature** ```ts -export declare const isEmpty: (as: A[]) => boolean +export declare const isEmpty: (as: A[]) => as is [] ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index a12000879..30ebeb037 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -68,6 +68,8 @@ Added in v2.5.0 - [chop](#chop) - [chunksOf](#chunksof) - [comprehension](#comprehension) + - [concat](#concat) + - [concatW](#concatw) - [difference](#difference) - [dropLeft](#dropleft) - [dropLeftWhile](#dropleftwhile) @@ -76,6 +78,7 @@ Added in v2.5.0 - [flap](#flap) - [flatten](#flatten) - [fromEitherK](#fromeitherk) + - [fromOptionK](#fromoptionk) - [intersection](#intersection) - [intersperse](#intersperse) - [prependAll](#prependall) @@ -111,8 +114,12 @@ Added in v2.5.0 - [destructors](#destructors) - [foldLeft](#foldleft) - [foldRight](#foldright) + - [match](#match) - [matchLeft](#matchleft) + - [matchLeftW](#matchleftw) - [matchRight](#matchright) + - [matchRightW](#matchrightw) + - [matchW](#matchw) - [guards](#guards) - [isNonEmpty](#isnonempty) - [instances](#instances) @@ -744,6 +751,26 @@ assert.deepStrictEqual( Added in v2.5.0 +## concat + +**Signature** + +```ts +export declare const concat: (second: readonly A[]) => (first: readonly A[]) => readonly A[] +``` + +Added in v2.11.0 + +## concatW + +**Signature** + +```ts +export declare const concatW: (second: readonly B[]) => (first: readonly A[]) => readonly (B | A)[] +``` + +Added in v2.11.0 + ## difference Creates an array of array values not included in the other given array using a `Eq` for equality @@ -890,6 +917,18 @@ export declare const fromEitherK: (f: (...a: A) => Either) => (.. Added in v2.11.0 +## fromOptionK + +**Signature** + +```ts +export declare const fromOptionK: ( + f: (...a: A) => Option +) => (...a: A) => readonly B[] +``` + +Added in v2.11.0 + ## intersection Creates an array of unique values that are included in all given arrays using a `Eq` for equality @@ -1563,9 +1602,22 @@ export declare const foldRight: ( Added in v2.5.0 +## match + +**Signature** + +```ts +export declare const match: ( + onEmpty: Lazy, + onNonEmpty: (as: RNEA.ReadonlyNonEmptyArray) => B +) => (as: readonly A[]) => B +``` + +Added in v2.11.0 + ## matchLeft -Break an array into its first element and remaining elements. +Break a `ReadonlyArray` into its first element and remaining elements. **Signature** @@ -1590,9 +1642,24 @@ assert.strictEqual(len([1, 2, 3]), 3) Added in v2.10.0 +## matchLeftW + +Less strict version of [`matchLeft`](#matchLeft). + +**Signature** + +```ts +export declare const matchLeftW: ( + onEmpty: Lazy, + onNonEmpty: (head: A, tail: readonly A[]) => C +) => (as: readonly A[]) => B | C +``` + +Added in v2.11.0 + ## matchRight -Break an array into its initial elements and the last element. +Break a `ReadonlyArray` into its initial elements and the last element. **Signature** @@ -1605,6 +1672,36 @@ export declare const matchRight: ( Added in v2.10.0 +## matchRightW + +Less strict version of [`matchRight`](#matchRight). + +**Signature** + +```ts +export declare const matchRightW: ( + onEmpty: Lazy, + onNonEmpty: (init: readonly A[], last: A) => C +) => (as: readonly A[]) => B | C +``` + +Added in v2.11.0 + +## matchW + +Less strict version of [`match`](#match). + +**Signature** + +```ts +export declare const matchW: ( + onEmpty: Lazy, + onNonEmpty: (as: RNEA.ReadonlyNonEmptyArray) => C +) => (as: readonly A[]) => B | C +``` + +Added in v2.11.0 + # guards ## isNonEmpty @@ -2497,7 +2594,7 @@ Test whether a `ReadonlyArray` is empty. **Signature** ```ts -export declare const isEmpty: (as: readonly A[]) => boolean +export declare const isEmpty: (as: readonly A[]) => as is readonly [] ``` **Example** diff --git a/src/Array.ts b/src/Array.ts index 2e95b03c9..5cb92647e 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -141,10 +141,10 @@ export function fromPredicate(predicate: Predicate): (a: A) => Array { * @category constructors * @since 2.11.0 */ -export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? [] : [ma.value]) +export const fromOption = (ma: Option): Array => (_.isNone(ma) ? [] : [ma.value]) /** - * Transforms an `Either` to a `ReadonlyArray`. + * Transforms an `Either` to a `Array`. * * @category constructors * @since 2.11.0 @@ -156,7 +156,34 @@ export const fromEither = (e: Either): Array => (_.isLeft(e) ? [] // ------------------------------------------------------------------------------------- /** - * Break an array into its first element and remaining elements + * Less strict version of [`match`](#match). + * + * @category destructors + * @since 2.11.0 + */ +export const matchW = (onEmpty: Lazy, onNonEmpty: (as: NonEmptyArray) => C) => (as: Array): B | C => + isNonEmpty(as) ? onNonEmpty(as) : onEmpty() + +/** + * Less strict version of [`match`](#match). + * + * @category destructors + * @since 2.11.0 + */ +export const match: (onEmpty: Lazy, onNonEmpty: (as: NonEmptyArray) => B) => (as: Array) => B = matchW + +/** + * Less strict version of [`matchLeft`](#matchLeft). + * + * @category destructors + * @since 3.0.0 + */ +export const matchLeftW = (onEmpty: Lazy, onNonEmpty: (head: A, tail: Array) => C) => ( + as: Array +): B | C => (isNonEmpty(as) ? onNonEmpty(NEA.head(as), NEA.tail(as)) : onEmpty()) + +/** + * Break an `Array` into its first element and remaining elements. * * @example * import { matchLeft } from 'fp-ts/Array' @@ -167,8 +194,10 @@ export const fromEither = (e: Either): Array => (_.isLeft(e) ? [] * @category destructors * @since 2.10.0 */ -export const matchLeft = (onEmpty: Lazy, onNonEmpty: (head: A, tail: Array) => B) => (as: Array): B => - isNonEmpty(as) ? onNonEmpty(NEA.head(as), NEA.tail(as)) : onEmpty() +export const matchLeft: ( + onEmpty: Lazy, + onNonEmpty: (head: A, tail: Array) => B +) => (as: Array) => B = matchLeftW /** * Alias of [`matchLeft`](#matchLeft). @@ -182,13 +211,25 @@ export const foldLeft: ( ) => (as: Array) => B = matchLeft /** - * Break an array into its initial elements and the last element + * Less strict version of [`matchRight`](#matchRight). + * + * @category destructors + * @since 2.11.0 + */ +export const matchRightW = (onEmpty: Lazy, onNonEmpty: (init: Array, last: A) => C) => ( + as: Array +): B | C => (isNonEmpty(as) ? onNonEmpty(NEA.init(as), NEA.last(as)) : onEmpty()) + +/** + * Break an `Array` into its initial elements and the last element. * * @category destructors * @since 2.10.0 */ -export const matchRight = (onEmpty: Lazy, onNonEmpty: (init: Array, last: A) => B) => (as: Array): B => - isNonEmpty(as) ? onNonEmpty(NEA.init(as), NEA.last(as)) : onEmpty() +export const matchRight: ( + onEmpty: Lazy, + onNonEmpty: (init: Array, last: A) => B +) => (as: Array) => B = matchRightW /** * Alias of [`matchRight`](#matchRight). @@ -269,7 +310,7 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: Array): N * * @since 2.0.0 */ -export const isEmpty: (as: Array) => boolean = RA.isEmpty +export const isEmpty = (as: Array): as is [] => as.length === 0 /** * Test whether an array is non empty narrowing down the type to `NonEmptyArray` @@ -1035,6 +1076,13 @@ export const chunksOf = (n: number): ((as: Array) => Array (isNonEmpty(as) ? f(as) : []) } +/** + * @category combinators + * @since 2.11.0 + */ +export const fromOptionK = , B>(f: (...a: A) => Option) => (...a: A): Array => + fromOption(f(...a)) + /** * `Array` comprehension. * @@ -1089,6 +1137,19 @@ export function comprehension( return go([], input) } +/** + * @category combinators + * @since 2.11.0 + */ +export const concatW = (second: Array) => (first: Array): Array => + isEmpty(first) ? copy(second) : isEmpty(second) ? copy(first) : (first as Array).concat(second) + +/** + * @category combinators + * @since 2.11.0 + */ +export const concat: (second: Array) => (first: Array) => Array = concatW + // TODO: remove non-curried overloading in v3 /** * Creates an array of unique values, in order, from all given arrays using a `Eq` for equality comparisons diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 4c326981b..5c5337b41 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -157,7 +157,36 @@ export const fromEither = (e: Either): ReadonlyArray => (_.isLeft // ------------------------------------------------------------------------------------- /** - * Break an array into its first element and remaining elements. + * Less strict version of [`match`](#match). + * + * @category destructors + * @since 2.11.0 + */ +export const matchW = (onEmpty: Lazy, onNonEmpty: (as: ReadonlyNonEmptyArray) => C) => ( + as: ReadonlyArray +): B | C => (isNonEmpty(as) ? onNonEmpty(as) : onEmpty()) + +/** + * @category destructors + * @since 2.11.0 + */ +export const match: ( + onEmpty: Lazy, + onNonEmpty: (as: ReadonlyNonEmptyArray) => B +) => (as: ReadonlyArray) => B = matchW + +/** + * Less strict version of [`matchLeft`](#matchLeft). + * + * @category destructors + * @since 2.11.0 + */ +export const matchLeftW = (onEmpty: Lazy, onNonEmpty: (head: A, tail: ReadonlyArray) => C) => ( + as: ReadonlyArray +): B | C => (isNonEmpty(as) ? onNonEmpty(RNEA.head(as), RNEA.tail(as)) : onEmpty()) + +/** + * Break a `ReadonlyArray` into its first element and remaining elements. * * @example * import { matchLeft } from 'fp-ts/ReadonlyArray' @@ -168,9 +197,10 @@ export const fromEither = (e: Either): ReadonlyArray => (_.isLeft * @category destructors * @since 2.10.0 */ -export const matchLeft = (onEmpty: Lazy, onNonEmpty: (head: A, tail: ReadonlyArray) => B) => ( - as: ReadonlyArray -): B => (isNonEmpty(as) ? onNonEmpty(RNEA.head(as), RNEA.tail(as)) : onEmpty()) +export const matchLeft: ( + onEmpty: Lazy, + onNonEmpty: (head: A, tail: ReadonlyArray) => B +) => (as: ReadonlyArray) => B = matchLeftW /** * Alias of [`matchLeft`](#matchLeft). @@ -184,14 +214,25 @@ export const foldLeft: ( ) => (as: ReadonlyArray) => B = matchLeft /** - * Break an array into its initial elements and the last element. + * Less strict version of [`matchRight`](#matchRight). * * @category destructors - * @since 2.10.0 + * @since 2.11.0 */ -export const matchRight = (onEmpty: Lazy, onNonEmpty: (init: ReadonlyArray, last: A) => B) => ( +export const matchRightW = (onEmpty: Lazy, onNonEmpty: (init: ReadonlyArray, last: A) => C) => ( as: ReadonlyArray -): B => (isNonEmpty(as) ? onNonEmpty(RNEA.init(as), RNEA.last(as)) : onEmpty()) +): B | C => (isNonEmpty(as) ? onNonEmpty(RNEA.init(as), RNEA.last(as)) : onEmpty()) + +/** + * Break a `ReadonlyArray` into its initial elements and the last element. + * + * @category destructors + * @since 2.10.0 + */ +export const matchRight: ( + onEmpty: Lazy, + onNonEmpty: (init: ReadonlyArray, last: A) => B +) => (as: ReadonlyArray) => B = matchRightW /** * Alias of [`matchRight`](#matchRight). @@ -277,7 +318,7 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: ReadonlyArra * * @since 2.5.0 */ -export const isEmpty = (as: ReadonlyArray): boolean => as.length === 0 +export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 /** * Test whether a `ReadonlyArray` is non empty. @@ -1124,6 +1165,14 @@ export const chunksOf = (n: number): ((as: ReadonlyArray) => ReadonlyArray return (as) => (isNonEmpty(as) ? f(as) : empty) } +/** + * @category combinators + * @since 2.11.0 + */ +export const fromOptionK = , B>(f: (...a: A) => Option) => ( + ...a: A +): ReadonlyArray => fromOption(f(...a)) + /** * `ReadonlyArray` comprehension. * @@ -1182,6 +1231,19 @@ export function comprehension( return go(empty, input) } +/** + * @category combinators + * @since 2.11.0 + */ +export const concatW = (second: ReadonlyArray) => (first: ReadonlyArray): ReadonlyArray => + isEmpty(first) ? second : isEmpty(second) ? first : (first as ReadonlyArray).concat(second) + +/** + * @category combinators + * @since 2.11.0 + */ +export const concat: (second: ReadonlyArray) => (first: ReadonlyArray) => ReadonlyArray = concatW + // TODO: remove non-curried overloading in v3 /** * Creates an array of unique values, in order, from all given arrays using a `Eq` for equality comparisons diff --git a/test/Array.ts b/test/Array.ts index db0958c6d..bb01e10ef 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -1175,4 +1175,28 @@ describe('Array', () => { U.deepStrictEqual(_.fromEither(E.right(1)), [1]) U.deepStrictEqual(_.fromEither(E.left('a')), []) }) + + it('match', () => { + const f = _.match( + () => 'empty', + (as) => `nonEmpty ${as.length}` + ) + U.deepStrictEqual(pipe([], f), 'empty') + U.deepStrictEqual(pipe([1, 2, 3], f), 'nonEmpty 3') + }) + + it('concatW', () => { + U.deepStrictEqual(pipe([1], _.concatW(['a'])), [1, 'a']) + const as = [1, 2, 3] + const empty: Array = [] + U.deepStrictEqual(pipe(empty, _.concatW(as)), as) + U.deepStrictEqual(pipe(as, _.concatW(empty)), as) + }) + + it('fromOptionK', () => { + const f = (n: number) => (n > 0 ? O.some(n) : O.none) + const g = _.fromOptionK(f) + U.deepStrictEqual(g(0), []) + U.deepStrictEqual(g(1), [1]) + }) }) diff --git a/test/ReadonlyArray.ts b/test/ReadonlyArray.ts index b5a9d05cd..68205872c 100644 --- a/test/ReadonlyArray.ts +++ b/test/ReadonlyArray.ts @@ -1411,4 +1411,30 @@ describe('ReadonlyArray', () => { U.deepStrictEqual(_.fromEither(E.right(1)), [1]) U.strictEqual(_.fromEither(E.left('a')), _.empty) }) + + it('match', () => { + const f = _.match( + () => 'empty', + (as) => `nonEmpty ${as.length}` + ) + U.deepStrictEqual(pipe(_.empty, f), 'empty') + U.deepStrictEqual(pipe([1, 2, 3], f), 'nonEmpty 3') + }) + + it('concatW', () => { + U.deepStrictEqual(pipe([1], _.concatW(['a'])), [1, 'a']) + const as = [1, 2, 3] + U.strictEqual(pipe(_.empty, _.concatW(as)), as) + U.strictEqual(pipe(as, _.concatW(_.empty)), as) + const empty: ReadonlyArray = [] + U.strictEqual(pipe(empty, _.concatW(as)), as) + U.strictEqual(pipe(as, _.concatW(empty)), as) + }) + + it('fromOptionK', () => { + const f = (n: number) => (n > 0 ? O.some(n) : O.none) + const g = _.fromOptionK(f) + U.strictEqual(g(0), _.empty) + U.deepStrictEqual(g(1), [1]) + }) }) From 40414cc3e22c21f62683fc048a1c301e6121f205 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 2 Apr 2021 17:57:46 +0200 Subject: [PATCH 064/162] - `Map` - add `getFoldable` - `Map` / `ReadonlyMap` - add `foldMap` - add `reduceRight` - add `reduceWithIndex` - add `foldMapWithIndex` - add `reduceRightWithIndex` --- CHANGELOG.md | 12 ++++ docs/modules/Map.ts.md | 81 ++++++++++++++++++++++ docs/modules/ReadonlyMap.ts.md | 72 ++++++++++++++++++++ src/Map.ts | 90 ++++++++++++------------- src/ReadonlyMap.ts | 120 +++++++++++++++++++++++---------- test/Map.ts | 35 +++++----- 6 files changed, 315 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cc526382..b1dbf3516 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,12 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - add `getFoldable` + - add `foldMap` + - add `reduceRight` + - add `reduceWithIndex` + - add `foldMapWithIndex` + - add `reduceRightWithIndex` - `NonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` @@ -135,6 +141,12 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionMonoid` - add `getIntersectionSemigroup` - add `getDifferenceMagma` + - add `reduce` + - add `foldMap` + - add `reduceRight` + - add `reduceWithIndex` + - add `foldMapWithIndex` + - add `reduceRightWithIndex` - `ReadonlyNonEmptyArray` - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) - add `union` diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index e2457bf8c..3db89ffd3 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -44,6 +44,7 @@ Added in v2.0.0 - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getFilterableWithIndex](#getfilterablewithindex) + - [getFoldable](#getfoldable) - [getFoldableWithIndex](#getfoldablewithindex) - [getIntersectionSemigroup](#getintersectionsemigroup) - [getMonoid](#getmonoid) @@ -57,6 +58,8 @@ Added in v2.0.0 - [collect](#collect) - [difference](#difference) - [elem](#elem) + - [foldMap](#foldmap) + - [foldMapWithIndex](#foldmapwithindex) - [intersection](#intersection) - [isEmpty](#isempty) - [isSubmap](#issubmap) @@ -66,6 +69,10 @@ Added in v2.0.0 - [member](#member) - [modifyAt](#modifyat) - [pop](#pop) + - [reduce](#reduce) + - [reduceRight](#reduceright) + - [reduceRightWithIndex](#reducerightwithindex) + - [reduceWithIndex](#reducewithindex) - [singleton](#singleton) - [size](#size) - [toArray](#toarray) @@ -382,6 +389,16 @@ export declare function getFilterableWithIndex(): FilterableWithIndex Added in v2.0.0 +## getFoldable + +**Signature** + +```ts +export declare const getFoldable: (O: Ord) => Foldable2C<'Map', K> +``` + +Added in v2.11.0 + ## getFoldableWithIndex **Signature** @@ -510,6 +527,28 @@ export declare const elem: (E: Eq) => { (a: A): (m: Map) => boole Added in v2.0.0 +## foldMap + +**Signature** + +```ts +export declare const foldMap: (O: Ord) => (M: Monoid) => (f: (a: A) => M) => (m: Map) => M +``` + +Added in v2.11.0 + +## foldMapWithIndex + +**Signature** + +```ts +export declare const foldMapWithIndex: ( + O: Ord +) => (M: Monoid) => (f: (k: K, a: A) => M) => (m: Map) => M +``` + +Added in v2.11.0 + ## intersection **Signature** @@ -628,6 +667,48 @@ export declare function pop(E: Eq): (k: K) => (m: Map) => Option< Added in v2.0.0 +## reduce + +**Signature** + +```ts +export declare const reduce: (O: Ord) => (b: B, f: (b: B, a: A) => B) => (m: Map) => B +``` + +Added in v2.11.0 + +## reduceRight + +**Signature** + +```ts +export declare const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (m: Map) => B +``` + +Added in v2.11.0 + +## reduceRightWithIndex + +**Signature** + +```ts +export declare const reduceRightWithIndex: ( + O: Ord +) => (b: B, f: (k: K, a: A, b: B) => B) => (m: Map) => B +``` + +Added in v2.11.0 + +## reduceWithIndex + +**Signature** + +```ts +export declare const reduceWithIndex: (O: Ord) => (b: B, f: (k: K, b: B, a: A) => B) => (m: Map) => B +``` + +Added in v2.11.0 + ## singleton Create a map with one key/value pair diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index 5948d197f..dff4134b2 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -66,6 +66,8 @@ Added in v2.5.0 - [difference](#difference) - [elem](#elem) - [empty](#empty) + - [foldMap](#foldmap) + - [foldMapWithIndex](#foldmapwithindex) - [intersection](#intersection) - [isEmpty](#isempty) - [isSubmap](#issubmap) @@ -75,6 +77,10 @@ Added in v2.5.0 - [member](#member) - [modifyAt](#modifyat) - [pop](#pop) + - [reduce](#reduce) + - [reduceRight](#reduceright) + - [reduceRightWithIndex](#reducerightwithindex) + - [reduceWithIndex](#reducewithindex) - [size](#size) - [toReadonlyArray](#toreadonlyarray) - [union](#union) @@ -628,6 +634,28 @@ export declare const empty: ReadonlyMap Added in v2.5.0 +## foldMap + +**Signature** + +```ts +export declare const foldMap: (O: Ord) => (M: Monoid) => (f: (a: A) => M) => (m: ReadonlyMap) => M +``` + +Added in v2.11.0 + +## foldMapWithIndex + +**Signature** + +```ts +export declare const foldMapWithIndex: ( + O: Ord +) => (M: Monoid) => (f: (k: K, a: A) => M) => (m: ReadonlyMap) => M +``` + +Added in v2.11.0 + ## intersection **Signature** @@ -759,6 +787,50 @@ export declare function pop(E: Eq): (k: K) => (m: ReadonlyMap) => Added in v2.5.0 +## reduce + +**Signature** + +```ts +export declare const reduce: (O: Ord) => (b: B, f: (b: B, a: A) => B) => (m: ReadonlyMap) => B +``` + +Added in v2.11.0 + +## reduceRight + +**Signature** + +```ts +export declare const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (m: ReadonlyMap) => B +``` + +Added in v2.11.0 + +## reduceRightWithIndex + +**Signature** + +```ts +export declare const reduceRightWithIndex: ( + O: Ord +) => (b: B, f: (k: K, a: A, b: B) => B) => (m: ReadonlyMap) => B +``` + +Added in v2.11.0 + +## reduceWithIndex + +**Signature** + +```ts +export declare const reduceWithIndex: ( + O: Ord +) => (b: B, f: (k: K, b: B, a: A) => B) => (m: ReadonlyMap) => B +``` + +Added in v2.11.0 + ## size Calculate the number of key/value pairs in a map diff --git a/src/Map.ts b/src/Map.ts index 3d1e871cb..202fba8d6 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -7,7 +7,7 @@ import { Either } from './Either' import { Eq } from './Eq' import { Filterable2 } from './Filterable' import { FilterableWithIndex2C } from './FilterableWithIndex' -import { Foldable, Foldable1, Foldable2, Foldable3 } from './Foldable' +import { Foldable, Foldable1, Foldable2, Foldable2C, Foldable3 } from './Foldable' import { FoldableWithIndex2C } from './FoldableWithIndex' import { pipe } from './function' import { flap as flap_, Functor2 } from './Functor' @@ -692,57 +692,57 @@ export function getWitherable(O: Ord): Witherable2C & TraversableW } /** - * @category instances - * @since 2.10.0 + * @since 2.11.0 */ -export const getFoldableWithIndex = (O: Ord): FoldableWithIndex2C => { - const keysO = keys(O) +export const reduce: (O: Ord) => (b: B, f: (b: B, a: A) => B) => (m: Map) => B = RM.reduce - const reduceWithIndex = (fa: Map, b: B, f: (k: K, b: B, a: A) => B): B => { - let out: B = b - const ks = keysO(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = f(k, out, fa.get(k)!) - } - return out - } +/** + * @since 2.11.0 + */ +export const foldMap: (O: Ord) => (M: Monoid) => (f: (a: A) => M) => (m: Map) => M = RM.foldMap - const foldMapWithIndex = (M: Monoid) => (fa: Map, f: (k: K, a: A) => M): M => { - let out: M = M.empty - const ks = keysO(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = M.concat(out, f(k, fa.get(k)!)) - } - return out - } +/** + * @since 2.11.0 + */ +export const reduceRight: (O: Ord) => (b: B, f: (a: A, b: B) => B) => (m: Map) => B = RM.reduceRight - const reduceRightWithIndex = (fa: Map, b: B, f: (k: K, a: A, b: B) => B): B => { - let out: B = b - const ks = keysO(fa) - const len = ks.length - for (let i = len - 1; i >= 0; i--) { - const k = ks[i] - out = f(k, fa.get(k)!, out) - } - return out +/** + * @category instances + * @since 2.11.0 + */ +export const getFoldable = (O: Ord): Foldable2C => { + return { + ...RM.getFoldable(O), + URI } +} + +/** + * @since 2.11.0 + */ +export const reduceWithIndex: (O: Ord) => (b: B, f: (k: K, b: B, a: A) => B) => (m: Map) => B = + RM.reduceWithIndex + +/** + * @since 2.11.0 + */ +export const foldMapWithIndex: (O: Ord) => (M: Monoid) => (f: (k: K, a: A) => M) => (m: Map) => M = + RM.foldMapWithIndex + +/** + * @since 2.11.0 + */ +export const reduceRightWithIndex: (O: Ord) => (b: B, f: (k: K, a: A, b: B) => B) => (m: Map) => B = + RM.reduceRightWithIndex +/** + * @category instances + * @since 2.10.0 + */ +export const getFoldableWithIndex = (O: Ord): FoldableWithIndex2C => { return { - URI, - _E: undefined as any, - reduce: (fa, b, f) => reduceWithIndex(fa, b, (_, b, a) => f(b, a)), - foldMap: (M) => { - const foldMapWithIndexM = foldMapWithIndex(M) - return (fa, f) => foldMapWithIndexM(fa, (_, a) => f(a)) - }, - reduceRight: (fa, b, f) => reduceRightWithIndex(fa, b, (_, a, b) => f(a, b)), - reduceWithIndex, - foldMapWithIndex, - reduceRightWithIndex + ...RM.getFoldableWithIndex(O), + URI } } diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 7eddb8bb6..48526f0b8 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -859,73 +859,125 @@ export const Filterable: Filterable2 = { partitionMap: _partitionMap } +/** + * @since 2.11.0 + */ +export const reduce = (O: Ord): ((b: B, f: (b: B, a: A) => B) => (m: ReadonlyMap) => B) => { + const reduceWithIndexO = reduceWithIndex(O) + return (b, f) => reduceWithIndexO(b, (_, b, a) => f(b, a)) +} + +/** + * @since 2.11.0 + */ +export const foldMap = (O: Ord): ((M: Monoid) => (f: (a: A) => M) => (m: ReadonlyMap) => M) => { + const foldMapWithIndexO = foldMapWithIndex(O) + return (M) => { + const foldMapWithIndexOM = foldMapWithIndexO(M) + return (f) => foldMapWithIndexOM((_, a) => f(a)) + } +} + +/** + * @since 2.11.0 + */ +export const reduceRight = (O: Ord): ((b: B, f: (a: A, b: B) => B) => (m: ReadonlyMap) => B) => { + const reduceRightWithIndexO = reduceRightWithIndex(O) + return (b, f) => reduceRightWithIndexO(b, (_, b, a) => f(b, a)) +} + /** * @category instances * @since 2.10.0 */ export const getFoldable = (O: Ord): Foldable2C => { - const FWI = getFoldableWithIndex(O) + const reduceO = reduce(O) + const foldMapO = foldMap(O) + const reduceRightO = reduceRight(O) return { URI, _E: undefined as any, - reduce: FWI.reduce, - foldMap: FWI.foldMap, - reduceRight: FWI.reduceRight + reduce: (fa, b, f) => pipe(fa, reduceO(b, f)), + foldMap: (M) => { + const foldMapOM = foldMapO(M) + return (fa, f) => pipe(fa, foldMapOM(f)) + }, + reduceRight: (fa, b, f) => pipe(fa, reduceRightO(b, f)) } } /** - * @category instances - * @since 2.10.0 + * @since 2.11.0 */ -export const getFoldableWithIndex = (O: Ord): FoldableWithIndex2C => { +export const reduceWithIndex = ( + O: Ord +): ((b: B, f: (k: K, b: B, a: A) => B) => (m: ReadonlyMap) => B) => { const keysO = keys(O) - - const reduceWithIndex = (fa: ReadonlyMap, b: B, f: (k: K, b: B, a: A) => B): B => { - let out: B = b - const ks = keysO(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = f(k, out, fa.get(k)!) + return (b, f) => (m) => { + let out = b + for (const k of keysO(m)) { + out = f(k, out, m.get(k)!) } return out } +} - const foldMapWithIndex = (M: Monoid) => (fa: ReadonlyMap, f: (k: K, a: A) => M): M => { - let out: M = M.empty - const ks = keysO(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = M.concat(out, f(k, fa.get(k)!)) +/** + * @since 2.11.0 + */ +export const foldMapWithIndex = ( + O: Ord +): ((M: Monoid) => (f: (k: K, a: A) => M) => (m: ReadonlyMap) => M) => { + const keysO = keys(O) + return (M) => (f) => (m) => { + let out = M.empty + for (const k of keysO(m)) { + out = M.concat(out, f(k, m.get(k)!)) } return out } +} - const reduceRightWithIndex = (fa: ReadonlyMap, b: B, f: (k: K, a: A, b: B) => B): B => { - let out: B = b - const ks = keysO(fa) +/** + * @since 2.11.0 + */ +export const reduceRightWithIndex = ( + O: Ord +): ((b: B, f: (k: K, a: A, b: B) => B) => (m: ReadonlyMap) => B) => { + const keysO = keys(O) + return (b, f) => (m) => { + let out = b + const ks = keysO(m) const len = ks.length for (let i = len - 1; i >= 0; i--) { const k = ks[i] - out = f(k, fa.get(k)!, out) + out = f(k, m.get(k)!, out) } return out } +} +/** + * @category instances + * @since 2.10.0 + */ +export const getFoldableWithIndex = (O: Ord): FoldableWithIndex2C => { + const F = getFoldable(O) + const reduceWithIndexO = reduceWithIndex(O) + const foldMapWithIndexO = foldMapWithIndex(O) + const reduceRightWithIndexO = reduceRightWithIndex(O) return { URI, _E: undefined as any, - reduce: (fa, b, f) => reduceWithIndex(fa, b, (_, b, a) => f(b, a)), - foldMap: (M) => { - const foldMapWithIndexM = foldMapWithIndex(M) - return (fa, f) => foldMapWithIndexM(fa, (_, a) => f(a)) + reduce: F.reduce, + foldMap: F.foldMap, + reduceRight: F.reduceRight, + reduceWithIndex: (fa, b, f) => pipe(fa, reduceWithIndexO(b, f)), + foldMapWithIndex: (M) => { + const foldMapWithIndexOM = foldMapWithIndexO(M) + return (fa, f) => pipe(fa, foldMapWithIndexOM(f)) }, - reduceRight: (fa, b, f) => reduceRightWithIndex(fa, b, (_, a, b) => f(a, b)), - reduceWithIndex, - foldMapWithIndex, - reduceRightWithIndex + reduceRightWithIndex: (fa, b, f) => pipe(fa, reduceRightWithIndexO(b, f)) } } diff --git a/test/Map.ts b/test/Map.ts index 5c0a58f17..fee42208d 100644 --- a/test/Map.ts +++ b/test/Map.ts @@ -741,25 +741,14 @@ describe('Map', () => { }) }) - describe('getWitherable', () => { - const W = _.getWitherable(ordUser) - - it('mapWithIndex', () => { - const mapWithIndex = W.mapWithIndex - const aa1 = new Map([[{ id: 'aa' }, 1]]) - const aa3 = new Map([[{ id: 'aa' }, 3]]) - U.deepStrictEqual( - mapWithIndex(aa1, (k, a) => a + k.id.length), - aa3 - ) - }) - + describe('getFoldable', () => { + const F = _.getFoldable(ordUser) it('reduce', () => { const d1 = new Map([ [{ id: 'k1' }, 'a'], [{ id: 'k2' }, 'b'] ]) - const reduceO = W.reduce + const reduceO = F.reduce U.deepStrictEqual( reduceO(d1, '', (b, a) => b + a), 'ab' @@ -775,7 +764,7 @@ describe('Map', () => { }) it('foldMap', () => { - const foldMapOM = W.foldMap(S.Monoid) + const foldMapOM = F.foldMap(S.Monoid) const m = new Map([ [{ id: 'a' }, 'a'], [{ id: 'a' }, 'b'] @@ -784,7 +773,7 @@ describe('Map', () => { }) it('reduceRight', () => { - const reduceRightO = W.reduceRight + const reduceRightO = F.reduceRight const m = new Map([ [{ id: 'a' }, 'a'], [{ id: 'b' }, 'b'] @@ -793,6 +782,20 @@ describe('Map', () => { const f = (a: string, acc: string) => acc + a U.deepStrictEqual(reduceRightO(m, init, f), 'ba') }) + }) + + describe('getWitherable', () => { + const W = _.getWitherable(ordUser) + + it('mapWithIndex', () => { + const mapWithIndex = W.mapWithIndex + const aa1 = new Map([[{ id: 'aa' }, 1]]) + const aa3 = new Map([[{ id: 'aa' }, 3]]) + U.deepStrictEqual( + mapWithIndex(aa1, (k, a) => a + k.id.length), + aa3 + ) + }) it('reduceWithIndex', () => { const d1 = new Map([ From 1155bf1ed7f45fac7c29a8fb2662da6c1f790ecf Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 2 Apr 2021 18:07:44 +0200 Subject: [PATCH 065/162] `NonEmptyArray` / `ReadonlyNonEmptyArray`: deprecate `groupSort` --- CHANGELOG.md | 4 +++ docs/modules/NonEmptyArray.ts.md | 45 ++++++++++-------------- docs/modules/ReadonlyNonEmptyArray.ts.md | 45 ++++++++++-------------- src/NonEmptyArray.ts | 43 ++++++++++------------ src/ReadonlyNonEmptyArray.ts | 43 ++++++++++------------ test/NonEmptyArray.ts | 2 ++ test/ReadonlyNonEmptyArray.ts | 2 ++ 7 files changed, 82 insertions(+), 102 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1dbf3516..1ea88ac1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,8 +28,12 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Refinement`, use `Refinement` module instead. - `Monoid` - deprecate `monoidVoid`, use `void` module instead. + - `NonEmptyArray` + - deprecate `groupSort` (it's just `sort` followed by `group`) - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. + - `ReadonlyNonEmptyArray` + - deprecate `groupSort` (it's just `sort` followed by `group`) - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): - `collect` - `reduce` diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 87182ae50..99891cde8 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -62,7 +62,6 @@ Added in v2.0.0 - [getUnionSemigroup](#getunionsemigroup) - [group](#group) - [groupBy](#groupby) - - [groupSort](#groupsort) - [insertAt](#insertat) - [intersperse](#intersperse) - [modifyAt](#modifyat) @@ -82,6 +81,7 @@ Added in v2.0.0 - [zipWith](#zipwith) - [~~filterWithIndex~~](#filterwithindex) - [~~filter~~](#filter) + - [~~groupSort~~](#groupsort) - [~~prependToAll~~](#prependtoall) - [constructors](#constructors) - [fromArray](#fromarray) @@ -519,32 +519,6 @@ assert.deepStrictEqual(groupBy((s: string) => String(s.length))(['a', 'b', 'ab'] Added in v2.0.0 -## groupSort - -Sort and then group the elements of an array into non empty arrays. - -**Signature** - -```ts -export declare function groupSort( - O: Ord -): { - (as: NonEmptyArray): NonEmptyArray> - (as: Array): Array> -} -``` - -**Example** - -```ts -import { groupSort } from 'fp-ts/NonEmptyArray' -import * as N from 'fp-ts/number' - -assert.deepStrictEqual(groupSort(N.Ord)([1, 2, 1, 1]), [[1, 1, 1], [2]]) -``` - -Added in v2.0.0 - ## insertAt **Signature** @@ -841,6 +815,23 @@ export declare function filter(predicate: Predicate): (as: NonEmptyArray( + O: Ord +): { + (as: NonEmptyArray): NonEmptyArray> + (as: Array): Array> +} +``` + +Added in v2.0.0 + ## ~~prependToAll~~ Use `prependAll` instead. diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 8c9fe4309..2cba71aa5 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -68,7 +68,6 @@ Added in v2.5.0 - [getUnionSemigroup](#getunionsemigroup) - [group](#group) - [groupBy](#groupby) - - [groupSort](#groupsort) - [intersperse](#intersperse) - [modifyAt](#modifyat) - [prependAll](#prependall) @@ -87,6 +86,7 @@ Added in v2.5.0 - [zipWith](#zipwith) - [~~filterWithIndex~~](#filterwithindex) - [~~filter~~](#filter) + - [~~groupSort~~](#groupsort) - [~~insertAt~~](#insertat) - [~~prependToAll~~](#prependtoall) - [constructors](#constructors) @@ -601,32 +601,6 @@ assert.deepStrictEqual(groupBy((s: string) => String(s.length))(['a', 'b', 'ab'] Added in v2.5.0 -## groupSort - -Sort and then group the elements of a `ReadonlyArray` into `ReadonlyNonEmptyArray`s. - -**Signature** - -```ts -export declare function groupSort( - O: Ord -): { - (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray> - (as: ReadonlyArray): ReadonlyArray> -} -``` - -**Example** - -```ts -import { groupSort } from 'fp-ts/ReadonlyNonEmptyArray' -import * as N from 'fp-ts/number' - -assert.deepStrictEqual(groupSort(N.Ord)([1, 2, 1, 1]), [[1, 1, 1], [2]]) -``` - -Added in v2.5.0 - ## intersperse Places an element in between members of a `ReadonlyNonEmptyArray`. @@ -934,6 +908,23 @@ export declare function filter( Added in v2.5.0 +## ~~groupSort~~ + +This is just `sort` followed by `group`. + +**Signature** + +```ts +export declare function groupSort( + O: Ord +): { + (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray> + (as: ReadonlyArray): ReadonlyArray> +} +``` + +Added in v2.5.0 + ## ~~insertAt~~ Use `ReadonlyArray`'s `insertAt` instead. diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index d3fb3d9d7..c2a7d8351 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -398,30 +398,6 @@ export function group(E: Eq): (as: Array) => Array> { } } -/** - * Sort and then group the elements of an array into non empty arrays. - * - * @example - * import { groupSort } from 'fp-ts/NonEmptyArray' - * import * as N from 'fp-ts/number' - * - * assert.deepStrictEqual(groupSort(N.Ord)([1, 2, 1, 1]), [[1, 1, 1], [2]]) - * - * @category combinators - * @since 2.0.0 - */ -export function groupSort( - O: Ord -): { - (as: NonEmptyArray): NonEmptyArray> - (as: Array): Array> -} -export function groupSort(O: Ord): (as: Array) => Array> { - const sortO = sort(O) - const groupO = group(O) - return (as) => (isNonEmpty(as) ? groupO(sortO(as)) : []) -} - /** * Splits an array into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning * function on each element, and grouping the results according to values returned @@ -1241,6 +1217,25 @@ export const updateLast = (a: A): ((as: NonEmptyArray) => NonEmptyArray // deprecated // ------------------------------------------------------------------------------------- +/** + * This is just `sort` followed by `group`. + * + * @category combinators + * @since 2.0.0 + * @deprecated + */ +export function groupSort( + O: Ord +): { + (as: NonEmptyArray): NonEmptyArray> + (as: Array): Array> +} +export function groupSort(O: Ord): (as: Array) => Array> { + const sortO = sort(O) + const groupO = group(O) + return (as) => (isNonEmpty(as) ? groupO(sortO(as)) : []) +} + /** * Use `Array`'s `filter` instead. * diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 14af534bf..16ecdb376 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -418,30 +418,6 @@ export function group(E: Eq): (as: ReadonlyArray) => ReadonlyArray( - O: Ord -): { - (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray> - (as: ReadonlyArray): ReadonlyArray> -} -export function groupSort(O: Ord): (as: ReadonlyArray) => ReadonlyArray> { - const sortO = sort(O) - const groupO = group(O) - return (as) => (isNonEmpty(as) ? groupO(sortO(as)) : empty) -} - /** * Splits an array into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning * function on each element, and grouping the results according to values returned @@ -1299,6 +1275,25 @@ export const updateLast = (a: A): ((as: ReadonlyNonEmptyArray) => Readonly // deprecated // ------------------------------------------------------------------------------------- +/** + * This is just `sort` followed by `group`. + * + * @category combinators + * @since 2.5.0 + * @deprecated + */ +export function groupSort( + O: Ord +): { + (as: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray> + (as: ReadonlyArray): ReadonlyArray> +} +export function groupSort(O: Ord): (as: ReadonlyArray) => ReadonlyArray> { + const sortO = sort(O) + const groupO = group(O) + return (as) => (isNonEmpty(as) ? groupO(sortO(as)) : empty) +} + /** * Use `ReadonlyArray`'s `filter` instead. * diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 3706edc49..12b6d3417 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -152,7 +152,9 @@ describe('NonEmptyArray', () => { }) it('groupSort', () => { + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.groupSort(N.Ord)([]), []) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.groupSort(N.Ord)([1, 2, 1, 1]), [[1, 1, 1], [2]]) }) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index e4012578d..b24759987 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -161,7 +161,9 @@ describe('ReadonlyNonEmptyArray', () => { }) it('groupSort', () => { + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.groupSort(N.Ord)([]), []) + // tslint:disable-next-line: deprecation U.deepStrictEqual(_.groupSort(N.Ord)([1, 2, 1, 1]), [[1, 1, 1], [2]]) }) From 87d5975c7ab5827d0339398e94bf5f4ffc286be7 Mon Sep 17 00:00:00 2001 From: gcanti Date: Sat, 3 Apr 2021 17:24:53 +0200 Subject: [PATCH 066/162] Option: deprecate `getRefinement`, use `Refinement` module instead. --- CHANGELOG.md | 2 ++ docs/modules/Option.ts.md | 38 ++++++++++++----------------------- docs/modules/Refinement.ts.md | 17 ++++++++++++++++ src/Option.ts | 31 +++++++++------------------- src/Refinement.ts | 17 ++++++++++++++++ test/Option.ts | 2 ++ test/Refinement.ts | 14 +++++++++++++ 7 files changed, 75 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ea88ac1a..068f3f602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ high state of flux, you're at risk of it changing without notice. - deprecate `monoidVoid`, use `void` module instead. - `NonEmptyArray` - deprecate `groupSort` (it's just `sort` followed by `group`) + - `Option` + - deprecate `getRefinement`, use `Refinement` module instead. - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index f7f70d13a..319f7355d 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -132,10 +132,10 @@ Added in v2.0.0 - [bindTo](#bindto) - [elem](#elem) - [exists](#exists) - - [getRefinement](#getrefinement) - [sequenceArray](#sequencearray) - [traverseArray](#traversearray) - [traverseArrayWithIndex](#traversearraywithindex) + - [~~getRefinement~~](#getrefinement) --- @@ -1545,30 +1545,6 @@ assert.strictEqual( Added in v2.0.0 -## getRefinement - -Returns a `Refinement` (i.e. a custom type guard) from a `Option` returning function. -This function ensures that a custom type guard definition is type-safe. - -```ts -import { some, none, getRefinement } from 'fp-ts/Option' - -type A = { type: 'A' } -type B = { type: 'B' } -type C = A | B - -const isA = (c: C): c is A => c.type === 'B' // <= typo but typescript doesn't complain -const isA = getRefinement((c) => (c.type === 'B' ? some(c) : none)) // static error: Type '"B"' is not assignable to type '"A"' -``` - -**Signature** - -```ts -export declare function getRefinement(getOption: (a: A) => Option): Refinement -``` - -Added in v2.0.0 - ## sequenceArray Equivalent to `ReadonlyArray#sequence(Applicative)`. @@ -1606,3 +1582,15 @@ export declare const traverseArrayWithIndex: ( ``` Added in v2.9.0 + +## ~~getRefinement~~ + +Use `Refinement` module instead. + +**Signature** + +```ts +export declare function getRefinement(getOption: (a: A) => Option): Refinement +``` + +Added in v2.0.0 diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md index e41544adb..7704fced9 100644 --- a/docs/modules/Refinement.ts.md +++ b/docs/modules/Refinement.ts.md @@ -12,6 +12,8 @@ Added in v2.11.0

Table of contents

+- [constructors](#constructors) + - [fromOptionK](#fromoptionk) - [utils](#utils) - [Refinement (interface)](#refinement-interface) - [and](#and) @@ -20,6 +22,21 @@ Added in v2.11.0 --- +# constructors + +## fromOptionK + +Returns a `Refinement` from a `Option` returning function. +This function ensures that a custom type guard definition is type-safe. + +**Signature** + +```ts +export declare const fromOptionK: (getOption: (a: A) => Option) => Refinement +``` + +Added in v2.11.0 + # utils ## Refinement (interface) diff --git a/src/Option.ts b/src/Option.ts index 96d6ee064..fae9986d8 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -1216,27 +1216,6 @@ export function exists
(predicate: Predicate): (ma: Option) => boolean { return (ma) => (isNone(ma) ? false : predicate(ma.value)) } -/** - * Returns a `Refinement` (i.e. a custom type guard) from a `Option` returning function. - * This function ensures that a custom type guard definition is type-safe. - * - * ```ts - * import { some, none, getRefinement } from 'fp-ts/Option' - * - * type A = { type: 'A' } - * type B = { type: 'B' } - * type C = A | B - * - * const isA = (c: C): c is A => c.type === 'B' // <= typo but typescript doesn't complain - * const isA = getRefinement(c => (c.type === 'B' ? some(c) : none)) // static error: Type '"B"' is not assignable to type '"A"' - * ``` - * - * @since 2.0.0 - */ -export function getRefinement(getOption: (a: A) => Option): Refinement { - return (a: A): a is B => isSome(getOption(a)) -} - // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- @@ -1317,6 +1296,16 @@ export const sequenceArray: (arr: ReadonlyArray>) => Option(getOption: (a: A) => Option): Refinement { + return (a: A): a is B => isSome(getOption(a)) +} + /** * @category combinators * @since 2.0.0 diff --git a/src/Refinement.ts b/src/Refinement.ts index e65e78c1a..97d21cfb3 100644 --- a/src/Refinement.ts +++ b/src/Refinement.ts @@ -1,6 +1,8 @@ /** * @since 2.11.0 */ +import { Option } from './Option' +import * as _ from './internal' // ------------------------------------------------------------------------------------- // model @@ -13,6 +15,21 @@ export interface Refinement { (a: A): a is B } +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + +/** + * Returns a `Refinement` from a `Option` returning function. + * This function ensures that a custom type guard definition is type-safe. + * + * @category constructors + * @since 2.11.0 + */ +export const fromOptionK = (getOption: (a: A) => Option): Refinement => { + return (a: A): a is B => _.isSome(getOption(a)) +} + // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- diff --git a/test/Option.ts b/test/Option.ts index 527d72480..049636591 100644 --- a/test/Option.ts +++ b/test/Option.ts @@ -421,12 +421,14 @@ describe('Option', () => { it('getRefinement', () => { const f = (s: string | number): _.Option => (typeof s === 'string' ? _.some(s) : _.none) + // tslint:disable-next-line: deprecation const isString = _.getRefinement(f) U.deepStrictEqual(isString('s'), true) U.deepStrictEqual(isString(1), false) type A = { readonly type: 'A' } type B = { readonly type: 'B' } type C = A | B + // tslint:disable-next-line: deprecation const isA = _.getRefinement((c) => (c.type === 'A' ? _.some(c) : _.none)) U.deepStrictEqual(isA({ type: 'A' }), true) U.deepStrictEqual(isA({ type: 'B' }), false) diff --git a/test/Refinement.ts b/test/Refinement.ts index a1c9a2e59..a6d86da38 100644 --- a/test/Refinement.ts +++ b/test/Refinement.ts @@ -1,4 +1,5 @@ import { pipe } from '../src/function' +import * as O from '../src/Option' import { ReadonlyRecord } from '../src/ReadonlyRecord' import * as _ from '../src/Refinement' import * as U from './util' @@ -36,4 +37,17 @@ describe('Refinement', () => { U.deepStrictEqual(r({ a: 1, b: 2 }), false) U.deepStrictEqual(r({ a: 'a', b: 1 }), true) }) + + it('getRefinement', () => { + const f = (s: string | number): O.Option => (typeof s === 'string' ? O.some(s) : O.none) + const isString = _.fromOptionK(f) + U.deepStrictEqual(isString('s'), true) + U.deepStrictEqual(isString(1), false) + type A = { readonly type: 'A' } + type B = { readonly type: 'B' } + type C = A | B + const isA = _.fromOptionK((c) => (c.type === 'A' ? O.some(c) : O.none)) + U.deepStrictEqual(isA({ type: 'A' }), true) + U.deepStrictEqual(isA({ type: 'B' }), false) + }) }) From 33dc703e07cb5b8374cc64aa9cccad449794cf17 Mon Sep 17 00:00:00 2001 From: gcanti Date: Sat, 3 Apr 2021 17:52:20 +0200 Subject: [PATCH 067/162] Refinement: - add `id` - add `zero` - add `compose` --- docs/modules/Refinement.ts.md | 84 +++++++++++++++++++++++++---------- src/Refinement.ts | 29 ++++++++++++ test/Refinement.ts | 27 ++++++++++- 3 files changed, 116 insertions(+), 24 deletions(-) diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md index 7704fced9..63699ce9e 100644 --- a/docs/modules/Refinement.ts.md +++ b/docs/modules/Refinement.ts.md @@ -12,75 +12,113 @@ Added in v2.11.0

Table of contents

+- [combinators](#combinators) + - [and](#and) + - [compose](#compose) + - [not](#not) + - [or](#or) + - [zero](#zero) - [constructors](#constructors) - [fromOptionK](#fromoptionk) + - [id](#id) - [utils](#utils) - [Refinement (interface)](#refinement-interface) - - [and](#and) - - [not](#not) - - [or](#or) --- -# constructors - -## fromOptionK +# combinators -Returns a `Refinement` from a `Option` returning function. -This function ensures that a custom type guard definition is type-safe. +## and **Signature** ```ts -export declare const fromOptionK: (getOption: (a: A) => Option) => Refinement +export declare const and: ( + second: Refinement +) => (first: Refinement) => Refinement ``` Added in v2.11.0 -# utils +## compose -## Refinement (interface) +**Signature** + +```ts +export declare const compose: ( + bc: Refinement +) => (ab: Refinement) => Refinement +``` + +Added in v2.11.0 + +## not **Signature** ```ts -export interface Refinement { - (a: A): a is B -} +export declare const not: (refinement: Refinement) => Refinement> ``` Added in v2.11.0 -## and +## or **Signature** ```ts -export declare const and: ( +export declare const or: ( second: Refinement -) => (first: Refinement) => Refinement +) => (first: Refinement) => Refinement ``` Added in v2.11.0 -## not +## zero **Signature** ```ts -export declare const not: (refinement: Refinement) => Refinement> +export declare const zero: () => Refinement ``` Added in v2.11.0 -## or +# constructors + +## fromOptionK + +Returns a `Refinement` from a `Option` returning function. +This function ensures that a custom type guard definition is type-safe. **Signature** ```ts -export declare const or: ( - second: Refinement -) => (first: Refinement) => Refinement +export declare const fromOptionK: (getOption: (a: A) => Option) => Refinement +``` + +Added in v2.11.0 + +## id + +**Signature** + +```ts +export declare const id:
() => Refinement +``` + +Added in v2.11.0 + +# utils + +## Refinement (interface) + +**Signature** + +```ts +export interface Refinement { + (a: A): a is B +} ``` Added in v2.11.0 diff --git a/src/Refinement.ts b/src/Refinement.ts index 97d21cfb3..9a8236815 100644 --- a/src/Refinement.ts +++ b/src/Refinement.ts @@ -30,11 +30,20 @@ export const fromOptionK = (getOption: (a: A) => Option): Ref return (a: A): a is B => _.isSome(getOption(a)) } +/** + * @category constructors + * @since 2.11.0 + */ +export const id = (): Refinement => { + return (_): _ is A => true +} + // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- /** + * @category combinators * @since 2.11.0 */ export const not = (refinement: Refinement): Refinement> => ( @@ -42,6 +51,7 @@ export const not = (refinement: Refinement): Refinement => !refinement(a) /** + * @category combinators * @since 2.11.0 */ export const or = (second: Refinement) => ( @@ -49,8 +59,27 @@ export const or = (second: Refinement) => ( ): Refinement => (a): a is B | C => first(a) || second(a) /** + * @category combinators * @since 2.11.0 */ export const and = (second: Refinement) => ( first: Refinement ): Refinement => (a): a is B & C => first(a) && second(a) + +/** + * @category combinators + * @since 2.11.0 + */ +export const zero = (): Refinement => { + return (_): _ is B => false +} + +/** + * @category combinators + * @since 2.11.0 + */ +export const compose = (bc: Refinement) => ( + ab: Refinement +): Refinement => { + return (i): i is C => ab(i) && bc(i) +} diff --git a/test/Refinement.ts b/test/Refinement.ts index a6d86da38..5c1f7b4f5 100644 --- a/test/Refinement.ts +++ b/test/Refinement.ts @@ -10,6 +10,14 @@ export const number = (u: unknown): u is number => typeof u === 'number' export const boolean = (u: unknown): u is boolean => typeof u === 'boolean' +interface NonEmptyStringBrand { + readonly NonEmptyString: unique symbol +} + +type NonEmptyString = string & NonEmptyStringBrand + +const NonEmptyString: _.Refinement = (s): s is NonEmptyString => s.length > 0 + describe('Refinement', () => { it('not', () => { const r1: _.Refinement = string @@ -38,7 +46,7 @@ describe('Refinement', () => { U.deepStrictEqual(r({ a: 'a', b: 1 }), true) }) - it('getRefinement', () => { + it('fromOptionK', () => { const f = (s: string | number): O.Option => (typeof s === 'string' ? O.some(s) : O.none) const isString = _.fromOptionK(f) U.deepStrictEqual(isString('s'), true) @@ -50,4 +58,21 @@ describe('Refinement', () => { U.deepStrictEqual(isA({ type: 'A' }), true) U.deepStrictEqual(isA({ type: 'B' }), false) }) + + it('zero', () => { + const refinement = _.zero() + U.strictEqual(refinement('a'), false) + }) + + it('id', () => { + const refinement = _.id() + U.strictEqual(refinement('a'), true) + }) + + it('compose', () => { + const refinement = pipe(string, _.compose(NonEmptyString)) + U.strictEqual(refinement('a'), true) + U.strictEqual(refinement(null), false) + U.strictEqual(refinement(''), false) + }) }) From 6f0b5827a4fe492b8dd7e96d4a7130182e2f10ff Mon Sep 17 00:00:00 2001 From: gcanti Date: Sat, 3 Apr 2021 18:11:11 +0200 Subject: [PATCH 068/162] - `string` - add `isString` - `number` - add `isNumber` - `boolean` - add `isBoolean` --- CHANGELOG.md | 5 ++ docs/modules/Array.ts.md | 74 ++++++++++++------------ docs/modules/Either.ts.md | 58 +++++++++---------- docs/modules/Option.ts.md | 94 +++++++++++++++---------------- docs/modules/ReadonlyArray.ts.md | 74 ++++++++++++------------ docs/modules/ReadonlyRecord.ts.md | 2 +- docs/modules/Record.ts.md | 2 +- docs/modules/Refinement.ts.md | 2 +- docs/modules/These.ts.md | 84 +++++++++++++-------------- docs/modules/boolean.ts.md | 14 +++++ docs/modules/number.ts.md | 14 +++++ docs/modules/string.ts.md | 14 +++++ src/Array.ts | 45 ++++++++------- src/Either.ts | 6 +- src/Option.ts | 6 +- src/ReadonlyArray.ts | 45 ++++++++------- src/ReadonlyRecord.ts | 2 +- src/Record.ts | 2 +- src/Refinement.ts | 2 +- src/These.ts | 68 ++++++++++++---------- src/boolean.ts | 11 ++++ src/number.ts | 15 +++++ src/string.ts | 11 ++++ test/Refinement.ts | 19 +++---- 24 files changed, 384 insertions(+), 285 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 068f3f602..6890765eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,6 +81,8 @@ high state of flux, you're at risk of it changing without notice. - add `concat` / `concatW` - add `match`, `matchW`, `matchLeftW`, `matchRightW` - add `fromOptionK` + - `boolean` + - add `isBoolean` - `Either` - add `chainOptionK` - `function` @@ -114,6 +116,9 @@ high state of flux, you're at risk of it changing without notice. - add `range` - `number` - add `MagmaSub` + - add `isNumber` + - `string` + - add `isString` - `Option` - add `FromEither` instance - add `fromEitherK` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index f804bc6ae..538aeec61 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -131,8 +131,6 @@ Added in v2.0.0 - [matchW](#matchw) - [spanLeft](#spanleft) - [tail](#tail) -- [guards](#guards) - - [isNonEmpty](#isnonempty) - [instances](#instances) - [Alt](#alt-1) - [Alternative](#alternative-1) @@ -168,6 +166,9 @@ Added in v2.0.0 - [getUnionMonoid](#getunionmonoid) - [getUnionSemigroup](#getunionsemigroup) - [~~array~~](#array) +- [refinements](#refinements) + - [isEmpty](#isempty) + - [isNonEmpty](#isnonempty) - [unsafe](#unsafe) - [unsafeDeleteAt](#unsafedeleteat) - [unsafeInsertAt](#unsafeinsertat) @@ -185,7 +186,6 @@ Added in v2.0.0 - [findIndex](#findindex) - [findLastIndex](#findlastindex) - [insertAt](#insertat) - - [isEmpty](#isempty) - [isOutOfBound](#isoutofbound) - [lookup](#lookup) - [modifyAt](#modifyat) @@ -1915,20 +1915,6 @@ assert.deepStrictEqual(tail([]), none) Added in v2.0.0 -# guards - -## isNonEmpty - -Test whether an array is non empty narrowing down the type to `NonEmptyArray` - -**Signature** - -```ts -export declare const isNonEmpty: (as: A[]) => as is NEA.NonEmptyArray -``` - -Added in v2.0.0 - # instances ## Alt @@ -2324,6 +2310,40 @@ export declare const array: FunctorWithIndex1<'Array', number> & Added in v2.0.0 +# refinements + +## isEmpty + +Test whether an array is empty + +**Signature** + +```ts +export declare const isEmpty: (as: A[]) => as is [] +``` + +**Example** + +```ts +import { isEmpty } from 'fp-ts/Array' + +assert.strictEqual(isEmpty([]), true) +``` + +Added in v2.0.0 + +## isNonEmpty + +Test whether an array is non empty narrowing down the type to `NonEmptyArray` + +**Signature** + +```ts +export declare const isNonEmpty: (as: A[]) => as is NEA.NonEmptyArray +``` + +Added in v2.0.0 + # unsafe ## unsafeDeleteAt @@ -2561,26 +2581,6 @@ assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4])) Added in v2.0.0 -## isEmpty - -Test whether an array is empty - -**Signature** - -```ts -export declare const isEmpty: (as: A[]) => as is [] -``` - -**Example** - -```ts -import { isEmpty } from 'fp-ts/Array' - -assert.strictEqual(isEmpty([]), true) -``` - -Added in v2.0.0 - ## isOutOfBound Test whether an array contains a particular index diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 90422de90..9aa06a2ae 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -80,9 +80,6 @@ Added in v2.0.0 - [getOrElseW](#getorelsew) - [match](#match) - [matchW](#matchw) -- [guards](#guards) - - [isLeft](#isleft) - - [isRight](#isright) - [instances](#instances) - [Alt](#alt-1) - [Applicative](#applicative) @@ -125,6 +122,9 @@ Added in v2.0.0 - [Either (type alias)](#either-type-alias) - [Left (interface)](#left-interface) - [Right (interface)](#right-interface) +- [refinements](#refinements) + - [isLeft](#isleft) + - [isRight](#isright) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -909,32 +909,6 @@ export declare const matchW: (onLeft: (e: E) => B, onRight: (a: A) = Added in v2.10.0 -# guards - -## isLeft - -Returns `true` if the either is an instance of `Left`, `false` otherwise. - -**Signature** - -```ts -export declare const isLeft: (ma: Either) => ma is Left -``` - -Added in v2.0.0 - -## isRight - -Returns `true` if the either is an instance of `Right`, `false` otherwise. - -**Signature** - -```ts -export declare const isRight: (ma: Either) => ma is Right -``` - -Added in v2.0.0 - # instances ## Alt @@ -1440,6 +1414,32 @@ export interface Right { Added in v2.0.0 +# refinements + +## isLeft + +Returns `true` if the either is an instance of `Left`, `false` otherwise. + +**Signature** + +```ts +export declare const isLeft: (ma: Either) => ma is Left +``` + +Added in v2.0.0 + +## isRight + +Returns `true` if the either is an instance of `Right`, `false` otherwise. + +**Signature** + +```ts +export declare const isRight: (ma: Either) => ma is Right +``` + +Added in v2.0.0 + # utils ## Do diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 319f7355d..a35a3b486 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -82,9 +82,6 @@ Added in v2.0.0 - [getOrElseW](#getorelsew) - [match](#match) - [matchW](#matchw) -- [guards](#guards) - - [isNone](#isnone) - - [isSome](#issome) - [instances](#instances) - [Alt](#alt-1) - [Alternative](#alternative-1) @@ -125,6 +122,9 @@ Added in v2.0.0 - [None (interface)](#none-interface) - [Option (type alias)](#option-type-alias) - [Some (interface)](#some-interface) +- [refinements](#refinements) + - [isNone](#isnone) + - [isSome](#issome) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -774,50 +774,6 @@ export declare const matchW: (onNone: Lazy, onSome: (a: A) => C) => Added in v2.10.0 -# guards - -## isNone - -Returns `true` if the option is `None`, `false` otherwise. - -**Signature** - -```ts -export declare const isNone: (fa: Option) => fa is None -``` - -**Example** - -```ts -import { some, none, isNone } from 'fp-ts/Option' - -assert.strictEqual(isNone(some(1)), false) -assert.strictEqual(isNone(none), true) -``` - -Added in v2.0.0 - -## isSome - -Returns `true` if the option is an instance of `Some`, `false` otherwise. - -**Signature** - -```ts -export declare const isSome: (fa: Option) => fa is Some -``` - -**Example** - -```ts -import { some, none, isSome } from 'fp-ts/Option' - -assert.strictEqual(isSome(some(1)), true) -assert.strictEqual(isSome(none), false) -``` - -Added in v2.0.0 - # instances ## Alt @@ -1433,6 +1389,50 @@ export interface Some { Added in v2.0.0 +# refinements + +## isNone + +Returns `true` if the option is `None`, `false` otherwise. + +**Signature** + +```ts +export declare const isNone: (fa: Option) => fa is None +``` + +**Example** + +```ts +import { some, none, isNone } from 'fp-ts/Option' + +assert.strictEqual(isNone(some(1)), false) +assert.strictEqual(isNone(none), true) +``` + +Added in v2.0.0 + +## isSome + +Returns `true` if the option is an instance of `Some`, `false` otherwise. + +**Signature** + +```ts +export declare const isSome: (fa: Option) => fa is Some +``` + +**Example** + +```ts +import { some, none, isSome } from 'fp-ts/Option' + +assert.strictEqual(isSome(some(1)), true) +assert.strictEqual(isSome(none), false) +``` + +Added in v2.0.0 + # utils ## Do diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 083edfe3f..f29bb0f90 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -121,8 +121,6 @@ Added in v2.5.0 - [matchRight](#matchright) - [matchRightW](#matchrightw) - [matchW](#matchw) -- [guards](#guards) - - [isNonEmpty](#isnonempty) - [instances](#instances) - [Alt](#alt-1) - [Alternative](#alternative-1) @@ -161,6 +159,9 @@ Added in v2.5.0 - [interop](#interop) - [fromArray](#fromarray) - [toArray](#toarray) +- [refinements](#refinements) + - [isEmpty](#isempty) + - [isNonEmpty](#isnonempty) - [unsafe](#unsafe) - [unsafeDeleteAt](#unsafedeleteat) - [unsafeInsertAt](#unsafeinsertat) @@ -185,7 +186,6 @@ Added in v2.5.0 - [head](#head) - [init](#init) - [insertAt](#insertat) - - [isEmpty](#isempty) - [isOutOfBound](#isoutofbound) - [last](#last) - [lookup](#lookup) @@ -1723,20 +1723,6 @@ export declare const matchW: ( Added in v2.11.0 -# guards - -## isNonEmpty - -Test whether a `ReadonlyArray` is non empty. - -**Signature** - -```ts -export declare const isNonEmpty: (as: readonly A[]) => as is RNEA.ReadonlyNonEmptyArray -``` - -Added in v2.5.0 - # instances ## Alt @@ -2154,6 +2140,40 @@ export declare const toArray: (as: readonly A[]) => A[] Added in v2.5.0 +# refinements + +## isEmpty + +Test whether a `ReadonlyArray` is empty. + +**Signature** + +```ts +export declare const isEmpty: (as: readonly A[]) => as is readonly [] +``` + +**Example** + +```ts +import { isEmpty } from 'fp-ts/ReadonlyArray' + +assert.strictEqual(isEmpty([]), true) +``` + +Added in v2.5.0 + +## isNonEmpty + +Test whether a `ReadonlyArray` is non empty. + +**Signature** + +```ts +export declare const isNonEmpty: (as: readonly A[]) => as is RNEA.ReadonlyNonEmptyArray +``` + +Added in v2.5.0 + # unsafe ## unsafeDeleteAt @@ -2608,26 +2628,6 @@ assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4])) Added in v2.5.0 -## isEmpty - -Test whether a `ReadonlyArray` is empty. - -**Signature** - -```ts -export declare const isEmpty: (as: readonly A[]) => as is readonly [] -``` - -**Example** - -```ts -import { isEmpty } from 'fp-ts/ReadonlyArray' - -assert.strictEqual(isEmpty([]), true) -``` - -Added in v2.5.0 - ## isOutOfBound Test whether an array contains a particular index diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 37a3a656e..621a5fa23 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -915,7 +915,7 @@ Added in v2.5.0 Test whether or not a key exists in a `ReadonlyRecord`. -Note. This function is not pipeable because is a custom type guard. +Note. This function is not pipeable because is a `Refinement`. **Signature** diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index ab90b5a6f..74ce6bf09 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -776,7 +776,7 @@ Added in v2.0.0 Test whether or not a key exists in a `Record`. -Note. This function is not pipeable because is a custom type guard. +Note. This function is not pipeable because is a `Refinement`. **Signature** diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md index 63699ce9e..831f13d2c 100644 --- a/docs/modules/Refinement.ts.md +++ b/docs/modules/Refinement.ts.md @@ -89,7 +89,7 @@ Added in v2.11.0 ## fromOptionK Returns a `Refinement` from a `Option` returning function. -This function ensures that a custom type guard definition is type-safe. +This function ensures that a `Refinement` definition is type-safe. **Signature** diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 59b0b010b..c484d09da 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -62,10 +62,6 @@ Added in v2.0.0 - [getRightOnly](#getrightonly) - [match](#match) - [matchW](#matchw) -- [guards](#guards) - - [isBoth](#isboth) - - [isLeft](#isleft) - - [isRight](#isright) - [instances](#instances) - [Bifunctor](#bifunctor-1) - [Foldable](#foldable-1) @@ -87,6 +83,10 @@ Added in v2.0.0 - [model](#model) - [Both (interface)](#both-interface) - [These (type alias)](#these-type-alias) +- [refinements](#refinements) + - [isBoth](#isboth) + - [isLeft](#isleft) + - [isRight](#isright) - [utils](#utils) - [sequence](#sequence) - [toTuple2](#totuple2) @@ -476,44 +476,6 @@ export declare const matchW: ( Added in v2.10.0 -# guards - -## isBoth - -Returns `true` if the these is an instance of `Both`, `false` otherwise - -**Signature** - -```ts -export declare function isBoth(fa: These): fa is Both -``` - -Added in v2.0.0 - -## isLeft - -Returns `true` if the these is an instance of `Left`, `false` otherwise - -**Signature** - -```ts -export declare function isLeft(fa: These): fa is Left -``` - -Added in v2.0.0 - -## isRight - -Returns `true` if the these is an instance of `Right`, `false` otherwise - -**Signature** - -```ts -export declare function isRight(fa: These): fa is Right -``` - -Added in v2.0.0 - # instances ## Bifunctor @@ -714,6 +676,44 @@ export type These = Either | Both Added in v2.0.0 +# refinements + +## isBoth + +Returns `true` if the these is an instance of `Both`, `false` otherwise + +**Signature** + +```ts +export declare function isBoth(fa: These): fa is Both +``` + +Added in v2.0.0 + +## isLeft + +Returns `true` if the these is an instance of `Left`, `false` otherwise + +**Signature** + +```ts +export declare function isLeft(fa: These): fa is Left +``` + +Added in v2.0.0 + +## isRight + +Returns `true` if the these is an instance of `Right`, `false` otherwise + +**Signature** + +```ts +export declare function isRight(fa: These): fa is Right +``` + +Added in v2.0.0 + # utils ## sequence diff --git a/docs/modules/boolean.ts.md b/docs/modules/boolean.ts.md index a59401e83..6faf73425 100644 --- a/docs/modules/boolean.ts.md +++ b/docs/modules/boolean.ts.md @@ -26,6 +26,8 @@ Added in v2.2.0 - [SemigroupAll](#semigroupall) - [SemigroupAny](#semigroupany) - [Show](#show) +- [refinements](#refinements) + - [isBoolean](#isboolean) --- @@ -233,3 +235,15 @@ export declare const Show: S.Show ``` Added in v2.10.0 + +# refinements + +## isBoolean + +**Signature** + +```ts +export declare const isBoolean: Refinement +``` + +Added in v2.11.0 diff --git a/docs/modules/number.ts.md b/docs/modules/number.ts.md index 418f1b9c7..839f3091a 100644 --- a/docs/modules/number.ts.md +++ b/docs/modules/number.ts.md @@ -23,6 +23,8 @@ Added in v2.10.0 - [SemigroupProduct](#semigroupproduct) - [SemigroupSum](#semigroupsum) - [Show](#show) +- [refinements](#refinements) + - [isNumber](#isnumber) --- @@ -171,3 +173,15 @@ export declare const Show: S.Show ``` Added in v2.10.0 + +# refinements + +## isNumber + +**Signature** + +```ts +export declare const isNumber: Refinement +``` + +Added in v2.11.0 diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index 908a7ea51..d28c6204c 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -18,6 +18,8 @@ Added in v2.10.0 - [Ord](#ord) - [Semigroup](#semigroup) - [Show](#show) +- [refinements](#refinements) + - [isString](#isstring) - [utils](#utils) - [empty](#empty) - [isEmpty](#isempty) @@ -100,6 +102,18 @@ export declare const Show: Sh.Show Added in v2.10.0 +# refinements + +## isString + +**Signature** + +```ts +export declare const isString: Refinement +``` + +Added in v2.11.0 + # utils ## empty diff --git a/src/Array.ts b/src/Array.ts index 5cb92647e..d61dae8b1 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -48,6 +48,31 @@ import { import NonEmptyArray = NEA.NonEmptyArray +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * Test whether an array is empty + * + * @example + * import { isEmpty } from 'fp-ts/Array' + * + * assert.strictEqual(isEmpty([]), true) + * + * @category refinements + * @since 2.0.0 + */ +export const isEmpty = (as: Array): as is [] => as.length === 0 + +/** + * Test whether an array is non empty narrowing down the type to `NonEmptyArray` + * + * @category refinements + * @since 2.0.0 + */ +export const isNonEmpty: (as: Array) => as is NonEmptyArray = NEA.isNonEmpty + // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- @@ -300,26 +325,6 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: Array): N return out } -/** - * Test whether an array is empty - * - * @example - * import { isEmpty } from 'fp-ts/Array' - * - * assert.strictEqual(isEmpty([]), true) - * - * @since 2.0.0 - */ -export const isEmpty = (as: Array): as is [] => as.length === 0 - -/** - * Test whether an array is non empty narrowing down the type to `NonEmptyArray` - * - * @category guards - * @since 2.0.0 - */ -export const isNonEmpty: (as: Array) => as is NonEmptyArray = NEA.isNonEmpty - /** * Calculate the number of elements in a `Array`. * diff --git a/src/Either.ts b/src/Either.ts index ab2ea93fe..c59eeb137 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -83,13 +83,13 @@ export interface Right { export type Either = Left | Right // ------------------------------------------------------------------------------------- -// guards +// refinements // ------------------------------------------------------------------------------------- /** * Returns `true` if the either is an instance of `Left`, `false` otherwise. * - * @category guards + * @category refinements * @since 2.0.0 */ export const isLeft: (ma: Either) => ma is Left = _.isLeft @@ -97,7 +97,7 @@ export const isLeft: (ma: Either) => ma is Left = _.isLeft /** * Returns `true` if the either is an instance of `Right`, `false` otherwise. * - * @category guards + * @category refinements * @since 2.0.0 */ export const isRight: (ma: Either) => ma is Right = _.isRight diff --git a/src/Option.ts b/src/Option.ts index fae9986d8..bce159035 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -75,7 +75,7 @@ export interface Some { export type Option = None | Some // ------------------------------------------------------------------------------------- -// guards +// refinements // ------------------------------------------------------------------------------------- /** @@ -87,7 +87,7 @@ export type Option = None | Some * assert.strictEqual(isSome(some(1)), true) * assert.strictEqual(isSome(none), false) * - * @category guards + * @category refinements * @since 2.0.0 */ export const isSome: (fa: Option) => fa is Some = _.isSome @@ -101,7 +101,7 @@ export const isSome: (fa: Option) => fa is Some = _.isSome * assert.strictEqual(isNone(some(1)), false) * assert.strictEqual(isNone(none), true) * - * @category guards + * @category refinements * @since 2.0.0 */ export const isNone = (fa: Option): fa is None => fa._tag === 'None' diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index ae6fe2123..f0fae54c3 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -49,6 +49,31 @@ import { import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * Test whether a `ReadonlyArray` is empty. + * + * @example + * import { isEmpty } from 'fp-ts/ReadonlyArray' + * + * assert.strictEqual(isEmpty([]), true) + * + * @category refinements + * @since 2.5.0 + */ +export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 + +/** + * Test whether a `ReadonlyArray` is non empty. + * + * @category refinements + * @since 2.5.0 + */ +export const isNonEmpty: (as: ReadonlyArray) => as is ReadonlyNonEmptyArray = RNEA.isNonEmpty + // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- @@ -308,26 +333,6 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: ReadonlyArra return out } -/** - * Test whether a `ReadonlyArray` is empty. - * - * @example - * import { isEmpty } from 'fp-ts/ReadonlyArray' - * - * assert.strictEqual(isEmpty([]), true) - * - * @since 2.5.0 - */ -export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 - -/** - * Test whether a `ReadonlyArray` is non empty. - * - * @category guards - * @since 2.5.0 - */ -export const isNonEmpty: (as: ReadonlyArray) => as is ReadonlyNonEmptyArray = RNEA.isNonEmpty - /** * Calculate the number of elements in a `ReadonlyArray`. * diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 0702b572b..4cd062d98 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -184,7 +184,7 @@ export const upsertAt = (k: string, a: A) => (r: ReadonlyRecord): /** * Test whether or not a key exists in a `ReadonlyRecord`. * - * Note. This function is not pipeable because is a custom type guard. + * Note. This function is not pipeable because is a `Refinement`. * * @since 2.10.0 */ diff --git a/src/Record.ts b/src/Record.ts index aa16c1f54..7eb84d953 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -140,7 +140,7 @@ export const upsertAt: (k: string, a: A) => (r: Record) => Record< /** * Test whether or not a key exists in a `Record`. * - * Note. This function is not pipeable because is a custom type guard. + * Note. This function is not pipeable because is a `Refinement`. * * @since 2.10.0 */ diff --git a/src/Refinement.ts b/src/Refinement.ts index 9a8236815..f9f446086 100644 --- a/src/Refinement.ts +++ b/src/Refinement.ts @@ -21,7 +21,7 @@ export interface Refinement { /** * Returns a `Refinement` from a `Option` returning function. - * This function ensures that a custom type guard definition is type-safe. + * This function ensures that a `Refinement` definition is type-safe. * * @category constructors * @since 2.11.0 diff --git a/src/These.ts b/src/These.ts index 02225dc74..ed7120ccf 100644 --- a/src/These.ts +++ b/src/These.ts @@ -61,6 +61,44 @@ export interface Both { */ export type These = Either | Both +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * Returns `true` if the these is an instance of `Left`, `false` otherwise + * + * @category refinements + * @since 2.0.0 + */ +export function isLeft(fa: These): fa is Left { + return fa._tag === 'Left' +} + +/** + * Returns `true` if the these is an instance of `Right`, `false` otherwise + * + * @category refinements + * @since 2.0.0 + */ +export function isRight(fa: These): fa is Right { + return fa._tag === 'Right' +} + +/** + * Returns `true` if the these is an instance of `Both`, `false` otherwise + * + * @category refinements + * @since 2.0.0 + */ +export function isBoth(fa: These): fa is Both { + return fa._tag === 'Both' +} + +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + /** * @category constructors * @since 2.0.0 @@ -318,36 +356,6 @@ export function getRight(fa: These): Option { return isLeft(fa) ? _.none : isRight(fa) ? _.some(fa.right) : _.some(fa.right) } -/** - * Returns `true` if the these is an instance of `Left`, `false` otherwise - * - * @category guards - * @since 2.0.0 - */ -export function isLeft(fa: These): fa is Left { - return fa._tag === 'Left' -} - -/** - * Returns `true` if the these is an instance of `Right`, `false` otherwise - * - * @category guards - * @since 2.0.0 - */ -export function isRight(fa: These): fa is Right { - return fa._tag === 'Right' -} - -/** - * Returns `true` if the these is an instance of `Both`, `false` otherwise - * - * @category guards - * @since 2.0.0 - */ -export function isBoth(fa: These): fa is Both { - return fa._tag === 'Both' -} - // TODO: make lazy in v3 /** * @example diff --git a/src/boolean.ts b/src/boolean.ts index 63b4c4b9b..502209ea2 100644 --- a/src/boolean.ts +++ b/src/boolean.ts @@ -8,6 +8,17 @@ import { Lazy } from './function' import { Monoid, monoidAll, monoidAny } from './Monoid' import * as O from './Ord' import * as S from './Show' +import { Refinement } from './Refinement' + +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * @category refinements + * @since 2.11.0 + */ +export const isBoolean: Refinement = (u: unknown): u is boolean => typeof u === 'boolean' // ------------------------------------------------------------------------------------- // destructors diff --git a/src/number.ts b/src/number.ts index 87d2f2532..f70405bde 100644 --- a/src/number.ts +++ b/src/number.ts @@ -9,6 +9,21 @@ import * as S from './Show' import { Semigroup, semigroupProduct, semigroupSum } from './Semigroup' import { Monoid, monoidProduct, monoidSum } from './Monoid' import { Magma } from './Magma' +import { Refinement } from './Refinement' + +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * @category refinements + * @since 2.11.0 + */ +export const isNumber: Refinement = (u: unknown): u is number => typeof u === 'number' + +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- /** * @category instances diff --git a/src/string.ts b/src/string.ts index 65ef1d79d..b626bd98f 100644 --- a/src/string.ts +++ b/src/string.ts @@ -6,6 +6,17 @@ import * as M from './Monoid' import * as S from './Semigroup' import * as O from './Ord' import * as Sh from './Show' +import { Refinement } from './Refinement' + +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * @category refinements + * @since 2.11.0 + */ +export const isString: Refinement = (u: unknown): u is string => typeof u === 'string' // ------------------------------------------------------------------------------------- // instances diff --git a/test/Refinement.ts b/test/Refinement.ts index 5c1f7b4f5..52900ee33 100644 --- a/test/Refinement.ts +++ b/test/Refinement.ts @@ -2,14 +2,11 @@ import { pipe } from '../src/function' import * as O from '../src/Option' import { ReadonlyRecord } from '../src/ReadonlyRecord' import * as _ from '../src/Refinement' +import * as S from '../src/string' +import * as N from '../src/number' +import * as B from '../src/boolean' import * as U from './util' -export const string = (u: unknown): u is string => typeof u === 'string' - -export const number = (u: unknown): u is number => typeof u === 'number' - -export const boolean = (u: unknown): u is boolean => typeof u === 'boolean' - interface NonEmptyStringBrand { readonly NonEmptyString: unique symbol } @@ -20,14 +17,14 @@ const NonEmptyString: _.Refinement = (s): s is NonEmptyS describe('Refinement', () => { it('not', () => { - const r1: _.Refinement = string + const r1: _.Refinement = S.isString const r2 = _.not(r1) U.deepStrictEqual(r2('a'), false) U.deepStrictEqual(r2(1), true) }) it('or', () => { - const r = pipe(string, _.or(number), _.or(boolean)) + const r = pipe(S.isString, _.or(N.isNumber), _.or(B.isBoolean)) U.deepStrictEqual(r({}), false) U.deepStrictEqual(r('a'), true) U.deepStrictEqual(r(1), true) @@ -35,8 +32,8 @@ describe('Refinement', () => { }) it('and', () => { - const ra = (r: ReadonlyRecord): r is { readonly a: string } => string(r['a']) - const rb = (r: ReadonlyRecord): r is { readonly b: number } => number(r['b']) + const ra = (r: ReadonlyRecord): r is { readonly a: string } => S.isString(r['a']) + const rb = (r: ReadonlyRecord): r is { readonly b: number } => N.isNumber(r['b']) const r = pipe(ra, _.and(rb)) U.deepStrictEqual(r({ a: 'a' }), false) U.deepStrictEqual(r({ b: 1 }), false) @@ -70,7 +67,7 @@ describe('Refinement', () => { }) it('compose', () => { - const refinement = pipe(string, _.compose(NonEmptyString)) + const refinement = pipe(S.isString, _.compose(NonEmptyString)) U.strictEqual(refinement('a'), true) U.strictEqual(refinement(null), false) U.strictEqual(refinement(''), false) From 76732e8970536a71641e4ab0636b573bfa8ba81e Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 6 Apr 2021 18:02:46 +0200 Subject: [PATCH 069/162] Record / ReadonlyRecord: traverse, sequence and traverseWithIndex should respect the order --- src/ReadonlyRecord.ts | 103 ++++++++++++++++++++++++----------------- src/Record.ts | 39 ++++++---------- test/ReadonlyRecord.ts | 28 +++++++---- test/Record.ts | 28 +++++++---- 4 files changed, 114 insertions(+), 84 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 4cd062d98..ced59546d 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -9,7 +9,7 @@ import { Filterable1 } from './Filterable' import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex' import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable' import { FoldableWithIndex1 } from './FoldableWithIndex' -import { identity, pipe, SK } from './function' +import { flow, identity, pipe, SK } from './function' import { flap as flap_, Functor1 } from './Functor' import { FunctorWithIndex1 } from './FunctorWithIndex' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT' @@ -533,23 +533,8 @@ export function traverseWithIndex( export function traverseWithIndex( F: Applicative ): (f: (k: string, a: A) => HKT) => (ta: ReadonlyRecord) => HKT> { - return (f: (k: string, a: A) => HKT) => (ta: ReadonlyRecord) => { - const ks = keys(ta) - if (ks.length === 0) { - return F.of(empty) - } - let fr: HKT> = F.of({}) - for (const key of ks) { - fr = F.ap( - F.map(fr, (r) => (b: B) => { - r[key] = b - return r - }), - f(key, ta[key]) - ) - } - return fr - } + const traverseWithIndexOF = _traverseWithIndex(S.Ord)(F) + return (f) => (ta) => traverseWithIndexOF(ta, f) } /** @@ -584,8 +569,8 @@ export function traverse( export function traverse( F: Applicative ): (f: (a: A) => HKT) => (ta: ReadonlyRecord) => HKT> { - const traverseWithIndexF = traverseWithIndex(F) - return (f) => traverseWithIndexF((_, a) => f(a)) + const traverseOF = _traverse(S.Ord)(F) + return (f) => (ta) => traverseOF(ta, f) } /** @@ -612,7 +597,7 @@ export function sequence( export function sequence( F: Applicative ): (ta: ReadonlyRecord>) => HKT> { - return traverseWithIndex(F)(SK) + return _sequence(S.Ord)(F) } /** @@ -993,13 +978,6 @@ const _foldMap: (O: Ord) => Foldable1['foldMap'] = (O) => (M) => { const _reduceRight: (O: Ord) => Foldable1['reduceRight'] = (O) => (fa, b, f) => pipe(fa, reduceRight(O)(b, f)) /* istanbul ignore next */ -const _traverse = ( - F: Applicative -): ((ta: ReadonlyRecord, f: (a: A) => HKT) => HKT>) => { - const traverseF = traverse(F) - return (ta, f) => pipe(ta, traverseF(f)) -} -/* istanbul ignore next */ const _filter = (fa: ReadonlyRecord, predicate: Predicate): ReadonlyRecord => pipe(fa, filter(predicate)) /* istanbul ignore next */ @@ -1039,12 +1017,51 @@ const _filterMapWithIndex = (fa: ReadonlyRecord, f: (key: strin /* istanbul ignore next */ const _filterWithIndex = (fa: ReadonlyRecord, predicateWithIndex: PredicateWithIndex) => pipe(fa, filterWithIndex(predicateWithIndex)) -/* istanbul ignore next */ -const _traverseWithIndex = ( + +/** @internal */ +export const _traverse = ( + O: Ord +): (( + F: Applicative +) => (ta: ReadonlyRecord, f: (a: A) => HKT) => HKT>) => { + const traverseWithIndexO = _traverseWithIndex(O) + return (F) => { + const traverseWithIndexOF = traverseWithIndexO(F) + return (ta, f) => traverseWithIndexOF(ta, flow(SK, f)) + } +} +/** @internal */ +export const _sequence = ( + O: Ord +): ((F: Applicative) => (ta: ReadonlyRecord>) => HKT>) => { + const traverseO = _traverse(O) + return (F) => { + const traverseOF = traverseO(F) + return (ta) => traverseOF(ta, identity) + } +} +/** @internal */ +export const _traverseWithIndex = (O: Ord) => ( F: Applicative ): ((ta: ReadonlyRecord, f: (k: string, a: A) => HKT) => HKT>) => { - const traverseWithIndexF = traverseWithIndex(F) - return (ta, f) => pipe(ta, traverseWithIndexF(f)) + const keysO = keys_(O) + return (ta: ReadonlyRecord, f: (k: string, a: A) => HKT) => { + const ks = keysO(ta) + if (ks.length === 0) { + return F.of(empty) + } + let fr: HKT> = F.of({}) + for (const key of ks) { + fr = F.ap( + F.map(fr, (r) => (b: B) => { + r[key] = b + return r + }), + f(key, ta[key]) + ) + } + return fr + } } // ------------------------------------------------------------------------------------- @@ -1418,8 +1435,8 @@ export const getTraversable = (O: Ord): Traversable1 => ({ reduce: _reduce(O), foldMap: _foldMap(O), reduceRight: _reduceRight(O), - traverse: _traverse, - sequence + traverse: _traverse(O), + sequence: _sequence(O) }) /** @@ -1436,9 +1453,9 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence } @@ -1574,9 +1591,9 @@ export const TraversableWithIndex: TraversableWithIndex1 = { reduceWithIndex: _reduceWithIndex(S.Ord), foldMapWithIndex: _foldMapWithIndex(S.Ord), reduceRightWithIndex: _reduceRightWithIndex(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, - traverseWithIndex: _traverseWithIndex + traverseWithIndex: _traverseWithIndex(S.Ord) } // tslint:disable-next-line: deprecation @@ -1597,7 +1614,7 @@ export const Witherable: Witherable1 = { reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, compact, separate, @@ -1649,7 +1666,7 @@ export const readonlyRecord: FunctorWithIndex1 & reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, compact, separate, @@ -1665,7 +1682,7 @@ export const readonlyRecord: FunctorWithIndex1 & filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, partitionWithIndex: _partitionWithIndex, - traverseWithIndex: _traverseWithIndex, + traverseWithIndex: _traverseWithIndex(S.Ord), wither: _wither, wilt: _wilt } diff --git a/src/Record.ts b/src/Record.ts index 7eb84d953..ddbcc62b1 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -636,13 +636,6 @@ const _foldMap = (O: Ord): Foldable1['foldMap'] => (M) => { /* istanbul ignore next */ const _reduceRight = (O: Ord): Foldable1['reduceRight'] => (fa, b, f) => pipe(fa, reduceRight(O)(b, f)) /* istanbul ignore next */ -const _traverse = ( - F: Applicative -): ((ta: Record, f: (a: A) => HKT) => HKT>) => { - const traverseF = traverse(F) - return (ta, f) => pipe(ta, traverseF(f)) -} -/* istanbul ignore next */ const _filter = (fa: Record, predicate: Predicate): Record => pipe(fa, filter(predicate)) /* istanbul ignore next */ const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) @@ -678,13 +671,9 @@ const _filterMapWithIndex = (fa: Record, f: (key: string, a: A) /* istanbul ignore next */ const _filterWithIndex = (fa: Record, predicateWithIndex: PredicateWithIndex) => pipe(fa, filterWithIndex(predicateWithIndex)) -/* istanbul ignore next */ -const _traverseWithIndex = ( - F: Applicative -): ((ta: Record, f: (k: string, a: A) => HKT) => HKT>) => { - const traverseWithIndexF = traverseWithIndex(F) - return (ta, f) => pipe(ta, traverseWithIndexF(f)) -} +const _traverse = RR._traverse +const _sequence = RR._sequence +const _traverseWithIndex = RR._traverseWithIndex // ------------------------------------------------------------------------------------- // type class members @@ -980,8 +969,8 @@ export const getTraversable = (O: Ord): Traversable1 => ({ reduce: _reduce(O), foldMap: _foldMap(O), reduceRight: _reduceRight(O), - traverse: _traverse, - sequence + traverse: _traverse(O), + sequence: _sequence(O) }) /** @@ -998,9 +987,9 @@ export const getTraversableWithIndex = (O: Ord): TraversableWithIndex1 = { reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence } @@ -1136,9 +1125,9 @@ export const TraversableWithIndex: TraversableWithIndex1 = { reduceWithIndex: _reduceWithIndex(S.Ord), foldMapWithIndex: _foldMapWithIndex(S.Ord), reduceRightWithIndex: _reduceRightWithIndex(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, - traverseWithIndex: _traverseWithIndex + traverseWithIndex: _traverseWithIndex(S.Ord) } // tslint:disable-next-line: deprecation @@ -1159,7 +1148,7 @@ export const Witherable: Witherable1 = { reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, compact, separate, @@ -1212,7 +1201,7 @@ export const record: FunctorWithIndex1 & reduce: _reduce(S.Ord), foldMap: _foldMap(S.Ord), reduceRight: _reduceRight(S.Ord), - traverse: _traverse, + traverse: _traverse(S.Ord), sequence, compact, separate, @@ -1228,7 +1217,7 @@ export const record: FunctorWithIndex1 & filterWithIndex: _filterWithIndex, partitionMapWithIndex: _partitionMapWithIndex, partitionWithIndex: _partitionWithIndex, - traverseWithIndex: _traverseWithIndex, + traverseWithIndex: _traverseWithIndex(S.Ord), wither: _wither, wilt: _wilt } diff --git a/test/ReadonlyRecord.ts b/test/ReadonlyRecord.ts index 8150305a4..c2df4e02e 100644 --- a/test/ReadonlyRecord.ts +++ b/test/ReadonlyRecord.ts @@ -4,6 +4,7 @@ import { identity, pipe } from '../src/function' import * as IO from '../src/IO' import * as N from '../src/number' import * as O from '../src/Option' +import { reverse } from '../src/Ord' import * as RA from '../src/ReadonlyArray' import * as _ from '../src/ReadonlyRecord' import * as Se from '../src/Semigroup' @@ -248,14 +249,18 @@ describe('ReadonlyRecord', () => { O.some({ a: 1, b: 2 }) ) U.deepStrictEqual(_.traverse(O.Applicative)((n: number) => (n >= 2 ? O.some(n) : O.none))({ a: 1, b: 2 }), O.none) + }) + it('getTraversable', () => { + const T = _.getTraversable(reverse(S.Ord)) + const f = (n: number) => (n <= 2 ? O.some(n) : O.none) + U.deepStrictEqual(T.traverse(O.Applicative)({ a: 1, b: 2 }, f), O.some({ a: 1, b: 2 })) + U.deepStrictEqual(T.traverse(O.Applicative)({ a: 1, b: 3 }, f), O.none) + // should respect the order + U.deepStrictEqual(pipe(T.traverse(O.Applicative)({ b: 2, a: 1 }, f), O.map(Object.keys)), O.some(['b', 'a'])) U.deepStrictEqual( - _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? O.some(n) : O.none)), - O.some({ a: 1, b: 2 }) - ) - U.deepStrictEqual( - _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? O.some(n) : O.none)), - O.none + pipe(T.sequence(O.Applicative)({ b: O.some(2), a: O.some(1) }), O.map(Object.keys)), + O.some(['b', 'a']) ) }) @@ -276,10 +281,17 @@ describe('ReadonlyRecord', () => { const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f) U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none) U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 })) + }) + it('getTraversableWithIndex', () => { + const TWI = _.getTraversableWithIndex(reverse(S.Ord)) + const f = (k: string, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) + U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ b: 2 }, f), O.some({ b: 2 })) + U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ a: 1, b: 2 }, f), O.none) + // should respect the order U.deepStrictEqual( - _.getTraversableWithIndex(S.Ord).traverseWithIndex(O.Applicative)({ b: 2 }, f), - O.some({ b: 2 }) + pipe(TWI.traverseWithIndex(O.Applicative)({ b: 2, c: 1 }, f), O.map(Object.keys)), + O.some(['c', 'b']) ) }) diff --git a/test/Record.ts b/test/Record.ts index bdf4d7487..bd5b3a3c6 100644 --- a/test/Record.ts +++ b/test/Record.ts @@ -5,6 +5,7 @@ import { identity, pipe } from '../src/function' import * as IO from '../src/IO' import * as N from '../src/number' import * as O from '../src/Option' +import { reverse } from '../src/Ord' import * as _ from '../src/Record' import * as Se from '../src/Semigroup' import { separated } from '../src/Separated' @@ -266,14 +267,18 @@ describe('Record', () => { O.some({ a: 1, b: 2 }) ) U.deepStrictEqual(_.traverse(O.Applicative)((n: number) => (n >= 2 ? O.some(n) : O.none))({ a: 1, b: 2 }), O.none) + }) + it('getTraversable', () => { + const T = _.getTraversable(reverse(S.Ord)) + const f = (n: number) => (n <= 2 ? O.some(n) : O.none) + U.deepStrictEqual(T.traverse(O.Applicative)({ a: 1, b: 2 }, f), O.some({ a: 1, b: 2 })) + U.deepStrictEqual(T.traverse(O.Applicative)({ a: 1, b: 3 }, f), O.none) + // should respect the order + U.deepStrictEqual(pipe(T.traverse(O.Applicative)({ b: 2, a: 1 }, f), O.map(Object.keys)), O.some(['b', 'a'])) U.deepStrictEqual( - _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? O.some(n) : O.none)), - O.some({ a: 1, b: 2 }) - ) - U.deepStrictEqual( - _.getTraversable(S.Ord).traverse(O.Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? O.some(n) : O.none)), - O.none + pipe(T.sequence(O.Applicative)({ b: O.some(2), a: O.some(1) }), O.map(Object.keys)), + O.some(['b', 'a']) ) }) @@ -294,10 +299,17 @@ describe('Record', () => { const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f) U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none) U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 })) + }) + it('getTraversableWithIndex', () => { + const TWI = _.getTraversableWithIndex(reverse(S.Ord)) + const f = (k: string, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) + U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ b: 2 }, f), O.some({ b: 2 })) + U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ a: 1, b: 2 }, f), O.none) + // should respect the order U.deepStrictEqual( - _.getTraversableWithIndex(S.Ord).traverseWithIndex(O.Applicative)({ b: 2 }, f), - O.some({ b: 2 }) + pipe(TWI.traverseWithIndex(O.Applicative)({ b: 2, c: 1 }, f), O.map(Object.keys)), + O.some(['c', 'b']) ) }) From 4b49a4dbcdf10651b55b277ef5d779eae80e6baa Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 05:25:30 +0200 Subject: [PATCH 070/162] undeprecate `local` --- docs/modules/ReaderEither.ts.md | 27 +++++++++++----------- docs/modules/ReaderTask.ts.md | 7 +++--- docs/modules/ReaderTaskEither.ts.md | 31 +++++++++++++------------ src/IOEither.ts | 8 +++---- src/ReaderEither.ts | 20 ++++++++-------- src/ReaderTask.ts | 36 ++++++++++++++--------------- src/ReaderTaskEither.ts | 34 +++++++++++++-------------- src/Task.ts | 8 +++---- src/TaskEither.ts | 16 ++++++------- test/ReaderEither.ts | 6 +---- test/ReaderTask.ts | 1 - test/ReaderTaskEither.ts | 1 - 12 files changed, 96 insertions(+), 99 deletions(-) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index aeb55bcac..52fa5e698 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -46,13 +46,13 @@ Added in v2.0.0 - [fromEitherK](#fromeitherk) - [fromOptionK](#fromoptionk) - [fromReaderK](#fromreaderk) + - [local](#local) - [orElse](#orelse) - [orElseFirst](#orelsefirst) - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) - [orLeft](#orleft) - [swap](#swap) - - [~~local~~](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -483,6 +483,19 @@ export declare const fromReaderK: ( Added in v2.11.0 +## local + +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). + +**Signature** + +```ts +export declare const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.0.0 + ## orElse **Signature** @@ -555,18 +568,6 @@ export declare const swap: (ma: ReaderEither) => ReaderEither< Added in v2.0.0 -## ~~local~~ - -Use `Reader`'s `local` instead. - -**Signature** - -```ts -export declare const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither -``` - -Added in v2.0.0 - # constructors ## ask diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 73eefe248..1c9880268 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -36,7 +36,7 @@ Added in v2.3.0 - [fromIOK](#fromiok) - [fromReaderK](#fromreaderk) - [fromTaskK](#fromtaskk) - - [~~local~~](#local) + - [local](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -316,9 +316,10 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a: A Added in v2.4.0 -## ~~local~~ +## local -Use `Reader`'s `local` instead. +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 98703e9ff..6781663ac 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -58,13 +58,13 @@ Added in v2.0.0 - [fromReaderK](#fromreaderk) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) + - [local](#local) - [orElse](#orelse) - [orElseFirst](#orelsefirst) - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) - [orLeft](#orleft) - [swap](#swap) - - [~~local~~](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -666,6 +666,21 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a Added in v2.10.0 +## local + +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). + +**Signature** + +```ts +export declare const local: ( + f: (r2: R2) => R1 +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.0.0 + ## orElse **Signature** @@ -738,20 +753,6 @@ export declare const swap: (ma: ReaderTaskEither) => ReaderTas Added in v2.0.0 -## ~~local~~ - -Use `Reader`'s `local` instead. - -**Signature** - -```ts -export declare const local: ( - f: (r2: R2) => R1 -) => (ma: ReaderTaskEither) => ReaderTaskEither -``` - -Added in v2.0.0 - # constructors ## ask diff --git a/src/IOEither.ts b/src/IOEither.ts index 83c91b6f7..6cc74f854 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -279,16 +279,16 @@ export const swap: (ma: IOEither) => IOEither = // ------------------------------------------------------------------------------------- /* istanbul ignore next */ -const _map: Monad2['map'] = (fa, f) => pipe(fa, map(f)) +const _map: Functor2['map'] = (fa, f) => pipe(fa, map(f)) /* istanbul ignore next */ -const _ap: Monad2['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _apSeq: Applicative2['ap'] = (fab, fa) => +const _ap: Apply2['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _apSeq: Apply2['ap'] = (fab, fa) => pipe( fab, chain((f) => pipe(fa, map(f))) ) /* istanbul ignore next */ -const _chain: Monad2['chain'] = (ma, f) => pipe(ma, chain(f)) +const _chain: Chain2['chain'] = (ma, f) => pipe(ma, chain(f)) /* istanbul ignore next */ const _bimap: Bifunctor2['bimap'] = (fa, f, g) => pipe(fa, bimap(f, g)) /* istanbul ignore next */ diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index faa1865f1..0927774a7 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -212,6 +212,16 @@ export const toUnion: (fa: ReaderEither) => Reader = // combinators // ------------------------------------------------------------------------------------- +/** + * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s + * `contramap`). + * + * @category combinators + * @since 2.0.0 + */ +export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = + R.local + /** * @category combinators * @since 2.0.0 @@ -983,13 +993,3 @@ export function getReaderValidation( throwError } } - -/** - * Use `Reader`'s `local` instead. - * - * @category combinators - * @since 2.0.0 - * @deprecated - */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = - R.local diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index e8fa21946..10d2644f0 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -81,22 +81,31 @@ export const fromIO: FromIO2['fromIO'] = flow(T.fromIO, fromTask) // ------------------------------------------------------------------------------------- -// non-pipeables +// combinators // ------------------------------------------------------------------------------------- -const _map: Monad2['map'] = (fa, f) => pipe(fa, map(f)) -const _apPar: Monad2['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _apSeq: Monad2['ap'] = (fab, fa) => - pipe( - fab, - chain((f) => pipe(fa, map(f))) - ) -const _chain: Monad2['chain'] = (ma, f) => pipe(ma, chain(f)) +/** + * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s + * `contramap`). + * + * @category combinators + * @since 2.3.0 + */ +export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local // ------------------------------------------------------------------------------------- // type class members // ------------------------------------------------------------------------------------- +const _map: Functor2['map'] = (fa, f) => pipe(fa, map(f)) +const _apPar: Apply2['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _apSeq: Apply2['ap'] = (fab, fa) => + pipe( + fab, + chain((f) => pipe(fa, map(f))) + ) +const _chain: Chain2['chain'] = (ma, f) => pipe(ma, chain(f)) + /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types * use the type constructor `F` to represent some computational context. @@ -644,12 +653,3 @@ export const getMonoid: (M: Monoid) => Monoid> = export function run(ma: ReaderTask, r: R): Promise { return ma(r)() } - -/** - * Use `Reader`'s `local` instead. - * - * @category combinators - * @since 2.3.0 - * @deprecated - */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 1c6744ed6..c155cd263 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -316,6 +316,17 @@ export const toUnion: (fa: ReaderTaskEither) => ReaderTask( + f: (r2: R2) => R1 +) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local + /** * @category combinators * @since 2.0.0 @@ -428,15 +439,15 @@ export const chainTaskEitherK: ( // non-pipeables // ------------------------------------------------------------------------------------- -const _map: Monad3['map'] = (fa, f) => pipe(fa, map(f)) -const _apPar: Monad3['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _apSeq: Monad3['ap'] = (fab, fa) => +const _map: Functor3['map'] = (fa, f) => pipe(fa, map(f)) +const _apPar: Apply3['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _apSeq: Apply3['ap'] = (fab, fa) => pipe( fab, chain((f) => pipe(fa, map(f))) ) /* istanbul ignore next */ -const _chain: Monad3['chain'] = (ma, f) => pipe(ma, chain(f)) +const _chain: Chain3['chain'] = (ma, f) => pipe(ma, chain(f)) /* istanbul ignore next */ const _alt: Alt3['alt'] = (fa, that) => pipe(fa, alt(that)) /* istanbul ignore next */ @@ -899,7 +910,7 @@ export const asks: (f: (r: R) => A) => ReaderTaskEither, R, B>( - f: (...a: A) => R.Reader + f: (...a: A) => Reader ) => (...a: A) => ReaderTaskEither = /*#__PURE__*/ fromReaderK_(FromReader) @@ -909,7 +920,7 @@ export const fromReaderK: , R, B>( * @since 2.11.0 */ export const chainReaderK: ( - f: (a: A) => R.Reader + f: (a: A) => Reader ) => (ma: ReaderTaskEither) => ReaderTaskEither = /*#__PURE__*/ chainReaderK_(FromReader, Chain) @@ -1351,14 +1362,3 @@ export function getReaderTaskValidation( export function run(ma: ReaderTaskEither, r: R): Promise> { return ma(r)() } - -/** - * Use `Reader`'s `local` instead. - * - * @category combinators - * @since 2.0.0 - * @deprecated - */ -export const local: ( - f: (r2: R2) => R1 -) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local diff --git a/src/Task.ts b/src/Task.ts index f6748da88..92970b0a7 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -97,14 +97,14 @@ export function delay(millis: number): (ma: Task) => Task { // non-pipeables // ------------------------------------------------------------------------------------- -const _map: Monad1['map'] = (fa, f) => pipe(fa, map(f)) -const _apPar: Monad1['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _apSeq: Monad1['ap'] = (fab, fa) => +const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) +const _apPar: Apply1['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _apSeq: Apply1['ap'] = (fab, fa) => pipe( fab, chain((f) => pipe(fa, map(f))) ) -const _chain: Monad1['chain'] = (ma, f) => pipe(ma, chain(f)) +const _chain: Chain1['chain'] = (ma, f) => pipe(ma, chain(f)) // ------------------------------------------------------------------------------------- // type class members diff --git a/src/TaskEither.ts b/src/TaskEither.ts index d90f4df28..56d26405a 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -402,19 +402,19 @@ export const chainIOEitherK: ( // non-pipeables // ------------------------------------------------------------------------------------- -const _map: Monad2['map'] = (fa, f) => pipe(fa, map(f)) -/* istanbul ignore next */ -const _bimap: Bifunctor2['bimap'] = (fa, f, g) => pipe(fa, bimap(f, g)) -/* istanbul ignore next */ -const _mapLeft: Bifunctor2['mapLeft'] = (fa, f) => pipe(fa, mapLeft(f)) -const _apPar: Monad2['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _apSeq: Applicative2['ap'] = (fab, fa) => +const _map: Functor2['map'] = (fa, f) => pipe(fa, map(f)) +const _apPar: Apply2['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _apSeq: Apply2['ap'] = (fab, fa) => pipe( fab, chain((f) => pipe(fa, map(f))) ) /* istanbul ignore next */ -const _chain: Monad2['chain'] = (ma, f) => pipe(ma, chain(f)) +const _chain: Chain2['chain'] = (ma, f) => pipe(ma, chain(f)) +/* istanbul ignore next */ +const _bimap: Bifunctor2['bimap'] = (fa, f, g) => pipe(fa, bimap(f, g)) +/* istanbul ignore next */ +const _mapLeft: Bifunctor2['mapLeft'] = (fa, f) => pipe(fa, mapLeft(f)) /* istanbul ignore next */ const _alt: Alt2['alt'] = (fa, that) => pipe(fa, alt(that)) diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index fe882ba63..bc5660b1b 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -211,11 +211,7 @@ describe('ReaderEither', () => { }) it('local', () => { - U.deepStrictEqual( - // tslint:disable-next-line: deprecation - _.local((n: number) => ({ a: n }))((r: { readonly a: number }) => E.right(r.a))(1), - E.right(1) - ) + U.deepStrictEqual(_.local((n: number) => ({ a: n }))((r: { readonly a: number }) => E.right(r.a))(1), E.right(1)) }) it('getApplicativeReaderValidation', () => { diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index fab9343b8..102cca302 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -81,7 +81,6 @@ describe('ReaderTask', () => { U.deepStrictEqual( await pipe( _.asks((n: number) => n + 1), - // tslint:disable-next-line: deprecation _.local(S.size) )('aaa')(), 4 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index b99ea1335..387256efd 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -163,7 +163,6 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual( await pipe( _.asks((n: number) => n + 1), - // tslint:disable-next-line: deprecation _.local(S.size) )('aaa')(), E.right(4) From 296b55a9afcd368deb4f1b99eeaaeae4c7b3d2fa Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 05:37:00 +0200 Subject: [PATCH 071/162] StateReaderTaskEither: add `local` --- CHANGELOG.md | 1 + docs/modules/StateReaderTaskEither.ts.md | 16 ++++++++++++++++ src/StateReaderTaskEither.ts | 11 +++++++++++ test/StateReaderTaskEither.ts | 12 +++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6890765eb..372db0f10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -189,6 +189,7 @@ high state of flux, you're at risk of it changing without notice. - `StateReaderTaskEither` - add `fromStateK` - add `chainStateK` + - add `local` - `string` - add `toUpperCase` - `struct` diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 68467c061..f34a30ce1 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -63,6 +63,7 @@ Added in v2.0.0 - [fromStateK](#fromstatek) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) + - [local](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -721,6 +722,21 @@ export declare const fromTaskK: ( Added in v2.10.0 +## local + +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). + +**Signature** + +```ts +export declare const local: ( + f: (r2: R2) => R1 +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + # constructors ## ask diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index d7a4d8c94..c58012f59 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -228,6 +228,17 @@ export const fromState: (sa: State) => StateReaderTask // combinators // ------------------------------------------------------------------------------------- +/** + * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s + * `contramap`). + * + * @category combinators + * @since 2.11.0 + */ +export const local = (f: (r2: R2) => R1) => ( + ma: StateReaderTaskEither +): StateReaderTaskEither => (s) => (r2) => ma(s)(f(r2)) + /** * @category combinators * @since 2.4.0 diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 4b35041ab..560066bfe 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -1,7 +1,7 @@ import * as assert from 'assert' import * as A from '../src/Array' import * as E from '../src/Either' -import { pipe } from '../src/function' +import { pipe, tuple } from '../src/function' import * as I from '../src/IO' import * as IE from '../src/IOEither' import * as O from '../src/Option' @@ -368,4 +368,14 @@ describe('StateReaderTaskEither', () => { const left: _.StateReaderTaskEither = _.left('a') U.deepStrictEqual(await pipe(left, f)(2)({})(), E.left('a')) }) + + it('local', async () => { + U.deepStrictEqual( + await pipe( + _.asks((n: number) => n + 1), + _.local(S.size) + )({})('aaa')(), + E.right(tuple(4, {})) + ) + }) }) From fb2b735d9673dc342a75a56b3e9c8966117992a6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:07:54 +0200 Subject: [PATCH 072/162] add `asksEW`, `asksW` --- CHANGELOG.md | 9 ++++++ docs/modules/Reader.ts.md | 26 +++++++++++++++++ docs/modules/ReaderEither.ts.md | 26 +++++++++++++++++ docs/modules/ReaderTask.ts.md | 26 +++++++++++++++++ docs/modules/ReaderTaskEither.ts.md | 28 ++++++++++++++++++ docs/modules/StateReaderTaskEither.ts.md | 36 ++++++++++++++++++++++-- src/Reader.ts | 16 +++++++++++ src/ReaderEither.ts | 16 +++++++++++ src/ReaderTask.ts | 16 +++++++++++ src/ReaderTaskEither.ts | 17 +++++++++++ src/StateReaderTaskEither.ts | 25 ++++++++++++++-- test/Reader.ts | 6 ++++ test/StateReaderTaskEither.ts | 9 ++++++ 13 files changed, 251 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 372db0f10..3b90956bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,14 @@ high state of flux, you're at risk of it changing without notice. - `Ord` - add `trivial` instance - add `equals` + - `Reader` + - add `asksEW`, `asksW` + - `ReaderEither` + - add `asksEW`, `asksW` + - `ReaderTask` + - add `asksEW`, `asksW` + - `ReaderTaskEither` + - add `asksEW`, `asksW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -190,6 +198,7 @@ high state of flux, you're at risk of it changing without notice. - add `fromStateK` - add `chainStateK` - add `local` + - add `asksEW`, `asksW` - `string` - add `toUpperCase` - `struct` diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 99883c6b3..743f059f3 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -37,6 +37,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [asksE](#askse) + - [asksEW](#asksew) - [chainFirst](#chainfirst) - [flap](#flap) - [flatten](#flatten) @@ -264,6 +266,30 @@ export declare const apSecond: (second: Reader) => (first: Reader Added in v2.0.0 +## asksE + +Effectfully accesses the environment. + +**Signature** + +```ts +export declare const asksE: (f: (r: R) => Reader) => Reader +``` + +Added in v2.11.0 + +## asksEW + +Less strict version of [`asksE`](#asksE). + +**Signature** + +```ts +export declare const asksEW: (f: (r1: R1) => Reader) => Reader +``` + +Added in v2.11.0 + ## chainFirst Composes computations in sequence, using the return value of one computation to determine the next computation and diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 52fa5e698..ced3e64ef 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -33,6 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [asksE](#askse) + - [asksEW](#asksew) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -306,6 +308,30 @@ export declare const apSecond: ( Added in v2.0.0 +## asksE + +Effectfully accesses the environment. + +**Signature** + +```ts +export declare const asksE: (f: (r: R) => ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + +## asksEW + +Less strict version of [`asksE`](#asksE). + +**Signature** + +```ts +export declare const asksEW: (f: (r1: R1) => ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + ## chainEitherK **Signature** diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 1c9880268..0178f841c 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -25,6 +25,8 @@ Added in v2.3.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [asksE](#askse) + - [asksEW](#asksew) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) - [chainFirstTaskK](#chainfirsttaskk) @@ -195,6 +197,30 @@ export declare const apSecond: (second: ReaderTask) => (first: Re Added in v2.3.0 +## asksE + +Effectfully accesses the environment. + +**Signature** + +```ts +export declare const asksE: (f: (r: R) => ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + +## asksEW + +Less strict version of [`asksE`](#asksE). + +**Signature** + +```ts +export declare const asksEW: (f: (r1: R1) => ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainFirst Composes computations in sequence, using the return value of one computation to determine the next computation and diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 6781663ac..beea89812 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -33,6 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [asksE](#askse) + - [asksEW](#asksew) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -343,6 +345,32 @@ export declare const apSecond: ( Added in v2.0.0 +## asksE + +Effectfully accesses the environment. + +**Signature** + +```ts +export declare const asksE: (f: (r: R) => ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## asksEW + +Less strict version of [`asksE`](#asksE). + +**Signature** + +```ts +export declare const asksEW: ( + f: (r1: R1) => ReaderTaskEither +) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainEitherK **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index f34a30ce1..510d4b9b8 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -33,6 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) + - [asksE](#askse) + - [asksEW](#asksew) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -329,6 +331,34 @@ export declare const apSecond: ( Added in v2.0.0 +## asksE + +Effectfully accesses the environment. + +**Signature** + +```ts +export declare const asksE: ( + f: (r: R) => StateReaderTaskEither +) => StateReaderTaskEither +``` + +Added in v2.11.0 + +## asksEW + +Less strict version of [`asksE`](#asksE). + +**Signature** + +```ts +export declare const asksEW: ( + f: (r1: R1) => StateReaderTaskEither +) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainEitherK **Signature** @@ -470,7 +500,7 @@ Added in v2.10.0 ```ts export declare const chainReaderK: ( - f: (a: A) => Reader + f: (a: A) => R.Reader ) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` @@ -668,7 +698,7 @@ Added in v2.10.0 ```ts export declare const fromReaderK: ( - f: (...a: A) => Reader + f: (...a: A) => R.Reader ) => (...a: A) => StateReaderTaskEither ``` @@ -823,7 +853,7 @@ Added in v2.4.4 **Signature** ```ts -export declare const fromReader: (ma: Reader) => StateReaderTaskEither +export declare const fromReader: (ma: R.Reader) => StateReaderTaskEither ``` Added in v2.11.0 diff --git a/src/Reader.ts b/src/Reader.ts index 01e92c980..e268e7eb0 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -63,6 +63,22 @@ export const asks: (f: (r: R) => A) => Reader = identity export const local: (f: (r2: R2) => R1) => (ma: Reader) => Reader = (f) => (ma) => (r2) => ma(f(r2)) +/** + * Less strict version of [`asksE`](#asksE). + * + * @category combinators + * @since 2.11.0 + */ +export const asksEW = (f: (r1: R1) => Reader): Reader => (r) => f(r)(r) + +/** + * Effectfully accesses the environment. + * + * @category combinators + * @since 2.11.0 + */ +export const asksE: (f: (r: R) => Reader) => Reader = asksEW + // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 0927774a7..c29226998 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -222,6 +222,22 @@ export const toUnion: (fa: ReaderEither) => Reader = export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = R.local +/** + * Less strict version of [`asksE`](#asksE). + * + * @category combinators + * @since 2.11.0 + */ +export const asksEW: (f: (r1: R1) => ReaderEither) => ReaderEither = R.asksEW + +/** + * Effectfully accesses the environment. + * + * @category combinators + * @since 2.11.0 + */ +export const asksE: (f: (r: R) => ReaderEither) => ReaderEither = asksEW + /** * @category combinators * @since 2.0.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 10d2644f0..1ed0f42aa 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -93,6 +93,22 @@ export const fromIO: FromIO2['fromIO'] = */ export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local +/** + * Less strict version of [`asksE`](#asksE). + * + * @category combinators + * @since 2.11.0 + */ +export const asksEW: (f: (r1: R1) => ReaderTask) => ReaderTask = R.asksEW + +/** + * Effectfully accesses the environment. + * + * @category combinators + * @since 2.11.0 + */ +export const asksE: (f: (r: R) => ReaderTask) => ReaderTask = asksEW + // ------------------------------------------------------------------------------------- // type class members // ------------------------------------------------------------------------------------- diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c155cd263..ef2c516d2 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -327,6 +327,23 @@ export const local: ( f: (r2: R2) => R1 ) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local +/** + * Less strict version of [`asksE`](#asksE). + * + * @category combinators + * @since 2.11.0 + */ +export const asksEW: (f: (r1: R1) => ReaderTaskEither) => ReaderTaskEither = + R.asksEW + +/** + * Effectfully accesses the environment. + * + * @category combinators + * @since 2.11.0 + */ +export const asksE: (f: (r: R) => ReaderTaskEither) => ReaderTaskEither = asksEW + /** * @category combinators * @since 2.0.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index c58012f59..5ce2a7588 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -53,7 +53,7 @@ import { MonadThrow4 } from './MonadThrow' import { Option } from './Option' import { Pointed4 } from './Pointed' import { Predicate } from './Predicate' -import { Reader } from './Reader' +import * as R from './Reader' import { ReaderEither } from './ReaderEither' import * as RTE from './ReaderTaskEither' import { Refinement } from './Refinement' @@ -68,6 +68,7 @@ import { TaskEither } from './TaskEither' import ReaderTaskEither = RTE.ReaderTaskEither import Either = E.Either +import Reader = R.Reader /** * @category model @@ -237,7 +238,27 @@ export const fromState: (sa: State) => StateReaderTask */ export const local = (f: (r2: R2) => R1) => ( ma: StateReaderTaskEither -): StateReaderTaskEither => (s) => (r2) => ma(s)(f(r2)) +): StateReaderTaskEither => flow(ma, R.local(f)) + +/** + * Less strict version of [`asksE`](#asksE). + * + * @category combinators + * @since 2.11.0 + */ +export const asksEW = ( + f: (r1: R1) => StateReaderTaskEither +): StateReaderTaskEither => (s) => (r) => f(r)(s)(r) + +/** + * Effectfully accesses the environment. + * + * @category combinators + * @since 2.11.0 + */ +export const asksE: ( + f: (r: R) => StateReaderTaskEither +) => StateReaderTaskEither = asksEW /** * @category combinators diff --git a/test/Reader.ts b/test/Reader.ts index 26f41c8b5..155e8ed71 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -128,4 +128,10 @@ describe('Reader', () => { it('sequenceArray', () => { U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray)(undefined), [1, 2]) }) + + it('asksE', () => { + const e: Env = { count: 0 } + const f = (e: Env) => _.of(e.count + 1) + U.deepStrictEqual(_.asksE(f)(e), 1) + }) }) diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 560066bfe..e9f6b081f 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -378,4 +378,13 @@ describe('StateReaderTaskEither', () => { E.right(tuple(4, {})) ) }) + + it('asksE', async () => { + interface Env { + readonly count: number + } + const e: Env = { count: 0 } + const f = (e: Env) => _.of(e.count + 1) + U.deepStrictEqual(await _.asksE(f)({})(e)(), E.right(tuple(1, {}))) + }) }) From 364745d1543cabf04273acf7ddb5eec0ce5ef4d6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 6 Apr 2021 19:23:40 +0200 Subject: [PATCH 073/162] Record / ReadonlyRecord: simplify collect --- src/ReadonlyRecord.ts | 17 ++++++----------- src/Record.ts | 25 ++++++++++--------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index ced59546d..8dd4ef504 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -114,23 +114,18 @@ export function collect( * @deprecated */ export function collect(f: (k: K, a: A) => B): (r: ReadonlyRecord) => ReadonlyArray -export function collect( - O: Ord | ((k: string, a: unknown) => unknown) +export function collect( + O: Ord | ((k: string, a: A) => B) ): | ((f: (k: K, a: A) => B) => (r: ReadonlyRecord) => ReadonlyArray) - | ((r: ReadonlyRecord) => ReadonlyArray) { + | ((r: ReadonlyRecord) => ReadonlyArray) { if (typeof O === 'function') { - return (r: ReadonlyRecord) => { - const out: Array = [] - for (const key of keys(r)) { - out.push(O(key, r[key])) - } - return out - } + return collect(S.Ord)(O) } + const keysO = keys_(O) return (f: (k: K, a: A) => B) => (r: ReadonlyRecord) => { const out: Array = [] - for (const key of keys_(O)(r)) { + for (const key of keysO(r)) { out.push(f(key, r[key])) } return out diff --git a/src/Record.ts b/src/Record.ts index ddbcc62b1..1dde92e83 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -80,23 +80,18 @@ export function collect(O: Ord): (f: (k: K, a: A * @deprecated */ export function collect(f: (k: K, a: A) => B): (r: Record) => Array -export function collect( - arg: Ord | ((k: string, a: unknown) => unknown) +export function collect( + O: Ord | ((k: string, a: A) => B) ): - | ((r: Record) => Array) - | ((f: (k: string, a: unknown) => unknown) => (r: Record) => Array) { - if (typeof arg === 'function') { - return (r: Record) => { - const out: Array = [] - for (const key of keys(r)) { - out.push(arg(key, r[key])) - } - return out - } + | ((f: (k: K, a: A) => B) => (r: Record) => Array) + | ((r: Record) => Array) { + if (typeof O === 'function') { + return collect(S.Ord)(O) } - return (f: (k: string, a: unknown) => unknown) => (r: Record) => { - const out: Array = [] - for (const key of keys_(arg)(r)) { + const keysO = keys_(O) + return (f: (k: K, a: A) => B) => (r: Record) => { + const out: Array = [] + for (const key of keysO(r)) { out.push(f(key, r[key])) } return out From 8c53b1e1ebdcdb63620bee410d2bbd5524dbf9e4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 6 Apr 2021 19:32:26 +0200 Subject: [PATCH 074/162] chore --- docs/modules/ReadonlyRecord.ts.md | 12 +++---- src/ReadonlyRecord.ts | 60 ++++++++++++++----------------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 621a5fa23..b1dbd07a8 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -193,8 +193,8 @@ Added in v2.5.0 ```ts export declare function foldMap( O: Ord -): (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M -export declare function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Readonly>) => M +): (M: Monoid) => (f: (a: A) => M) => (fa: ReadonlyRecord) => M +export declare function foldMap(M: Monoid): (f: (a: A) => M) => (fa: ReadonlyRecord) => M ``` Added in v2.5.0 @@ -206,8 +206,8 @@ Added in v2.5.0 ```ts export declare function reduce( O: Ord -): (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B -export declare function reduce(b: B, f: (b: B, a: A) => B): (fa: Readonly>) => B +): (b: B, f: (b: B, a: A) => B) => (fa: ReadonlyRecord) => B +export declare function reduce(b: B, f: (b: B, a: A) => B): (fa: ReadonlyRecord) => B ``` Added in v2.5.0 @@ -219,8 +219,8 @@ Added in v2.5.0 ```ts export declare function reduceRight( O: Ord -): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B -export declare function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B +): (b: B, f: (a: A, b: B) => B) => (fa: ReadonlyRecord) => B +export declare function reduceRight(b: B, f: (a: A, b: B) => B): (fa: ReadonlyRecord) => B ``` Added in v2.5.0 diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 8dd4ef504..d791f74af 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1002,7 +1002,7 @@ const _reduceRightWithIndex: (O: Ord) => FoldableWithIndex1 const _partitionMapWithIndex = ( fa: ReadonlyRecord, f: (key: string, a: A) => Either -): Separated>, Readonly>> => pipe(fa, partitionMapWithIndex(f)) +): Separated, ReadonlyRecord> => pipe(fa, partitionMapWithIndex(f)) /* istanbul ignore next */ const _partitionWithIndex = (fa: ReadonlyRecord, predicateWithIndex: PredicateWithIndex) => pipe(fa, partitionWithIndex(predicateWithIndex)) @@ -1068,9 +1068,9 @@ export const _traverseWithIndex = (O: Ord) => ( * @since 2.5.0 */ export const filter: { - (refinement: Refinement): (fa: Readonly>) => Readonly> - (predicate: Predicate): (fa: Readonly>) => Readonly> -} = (predicate: Predicate): ((fa: Readonly>) => Readonly>) => + (refinement: Refinement): (fa: ReadonlyRecord) => ReadonlyRecord + (predicate: Predicate): (fa: ReadonlyRecord) => ReadonlyRecord +} = (predicate: Predicate): ((fa: ReadonlyRecord) => ReadonlyRecord) => filterWithIndex((_, a) => predicate(a)) /** @@ -1079,7 +1079,7 @@ export const filter: { */ export const filterMap: ( f: (a: A) => Option -) => (fa: Readonly>) => Readonly> = (f) => filterMapWithIndex((_, a) => f(a)) +) => (fa: ReadonlyRecord) => ReadonlyRecord = (f) => filterMapWithIndex((_, a) => f(a)) /** * @category Filterable @@ -1087,14 +1087,14 @@ export const filterMap: ( */ export const partition: { (refinement: Refinement): ( - fa: Readonly> - ) => Separated>, Readonly>> + fa: ReadonlyRecord + ) => Separated, ReadonlyRecord> (predicate: Predicate): ( - fa: Readonly> - ) => Separated>, Readonly>> + fa: ReadonlyRecord + ) => Separated, ReadonlyRecord> } = ( predicate: Predicate -): ((fa: Readonly>) => Separated>, Readonly>>) => +): ((fa: ReadonlyRecord) => Separated, ReadonlyRecord>) => partitionWithIndex((_, a) => predicate(a)) /** @@ -1103,25 +1103,24 @@ export const partition: { */ export const partitionMap: ( f: (a: A) => Either -) => (fa: Readonly>) => Separated>, Readonly>> = (f) => +) => (fa: ReadonlyRecord) => Separated, ReadonlyRecord> = (f) => partitionMapWithIndex((_, a) => f(a)) - /** * @category Foldable * @since 2.5.0 */ -export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: Readonly>) => B +export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (fa: ReadonlyRecord) => B /** * Use the overload constrained by `Ord` instead. * * @deprecated */ -export function reduce(b: B, f: (b: B, a: A) => B): (fa: Readonly>) => B +export function reduce(b: B, f: (b: B, a: A) => B): (fa: ReadonlyRecord) => B export function reduce( ...args: [Ord] | [unknown, (b: unknown, A: unknown) => unknown] ): - | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: Readonly>) => unknown) - | ((fa: Readonly>) => unknown) { + | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) + | ((fa: ReadonlyRecord) => unknown) { if (args.length === 1) { return (b: unknown, f: (b: unknown, a: unknown) => unknown) => reduceWithIndex(args[0])(b, (_, b, a) => f(b, a)) } @@ -1135,18 +1134,18 @@ export function reduce( */ export function foldMap( O: Ord -): (M: Monoid) => (f: (a: A) => M) => (fa: Readonly>) => M +): (M: Monoid) => (f: (a: A) => M) => (fa: ReadonlyRecord) => M /** * Use the overload constrained by `Ord` instead. * * @deprecated */ -export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Readonly>) => M +export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: ReadonlyRecord) => M export function foldMap( arg: Ord | Monoid ): - | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: Readonly>) => unknown) - | ((f: (a: unknown) => unknown) => (fa: Readonly>) => unknown) { + | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) + | ((f: (a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) { if ('compare' in arg) { return (M: Monoid) => { const foldMapWithIndexM = foldMapWithIndex(arg)(M) @@ -1162,20 +1161,18 @@ export function foldMap( * @category Foldable * @since 2.5.0 */ -export function reduceRight( - O: Ord -): (b: B, f: (a: A, b: B) => B) => (fa: Readonly>) => B +export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) => (fa: ReadonlyRecord) => B /** * Use the overload constrained by `Ord` instead. * * @deprecated */ -export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Readonly>) => B +export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: ReadonlyRecord) => B export function reduceRight( ...args: [Ord] | [unknown, (a: unknown, b: unknown) => unknown] ): - | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: Readonly>) => unknown) - | ((fa: Readonly>) => unknown) { + | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) + | ((fa: ReadonlyRecord) => unknown) { if (args.length === 2) { // tslint:disable-next-line: deprecation return reduceRightWithIndex(args[0], (_, a, b) => args[1](a, b)) @@ -1187,7 +1184,7 @@ export function reduceRight( * @category Compactable * @since 2.5.0 */ -export const compact = (r: Readonly>>): Readonly> => { +export const compact = (r: ReadonlyRecord>): ReadonlyRecord => { const out: Record = {} for (const k in r) { if (_.hasOwnProperty.call(r, k)) { @@ -1205,8 +1202,8 @@ export const compact = (r: Readonly>>): Readonly( - r: Readonly>> -): Separated>, Readonly>> => { + r: ReadonlyRecord> +): Separated, ReadonlyRecord> => { const left: Record = {} const right: Record = {} for (const k in r) { @@ -1628,10 +1625,7 @@ export const Witherable: Witherable1 = { * @since 2.5.0 * @deprecated */ -export const insertAt: ( - k: string, - a: A -) => (r: Readonly>) => Readonly> = upsertAt +export const insertAt: (k: string, a: A) => (r: ReadonlyRecord) => ReadonlyRecord = upsertAt /** * Use `has` instead. From a24f8e8aa82e5f641f3b4be87591a392dbd9e28f Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 6 Apr 2021 20:13:19 +0200 Subject: [PATCH 075/162] chore --- dtslint/ts3.5/ReadonlyRecord.ts | 13 +++++ dtslint/ts3.5/Record.ts | 13 +++++ src/ReadonlyRecord.ts | 87 ++++++++++++++++++++------------ src/Record.ts | 88 ++++++++++++++------------------- test/Record.ts | 2 + 5 files changed, 122 insertions(+), 81 deletions(-) diff --git a/dtslint/ts3.5/ReadonlyRecord.ts b/dtslint/ts3.5/ReadonlyRecord.ts index 0ba2ab16a..af43096b8 100644 --- a/dtslint/ts3.5/ReadonlyRecord.ts +++ b/dtslint/ts3.5/ReadonlyRecord.ts @@ -196,3 +196,16 @@ _.lookup('a') // $ExpectType (r: Readonly>) => Option _.elem(N.Eq)(1, recordString) // $ExpectType boolean _.elem(N.Eq)(1) // $ExpectType (fa: Readonly>) => boolean + +// +// reduce +// + +pipe( + r1, + _.reduce(1, (acc, n) => acc + n) +) +pipe( + r1, + _.reduce(S.Ord)(1, (acc, n) => acc + n) +) diff --git a/dtslint/ts3.5/Record.ts b/dtslint/ts3.5/Record.ts index 3c5f50c80..e7933f2eb 100644 --- a/dtslint/ts3.5/Record.ts +++ b/dtslint/ts3.5/Record.ts @@ -205,3 +205,16 @@ _.lookup('a') // $ExpectType (r: Record) => Option _.elem(N.Eq)(1, recordString) // $ExpectType boolean _.elem(N.Eq)(1) // $ExpectType (fa: Record) => boolean + +// +// reduce +// + +pipe( + r1, + _.reduce(1, (acc, n) => acc + n) +) +pipe( + r1, + _.reduce(S.Ord)(1, (acc, n) => acc + n) +) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index d791f74af..c5bd2ceed 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -958,61 +958,86 @@ export const difference = (second: ReadonlyRecord) => ( // non-pipeables // ------------------------------------------------------------------------------------- -const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) -/* istanbul ignore next */ -const _mapWithIndex: FunctorWithIndex1['mapWithIndex'] = (fa, f) => pipe(fa, mapWithIndex(f)) +/** @internal */ +export const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) +/** @internal */ /* istanbul ignore next */ -const _reduce: (O: Ord) => Foldable1['reduce'] = (O: Ord) => (fa, b, f) => - pipe(fa, reduce(O)(b, f)) +export const _mapWithIndex: FunctorWithIndex1['mapWithIndex'] = (fa, f) => pipe(fa, mapWithIndex(f)) +/** @internal */ /* istanbul ignore next */ -const _foldMap: (O: Ord) => Foldable1['foldMap'] = (O) => (M) => { +export const _reduce: (O: Ord) => Foldable1['reduce'] = (O: Ord) => { + const reduceO = reduce(O) + return (fa, b, f) => pipe(fa, reduceO(b, f)) +} +/** @internal */ +export const _foldMap: (O: Ord) => Foldable1['foldMap'] = (O) => (M) => { const foldMapM = foldMap(O)(M) return (fa, f) => pipe(fa, foldMapM(f)) } +/** @internal */ /* istanbul ignore next */ -const _reduceRight: (O: Ord) => Foldable1['reduceRight'] = (O) => (fa, b, f) => - pipe(fa, reduceRight(O)(b, f)) +export const _reduceRight: (O: Ord) => Foldable1['reduceRight'] = (O) => { + const reduceRightO = reduceRight(O) + return (fa, b, f) => pipe(fa, reduceRightO(b, f)) +} +/** @internal */ /* istanbul ignore next */ -const _filter = (fa: ReadonlyRecord, predicate: Predicate): ReadonlyRecord => +export const _filter = (fa: ReadonlyRecord, predicate: Predicate): ReadonlyRecord => pipe(fa, filter(predicate)) +/** @internal */ /* istanbul ignore next */ -const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) +export const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) +/** @internal */ /* istanbul ignore next */ -const _partition = ( +export const _partition = ( fa: ReadonlyRecord, predicate: Predicate ): Separated, ReadonlyRecord> => pipe(fa, partition(predicate)) +/** @internal */ /* istanbul ignore next */ -const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) -/* istanbul ignore next */ -const _reduceWithIndex: (O: Ord) => FoldableWithIndex1['reduceWithIndex'] = (O) => (fa, b, f) => - pipe(fa, reduceWithIndex(O)(b, f)) +export const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) +/** @internal */ /* istanbul ignore next */ -const _foldMapWithIndex: (O: Ord) => FoldableWithIndex1['foldMapWithIndex'] = (O) => (M) => { - const foldMapWithIndexM = foldMapWithIndex(O)(M) - return (fa, f) => pipe(fa, foldMapWithIndexM(f)) +export const _reduceWithIndex: (O: Ord) => FoldableWithIndex1['reduceWithIndex'] = (O) => { + const reduceWithIndexO = reduceWithIndex(O) + return (fa, b, f) => pipe(fa, reduceWithIndexO(b, f)) } +/** @internal */ +export const _foldMapWithIndex: (O: Ord) => FoldableWithIndex1['foldMapWithIndex'] = (O) => { + const foldMapWithIndexO = foldMapWithIndex(O) + return (M) => { + const foldMapWithIndexM = foldMapWithIndexO(M) + return (fa, f) => pipe(fa, foldMapWithIndexM(f)) + } +} +/** @internal */ /* istanbul ignore next */ -const _reduceRightWithIndex: (O: Ord) => FoldableWithIndex1['reduceRightWithIndex'] = (O) => ( - fa, - b, - f -) => pipe(fa, reduceRightWithIndex(O)(b, f)) +export const _reduceRightWithIndex: (O: Ord) => FoldableWithIndex1['reduceRightWithIndex'] = ( + O +) => { + const reduceRightWithIndexO = reduceRightWithIndex(O) + return (fa, b, f) => pipe(fa, reduceRightWithIndexO(b, f)) +} +/** @internal */ /* istanbul ignore next */ -const _partitionMapWithIndex = ( +export const _partitionMapWithIndex = ( fa: ReadonlyRecord, f: (key: string, a: A) => Either ): Separated, ReadonlyRecord> => pipe(fa, partitionMapWithIndex(f)) +/** @internal */ /* istanbul ignore next */ -const _partitionWithIndex = (fa: ReadonlyRecord, predicateWithIndex: PredicateWithIndex) => - pipe(fa, partitionWithIndex(predicateWithIndex)) +export const _partitionWithIndex = ( + fa: ReadonlyRecord, + predicateWithIndex: PredicateWithIndex +) => pipe(fa, partitionWithIndex(predicateWithIndex)) +/** @internal */ /* istanbul ignore next */ -const _filterMapWithIndex = (fa: ReadonlyRecord, f: (key: string, a: A) => Option) => +export const _filterMapWithIndex = (fa: ReadonlyRecord, f: (key: string, a: A) => Option) => pipe(fa, filterMapWithIndex(f)) +/** @internal */ /* istanbul ignore next */ -const _filterWithIndex = (fa: ReadonlyRecord, predicateWithIndex: PredicateWithIndex) => +export const _filterWithIndex = (fa: ReadonlyRecord, predicateWithIndex: PredicateWithIndex) => pipe(fa, filterWithIndex(predicateWithIndex)) - /** @internal */ export const _traverse = ( O: Ord @@ -1035,8 +1060,7 @@ export const _sequence = ( return (ta) => traverseOF(ta, identity) } } -/** @internal */ -export const _traverseWithIndex = (O: Ord) => ( +const _traverseWithIndex = (O: Ord) => ( F: Applicative ): ((ta: ReadonlyRecord, f: (k: string, a: A) => HKT) => HKT>) => { const keysO = keys_(O) @@ -1105,6 +1129,7 @@ export const partitionMap: ( f: (a: A) => Either ) => (fa: ReadonlyRecord) => Separated, ReadonlyRecord> = (f) => partitionMapWithIndex((_, a) => f(a)) + /** * @category Foldable * @since 2.5.0 diff --git a/src/Record.ts b/src/Record.ts index 1dde92e83..8730cb1b8 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -617,58 +617,46 @@ export const difference = (second: Record) => (first: Record['map'] = (fa, f) => pipe(fa, map(f)) -/* istanbul ignore next */ -const _mapWithIndex: FunctorWithIndex1['mapWithIndex'] = (fa, f) => pipe(fa, mapWithIndex(f)) -/* istanbul ignore next */ -const _reduce = (O: Ord): Foldable1['reduce'] => (fa, b, f) => pipe(fa, reduce(O)(b, f)) -/* istanbul ignore next */ -const _foldMap = (O: Ord): Foldable1['foldMap'] => (M) => { - const foldMapOM = foldMap(O)(M) - return (fa, f) => pipe(fa, foldMapOM(f)) -} -/* istanbul ignore next */ -const _reduceRight = (O: Ord): Foldable1['reduceRight'] => (fa, b, f) => pipe(fa, reduceRight(O)(b, f)) -/* istanbul ignore next */ -const _filter = (fa: Record, predicate: Predicate): Record => pipe(fa, filter(predicate)) -/* istanbul ignore next */ -const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) -/* istanbul ignore next */ -const _partition = ( - fa: Record, - predicate: Predicate -): Separated, Record> => pipe(fa, partition(predicate)) -/* istanbul ignore next */ -const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) -/* istanbul ignore next */ -const _reduceWithIndex = (O: Ord): FoldableWithIndex1['reduceWithIndex'] => (fa, b, f) => - pipe(fa, reduceWithIndex(O)(b, f)) -/* istanbul ignore next */ -const _foldMapWithIndex = (O: Ord): FoldableWithIndex1['foldMapWithIndex'] => (M) => { - const foldMapWithIndexOM = foldMapWithIndex(O)(M) - return (fa, f) => pipe(fa, foldMapWithIndexOM(f)) -} -/* istanbul ignore next */ -const _reduceRightWithIndex = (O: Ord): FoldableWithIndex1['reduceRightWithIndex'] => (fa, b, f) => - pipe(fa, reduceRightWithIndex(O)(b, f)) -/* istanbul ignore next */ -const _partitionMapWithIndex = ( - fa: Record, - f: (key: string, a: A) => Either -): Separated, Record> => pipe(fa, partitionMapWithIndex(f)) -/* istanbul ignore next */ -const _partitionWithIndex = (fa: Record, predicateWithIndex: PredicateWithIndex) => - pipe(fa, partitionWithIndex(predicateWithIndex)) -/* istanbul ignore next */ -const _filterMapWithIndex = (fa: Record, f: (key: string, a: A) => Option) => - pipe(fa, filterMapWithIndex(f)) -/* istanbul ignore next */ -const _filterWithIndex = (fa: Record, predicateWithIndex: PredicateWithIndex) => - pipe(fa, filterWithIndex(predicateWithIndex)) +const _map = RR._map +const _mapWithIndex = RR._mapWithIndex +const _reduce = RR._reduce +const _foldMap = RR._foldMap +const _reduceRight = RR._reduceRight +const _filter = RR._filter +const _filterMap = RR._filterMap +const _partition = RR._partition +const _partitionMap = RR._partitionMap +const _reduceWithIndex = RR._reduceWithIndex +const _foldMapWithIndex = RR._foldMapWithIndex +const _reduceRightWithIndex = RR._reduceRightWithIndex +const _partitionMapWithIndex = RR._partitionMapWithIndex +const _partitionWithIndex = RR._partitionWithIndex +const _filterMapWithIndex = RR._filterMapWithIndex +const _filterWithIndex = RR._filterWithIndex const _traverse = RR._traverse const _sequence = RR._sequence -const _traverseWithIndex = RR._traverseWithIndex +const _traverseWithIndex = (O: Ord) => ( + F: Applicative +): ((ta: Record, f: (k: string, a: A) => HKT) => HKT>) => { + const keysO = keys_(O) + return (ta: Record, f: (k: string, a: A) => HKT) => { + const ks = keysO(ta) + if (ks.length === 0) { + return F.of({}) + } + let fr: HKT> = F.of({}) + for (const key of ks) { + fr = F.ap( + F.map(fr, (r) => (b: B) => { + r[key] = b + return r + }), + f(key, ta[key]) + ) + } + return fr + } +} // ------------------------------------------------------------------------------------- // type class members diff --git a/test/Record.ts b/test/Record.ts index bd5b3a3c6..100300190 100644 --- a/test/Record.ts +++ b/test/Record.ts @@ -299,6 +299,7 @@ describe('Record', () => { const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f) U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none) U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 })) + U.deepStrictEqual(pipe({}, traverseWithIndex), O.some({})) }) it('getTraversableWithIndex', () => { @@ -306,6 +307,7 @@ describe('Record', () => { const f = (k: string, n: number): O.Option => (k !== 'a' ? O.some(n) : O.none) U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ b: 2 }, f), O.some({ b: 2 })) U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({ a: 1, b: 2 }, f), O.none) + U.deepStrictEqual(TWI.traverseWithIndex(O.Applicative)({}, f), O.some({})) // should respect the order U.deepStrictEqual( pipe(TWI.traverseWithIndex(O.Applicative)({ b: 2, c: 1 }, f), O.map(Object.keys)), From b0d72775406c5bb0792d381a07ff872ea7f3c53d Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:19:43 +0200 Subject: [PATCH 076/162] Record / ReadonlyRecord: simplify reduce --- src/ReadonlyRecord.ts | 14 ++++++-------- src/Record.ts | 14 ++++---------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index c5bd2ceed..f41396671 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1141,16 +1141,14 @@ export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (f * @deprecated */ export function reduce(b: B, f: (b: B, a: A) => B): (fa: ReadonlyRecord) => B -export function reduce( - ...args: [Ord] | [unknown, (b: unknown, A: unknown) => unknown] -): - | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) - | ((fa: ReadonlyRecord) => unknown) { +export function reduce( + ...args: [Ord] | [B, (b: B, a: A) => B] +): ((b: B, f: (b: B, a: A) => B) => (fa: ReadonlyRecord) => B) | ((fa: ReadonlyRecord) => B) { if (args.length === 1) { - return (b: unknown, f: (b: unknown, a: unknown) => unknown) => reduceWithIndex(args[0])(b, (_, b, a) => f(b, a)) + const reduceWithIndexO = reduceWithIndex(args[0]) + return (b: B, f: (b: B, a: A) => B) => reduceWithIndexO(b, (_, b, a) => f(b, a)) } - // tslint:disable-next-line: deprecation - return reduceWithIndex(args[0], (_, b, a) => args[1](b, a)) + return reduce(S.Ord)(...args) } /** diff --git a/src/Record.ts b/src/Record.ts index 8730cb1b8..6b1f50ef9 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -730,16 +730,10 @@ export function reduce(O: Ord): (b: B, f: (b: B, a: A) => B) => (f * @deprecated */ export function reduce(b: B, f: (b: B, a: A) => B): (fa: Record) => B -export function reduce( - ...args: [Ord] | [unknown, (b: unknown, a: unknown) => unknown] -): - | ((b: unknown, f: (b: unknown, a: unknown) => unknown) => (fa: Record) => unknown) - | ((fa: Record) => unknown) { - if (args.length === 2) { - // tslint:disable-next-line: deprecation - return RR.reduce(args[0], args[1]) - } - return RR.reduce(args[0]) +export function reduce( + ...args: [Ord] | [B, (b: B, a: A) => B] +): ((b: B, f: (b: B, a: A) => B) => (fa: Record) => B) | ((fa: Record) => B) { + return args.length === 2 ? RR.reduce(S.Ord)(...args) : RR.reduce(args[0]) } /** From a7830d2ff9eb5e6e9256cdf9352d0963f771bf9c Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:29:58 +0200 Subject: [PATCH 077/162] Record / ReadonlyRecord: simplify foldMap --- src/ReadonlyRecord.ts | 21 ++++++++++----------- src/Record.ts | 42 +++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index f41396671..12836228d 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1164,20 +1164,19 @@ export function foldMap( * @deprecated */ export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: ReadonlyRecord) => M -export function foldMap( - arg: Ord | Monoid +export function foldMap( + O: Ord | Monoid ): - | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) - | ((f: (a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) { - if ('compare' in arg) { - return (M: Monoid) => { - const foldMapWithIndexM = foldMapWithIndex(arg)(M) - return (f: (a: unknown) => unknown) => foldMapWithIndexM((_, a) => f(a)) + | ((M: Monoid) => (f: (a: A) => M) => (fa: ReadonlyRecord) => M) + | ((f: (a: A) => M) => (fa: ReadonlyRecord) => M) { + if ('compare' in O) { + const foldMapWithIndexO = foldMapWithIndex(O) + return (M: Monoid) => { + const foldMapWithIndexM = foldMapWithIndexO(M) + return (f: (a: A) => M): ((fa: ReadonlyRecord) => M) => foldMapWithIndexM((_, a) => f(a)) } } - // tslint:disable-next-line: deprecation - const foldMapWithIndexM = foldMapWithIndex(arg) - return (f: (a: unknown) => unknown) => foldMapWithIndexM((_, a) => f(a)) + return foldMap(S.Ord)(O) } /** diff --git a/src/Record.ts b/src/Record.ts index 6b1f50ef9..6d3d9f9a2 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -677,29 +677,6 @@ export const filter: { */ export const filterMap: (f: (a: A) => Option) => (fa: Record) => Record = RR.filterMap -/** - * @category Foldable - * @since 2.0.0 - */ -export function foldMap(O: Ord): (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M -/** - * Use the overload constrained by `Ord` instead. - * - * @deprecated - */ -export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M -export function foldMap( - O: Ord | Monoid -): - | ((M: Monoid) => (f: (a: unknown) => unknown) => (fa: Record) => unknown) - | ((f: (a: unknown) => unknown) => (fa: Record) => unknown) { - if ('compare' in O) { - return RR.foldMap(O) - } - // tslint:disable-next-line: deprecation - return RR.foldMap(O) -} - /** * @category Filterable * @since 2.0.0 @@ -736,6 +713,25 @@ export function reduce( return args.length === 2 ? RR.reduce(S.Ord)(...args) : RR.reduce(args[0]) } +/** + * @category Foldable + * @since 2.0.0 + */ +export function foldMap(O: Ord): (M: Monoid) => (f: (a: A) => M) => (fa: Record) => M +/** + * Use the overload constrained by `Ord` instead. + * + * @deprecated + */ +export function foldMap(M: Monoid): (f: (a: A) => M) => (fa: Record) => M +export function foldMap( + O: Ord | Monoid +): + | ((M: Monoid) => (f: (a: A) => M) => (fa: Record) => M) + | ((f: (a: A) => M) => (fa: Record) => M) { + return 'compare' in O ? RR.foldMap(O) : RR.foldMap(S.Ord)(O) +} + /** * @category Foldable * @since 2.0.0 From 4668d250af0cd53c6d841d4934a83f33016d10ac Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:34:14 +0200 Subject: [PATCH 078/162] Record / ReadonlyRecord: simplify reduceRight --- src/ReadonlyRecord.ts | 16 +++++++--------- src/Record.ts | 14 ++++---------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 12836228d..4d35b569e 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1190,16 +1190,14 @@ export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: ReadonlyRecord) => B -export function reduceRight( - ...args: [Ord] | [unknown, (a: unknown, b: unknown) => unknown] -): - | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) - | ((fa: ReadonlyRecord) => unknown) { - if (args.length === 2) { - // tslint:disable-next-line: deprecation - return reduceRightWithIndex(args[0], (_, a, b) => args[1](a, b)) +export function reduceRight( + ...args: [Ord] | [B, (a: A, b: B) => B] +): ((b: B, f: (a: A, b: B) => B) => (fa: ReadonlyRecord) => B) | ((fa: ReadonlyRecord) => B) { + if (args.length === 1) { + const reduceRightWithIndexO = reduceRightWithIndex(args[0]) + return (b: B, f: (a: A, b: B) => B) => reduceRightWithIndexO(b, (_, b, a) => f(b, a)) } - return (b, f) => reduceRightWithIndex(args[0])(b, (_, a, b) => f(a, b)) + return reduceRight(S.Ord)(...args) } /** diff --git a/src/Record.ts b/src/Record.ts index 6d3d9f9a2..7842bf823 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -743,16 +743,10 @@ export function reduceRight(O: Ord): (b: B, f: (a: A, b: B) => B) * @deprecated */ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record) => B -export function reduceRight( - ...args: [Ord] | [unknown, (a: unknown, b: unknown) => unknown] -): - | ((b: unknown, f: (a: unknown, b: unknown) => unknown) => (fa: Record) => unknown) - | ((fa: Record) => unknown) { - if (args.length === 2) { - // tslint:disable-next-line: deprecation - return RR.reduceRight(args[0], args[1]) - } - return RR.reduceRight(args[0]) +export function reduceRight( + ...args: [Ord] | [B, (a: A, b: B) => B] +): ((b: B, f: (a: A, b: B) => B) => (fa: Record) => B) | ((fa: Record) => B) { + return args.length === 2 ? RR.reduceRight(S.Ord)(...args) : RR.reduceRight(args[0]) } /** From 8eaf1b051effbe18afecb510b6f1dacff9396a80 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:41:13 +0200 Subject: [PATCH 079/162] Record / ReadonlyRecord: simplify reduceWithIndex --- src/ReadonlyRecord.ts | 27 ++++++++------------------- src/Record.ts | 18 ++++++------------ 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 4d35b569e..929996b33 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -362,29 +362,18 @@ export function reduceWithIndex( b: B, f: (k: K, b: B, a: A) => B ): (fa: ReadonlyRecord) => B -export function reduceWithIndex( - ...args: [Ord] | [unknown, (k: string, b: unknown, a: unknown) => unknown] +export function reduceWithIndex( + ...args: [Ord] | [B, (k: string, b: B, a: A) => B] ): - | (( - b: unknown, - f: (k: string, b: unknown, a: unknown) => unknown - ) => (fa: ReadonlyRecord) => unknown) - | ((fa: ReadonlyRecord) => unknown) { + | ((b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord) => B) + | ((fa: ReadonlyRecord) => B) { if (args.length === 2) { - return (fa: ReadonlyRecord) => { - let out = args[0] - const ks = keys(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = args[1](k, out, fa[k]) - } - return out - } + return reduceWithIndex(S.Ord)(...args) } + const keysO = keys_(args[0]) return (b, f) => (fa) => { - let out = b - const ks = keys_(args[0])(fa) + let out: B = b + const ks = keysO(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] diff --git a/src/Record.ts b/src/Record.ts index 7842bf823..62352bc28 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -247,16 +247,10 @@ export function reduceWithIndex( * @deprecated */ export function reduceWithIndex(b: B, f: (k: K, b: B, a: A) => B): (fa: Record) => B -export function reduceWithIndex( - ...args: [Ord] | [unknown, (k: unknown, b: unknown, a: unknown) => unknown] -): - | ((b: unknown, f: (k: unknown, b: unknown, a: unknown) => unknown) => (fa: Record) => unknown) - | ((fa: Record) => unknown) { - if (args.length === 2) { - // tslint:disable-next-line: deprecation - return RR.reduceWithIndex(args[0], args[1]) - } - return RR.reduceWithIndex(args[0]) +export function reduceWithIndex( + ...args: [Ord] | [B, (k: string, b: B, a: A) => B] +): ((b: B, f: (k: string, b: B, a: A) => B) => (fa: Record) => B) | ((fa: Record) => B) { + return args.length === 1 ? RR.reduceWithIndex(args[0]) : RR.reduceWithIndex(S.Ord)(...args) } /** @@ -710,7 +704,7 @@ export function reduce(b: B, f: (b: B, a: A) => B): (fa: Record export function reduce( ...args: [Ord] | [B, (b: B, a: A) => B] ): ((b: B, f: (b: B, a: A) => B) => (fa: Record) => B) | ((fa: Record) => B) { - return args.length === 2 ? RR.reduce(S.Ord)(...args) : RR.reduce(args[0]) + return args.length === 1 ? RR.reduce(args[0]) : RR.reduce(S.Ord)(...args) } /** @@ -746,7 +740,7 @@ export function reduceRight(b: B, f: (a: A, b: B) => B): (fa: Record( ...args: [Ord] | [B, (a: A, b: B) => B] ): ((b: B, f: (a: A, b: B) => B) => (fa: Record) => B) | ((fa: Record) => B) { - return args.length === 2 ? RR.reduceRight(S.Ord)(...args) : RR.reduceRight(args[0]) + return args.length === 1 ? RR.reduceRight(args[0]) : RR.reduceRight(S.Ord)(...args) } /** From f0880df2d8412a596032b8955ee4e752c98bb158 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:45:33 +0200 Subject: [PATCH 080/162] Record / ReadonlyRecord: simplify foldMapWithIndex --- src/ReadonlyRecord.ts | 26 ++++++++------------------ src/Record.ts | 14 +++++--------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 929996b33..ea8481aac 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -397,17 +397,16 @@ export function foldMapWithIndex( export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M -export function foldMapWithIndex( - arg: Ord | Monoid +export function foldMapWithIndex( + arg: Ord | Monoid ): - | (( - M: Monoid - ) => (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) - | ((f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => unknown) { + | ((M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M) + | ((f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M) { if ('compare' in arg) { - return (M: Monoid) => (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => { + const keysO = keys_(arg) + return (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => { let out = M.empty - const ks = keys_(arg)(fa) + const ks = keysO(fa) const len = ks.length for (let i = 0; i < len; i++) { const k = ks[i] @@ -416,16 +415,7 @@ export function foldMapWithIndex( return out } } - return (f: (k: string, a: unknown) => unknown) => (fa: ReadonlyRecord) => { - let out = arg.empty - const ks = keys(fa) - const len = ks.length - for (let i = 0; i < len; i++) { - const k = ks[i] - out = arg.concat(out, f(k, fa[k])) - } - return out - } + return foldMapWithIndex(S.Ord)(arg) } /** diff --git a/src/Record.ts b/src/Record.ts index 62352bc28..c8a790b42 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -267,16 +267,12 @@ export function foldMapWithIndex( export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: Record) => M -export function foldMapWithIndex( - arg: Ord | Monoid +export function foldMapWithIndex( + arg: Ord | Monoid ): - | ((M: Monoid) => (f: (k: string, a: unknown) => unknown) => (fa: Record) => unknown) - | ((f: (k: string, a: unknown) => unknown) => (fa: Record) => unknown) { - if ('compare' in arg) { - return RR.foldMapWithIndex(arg) - } - // tslint:disable-next-line: deprecation - return RR.foldMapWithIndex(arg) + | ((M: Monoid) => (f: (k: string, a: A) => M) => (fa: Record) => M) + | ((f: (k: string, a: A) => M) => (fa: Record) => M) { + return 'compare' in arg ? RR.foldMapWithIndex(arg) : RR.foldMapWithIndex(S.Ord)(arg) } /** From a0da5976e3a3ae161b9ec9555c8a59d668742f69 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:48:36 +0200 Subject: [PATCH 081/162] Record / ReadonlyRecord: simplify reduceRightWithIndex --- src/ReadonlyRecord.ts | 29 +++++++++-------------------- src/Record.ts | 14 ++++---------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index ea8481aac..8ea0fe81f 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -405,7 +405,7 @@ export function foldMapWithIndex( if ('compare' in arg) { const keysO = keys_(arg) return (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => { - let out = M.empty + let out: M = M.empty const ks = keysO(fa) const len = ks.length for (let i = 0; i < len; i++) { @@ -433,29 +433,18 @@ export function reduceRightWithIndex( b: B, f: (k: K, a: A, b: B) => B ): (fa: ReadonlyRecord) => B -export function reduceRightWithIndex( - ...args: [Ord] | [unknown, (k: string, a: unknown, b: unknown) => unknown] +export function reduceRightWithIndex( + ...args: [Ord] | [B, (k: string, a: A, b: B) => B] ): - | (( - b: unknown, - f: (k: string, a: unknown, b: unknown) => unknown - ) => (fa: ReadonlyRecord) => unknown) - | ((fa: ReadonlyRecord) => unknown) { + | ((b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord) => B) + | ((fa: ReadonlyRecord) => B) { if (args.length === 2) { - return (fa: ReadonlyRecord) => { - let out = args[0] - const ks = keys(fa) - const len = ks.length - for (let i = len - 1; i >= 0; i--) { - const k = ks[i] - out = args[1](k, fa[k], out) - } - return out - } + return reduceRightWithIndex(S.Ord)(...args) } + const keysO = keys_(args[0]) return (b, f) => (fa) => { - let out = b - const ks = keys_(args[0])(fa) + let out: B = b + const ks = keysO(fa) const len = ks.length for (let i = len - 1; i >= 0; i--) { const k = ks[i] diff --git a/src/Record.ts b/src/Record.ts index c8a790b42..443736c2a 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -287,16 +287,10 @@ export function reduceRightWithIndex( * @deprecated */ export function reduceRightWithIndex(b: B, f: (k: K, a: A, b: B) => B): (fa: Record) => B -export function reduceRightWithIndex( - ...args: [Ord] | [unknown, (k: string, a: unknown, b: unknown) => unknown] -): - | ((b: unknown, f: (k: string, a: unknown, b: unknown) => unknown) => (fa: Record) => unknown) - | ((fa: Record) => unknown) { - if (args.length === 2) { - // tslint:disable-next-line: deprecation - return RR.reduceRightWithIndex(args[0], args[1]) - } - return RR.reduceRightWithIndex(args[0]) +export function reduceRightWithIndex( + ...args: [Ord] | [B, (k: string, a: A, b: B) => B] +): ((b: B, f: (k: string, a: A, b: B) => B) => (fa: Record) => B) | ((fa: Record) => B) { + return args.length === 1 ? RR.reduceRightWithIndex(args[0]) : RR.reduceRightWithIndex(S.Ord)(...args) } /** From 8f516935f8054dbb95c9771e35536d7a51bbad23 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 7 Apr 2021 06:59:31 +0200 Subject: [PATCH 082/162] Record / ReadonlyRecord: simplify getShow --- src/ReadonlyRecord.ts | 30 ++++++++++++------------------ src/Record.ts | 16 ++++++---------- test/ReadonlyRecord.ts | 13 +++++++++++++ 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 8ea0fe81f..600ec4970 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -398,12 +398,12 @@ export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: ReadonlyRecord) => M export function foldMapWithIndex( - arg: Ord | Monoid + O: Ord | Monoid ): | ((M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M) | ((f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => M) { - if ('compare' in arg) { - const keysO = keys_(arg) + if ('compare' in O) { + const keysO = keys_(O) return (M: Monoid) => (f: (k: string, a: A) => M) => (fa: ReadonlyRecord) => { let out: M = M.empty const ks = keysO(fa) @@ -415,7 +415,7 @@ export function foldMapWithIndex( return out } } - return foldMapWithIndex(S.Ord)(arg) + return foldMapWithIndex(S.Ord)(O) } /** @@ -1241,24 +1241,18 @@ export function getShow(O: Ord): (S: Show) => Show(S: Show): Show> -export function getShow( - arg: Ord | Show -): ((S: Show) => Show>) | Show> { - if ('compare' in arg) { - return (S: Show) => ({ - show: (r: ReadonlyRecord) => { - const elements = collect(arg)((k, a: unknown) => `${JSON.stringify(k)}: ${S.show(a)}`)(r).join(', ') +export function getShow( + O: Ord | Show +): ((S: Show) => Show>) | Show> { + if ('compare' in O) { + return (S: Show) => ({ + show: (r: ReadonlyRecord) => { + const elements = collect(O)((k, a: A) => `${JSON.stringify(k)}: ${S.show(a)}`)(r).join(', ') return elements === '' ? '{}' : `{ ${elements} }` } }) } - return { - show: (r: ReadonlyRecord) => { - // tslint:disable-next-line: deprecation - const elements = collect((k, a: unknown) => `${JSON.stringify(k)}: ${arg.show(a)}`)(r).join(', ') - return elements === '' ? '{}' : `{ ${elements} }` - } - } + return getShow(S.Ord)(O) } /** diff --git a/src/Record.ts b/src/Record.ts index 443736c2a..cf849f4e7 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -268,11 +268,11 @@ export function foldMapWithIndex( M: Monoid ): (f: (k: K, a: A) => M) => (fa: Record) => M export function foldMapWithIndex( - arg: Ord | Monoid + O: Ord | Monoid ): | ((M: Monoid) => (f: (k: string, a: A) => M) => (fa: Record) => M) | ((f: (k: string, a: A) => M) => (fa: Record) => M) { - return 'compare' in arg ? RR.foldMapWithIndex(arg) : RR.foldMapWithIndex(S.Ord)(arg) + return 'compare' in O ? RR.foldMapWithIndex(O) : RR.foldMapWithIndex(S.Ord)(O) } /** @@ -780,14 +780,10 @@ export function getShow(O: Ord): (S: Show) => Show(S: Show): Show> -export function getShow( - arg: Ord | Show -): ((S: Show) => Show>) | Show> { - if ('compare' in arg) { - return RR.getShow(arg) - } - // tslint:disable-next-line: deprecation - return RR.getShow(arg) +export function getShow( + O: Ord | Show +): ((S: Show) => Show>) | Show> { + return 'compare' in O ? RR.getShow(O) : RR.getShow(S.Ord)(O) } /** diff --git a/test/ReadonlyRecord.ts b/test/ReadonlyRecord.ts index c2df4e02e..abbe8cca5 100644 --- a/test/ReadonlyRecord.ts +++ b/test/ReadonlyRecord.ts @@ -19,6 +19,19 @@ const noPrototype = Object.create(null) describe('ReadonlyRecord', () => { describe('pipeables', () => { + it('collect', () => { + const x: { readonly a: string; readonly b: boolean } = { a: 'c', b: false } + U.deepStrictEqual(_.collect(S.Ord)((key, val) => ({ key: key, value: val }))(x), [ + { key: 'a', value: 'c' }, + { key: 'b', value: false } + ]) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.collect((key, val) => ({ key: key, value: val }))(x), [ + { key: 'a', value: 'c' }, + { key: 'b', value: false } + ]) + }) + it('map', () => { U.deepStrictEqual(pipe({ k1: 1, k2: 2 }, _.map(U.double)), { k1: 2, k2: 4 }) U.deepStrictEqual(pipe({ a: 1, b: 2 }, _.map(U.double)), { a: 2, b: 4 }) From cee485efdf7c92761c7d335e55b22061fb49a5bc Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 8 Apr 2021 17:58:47 +0200 Subject: [PATCH 083/162] `Array` / `ReadonlyArray`: - add `chainRecDepthFirst` - add `chainRecBreadthFirst` --- CHANGELOG.md | 4 ++ docs/modules/Array.ts.md | 25 ++++++++ docs/modules/ReadonlyArray.ts.md | 25 ++++++++ src/Array.ts | 22 +++++-- src/ReadonlyArray.ts | 100 ++++++++++++++++--------------- 5 files changed, 124 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b90956bf..3aadf65cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,7 +69,9 @@ high state of flux, you're at risk of it changing without notice. - add `fromOption`, `fromPredicate` (@cdimitroulas) - add `filterE` - add `ChainRecDepthFirst` instance (@qlonik) + - add `chainRecDepthFirst` - add `ChainRecBreadthFirst` instance (@qlonik) + - add `chainRecBreadthFirst` - add `getUnionSemigroup` - add `getUnionMonoid` - add `getIntersectionSemigroup` @@ -138,7 +140,9 @@ high state of flux, you're at risk of it changing without notice. - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` - add `ChainRecDepthFirst` instance (@qlonik) + - add `chainRecDepthFirst` - add `ChainRecBreadthFirst` instance (@qlonik) + - add `chainRecBreadthFirst` - add `getUnionSemigroup` - add `getUnionMonoid` - add `getIntersectionSemigroup` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 538aeec61..d3f5444b0 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -19,6 +19,9 @@ Added in v2.0.0 - [zero](#zero) - [Apply](#apply) - [ap](#ap) +- [ChainRec](#chainrec) + - [chainRecBreadthFirst](#chainrecbreadthfirst) + - [chainRecDepthFirst](#chainrecdepthfirst) - [Compactable](#compactable) - [compact](#compact) - [separate](#separate) @@ -250,6 +253,28 @@ export declare const ap: (fa: A[]) => (fab: ((a: A) => B)[]) => B[] Added in v2.0.0 +# ChainRec + +## chainRecBreadthFirst + +**Signature** + +```ts +export declare const chainRecBreadthFirst: (f: (a: A) => Either[]) => (a: A) => B[] +``` + +Added in v2.11.0 + +## chainRecDepthFirst + +**Signature** + +```ts +export declare const chainRecDepthFirst: (f: (a: A) => Either[]) => (a: A) => B[] +``` + +Added in v2.11.0 + # Compactable ## compact diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index f29bb0f90..6b3c722ef 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -19,6 +19,9 @@ Added in v2.5.0 - [zero](#zero) - [Apply](#apply) - [ap](#ap) +- [ChainRec](#chainrec) + - [chainRecBreadthFirst](#chainrecbreadthfirst) + - [chainRecDepthFirst](#chainrecdepthfirst) - [Compactable](#compactable) - [compact](#compact) - [separate](#separate) @@ -252,6 +255,28 @@ export declare const ap: (fa: readonly A[]) => (fab: readonly ((a: A) => B Added in v2.5.0 +# ChainRec + +## chainRecBreadthFirst + +**Signature** + +```ts +export declare const chainRecBreadthFirst: (f: (a: A) => readonly Either[]) => (a: A) => readonly B[] +``` + +Added in v2.11.0 + +## chainRecDepthFirst + +**Signature** + +```ts +export declare const chainRecDepthFirst: (f: (a: A) => readonly Either[]) => (a: A) => readonly B[] +``` + +Added in v2.11.0 + # Compactable ## compact diff --git a/src/Array.ts b/src/Array.ts index d61dae8b1..24d2ea52a 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -1329,10 +1329,8 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' const traverseWithIndexF = traverseWithIndex(F) return (ta, f) => pipe(ta, traverseWithIndexF(f)) } -/* istanbul ignore next */ -const _chainRecDepthFirst: ChainRec1['chainRec'] = RA.ChainRecDepthFirst.chainRec as any -/* istanbul ignore next */ -const _chainRecBreadthFirst: ChainRec1['chainRec'] = RA.ChainRecBreadthFirst.chainRec as any +const _chainRecDepthFirst: ChainRec1['chainRec'] = RA._chainRecDepthFirst as any +const _chainRecBreadthFirst: ChainRec1['chainRec'] = RA._chainRecBreadthFirst as any // ------------------------------------------------------------------------------------- // type class members @@ -2099,6 +2097,14 @@ export const Witherable: Witherable1 = { wilt: _wilt } +/** + * @category ChainRec + * @since 2.11.0 + */ +export const chainRecDepthFirst: ( + f: (a: A) => Array> +) => (a: A) => Array = RA.chainRecDepthFirst as any + /** * @category instances * @since 2.11.0 @@ -2111,6 +2117,14 @@ export const ChainRecDepthFirst: ChainRec1 = { chainRec: _chainRecDepthFirst } +/** + * @category ChainRec + * @since 2.11.0 + */ +export const chainRecBreadthFirst: ( + f: (a: A) => Array> +) => (a: A) => Array = RA.chainRecBreadthFirst as any + /** * @category instances * @since 2.11.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index f0fae54c3..ca4ecc3d1 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -1410,54 +1410,10 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' const traverseWithIndexF = traverseWithIndex(F) return (ta, f) => pipe(ta, traverseWithIndexF(f)) } -const _chainRecDepthFirst: ChainRec1['chainRec'] = ( - a: A, - f: (a: A) => ReadonlyArray> -): ReadonlyArray => { - // tslint:disable-next-line: readonly-array - const todo: Array> = [...f(a)] - // tslint:disable-next-line: readonly-array - const result: Array = [] - - while (todo.length > 0) { - const e = todo.shift()! - if (e._tag === 'Left') { - todo.unshift(...f(e.left)) - } else { - result.push(e.right) - } - } - - return result -} -const _chainRecBreadthFirst: ChainRec1['chainRec'] = ( - a: A, - f: (a: A) => ReadonlyArray> -): ReadonlyArray => { - const initial = f(a) - // tslint:disable-next-line: readonly-array - const todo: Array> = [] - // tslint:disable-next-line: readonly-array - const result: Array = [] - - function go(e: Either): void { - if (e._tag === 'Left') { - f(e.left).forEach((v) => todo.push(v)) - } else { - result.push(e.right) - } - } - - for (const e of initial) { - go(e) - } - - while (todo.length > 0) { - go(todo.shift()!) - } - - return result -} +/** @internal */ +export const _chainRecDepthFirst: ChainRec1['chainRec'] = (a, f) => pipe(a, chainRecDepthFirst(f)) +/** @internal */ +export const _chainRecBreadthFirst: ChainRec1['chainRec'] = (a, f) => pipe(a, chainRecBreadthFirst(f)) // ------------------------------------------------------------------------------------- // type class members @@ -2247,6 +2203,26 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex } +/** + * @category ChainRec + * @since 2.11.0 + */ +export const chainRecDepthFirst = (f: (a: A) => ReadonlyArray>) => (a: A): ReadonlyArray => { + const todo: Array> = [...f(a)] + const out: Array = [] + + while (todo.length > 0) { + const e = todo.shift()! + if (_.isLeft(e)) { + todo.unshift(...f(e.left)) + } else { + out.push(e.right) + } + } + + return out +} + /** * @category instances * @since 2.11.0 @@ -2259,6 +2235,34 @@ export const ChainRecDepthFirst: ChainRec1 = { chainRec: _chainRecDepthFirst } +/** + * @category ChainRec + * @since 2.11.0 + */ +export const chainRecBreadthFirst = (f: (a: A) => ReadonlyArray>) => (a: A): ReadonlyArray => { + const initial = f(a) + const todo: Array> = [] + const out: Array = [] + + function go(e: Either): void { + if (_.isLeft(e)) { + f(e.left).forEach((v) => todo.push(v)) + } else { + out.push(e.right) + } + } + + for (const e of initial) { + go(e) + } + + while (todo.length > 0) { + go(todo.shift()!) + } + + return out +} + /** * @category instances * @since 2.11.0 From bad3dd725c2e0b0e98d2aa674015c1438e671097 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 13 Apr 2021 13:51:59 +0200 Subject: [PATCH 084/162] Add ReaderTask.chainFirstW --- src/ReaderTask.ts | 12 ++++++++++++ test/ReaderTask.ts | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 1ed0f42aa..239485463 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -370,6 +370,18 @@ export const chainFirst = /*#__PURE__*/ chainFirst_(Chain) +/** + * Less strict version of [`chainFirst`](#chainFirst). + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstW: ( + f: (a: A) => ReaderTask +) => (ma: ReaderTask) => ReaderTask = chainFirst as any + /** * @category instances * @since 2.10.0 diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index 102cca302..0248ebb18 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -41,6 +41,11 @@ describe('ReaderTask', () => { U.deepStrictEqual(await pipe(_.of('foo'), _.chainFirst(f))({})(), 'foo') }) + it('chainFirstW', async () => { + const f = (a: string) => _.of(a.length) + U.deepStrictEqual(await pipe(_.of('foo'), _.chainFirstW(f))({})(), 'foo') + }) + it('flatten', async () => { U.deepStrictEqual(await pipe(_.of(_.of('a')), _.flatten)({})(), 'a') }) From cf7521ef61b2357dac57f5cd077f0495edd5d975 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 13 Apr 2021 13:56:20 +0200 Subject: [PATCH 085/162] Add Reader.chainFirstW --- src/Reader.ts | 12 ++++++++++++ test/Reader.ts | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/Reader.ts b/src/Reader.ts index e268e7eb0..4d3fca2d0 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -332,6 +332,18 @@ export const chainFirst = /*#__PURE__*/ chainFirst_(Chain) +/** + * Less strict version of [`chainFirst`](#chainFirst). + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstW: ( + f: (a: A) => Reader +) => (ma: Reader) => Reader = chainFirst as any + /** * @category instances * @since 2.7.0 diff --git a/test/Reader.ts b/test/Reader.ts index 155e8ed71..948ef8b24 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -36,6 +36,11 @@ describe('Reader', () => { U.deepStrictEqual(pipe(_.of('foo'), _.chainFirst(f))({}), 'foo') }) + it('chainFirstW', () => { + const f = (s: string) => _.of(s.length) + U.deepStrictEqual(pipe(_.of('foo'), _.chainFirstW(f))({}), 'foo') + }) + it('chain', () => { U.deepStrictEqual(pipe(_.of(_.of('a')), _.flatten)({}), 'a') }) From a2891aa8e9d0482ab8fca6c6e67c4efca25ef1ca Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 13 Apr 2021 14:02:39 +0200 Subject: [PATCH 086/162] Add doc for ReaderTask.chainFirstW and Reader.chainFirstW --- docs/modules/Reader.ts.md | 17 +++++++++++++++++ docs/modules/ReaderTask.ts.md | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 743f059f3..b670a2b48 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -40,6 +40,7 @@ Added in v2.0.0 - [asksE](#askse) - [asksEW](#asksew) - [chainFirst](#chainfirst) + - [chainFirstW](#chainfirstw) - [flap](#flap) - [flatten](#flatten) - [local](#local) @@ -305,6 +306,22 @@ export declare const chainFirst: (f: (a: A) => Reader) => (first: Added in v2.0.0 +## chainFirstW + +Less strict version of [`chainFirst`](#chainFirst). + +Derivable from `Chain`. + +**Signature** + +```ts +export declare const chainFirstW: ( + f: (a: A) => Reader +) => (ma: Reader) => Reader +``` + +Added in v2.11.0 + ## flap Derivable from `Functor`. diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 0178f841c..8cbc3a2b7 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -30,6 +30,7 @@ Added in v2.3.0 - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) - [chainFirstTaskK](#chainfirsttaskk) + - [chainFirstW](#chainfirstw) - [chainIOK](#chainiok) - [chainReaderK](#chainreaderk) - [chainTaskK](#chaintaskk) @@ -258,6 +259,22 @@ export declare const chainFirstTaskK: (f: (a: A) => T.Task) => (firs Added in v2.10.0 +## chainFirstW + +Less strict version of [`chainFirst`](#chainFirst). + +Derivable from `Chain`. + +**Signature** + +```ts +export declare const chainFirstW: ( + f: (a: A) => ReaderTask +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainIOK **Signature** From d42f6160ba60956ec654fe57eab6e278e34e4160 Mon Sep 17 00:00:00 2001 From: gcanti Date: Sat, 17 Apr 2021 05:24:01 +0200 Subject: [PATCH 087/162] update changelog --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e49f3d96..82aeaed00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,9 +87,15 @@ high state of flux, you're at risk of it changing without notice. - add `isBoolean` - `Either` - add `chainOptionK` + - `EitherT` + - add `orElseFirst` + - add `orLeft` - `function` - add `SK` (@cdimitroulas) - add `apply` + - `IOEither` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` - `Magma` - add `reverse` - add `filterFirst` @@ -132,10 +138,14 @@ high state of flux, you're at risk of it changing without notice. - add `asksEW`, `asksW` - `ReaderEither` - add `asksEW`, `asksW` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` - `ReaderTask` - add `asksEW`, `asksW` - `ReaderTaskEither` - add `asksEW`, `asksW` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -211,6 +221,8 @@ high state of flux, you're at risk of it changing without notice. - add `fromTaskOption` (@thewilkybarkid) - add `fromTaskOptionK` - add `chainTaskOptionK` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) - `Witherable` From 3dcfc1b0fb5449f810a76da3297cfafe4250ef3e Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 21 Apr 2021 16:52:55 +0200 Subject: [PATCH 088/162] - remove unnecessary type parameters - `Either` - `exists` - `isLeft` - `isRight` - `elem` - `Option` - `isNone` - `These` - `isLeft` - `isRight` - `Set` / `ReadonlySet` - `isEmpty` - `size` - `Array` / `ReadonlyArray` - `isEmpty` - `isOutOfBound` - `size` - `Map` / `ReadonlyMap` - `isEmpty` - `size` - `NonEmptyArray` / `ReadonlyNonEmptyArray` - `isOutOfBound` --- CHANGELOG.md | 24 ++++++++++++++++++++++++ docs/modules/Array.ts.md | 6 +++--- docs/modules/Either.ts.md | 8 ++++---- docs/modules/Map.ts.md | 4 ++-- docs/modules/Option.ts.md | 2 +- docs/modules/ReadonlyArray.ts.md | 6 +++--- docs/modules/ReadonlyMap.ts.md | 4 ++-- docs/modules/ReadonlySet.ts.md | 4 ++-- docs/modules/Set.ts.md | 4 ++-- docs/modules/These.ts.md | 4 ++-- src/Array.ts | 6 +++--- src/Either.ts | 11 +++++------ src/Map.ts | 4 ++-- src/NonEmptyArray.ts | 2 +- src/Option.ts | 2 +- src/ReadonlyArray.ts | 6 +++--- src/ReadonlyMap.ts | 8 ++------ src/ReadonlyNonEmptyArray.ts | 2 +- src/ReadonlySet.ts | 4 ++-- src/Set.ts | 4 ++-- src/These.ts | 8 ++------ src/internal.ts | 6 +++--- 22 files changed, 72 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d42d71b0e..f4e1ceeb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -229,6 +229,30 @@ high state of flux, you're at risk of it changing without notice. - add `filterE`, #1458 (@vinassefranche) - add `wiltDefault` - add `witherDefault` +- **Polish** + - remove unnecessary type parameters + - `Either` + - `exists` + - `isLeft` + - `isRight` + - `elem` + - `Option` + - `isNone` + - `These` + - `isLeft` + - `isRight` + - `Set` / `ReadonlySet` + - `isEmpty` + - `size` + - `Array` / `ReadonlyArray` + - `isEmpty` + - `isOutOfBound` + - `size` + - `Map` / `ReadonlyMap` + - `isEmpty` + - `size` + - `NonEmptyArray` / `ReadonlyNonEmptyArray` + - `isOutOfBound` # 2.10.4 diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 7b1aaa9a1..1c2aee108 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -2344,7 +2344,7 @@ Test whether an array is empty **Signature** ```ts -export declare const isEmpty: (as: A[]) => as is [] +export declare const isEmpty: (as: Array) => as is [] ``` **Example** @@ -2613,7 +2613,7 @@ Test whether an array contains a particular index **Signature** ```ts -export declare const isOutOfBound: (i: number, as: A[]) => boolean +export declare const isOutOfBound: (i: number, as: Array) => boolean ``` Added in v2.0.0 @@ -2672,7 +2672,7 @@ Calculate the number of elements in a `Array`. **Signature** ```ts -export declare const size: (as: A[]) => number +export declare const size: (as: Array) => number ``` Added in v2.10.0 diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 5898ecc62..68ae72218 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1423,7 +1423,7 @@ Returns `true` if the either is an instance of `Left`, `false` otherwise. **Signature** ```ts -export declare const isLeft: (ma: Either) => ma is Left +export declare const isLeft: (ma: Either) => ma is Left ``` Added in v2.0.0 @@ -1435,7 +1435,7 @@ Returns `true` if the either is an instance of `Right`, `false` otherwise. **Signature** ```ts -export declare const isRight: (ma: Either) => ma is Right +export declare const isRight: (ma: Either) => ma is Right ``` Added in v2.0.0 @@ -1519,7 +1519,7 @@ Added in v2.8.0 **Signature** ```ts -export declare function elem(E: Eq): (a: A, ma: Either) => boolean +export declare const elem: (E: Eq) => (a: A, ma: Either) => boolean ``` Added in v2.0.0 @@ -1531,7 +1531,7 @@ Returns `false` if `Left` or returns the result of the application of the given **Signature** ```ts -export declare function exists(predicate: Predicate): (ma: Either) => boolean +export declare function exists(predicate: Predicate): (ma: Either) => boolean ``` **Example** diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index 64ede0ad9..18bdca4a1 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -569,7 +569,7 @@ Test whether or not a map is empty **Signature** ```ts -export declare const isEmpty: (d: Map) => boolean +export declare const isEmpty: (m: Map) => boolean ``` Added in v2.0.0 @@ -728,7 +728,7 @@ Calculate the number of key/value pairs in a map **Signature** ```ts -export declare const size: (d: Map) => number +export declare const size: (m: Map) => number ``` Added in v2.0.0 diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index f9322fc6d..0a49afbf8 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1400,7 +1400,7 @@ Returns `true` if the option is `None`, `false` otherwise. **Signature** ```ts -export declare const isNone: (fa: Option) => fa is None +export declare const isNone: (fa: Option) => fa is None ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 1f10d7c37..911154da3 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -2174,7 +2174,7 @@ Test whether a `ReadonlyArray` is empty. **Signature** ```ts -export declare const isEmpty: (as: readonly A[]) => as is readonly [] +export declare const isEmpty: (as: ReadonlyArray) => as is readonly [] ``` **Example** @@ -2660,7 +2660,7 @@ Test whether an array contains a particular index **Signature** ```ts -export declare const isOutOfBound: (i: number, as: readonly A[]) => boolean +export declare const isOutOfBound: (i: number, as: ReadonlyArray) => boolean ``` Added in v2.5.0 @@ -2742,7 +2742,7 @@ Calculate the number of elements in a `ReadonlyArray`. **Signature** ```ts -export declare const size: (as: readonly A[]) => number +export declare const size: (as: ReadonlyArray) => number ``` Added in v2.10.0 diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index f4fba9c0d..34efff9ce 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -676,7 +676,7 @@ Test whether or not a map is empty **Signature** ```ts -export declare function isEmpty(d: ReadonlyMap): boolean +export declare const isEmpty: (m: ReadonlyMap) => boolean ``` Added in v2.5.0 @@ -838,7 +838,7 @@ Calculate the number of key/value pairs in a map **Signature** ```ts -export declare function size(d: ReadonlyMap): number +export declare const size: (m: ReadonlyMap) => number ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 827bbec73..272652720 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -388,7 +388,7 @@ Test whether a `ReadonlySet` is empty. **Signature** ```ts -export declare const isEmpty: (set: ReadonlySet) => boolean +export declare const isEmpty: (set: ReadonlySet) => boolean ``` Added in v2.10.0 @@ -478,7 +478,7 @@ Calculate the number of elements in a `ReadonlySet`. **Signature** ```ts -export declare const size: (set: ReadonlySet) => number +export declare const size: (set: ReadonlySet) => number ``` Added in v2.10.0 diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index af7629b20..e9d670137 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -349,7 +349,7 @@ Test whether a `Set` is empty. **Signature** ```ts -export declare const isEmpty: (set: Set) => boolean +export declare const isEmpty: (set: Set) => boolean ``` Added in v2.10.0 @@ -427,7 +427,7 @@ Calculate the number of elements in a `Set`. **Signature** ```ts -export declare const size: (set: Set) => number +export declare const size: (set: Set) => number ``` Added in v2.10.0 diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index d857019fe..fbdeec2be 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -697,7 +697,7 @@ Returns `true` if the these is an instance of `Left`, `false` otherwise **Signature** ```ts -export declare function isLeft(fa: These): fa is Left +export declare const isLeft: (fa: These) => fa is Left ``` Added in v2.0.0 @@ -709,7 +709,7 @@ Returns `true` if the these is an instance of `Right`, `false` otherwise **Signature** ```ts -export declare function isRight(fa: These): fa is Right +export declare const isRight: (fa: These) => fa is Right ``` Added in v2.0.0 diff --git a/src/Array.ts b/src/Array.ts index 25c45d3ed..6cb88aed4 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -63,7 +63,7 @@ import NonEmptyArray = NEA.NonEmptyArray * @category refinements * @since 2.0.0 */ -export const isEmpty = (as: Array): as is [] => as.length === 0 +export const isEmpty = (as: Array): as is [] => as.length === 0 /** * Test whether an array is non empty narrowing down the type to `NonEmptyArray` @@ -330,14 +330,14 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: Array): N * * @since 2.10.0 */ -export const size = (as: Array): number => as.length +export const size = (as: Array): number => as.length /** * Test whether an array contains a particular index * * @since 2.0.0 */ -export const isOutOfBound: (i: number, as: Array) => boolean = NEA.isOutOfBound +export const isOutOfBound: (i: number, as: Array) => boolean = NEA.isOutOfBound // TODO: remove non-curried overloading in v3 /** diff --git a/src/Either.ts b/src/Either.ts index e07f28e6e..b3c52d692 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -92,7 +92,7 @@ export type Either = Left | Right * @category refinements * @since 2.0.0 */ -export const isLeft: (ma: Either) => ma is Left = _.isLeft +export const isLeft: (ma: Either) => ma is Left = _.isLeft /** * Returns `true` if the either is an instance of `Right`, `false` otherwise. @@ -100,7 +100,7 @@ export const isLeft: (ma: Either) => ma is Left = _.isLeft * @category refinements * @since 2.0.0 */ -export const isRight: (ma: Either) => ma is Right = _.isRight +export const isRight: (ma: Either) => ma is Right = _.isRight // ------------------------------------------------------------------------------------- // constructors @@ -1217,9 +1217,8 @@ export function toError(e: unknown): Error { /** * @since 2.0.0 */ -export function elem(E: Eq): (a: A, ma: Either) => boolean { - return (a, ma) => (isLeft(ma) ? false : E.equals(a, ma.right)) -} +export const elem = (E: Eq) => (a: A, ma: Either): boolean => + isLeft(ma) ? false : E.equals(a, ma.right) /** * Returns `false` if `Left` or returns the result of the application of the given predicate to the `Right` value. @@ -1235,7 +1234,7 @@ export function elem(E: Eq): (a: A, ma: Either) => boolean { * * @since 2.0.0 */ -export function exists(predicate: Predicate): (ma: Either) => boolean { +export function exists(predicate: Predicate): (ma: Either) => boolean { return (ma) => (isLeft(ma) ? false : predicate(ma.right)) } diff --git a/src/Map.ts b/src/Map.ts index 894a6fa5a..9ba21732d 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -40,14 +40,14 @@ export const getShow: (SK: Show, SA: Show) => Show> = RM.g * * @since 2.0.0 */ -export const size: (d: Map) => number = RM.size +export const size: (m: Map) => number = RM.size /** * Test whether or not a map is empty * * @since 2.0.0 */ -export const isEmpty: (d: Map) => boolean = RM.isEmpty +export const isEmpty: (m: Map) => boolean = RM.isEmpty // TODO: remove non-curried overloading in v3 /** diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 10f3e83ee..7efc71601 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -67,7 +67,7 @@ export const isNonEmpty = (as: Array): as is NonEmptyArray => as.length /** * @internal */ -export const isOutOfBound = (i: number, as: Array): boolean => i < 0 || i >= as.length +export const isOutOfBound = (i: number, as: Array): boolean => i < 0 || i >= as.length /** * @internal diff --git a/src/Option.ts b/src/Option.ts index fdefc6d71..2c57ca591 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -104,7 +104,7 @@ export const isSome: (fa: Option) => fa is Some = _.isSome * @category refinements * @since 2.0.0 */ -export const isNone = (fa: Option): fa is None => fa._tag === 'None' +export const isNone = (fa: Option): fa is None => fa._tag === 'None' // ------------------------------------------------------------------------------------- // constructors diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index c3815ff55..0d973d9e9 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -64,7 +64,7 @@ import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray * @category refinements * @since 2.5.0 */ -export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 +export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 /** * Test whether a `ReadonlyArray` is non empty. @@ -338,14 +338,14 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: ReadonlyArra * * @since 2.10.0 */ -export const size = (as: ReadonlyArray): number => as.length +export const size = (as: ReadonlyArray): number => as.length /** * Test whether an array contains a particular index * * @since 2.5.0 */ -export const isOutOfBound: (i: number, as: ReadonlyArray) => boolean = RNEA.isOutOfBound +export const isOutOfBound: (i: number, as: ReadonlyArray) => boolean = RNEA.isOutOfBound // TODO: remove non-curried overloading in v3 /** diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 1d88a54f7..e95682edc 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -71,18 +71,14 @@ export function getShow(SK: Show, SA: Show): Show> * * @since 2.5.0 */ -export function size(d: ReadonlyMap): number { - return d.size -} +export const size = (m: ReadonlyMap): number => m.size /** * Test whether or not a map is empty * * @since 2.5.0 */ -export function isEmpty(d: ReadonlyMap): boolean { - return d.size === 0 -} +export const isEmpty = (m: ReadonlyMap): boolean => m.size === 0 // TODO: remove non-curried overloading in v3 /** diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 82ecd2eae..5876be33e 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -71,7 +71,7 @@ export const isNonEmpty: (as: ReadonlyArray) => as is ReadonlyNonEmptyArra /** * @internal */ -export const isOutOfBound = (i: number, as: ReadonlyArray): boolean => i < 0 || i >= as.length +export const isOutOfBound = (i: number, as: ReadonlyArray): boolean => i < 0 || i >= as.length /** * @internal diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index b24d6fd38..ca0627f06 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -486,14 +486,14 @@ export const empty: ReadonlySet = new Set() * * @since 2.10.0 */ -export const isEmpty = (set: ReadonlySet): boolean => set.size === 0 +export const isEmpty = (set: ReadonlySet): boolean => set.size === 0 /** * Calculate the number of elements in a `ReadonlySet`. * * @since 2.10.0 */ -export const size = (set: ReadonlySet): number => set.size +export const size = (set: ReadonlySet): number => set.size /** * @since 2.5.0 diff --git a/src/Set.ts b/src/Set.ts index 2a9004b33..a4b024ee3 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -436,14 +436,14 @@ export const empty: Set = new Set() * * @since 2.10.0 */ -export const isEmpty = (set: Set): boolean => set.size === 0 +export const isEmpty = (set: Set): boolean => set.size === 0 /** * Calculate the number of elements in a `Set`. * * @since 2.10.0 */ -export const size = (set: Set): number => set.size +export const size = (set: Set): number => set.size /** * @since 2.0.0 diff --git a/src/These.ts b/src/These.ts index 6eccb4731..e814f4a76 100644 --- a/src/These.ts +++ b/src/These.ts @@ -71,9 +71,7 @@ export type These = Either | Both * @category refinements * @since 2.0.0 */ -export function isLeft(fa: These): fa is Left { - return fa._tag === 'Left' -} +export const isLeft = (fa: These): fa is Left => fa._tag === 'Left' /** * Returns `true` if the these is an instance of `Right`, `false` otherwise @@ -81,9 +79,7 @@ export function isLeft(fa: These): fa is Left { * @category refinements * @since 2.0.0 */ -export function isRight(fa: These): fa is Right { - return fa._tag === 'Right' -} +export const isRight = (fa: These): fa is Right => fa._tag === 'Right' /** * Returns `true` if the these is an instance of `Both`, `false` otherwise diff --git a/src/internal.ts b/src/internal.ts index bc239a125..0f70718e7 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -11,7 +11,7 @@ import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' // ------------------------------------------------------------------------------------- /** @internal */ -export const isNone = (fa: Option): fa is None => fa._tag === 'None' +export const isNone = (fa: Option): fa is None => fa._tag === 'None' /** @internal */ export const isSome = (fa: Option): fa is Some => fa._tag === 'Some' @@ -27,10 +27,10 @@ export const some = (a: A): Option => ({ _tag: 'Some', value: a }) // ------------------------------------------------------------------------------------- /** @internal */ -export const isLeft = (ma: Either): ma is Left => ma._tag === 'Left' +export const isLeft = (ma: Either): ma is Left => ma._tag === 'Left' /** @internal */ -export const isRight = (ma: Either): ma is Right => ma._tag === 'Right' +export const isRight = (ma: Either): ma is Right => ma._tag === 'Right' /** @internal */ export const left = (e: E): Either => ({ _tag: 'Left', left: e }) From da72dcbcdbb530a69d7ec686b8ef92a7e2975baa Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 21 Apr 2021 19:00:09 +0200 Subject: [PATCH 089/162] remove useless _hasOwnProperty private function --- src/Record.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Record.ts b/src/Record.ts index 800530e83..f7836a9f2 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -141,8 +141,6 @@ export const upsertAt: (k: string, a: A) => (r: Record) => Record< */ export const has: (k: string, r: Record) => k is K = RR.has -const _hasOwnProperty = Object.prototype.hasOwnProperty - /** * Delete a key and value from a `Record`. * @@ -153,7 +151,7 @@ export function deleteAt( ): (r: Record) => Record, A> export function deleteAt(k: string): (r: Record) => Record { return (r: Record) => { - if (!_hasOwnProperty.call(r, k)) { + if (!_.has.call(r, k)) { return r } const out: Record = Object.assign({}, r) From 4e86e38c6e5e2be10d9d6dd73e77d9e6796d789d Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 21 Apr 2021 19:04:54 +0200 Subject: [PATCH 090/162] Refinement: add fromEitherK --- docs/modules/Refinement.ts.md | 11 +++++++++++ src/Refinement.ts | 9 +++++++++ test/Refinement.ts | 19 +++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md index 831f13d2c..ada4c1ee0 100644 --- a/docs/modules/Refinement.ts.md +++ b/docs/modules/Refinement.ts.md @@ -19,6 +19,7 @@ Added in v2.11.0 - [or](#or) - [zero](#zero) - [constructors](#constructors) + - [fromEitherK](#fromeitherk) - [fromOptionK](#fromoptionk) - [id](#id) - [utils](#utils) @@ -86,6 +87,16 @@ Added in v2.11.0 # constructors +## fromEitherK + +**Signature** + +```ts +export declare const fromEitherK: (getEither: (a: A) => Either) => Refinement +``` + +Added in v2.11.0 + ## fromOptionK Returns a `Refinement` from a `Option` returning function. diff --git a/src/Refinement.ts b/src/Refinement.ts index f9f446086..96f25f11f 100644 --- a/src/Refinement.ts +++ b/src/Refinement.ts @@ -3,6 +3,7 @@ */ import { Option } from './Option' import * as _ from './internal' +import { Either } from './Either' // ------------------------------------------------------------------------------------- // model @@ -30,6 +31,14 @@ export const fromOptionK = (getOption: (a: A) => Option): Ref return (a: A): a is B => _.isSome(getOption(a)) } +/** + * @category constructors + * @since 2.11.0 + */ +export const fromEitherK = (getEither: (a: A) => Either): Refinement => { + return (a: A): a is B => _.isRight(getEither(a)) +} + /** * @category constructors * @since 2.11.0 diff --git a/test/Refinement.ts b/test/Refinement.ts index 52900ee33..7640ee271 100644 --- a/test/Refinement.ts +++ b/test/Refinement.ts @@ -1,10 +1,11 @@ +import * as B from '../src/boolean' +import * as E from '../src/Either' import { pipe } from '../src/function' +import * as N from '../src/number' import * as O from '../src/Option' import { ReadonlyRecord } from '../src/ReadonlyRecord' import * as _ from '../src/Refinement' import * as S from '../src/string' -import * as N from '../src/number' -import * as B from '../src/boolean' import * as U from './util' interface NonEmptyStringBrand { @@ -72,4 +73,18 @@ describe('Refinement', () => { U.strictEqual(refinement(null), false) U.strictEqual(refinement(''), false) }) + + it('fromEitherK', () => { + const f = (s: string | number): E.Either => + typeof s === 'string' ? E.right(s) : E.left('not a string') + const isString = _.fromEitherK(f) + U.deepStrictEqual(isString('s'), true) + U.deepStrictEqual(isString(1), false) + type A = { readonly type: 'A' } + type B = { readonly type: 'B' } + type C = A | B + const isA = _.fromEitherK((c) => (c.type === 'A' ? E.right(c) : E.left('not as A'))) + U.deepStrictEqual(isA({ type: 'A' }), true) + U.deepStrictEqual(isA({ type: 'B' }), false) + }) }) From a4c0afc03c5bf97c8b845467146637dadaa98a5b Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Thu, 22 Apr 2021 16:46:24 +0200 Subject: [PATCH 091/162] Add RTE.fromReaderTaskK, RTE.chainReaderTaskK and RTE.chainReaderTaskKW --- docs/modules/ReaderTaskEither.ts.md | 41 +++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 27 +++++++++++++++++++ test/ReaderTaskEither.ts | 12 ++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index d2d381ea0..e366da32f 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -46,6 +46,8 @@ Added in v2.0.0 - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) - [chainReaderK](#chainreaderk) + - [chainReaderTaskK](#chainreadertaskk) + - [chainReaderTaskKW](#chainreadertaskkw) - [chainTaskEitherK](#chaintaskeitherk) - [chainTaskEitherKW](#chaintaskeitherkw) - [chainTaskK](#chaintaskk) @@ -58,6 +60,7 @@ Added in v2.0.0 - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) - [fromReaderK](#fromreaderk) + - [fromReaderTaskK](#fromreadertaskk) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) - [orElse](#orelse) @@ -516,6 +519,32 @@ export declare const chainReaderK: ( Added in v2.11.0 +## chainReaderTaskK + +**Signature** + +```ts +export declare const chainReaderTaskK: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainReaderTaskKW + +Less strict version of [`chainReaderTaskK`](#chainReaderTaskK). + +**Signature** + +```ts +export declare const chainReaderTaskKW: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainTaskEitherK **Signature** @@ -672,6 +701,18 @@ export declare const fromReaderK: ( Added in v2.11.0 +## fromReaderTaskK + +**Signature** + +```ts +export declare const fromReaderTaskK: ( + f: (...a: A) => RT.ReaderTask +) => (...a: A) => ReaderTaskEither +``` + +Added in v2.11.0 + ## fromTaskEitherK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index f28fd2f93..e932d8a46 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -931,6 +931,33 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderTaskK = , R, B>( + f: (...a: A) => ReaderTask +): ((...a: A) => ReaderTaskEither) => (...a) => rightReaderTask(f(...a)) + +/** + * Less strict version of [`chainReaderTaskK`](#chainReaderTaskK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderTaskKW: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => + chainW(fromReaderTaskK(f)) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderTaskK: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderTaskKW + /** * @category instances * @since 2.10.0 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index b99ea1335..fce9fc614 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -1,7 +1,7 @@ import * as U from './util' import { sequenceT } from '../src/Apply' import * as E from '../src/Either' -import { pipe } from '../src/function' +import { flow, pipe } from '../src/function' import * as I from '../src/IO' import * as IE from '../src/IOEither' import * as N from '../src/number' @@ -414,6 +414,16 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right('a'), _.chainTaskEitherK(f))(undefined)(), E.right(1)) }) + it('chainReaderTaskK', async () => { + const f = flow(S.size, RT.of) + U.deepStrictEqual(await pipe(_.right('a'), _.chainReaderTaskK(f))(undefined)(), E.right(1)) + }) + + it('chainReaderTaskKW', async () => { + const f = flow(S.size, RT.of) + U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainReaderTaskKW(f))({})(), E.right(1)) + }) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- From 2a258dd7e8fe024b10437216a22b07537335d8a7 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Thu, 22 Apr 2021 16:56:05 +0200 Subject: [PATCH 092/162] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4e1ceeb7..326c4c905 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -146,6 +146,9 @@ high state of flux, you're at risk of it changing without notice. - add `asksEW`, `asksW` - add `orElseFirst` / `orElseFirstW` - add `orLeft` + - add `fromReaderTaskK` + - add `chainReaderTaskK` + - add `chainReaderTaskKW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` From f61e85ca21ce5703057a7d61422ea2d8c6e752d4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 22 Apr 2021 17:19:59 +0200 Subject: [PATCH 093/162] revert `local` deprecations --- docs/modules/ReaderEither.ts.md | 27 +++++++++++++------------ docs/modules/ReaderTask.ts.md | 7 ++++--- docs/modules/ReaderTaskEither.ts.md | 31 +++++++++++++++-------------- src/ReaderEither.ts | 20 +++++++++---------- src/ReaderTask.ts | 18 ++++++++--------- src/ReaderTaskEither.ts | 22 ++++++++++---------- test/ReaderEither.ts | 1 - test/ReaderTask.ts | 1 - test/ReaderTaskEither.ts | 1 - test/StateReaderTaskEither.ts | 1 + 10 files changed, 65 insertions(+), 64 deletions(-) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 8f6015ed6..ca864c3a5 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -48,13 +48,13 @@ Added in v2.0.0 - [fromEitherK](#fromeitherk) - [fromOptionK](#fromoptionk) - [fromReaderK](#fromreaderk) + - [local](#local) - [orElse](#orelse) - [orElseFirst](#orelsefirst) - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) - [orLeft](#orleft) - [swap](#swap) - - [~~local~~](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -509,6 +509,19 @@ export declare const fromReaderK: ( Added in v2.11.0 +## local + +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). + +**Signature** + +```ts +export declare const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.0.0 + ## orElse **Signature** @@ -581,18 +594,6 @@ export declare const swap: (ma: ReaderEither) => ReaderEither< Added in v2.0.0 -## ~~local~~ - -Use [`local`](./Reader.ts.html#local) instead. - -**Signature** - -```ts -export declare const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither -``` - -Added in v2.0.0 - # constructors ## ask diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 5f75b937c..44918f59b 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -39,7 +39,7 @@ Added in v2.3.0 - [fromIOK](#fromiok) - [fromReaderK](#fromreaderk) - [fromTaskK](#fromtaskk) - - [~~local~~](#local) + - [local](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -359,9 +359,10 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a: A Added in v2.4.0 -## ~~local~~ +## local -Use [`local`](./Reader.ts.html#local) instead. +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index e366da32f..fa7dc986d 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -63,13 +63,13 @@ Added in v2.0.0 - [fromReaderTaskK](#fromreadertaskk) - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) + - [local](#local) - [orElse](#orelse) - [orElseFirst](#orelsefirst) - [orElseFirstW](#orelsefirstw) - [orElseW](#orelsew) - [orLeft](#orleft) - [swap](#swap) - - [~~local~~](#local) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -735,6 +735,21 @@ export declare const fromTaskK: (f: (...a: A) => T.Task) => (...a Added in v2.10.0 +## local + +Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +`contramap`). + +**Signature** + +```ts +export declare const local: ( + f: (r2: R2) => R1 +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.0.0 + ## orElse **Signature** @@ -807,20 +822,6 @@ export declare const swap: (ma: ReaderTaskEither) => ReaderTas Added in v2.0.0 -## ~~local~~ - -Use [`local`](./Reader.ts.html#local) instead. - -**Signature** - -```ts -export declare const local: ( - f: (r2: R2) => R1 -) => (ma: ReaderTaskEither) => ReaderTaskEither -``` - -Added in v2.0.0 - # constructors ## ask diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 6f8b7efae..6063d3aca 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -212,6 +212,16 @@ export const toUnion: (fa: ReaderEither) => Reader = // combinators // ------------------------------------------------------------------------------------- +/** + * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s + * `contramap`). + * + * @category combinators + * @since 2.0.0 + */ +export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = + R.local + /** * Less strict version of [`asksE`](#asksE). * @@ -993,13 +1003,3 @@ export function getReaderValidation( throwError } } - -/** - * Use [`local`](./Reader.ts.html#local) instead. - * - * @category combinators - * @since 2.0.0 - * @deprecated - */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = - R.local diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 557b313cf..d7d12a647 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -84,6 +84,15 @@ export const fromIO: FromIO2['fromIO'] = // combinators // ------------------------------------------------------------------------------------- +/** + * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s + * `contramap`). + * + * @category combinators + * @since 2.3.0 + */ +export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local + /** * Less strict version of [`asksE`](#asksE). * @@ -672,12 +681,3 @@ export const getMonoid: (M: Monoid) => Monoid> = export function run(ma: ReaderTask, r: R): Promise { return ma(r)() } - -/** - * Use [`local`](./Reader.ts.html#local) instead. - * - * @category combinators - * @since 2.3.0 - * @deprecated - */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index e932d8a46..1a56c2e21 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -316,6 +316,17 @@ export const toUnion: (fa: ReaderTaskEither) => ReaderTask( + f: (r2: R2) => R1 +) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local + /** * Less strict version of [`asksE`](#asksE). * @@ -1392,14 +1403,3 @@ export function getReaderTaskValidation( export function run(ma: ReaderTaskEither, r: R): Promise> { return ma(r)() } - -/** - * Use [`local`](./Reader.ts.html#local) instead. - * - * @category combinators - * @since 2.0.0 - * @deprecated - */ -export const local: ( - f: (r2: R2) => R1 -) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index bd379eede..bc5660b1b 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -211,7 +211,6 @@ describe('ReaderEither', () => { }) it('local', () => { - // tslint:disable-next-line: deprecation U.deepStrictEqual(_.local((n: number) => ({ a: n }))((r: { readonly a: number }) => E.right(r.a))(1), E.right(1)) }) diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index 40d0d561a..0248ebb18 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -86,7 +86,6 @@ describe('ReaderTask', () => { U.deepStrictEqual( await pipe( _.asks((n: number) => n + 1), - // tslint:disable-next-line: deprecation _.local(S.size) )('aaa')(), 4 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index fce9fc614..0f0d6363e 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -163,7 +163,6 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual( await pipe( _.asks((n: number) => n + 1), - // tslint:disable-next-line: deprecation _.local(S.size) )('aaa')(), E.right(4) diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 5574cef55..4febdaf94 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -387,6 +387,7 @@ describe('StateReaderTaskEither', () => { const f = (e: Env) => _.of(e.count + 1) U.deepStrictEqual(await _.asksE(f)({})(e)(), E.right(tuple(1, {}))) }) + it('#1486', async () => { const append = (n: number): _.StateReaderTaskEither, {}, Error, void> => _.modify((a) => [...a, n]) From 172928ab3d86714ac6af17d7c7adb70cd7ab2123 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 23 Apr 2021 09:27:32 +0200 Subject: [PATCH 094/162] tmp --- docs/modules/Array.ts.md | 16 +-- docs/modules/Either.ts.md | 6 +- docs/modules/Filterable.ts.md | 22 ++-- docs/modules/FromEither.ts.md | 26 ++--- docs/modules/IOEither.ts.md | 6 +- docs/modules/Map.ts.md | 4 +- docs/modules/Option.ts.md | 2 +- docs/modules/OptionT.ts.md | 14 +-- docs/modules/ReaderEither.ts.md | 10 +- docs/modules/ReaderTaskEither.ts.md | 10 +- docs/modules/ReadonlyArray.ts.md | 18 ++-- docs/modules/ReadonlyMap.ts.md | 6 +- docs/modules/ReadonlyRecord.ts.md | 8 +- docs/modules/ReadonlySet.ts.md | 4 +- docs/modules/Record.ts.md | 4 +- docs/modules/Set.ts.md | 4 +- docs/modules/StateReaderTaskEither.ts.md | 12 +-- docs/modules/TaskEither.ts.md | 6 +- docs/modules/TaskOption.ts.md | 6 +- docs/modules/TaskThese.ts.md | 2 +- dtslint/ts3.5/Option.ts | 39 +++---- dtslint/ts3.5/ReadonlyArray.ts | 126 ++++++++++++++++------- dtslint/ts3.5/ReadonlySet.ts | 35 +++++++ src/Array.ts | 48 ++++----- src/Either.ts | 2 +- src/Filterable.ts | 45 ++++---- src/FromEither.ts | 38 +++---- src/IOEither.ts | 6 +- src/Map.ts | 8 +- src/Option.ts | 17 ++- src/OptionT.ts | 18 ++-- src/ReaderEither.ts | 10 +- src/ReaderTaskEither.ts | 10 +- src/ReadonlyArray.ts | 72 ++++++------- src/ReadonlyMap.ts | 10 +- src/ReadonlyRecord.ts | 16 +-- src/ReadonlySet.ts | 22 ++-- src/Record.ts | 4 +- src/Set.ts | 22 ++-- src/StateReaderTaskEither.ts | 12 +-- src/TaskEither.ts | 6 +- src/TaskOption.ts | 6 +- 42 files changed, 433 insertions(+), 325 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 1c2aee108..29747f1a2 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -318,7 +318,7 @@ Added in v2.0.0 ```ts export declare const filter: { (refinement: Refinement): (fa: A[]) => B[] - (predicate: Predicate): (fa: A[]) => A[] + (predicate: Predicate): (fb: B[]) => B[] } ``` @@ -341,7 +341,7 @@ Added in v2.0.0 ```ts export declare const partition: { (refinement: Refinement): (fa: A[]) => Separated - (predicate: Predicate): (fa: A[]) => Separated + (predicate: Predicate): (fb: B[]) => Separated } ``` @@ -376,7 +376,7 @@ Added in v2.0.0 ```ts export declare const filterWithIndex: { (refinementWithIndex: RefinementWithIndex): (fa: A[]) => B[] - (predicateWithIndex: PredicateWithIndex): (fa: A[]) => A[] + (predicateWithIndex: PredicateWithIndex): (fb: B[]) => B[] } ``` @@ -851,7 +851,7 @@ Remove the longest initial subarray for which all element satisfy the specified **Signature** ```ts -export declare const dropLeftWhile: (predicate: Predicate) => (as: A[]) => A[] +export declare const dropLeftWhile: (predicate: Predicate) => (bs: B[]) => B[] ``` **Example** @@ -1257,7 +1257,7 @@ Calculate the longest initial subarray for which all element satisfy the specifi ```ts export declare function takeLeftWhile(refinement: Refinement): (as: Array) => Array -export declare function takeLeftWhile(predicate: Predicate): (as: Array) => Array +export declare function takeLeftWhile(predicate: Predicate): (bs: Array) => Array ``` **Example** @@ -1595,7 +1595,7 @@ Find the first element which satisfies a predicate (or a refinement) function ```ts export declare function findFirst(refinement: Refinement): (as: Array) => Option -export declare function findFirst(predicate: Predicate): (as: Array) => Option +export declare function findFirst(predicate: Predicate): (bs: Array) => Option ``` **Example** @@ -1657,7 +1657,7 @@ Find the last element which satisfies a predicate function ```ts export declare function findLast(refinement: Refinement): (as: Array) => Option -export declare function findLast(predicate: Predicate): (as: Array) => Option +export declare function findLast(predicate: Predicate): (bs: Array) => Option ``` **Example** @@ -1905,7 +1905,7 @@ Split an array into two parts: ```ts export declare function spanLeft(refinement: Refinement): (as: Array) => Spanned -export declare function spanLeft(predicate: Predicate): (as: Array) => Spanned +export declare function spanLeft(predicate: Predicate): (bs: Array) => Spanned ``` **Example** diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 68ae72218..273c4b373 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -523,7 +523,7 @@ Added in v2.0.0 ```ts export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either - (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E): (mb: Either) => Either } ``` @@ -578,7 +578,7 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: Either ) => Either - (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either } ``` @@ -710,7 +710,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Either } ``` diff --git a/docs/modules/Filterable.ts.md b/docs/modules/Filterable.ts.md index d9cd89436..080e04eca 100644 --- a/docs/modules/Filterable.ts.md +++ b/docs/modules/Filterable.ts.md @@ -69,19 +69,19 @@ Added in v2.0.0 export declare function filter( F: Functor2, G: Filterable2C -): (predicate: Predicate) => (fga: Kind2>) => Kind2> +): (predicate: Predicate) => (fgb: Kind2>) => Kind2> export declare function filter( F: Functor1, G: Filterable2C -): (predicate: Predicate) => (fga: Kind>) => Kind> +): (predicate: Predicate) => (fgb: Kind>) => Kind> export declare function filter( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fga: Kind>) => Kind> +): (predicate: Predicate) => (fgb: Kind>) => Kind> export declare function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => HKT> +): (predicate: Predicate) => (fgb: HKT>) => HKT> ``` Added in v2.10.0 @@ -125,21 +125,27 @@ export declare function partition( G: Filterable2C ): ( predicate: Predicate -) => (fga: Kind2>) => Separated>, Kind2>> +) => ( + fgb: Kind2> +) => Separated>, Kind2>> export declare function partition( F: Functor1, G: Filterable2C ): ( predicate: Predicate -) => (fga: Kind>) => Separated>, Kind>> +) => (fgb: Kind>) => Separated>, Kind>> export declare function partition( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fga: Kind>) => Separated>, Kind>> +): ( + predicate: Predicate +) => (fgb: Kind>) => Separated>, Kind>> export declare function partition( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => Separated>, HKT>> +): ( + predicate: Predicate +) => (fgb: HKT>) => Separated>, HKT>> ``` Added in v2.10.0 diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index 61a13015f..b9047488a 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -118,7 +118,9 @@ export declare function filterOrElse( (refinement: Refinement, onFalse: (a: A) => E): ( ma: Kind4 ) => Kind4 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind4) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: Kind4 + ) => Kind4 } export declare function filterOrElse( F: FromEither3, @@ -127,35 +129,35 @@ export declare function filterOrElse( (refinement: Refinement, onFalse: (a: A) => E): ( ma: Kind3 ) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 } export declare function filterOrElse( F: FromEither3C, M: Chain3C ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind3) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 } export declare function filterOrElse( F: FromEither2, M: Chain2 ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 } export declare function filterOrElse( F: FromEither2C, M: Chain2C ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 } export declare function filterOrElse( F: FromEither, M: Chain ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: HKT2) => HKT2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: HKT2) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: HKT2) => HKT2 } ``` @@ -264,37 +266,37 @@ export declare function fromPredicate( F: FromEither4 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind4 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind4 } export declare function fromPredicate( F: FromEither3 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 } export declare function fromPredicate( F: FromEither3C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 } export declare function fromPredicate( F: FromEither2 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 } export declare function fromPredicate( F: FromEither2C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 } export declare function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 } ``` diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 7fe27a9b3..52153536d 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -395,7 +395,7 @@ Added in v2.10.0 ```ts export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: IOEither) => IOEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (mb: IOEither) => IOEither } ``` @@ -412,7 +412,9 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: IOEither ) => IOEither - (predicate: Predicate, onFalse: (a: A) => E2): (ma: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: IOEither + ) => IOEither } ``` diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index 18bdca4a1..ff92b7fd5 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -115,7 +115,7 @@ Added in v2.0.0 ```ts export declare const filter: { (refinement: Refinement): (fa: Map) => Map - (predicate: Predicate): (fa: Map) => Map + (predicate: Predicate): (fb: Map) => Map } ``` @@ -138,7 +138,7 @@ Added in v2.0.0 ```ts export declare const partition: { (refinement: Refinement): (fa: Map) => Separated, Map> - (predicate: Predicate): (fa: Map) => Separated, Map> + (predicate: Predicate): (fb: Map) => Separated, Map> } ``` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 0a49afbf8..a9ba35c6c 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -259,7 +259,7 @@ Added in v2.0.0 ```ts export declare const filter: { (refinement: Refinement): (fa: Option) => Option - (predicate: Predicate): (fa: Option) => Option + (predicate: Predicate): (fb: Option) => Option } ``` diff --git a/docs/modules/OptionT.ts.md b/docs/modules/OptionT.ts.md index 052e50d62..e2ad94820 100644 --- a/docs/modules/OptionT.ts.md +++ b/docs/modules/OptionT.ts.md @@ -402,43 +402,43 @@ export declare function fromPredicate( F: Pointed4 ): { (refinement: Refinement): (a: A) => Kind4> - (predicate: Predicate): (a: A) => Kind4> + (predicate: Predicate): (b: B) => Kind4> } export declare function fromPredicate( F: Pointed3 ): { (refinement: Refinement): (a: A) => Kind3> - (predicate: Predicate): (a: A) => Kind3> + (predicate: Predicate): (b: B) => Kind3> } export declare function fromPredicate( F: Pointed3C ): { (refinement: Refinement): (a: A) => Kind3> - (predicate: Predicate): (a: A) => Kind3> + (predicate: Predicate): (b: B) => Kind3> } export declare function fromPredicate( F: Pointed2 ): { (refinement: Refinement): (a: A) => Kind2> - (predicate: Predicate): (a: A) => Kind2> + (predicate: Predicate): (b: B) => Kind2> } export declare function fromPredicate( F: Pointed2C ): { (refinement: Refinement): (a: A) => Kind2> - (predicate: Predicate): (a: A) => Kind2> + (predicate: Predicate): (b: B) => Kind2> } export declare function fromPredicate( F: Pointed1 ): { (refinement: Refinement): (a: A) => Kind> - (predicate: Predicate): (a: A) => Kind> + (predicate: Predicate): (b: B) => Kind> } export declare function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> - (predicate: Predicate): (a: A) => HKT> + (predicate: Predicate): (b: B) => HKT> } ``` diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index ca864c3a5..72152fd10 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -424,7 +424,9 @@ export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: ReaderEither ) => ReaderEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderEither) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: ReaderEither + ) => ReaderEither } ``` @@ -441,9 +443,9 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: ReaderEither ) => ReaderEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: ReaderEither - ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: ReaderEither + ) => ReaderEither } ``` diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index fa7dc986d..bd84f0902 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -592,7 +592,9 @@ export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: ReaderTaskEither ) => ReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderTaskEither) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: ReaderTaskEither + ) => ReaderTaskEither } ``` @@ -609,9 +611,9 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: ReaderTaskEither ) => ReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: ReaderTaskEither - ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: ReaderTaskEither + ) => ReaderTaskEither } ``` diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 911154da3..e67c59c20 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -320,7 +320,7 @@ Added in v2.5.0 ```ts export declare const filter: { (refinement: Refinement): (fa: readonly A[]) => readonly B[] - (predicate: Predicate): (fa: readonly A[]) => readonly A[] + (predicate: Predicate): (fb: readonly B[]) => readonly B[] } ``` @@ -343,7 +343,7 @@ Added in v2.5.0 ```ts export declare const partition: { (refinement: Refinement): (fa: readonly A[]) => Separated - (predicate: Predicate): (fa: readonly A[]) => Separated + (predicate: Predicate): (fa: readonly B[]) => Separated } ``` @@ -380,7 +380,7 @@ Added in v2.5.0 ```ts export declare const filterWithIndex: { (refinementWithIndex: RefinementWithIndex): (fa: readonly A[]) => readonly B[] - (predicateWithIndex: PredicateWithIndex): (fa: readonly A[]) => readonly A[] + (predicateWithIndex: PredicateWithIndex): (fb: readonly B[]) => readonly B[] } ``` @@ -857,7 +857,7 @@ Remove the longest initial subarray for which all element satisfy the specified **Signature** ```ts -export declare const dropLeftWhile: (predicate: Predicate) => (as: readonly A[]) => readonly A[] +export declare const dropLeftWhile: (predicate: Predicate) => (bs: readonly B[]) => readonly B[] ``` **Example** @@ -1280,7 +1280,9 @@ Calculate the longest initial subarray for which all element satisfy the specifi export declare function takeLeftWhile( refinement: Refinement ): (as: ReadonlyArray) => ReadonlyArray -export declare function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray +export declare function takeLeftWhile( + predicate: Predicate +): (bs: ReadonlyArray) => ReadonlyArray ``` **Example** @@ -2420,7 +2422,7 @@ Find the first element which satisfies a predicate (or a refinement) function ```ts export declare function findFirst(refinement: Refinement): (as: ReadonlyArray) => Option -export declare function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option +export declare function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option ``` **Example** @@ -2504,7 +2506,7 @@ Find the last element which satisfies a predicate function ```ts export declare function findLast(refinement: Refinement): (as: ReadonlyArray) => Option -export declare function findLast(predicate: Predicate): (as: ReadonlyArray) => Option +export declare function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option ``` **Example** @@ -2782,7 +2784,7 @@ Split an array into two parts: ```ts export declare function spanLeft(refinement: Refinement): (as: ReadonlyArray) => Spanned -export declare function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned +export declare function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned ``` **Example** diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index 34efff9ce..cfc37fe1f 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -122,7 +122,7 @@ Added in v2.5.0 ```ts export declare const filter: { (refinement: Refinement): (fa: ReadonlyMap) => ReadonlyMap - (predicate: Predicate): (fa: ReadonlyMap) => ReadonlyMap + (predicate: Predicate): (fb: ReadonlyMap) => ReadonlyMap } ``` @@ -147,7 +147,9 @@ export declare const partition: { (refinement: Refinement): ( fa: ReadonlyMap ) => Separated, ReadonlyMap> - (predicate: Predicate): (fa: ReadonlyMap) => Separated, ReadonlyMap> + (predicate: Predicate): ( + fb: ReadonlyMap + ) => Separated, ReadonlyMap> } ``` diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 1052604df..898d91e75 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -137,7 +137,7 @@ Added in v2.5.0 ```ts export declare const filter: { (refinement: Refinement): (fa: Readonly>) => Readonly> - (predicate: Predicate): (fa: Readonly>) => Readonly> + (predicate: Predicate): (fb: Readonly>) => Readonly> } ``` @@ -164,9 +164,9 @@ export declare const partition: { (refinement: Refinement): ( fa: Readonly> ) => Separated>, Readonly>> - (predicate: Predicate): ( - fa: Readonly> - ) => Separated>, Readonly>> + (predicate: Predicate): ( + fb: Readonly> + ) => Separated>, Readonly>> } ``` diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 272652720..98c09760f 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -111,7 +111,7 @@ Added in v2.5.0 ```ts export declare function filter(refinement: Refinement): (set: ReadonlySet) => ReadonlySet -export declare function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet +export declare function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet ``` Added in v2.5.0 @@ -420,7 +420,7 @@ export declare function partition( ): (set: ReadonlySet) => Separated, ReadonlySet> export declare function partition( predicate: Predicate -): (set: ReadonlySet) => Separated, ReadonlySet> +): (set: ReadonlySet) => Separated, ReadonlySet> ``` Added in v2.5.0 diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index da39c8805..f33edbbbe 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -130,7 +130,7 @@ Added in v2.0.0 ```ts export declare const filter: { (refinement: Refinement): (fa: Record) => Record - (predicate: Predicate): (fa: Record) => Record + (predicate: Predicate): (fb: Record) => Record } ``` @@ -155,7 +155,7 @@ export declare const partition: { (refinement: Refinement): ( fa: Record ) => Separated, Record> - (predicate: Predicate): (fa: Record) => Separated, Record> + (predicate: Predicate): (fb: Record) => Separated, Record> } ``` diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index e9d670137..0e7be1fe8 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -110,7 +110,7 @@ Added in v2.0.0 ```ts export declare function filter(refinement: Refinement): (set: Set) => Set -export declare function filter(predicate: Predicate): (set: Set) => Set +export declare function filter(predicate: Predicate): (set: Set) => Set ``` Added in v2.0.0 @@ -372,7 +372,7 @@ Added in v2.10.0 export declare function partition( refinement: Refinement ): (set: Set) => Separated, Set> -export declare function partition(predicate: Predicate): (set: Set) => Separated, Set> +export declare function partition(predicate: Predicate): (set: Set) => Separated, Set> ``` Added in v2.0.0 diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index b6cf1e84b..f63e2e8af 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -591,9 +591,9 @@ export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: StateReaderTaskEither ) => StateReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E): ( - ma: StateReaderTaskEither - ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: StateReaderTaskEither + ) => StateReaderTaskEither } ``` @@ -610,9 +610,9 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: StateReaderTaskEither ) => StateReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: StateReaderTaskEither - ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: StateReaderTaskEither + ) => StateReaderTaskEither } ``` diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index aa9d61e64..93384d6cf 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -514,7 +514,7 @@ Added in v2.11.0 ```ts export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (mb: TaskEither) => TaskEither } ``` @@ -531,7 +531,9 @@ export declare const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: TaskEither ) => TaskEither - (predicate: Predicate, onFalse: (a: A) => E2): (ma: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: TaskEither + ) => TaskEither } ``` diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 0b0966218..7e00d528c 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -182,7 +182,7 @@ Added in v2.10.0 **Signature** ```ts -export declare const filter: (predicate: Predicate) => (fga: TaskOption) => TaskOption +export declare const filter: (predicate: Predicate) => (fb: TaskOption) => TaskOption ``` Added in v2.10.0 @@ -204,7 +204,7 @@ Added in v2.10.0 ```ts export declare const partition: ( predicate: Predicate -) => (fga: TaskOption) => Separated, TaskOption> +) => (fb: TaskOption) => Separated, TaskOption> ``` Added in v2.10.0 @@ -450,7 +450,7 @@ Added in v2.10.0 ```ts export declare const fromPredicate: { (refinement: Refinement): (a: A) => TaskOption - (predicate: Predicate): (a: A) => TaskOption + (predicate: Predicate): (b: B) => TaskOption } ``` diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index 9bf82cb32..63849f9ab 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -252,7 +252,7 @@ Added in v2.10.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => TaskThese - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => TaskThese + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => TaskThese } ``` diff --git a/dtslint/ts3.5/Option.ts b/dtslint/ts3.5/Option.ts index 732cfd014..42bba82b8 100644 --- a/dtslint/ts3.5/Option.ts +++ b/dtslint/ts3.5/Option.ts @@ -11,21 +11,6 @@ pipe( _.getOrElseW(() => null) ) -// -// getRefinement -// - -interface A { - type: 'A' -} -interface B { - type: 'B' -} -type C = A | B - -// $ExpectError -_.getRefinement((c) => (c.type === 'B' ? _.some(c) : _.none)) - // // fromNullable // @@ -40,15 +25,6 @@ declare const f: (key: K) => D[K] // $ExpectType Option flow(f, _.fromNullable)('foo') -// -// Filterable overlodings -// - -declare function isString(x: unknown): x is string - -_.option.filter(_.some('a'), isString) // $ExpectType Option -_.option.partition(_.some('a'), isString) // $ExpectType Separated, Option> - // // Do // @@ -59,3 +35,18 @@ pipe( _.bind('a', () => _.of(1)), _.bind('b', () => _.of('b')) ) + +// +// filter +// + +declare function isString(x: unknown): x is string + +_.option.filter(_.some('a'), isString) // $ExpectType Option +pipe(_.some('a'), _.filter(isString)) // $ExpectType Option + +// +// partition +// +_.option.partition(_.some('a'), isString) // $ExpectType Separated, Option> +pipe(_.some('a'), _.partition(isString)) // $ExpectType Separated, Option> diff --git a/dtslint/ts3.5/ReadonlyArray.ts b/dtslint/ts3.5/ReadonlyArray.ts index f440527b7..c1c062777 100644 --- a/dtslint/ts3.5/ReadonlyArray.ts +++ b/dtslint/ts3.5/ReadonlyArray.ts @@ -38,46 +38,6 @@ _.zipWith(rns, rss, (n, s) => [n, s] as const) // $ExpectType readonly (readonly _.unzip(rtns) // $ExpectType readonly [readonly number[], readonly string[]] pipe(rtns, _.unzip) // $ExpectType readonly [readonly number[], readonly string[]] -// -// filter -// - -// $ExpectType readonly number[] -pipe( - rus, - _.filter((u: unknown): u is number => typeof u === 'number') -) - -// -// filterWithIndex -// - -// $ExpectType readonly number[] -pipe( - rus, - _.filterWithIndex((_, u: unknown): u is number => typeof u === 'number') -) - -// -// partition -// - -// $ExpectType Separated -pipe( - rus, - _.partition((u: unknown): u is number => typeof u === 'number') -) - -// -// partitionWithIndex -// - -// $ExpectType Separated -pipe( - rus, - _.partitionWithIndex((_, u: unknown): u is number => typeof u === 'number') -) - // // spanLeft // @@ -190,3 +150,89 @@ pipe( identity ) ) + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const rsns: ReadonlyArray +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const isStringWithIndex: (i: number, u: unknown) => u is string +declare const isNumberWithIndex: (i: number, sn: string | number) => sn is number + +// +// filter +// + +// $ExpectType readonly number[] +pipe( + rsns, + _.filter((u: unknown): u is number => typeof u === 'number') +) +// $ExpectType readonly number[] +pipe( + rns, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// filterWithIndex +// + +// $ExpectType readonly number[] +pipe( + rsns, + _.filterWithIndex((_, u: unknown): u is number => typeof u === 'number') +) +// $ExpectType readonly number[] +pipe( + rns, + _.filterWithIndex( + ( + i, // $ExpectType number + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated +pipe(rsns, _.partition(isString)) +// $ExpectType Separated +pipe(rsns, _.partition(isNumber)) +// $ExpectType Separated +pipe( + rns, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partitionWithIndex +// + +// $ExpectType Separated +pipe(rsns, _.partitionWithIndex(isStringWithIndex)) +// $ExpectType Separated +pipe(rsns, _.partitionWithIndex(isNumberWithIndex)) +// $ExpectType Separated +pipe( + rns, + _.partitionWithIndex( + ( + i, // $ExpectType number + x // $ExpectType number + ) => true + ) +) diff --git a/dtslint/ts3.5/ReadonlySet.ts b/dtslint/ts3.5/ReadonlySet.ts index 93f0be837..1a86c8074 100644 --- a/dtslint/ts3.5/ReadonlySet.ts +++ b/dtslint/ts3.5/ReadonlySet.ts @@ -1,5 +1,6 @@ import * as _ from '../../src/ReadonlySet' import * as N from '../../src/number' +import { pipe } from '../../src/function' declare const me: ReadonlySet @@ -37,3 +38,37 @@ _.intersection(N.Eq)(me) // $ExpectType (me: ReadonlySet) => ReadonlySet _.difference(N.Eq)(me, me) // $ExpectType ReadonlySet _.difference(N.Eq)(me) // $ExpectType (me: ReadonlySet) => ReadonlySet + +// +// filter +// + +declare const refinement: (u: unknown) => u is string +pipe(new Set(), _.filter(refinement)) // $ExpectType ReadonlySet +declare const predicate: (u: unknown) => boolean +pipe(new Set(), _.filter(predicate)) // $ExpectType ReadonlySet + +pipe( + new Set(), + _.filter( + ( + a // $ExpectType string | number + ) => true + ) +) + +// +// partition +// + +pipe(new Set(), _.partition(refinement)) // $ExpectType Separated, ReadonlySet> +pipe(new Set(), _.partition(predicate)) // $ExpectType Separated, ReadonlySet> + +pipe( + new Set(), + _.partition( + ( + a // $ExpectType string | number + ) => true + ) +) diff --git a/src/Array.ts b/src/Array.ts index 6cb88aed4..967e3ae9b 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -461,15 +461,15 @@ export const takeRight = (n: number) => (as: Array): Array => * @since 2.0.0 */ export function takeLeftWhile(refinement: Refinement): (as: Array) => Array -export function takeLeftWhile(predicate: Predicate): (as: Array) => Array -export function takeLeftWhile(predicate: Predicate): (as: Array) => Array { - return (as) => { - const out: Array = [] - for (const a of as) { - if (!predicate(a)) { +export function takeLeftWhile(predicate: Predicate): (bs: Array) => Array +export function takeLeftWhile(predicate: Predicate): (bs: Array) => Array { + return (bs: Array) => { + const out: Array = [] + for (const b of bs) { + if (!predicate(b)) { break } - out.push(a) + out.push(b) } return out } @@ -510,10 +510,10 @@ export interface Spanned { * @since 2.0.0 */ export function spanLeft(refinement: Refinement): (as: Array) => Spanned -export function spanLeft(predicate: Predicate): (as: Array) => Spanned -export function spanLeft(predicate: Predicate): (as: Array) => Spanned { - return (as) => { - const [init, rest] = splitAt(spanLeftIndex(as, predicate))(as) +export function spanLeft(predicate: Predicate): (bs: Array) => Spanned +export function spanLeft(predicate: Predicate): (bs: Array) => Spanned { + return (bs) => { + const [init, rest] = splitAt(spanLeftIndex(bs, predicate))(bs) return { init, rest } } } @@ -561,8 +561,8 @@ export const dropRight = (n: number) => (as: Array): Array => * @category combinators * @since 2.0.0 */ -export const dropLeftWhile = (predicate: Predicate) => (as: Array): Array => - as.slice(spanLeftIndex(as, predicate)) +export const dropLeftWhile = (predicate: Predicate) => (bs: Array): Array => + bs.slice(spanLeftIndex(bs, predicate)) /** * Find the first index for which a predicate holds @@ -596,8 +596,8 @@ export const findIndex: (predicate: Predicate) => (as: Array) => Option * @since 2.0.0 */ export function findFirst(refinement: Refinement): (as: Array) => Option -export function findFirst(predicate: Predicate): (as: Array) => Option -export function findFirst(predicate: Predicate): (as: Array) => Option { +export function findFirst(predicate: Predicate): (bs: Array) => Option +export function findFirst(predicate: Predicate): (bs: Array) => Option { return RA.findFirst(predicate) } @@ -641,8 +641,8 @@ export const findFirstMap: (f: (a: A) => Option) => (as: Array) => O * @since 2.0.0 */ export function findLast(refinement: Refinement): (as: Array) => Option -export function findLast(predicate: Predicate): (as: Array) => Option -export function findLast(predicate: Predicate): (as: Array) => Option { +export function findLast(predicate: Predicate): (bs: Array) => Option +export function findLast(predicate: Predicate): (bs: Array) => Option { return RA.findLast(predicate) } @@ -1447,8 +1447,8 @@ export const separate = (fa: Array>): Separated, Arr */ export const filter: { (refinement: Refinement): (fa: Array) => Array - (predicate: Predicate): (fa: Array) => Array -} = (predicate: Predicate) => (fa: Array) => fa.filter(predicate) + (predicate: Predicate): (fb: Array) => Array +} = (predicate: Predicate) => (fb: Array) => fb.filter(predicate) /** * @category Filterable @@ -1456,8 +1456,8 @@ export const filter: { */ export const partition: { (refinement: Refinement): (fa: Array) => Separated, Array> - (predicate: Predicate): (fa: Array) => Separated, Array> -} = (predicate: Predicate): ((fa: Array) => Separated, Array>) => + (predicate: Predicate): (fb: Array) => Separated, Array> +} = (predicate: Predicate): ((fb: Array) => Separated, Array>) => partitionWithIndex((_, a) => predicate(a)) /** @@ -1534,9 +1534,9 @@ export const alt: (that: Lazy>) => (fa: Array) => Array = altW */ export const filterWithIndex: { (refinementWithIndex: RefinementWithIndex): (fa: Array) => Array - (predicateWithIndex: PredicateWithIndex): (fa: Array) => Array -} = (predicateWithIndex: PredicateWithIndex) => (fa: Array): Array => - fa.filter((a, i) => predicateWithIndex(i, a)) + (predicateWithIndex: PredicateWithIndex): (fb: Array) => Array +} = (predicateWithIndex: PredicateWithIndex) => (fb: Array): Array => + fb.filter((b, i) => predicateWithIndex(i, b)) /** * @category Extend diff --git a/src/Either.ts b/src/Either.ts index b3c52d692..fdd5219c2 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1198,7 +1198,7 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: Either ) => Either - (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either } = filterOrElse // ------------------------------------------------------------------------------------- diff --git a/src/Filterable.ts b/src/Filterable.ts index eb42d0afc..6c5d20db7 100644 --- a/src/Filterable.ts +++ b/src/Filterable.ts @@ -44,7 +44,7 @@ import { } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { getLeft, getRight, Option } from './Option' -import { Predicate } from './Predicate' +import { not, Predicate } from './Predicate' import { Refinement } from './Refinement' import { separated, Separated } from './Separated' @@ -290,24 +290,24 @@ export interface Filterable4 extends Functor4, Compactable4< export function filter( F: Functor2, G: Filterable2C -): (predicate: Predicate) => (fga: Kind2>) => Kind2> +): (predicate: Predicate) => (fgb: Kind2>) => Kind2> export function filter( F: Functor1, G: Filterable2C -): (predicate: Predicate) => (fga: Kind>) => Kind> +): (predicate: Predicate) => (fgb: Kind>) => Kind> export function filter( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fga: Kind>) => Kind> +): (predicate: Predicate) => (fgb: Kind>) => Kind> export function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => HKT> +): (predicate: Predicate) => (fgb: HKT>) => HKT> export function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => HKT> { - return (predicate) => (fga) => F.map(fga, (ga) => G.filter(ga, predicate)) +): (predicate: Predicate) => (fgb: HKT>) => HKT> { + return (predicate) => (fgb) => F.map(fgb, (ga) => G.filter(ga, predicate)) } /** @@ -350,34 +350,39 @@ export function partition( G: Filterable2C ): ( predicate: Predicate -) => (fga: Kind2>) => Separated>, Kind2>> +) => ( + fgb: Kind2> +) => Separated>, Kind2>> export function partition( F: Functor1, G: Filterable2C ): ( predicate: Predicate -) => (fga: Kind>) => Separated>, Kind>> +) => (fgb: Kind>) => Separated>, Kind>> export function partition( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fga: Kind>) => Separated>, Kind>> +): ( + predicate: Predicate +) => (fgb: Kind>) => Separated>, Kind>> export function partition( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => Separated>, HKT>> +): ( + predicate: Predicate +) => (fgb: HKT>) => Separated>, HKT>> export function partition( F: Functor, G: Filterable -): (predicate: Predicate) => (fga: HKT>) => Separated>, HKT>> { +): ( + predicate: Predicate +) => (fgb: HKT>) => Separated>, HKT>> { const _filter = filter(F, G) - return (predicate) => (fga) => - separated( - pipe( - fga, - _filter((a) => !predicate(a)) - ), - pipe(fga, _filter(predicate)) - ) + return (predicate) => { + const left = _filter(not(predicate)) + const right = _filter(predicate) + return (fgb) => separated(left(fgb), right(fgb)) + } } /** diff --git a/src/FromEither.ts b/src/FromEither.ts index 57eb10690..07c994bf7 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -118,46 +118,46 @@ export function fromPredicate( F: FromEither4 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind4 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind4 } export function fromPredicate( F: FromEither3 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 } export function fromPredicate( F: FromEither3C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 } export function fromPredicate( F: FromEither2 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 } export function fromPredicate( F: FromEither2C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 } export function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 } export function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 - (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => (a: A) => - F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a))) + return (predicate: Predicate, onFalse: (a: A) => E) => (b: B) => + F.fromEither(predicate(b) ? _.right(b) : _.left(onFalse(b))) } // ------------------------------------------------------------------------------------- @@ -329,7 +329,9 @@ export function filterOrElse( (refinement: Refinement, onFalse: (a: A) => E): ( ma: Kind4 ) => Kind4 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind4) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: Kind4 + ) => Kind4 } export function filterOrElse( F: FromEither3, @@ -338,43 +340,43 @@ export function filterOrElse( (refinement: Refinement, onFalse: (a: A) => E): ( ma: Kind3 ) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 } export function filterOrElse( F: FromEither3C, M: Chain3C ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind3) => Kind3 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 } export function filterOrElse( F: FromEither2, M: Chain2 ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 } export function filterOrElse( F: FromEither2C, M: Chain2C ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 } export function filterOrElse( F: FromEither, M: Chain ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: HKT2) => HKT2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: HKT2) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: HKT2) => HKT2 } export function filterOrElse( F: FromEither2, M: Chain2 ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 - (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => (ma: Kind2): Kind2 => - M.chain(ma, (a) => F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)))) + return (predicate: Predicate, onFalse: (a: A) => E) => (mb: Kind2): Kind2 => + M.chain(mb, (a) => F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)))) } diff --git a/src/IOEither.ts b/src/IOEither.ts index 81c224ea7..2886ea0c4 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -783,7 +783,7 @@ export const fromPredicate: { */ export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: IOEither) => IOEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (mb: IOEither) => IOEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -798,7 +798,9 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: IOEither ) => IOEither - (predicate: Predicate, onFalse: (a: A) => E2): (ma: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: IOEither + ) => IOEither } = filterOrElse /** diff --git a/src/Map.ts b/src/Map.ts index 9ba21732d..4fbfb5a10 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -509,8 +509,8 @@ export const compact = (fa: Map>): Map => { */ export const filter: { (refinement: Refinement): (fa: Map) => Map - (predicate: Predicate): (fa: Map) => Map -} = (predicate: Predicate) => (fa: Map) => _filter(fa, predicate) + (predicate: Predicate): (fb: Map) => Map +} = (predicate: Predicate) => (fb: Map) => _filter(fb, predicate) /** * @category Filterable @@ -541,8 +541,8 @@ export const mapWithIndex: (f: (k: K, a: A) => B) => (fa: Map) => */ export const partition: { (refinement: Refinement): (fa: Map) => Separated, Map> - (predicate: Predicate): (fa: Map) => Separated, Map> -} = (predicate: Predicate) => (fa: Map) => _partition(fa, predicate) + (predicate: Predicate): (fb: Map) => Separated, Map> +} = (predicate: Predicate) => (fb: Map) => _partition(fb, predicate) /** * @category Filterable diff --git a/src/Option.ts b/src/Option.ts index 2c57ca591..d1dc6170c 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -39,7 +39,7 @@ import { MonadThrow1 } from './MonadThrow' import { Monoid } from './Monoid' import { Ord } from './Ord' import { Pointed1 } from './Pointed' -import { Predicate } from './Predicate' +import { not, Predicate } from './Predicate' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' @@ -660,8 +660,9 @@ export const separate: (ma: Option>) => Separated, */ export const filter: { (refinement: Refinement): (fa: Option) => Option - (predicate: Predicate): (fa: Option) => Option -} = (predicate: Predicate) => (fa: Option) => (isNone(fa) ? none : predicate(fa.value) ? fa : none) + (predicate: Predicate): (fb: Option) => Option +} = (predicate: Predicate) => (fb: Option) => + isNone(fb) ? none : predicate(fb.value) ? fb : none /** * @category Filterable @@ -676,13 +677,9 @@ export const filterMap: (f: (a: A) => Option) => (fa: Option) => Opt */ export const partition: { (refinement: Refinement): (fa: Option) => Separated, Option> - (predicate: Predicate): (fa: Option) => Separated, Option> -} = (predicate: Predicate) => (fa: Option) => { - return separated( - _filter(fa, (a) => !predicate(a)), - _filter(fa, predicate) - ) -} + (predicate: Predicate): (fb: Option) => Separated, Option> +} = (predicate: Predicate) => (fb: Option) => + separated(_filter(fb, not(predicate)), _filter(fb, predicate)) /** * @category Filterable diff --git a/src/OptionT.ts b/src/OptionT.ts index cac264eef..6333bce92 100644 --- a/src/OptionT.ts +++ b/src/OptionT.ts @@ -245,51 +245,51 @@ export function fromPredicate( F: Pointed4 ): { (refinement: Refinement): (a: A) => Kind4> - (predicate: Predicate): (a: A) => Kind4> + (predicate: Predicate): (b: B) => Kind4> } export function fromPredicate( F: Pointed3 ): { (refinement: Refinement): (a: A) => Kind3> - (predicate: Predicate): (a: A) => Kind3> + (predicate: Predicate): (b: B) => Kind3> } export function fromPredicate( F: Pointed3C ): { (refinement: Refinement): (a: A) => Kind3> - (predicate: Predicate): (a: A) => Kind3> + (predicate: Predicate): (b: B) => Kind3> } export function fromPredicate( F: Pointed2 ): { (refinement: Refinement): (a: A) => Kind2> - (predicate: Predicate): (a: A) => Kind2> + (predicate: Predicate): (b: B) => Kind2> } export function fromPredicate( F: Pointed2C ): { (refinement: Refinement): (a: A) => Kind2> - (predicate: Predicate): (a: A) => Kind2> + (predicate: Predicate): (b: B) => Kind2> } export function fromPredicate( F: Pointed1 ): { (refinement: Refinement): (a: A) => Kind> - (predicate: Predicate): (a: A) => Kind> + (predicate: Predicate): (b: B) => Kind> } export function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> - (predicate: Predicate): (a: A) => HKT> + (predicate: Predicate): (b: B) => HKT> } export function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> - (predicate: Predicate): (a: A) => HKT> + (predicate: Predicate): (b: B) => HKT> } { - return (predicate: Predicate) => (a: A) => F.of(O.fromPredicate(predicate)(a)) + return (predicate: Predicate) => (b: B) => F.of(O.fromPredicate(predicate)(b)) } /** diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 6063d3aca..e1a9020eb 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -803,7 +803,9 @@ export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: ReaderEither ) => ReaderEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderEither) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: ReaderEither + ) => ReaderEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -818,9 +820,9 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: ReaderEither ) => ReaderEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: ReaderEither - ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: ReaderEither + ) => ReaderEither } = filterOrElse /** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 1a56c2e21..c40e404ac 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1041,7 +1041,9 @@ export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: ReaderTaskEither ) => ReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderTaskEither) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: ReaderTaskEither + ) => ReaderTaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -1056,9 +1058,9 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: ReaderTaskEither ) => ReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: ReaderTaskEither - ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: ReaderTaskEither + ) => ReaderTaskEither } = filterOrElse /** diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 0d973d9e9..3319c0f64 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -480,18 +480,18 @@ export const takeRight = (n: number) => (as: ReadonlyArray): ReadonlyArray * @since 2.5.0 */ export function takeLeftWhile(refinement: Refinement): (as: ReadonlyArray) => ReadonlyArray -export function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray -export function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray { - return (as) => { - const out: Array = [] - for (const a of as) { - if (!predicate(a)) { +export function takeLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray +export function takeLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray { + return (bs: ReadonlyArray) => { + const out: Array = [] + for (const b of bs) { + if (!predicate(b)) { break } - out.push(a) + out.push(b) } const len = out.length - return len === as.length ? as : len === 0 ? empty : out + return len === bs.length ? bs : len === 0 ? empty : out } } @@ -527,10 +527,10 @@ const spanLeftIndex = (as: ReadonlyArray, predicate: Predicate): number * @since 2.5.0 */ export function spanLeft(refinement: Refinement): (as: ReadonlyArray) => Spanned -export function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned -export function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned { - return (as) => { - const [init, rest] = splitAt(spanLeftIndex(as, predicate))(as) +export function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned +export function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned { + return (bs) => { + const [init, rest] = splitAt(spanLeftIndex(bs, predicate))(bs) return { init, rest } } } @@ -586,9 +586,9 @@ export const dropRight = (n: number) => (as: ReadonlyArray): ReadonlyArray * @category combinators * @since 2.5.0 */ -export const dropLeftWhile = (predicate: Predicate) => (as: ReadonlyArray): ReadonlyArray => { - const i = spanLeftIndex(as, predicate) - return i === 0 ? as : i === as.length ? empty : as.slice(i) +export const dropLeftWhile = (predicate: Predicate) => (bs: ReadonlyArray): ReadonlyArray => { + const i = spanLeftIndex(bs, predicate) + return i === 0 ? bs : i === bs.length ? empty : bs.slice(i) } /** @@ -629,12 +629,12 @@ export const findIndex = (predicate: Predicate) => (as: ReadonlyArray): * @since 2.5.0 */ export function findFirst(refinement: Refinement): (as: ReadonlyArray) => Option -export function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option -export function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option { - return (as) => { - for (let i = 0; i < as.length; i++) { - if (predicate(as[i])) { - return _.some(as[i]) +export function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option +export function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option { + return (bs) => { + for (let i = 0; i < bs.length; i++) { + if (predicate(bs[i])) { + return _.some(bs[i]) } } return _.none @@ -687,12 +687,12 @@ export const findFirstMap = (f: (a: A) => Option) => (as: ReadonlyArray * @since 2.5.0 */ export function findLast(refinement: Refinement): (as: ReadonlyArray) => Option -export function findLast(predicate: Predicate): (as: ReadonlyArray) => Option -export function findLast(predicate: Predicate): (as: ReadonlyArray) => Option { - return (as) => { - for (let i = as.length - 1; i >= 0; i--) { - if (predicate(as[i])) { - return _.some(as[i]) +export function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option +export function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option { + return (bs) => { + for (let i = bs.length - 1; i >= 0; i--) { + if (predicate(bs[i])) { + return _.some(bs[i]) } } return _.none @@ -1521,8 +1521,8 @@ export const separate = (fa: ReadonlyArray>): Separated(refinement: Refinement): (fa: ReadonlyArray) => ReadonlyArray - (predicate: Predicate): (fa: ReadonlyArray) => ReadonlyArray -} = (predicate: Predicate) => (fa: ReadonlyArray) => fa.filter(predicate) + (predicate: Predicate): (fb: ReadonlyArray) => ReadonlyArray +} = (predicate: Predicate) => (fb: ReadonlyArray) => fb.filter(predicate) /** * @category FilterableWithIndex @@ -1564,9 +1564,11 @@ export const partition: { (refinement: Refinement): ( fa: ReadonlyArray ) => Separated, ReadonlyArray> - (predicate: Predicate): (fa: ReadonlyArray) => Separated, ReadonlyArray> -} = (predicate: Predicate): ((fa: ReadonlyArray) => Separated, ReadonlyArray>) => - partitionWithIndex((_, a) => predicate(a)) + (predicate: Predicate): (fa: ReadonlyArray) => Separated, ReadonlyArray> +} = ( + predicate: Predicate +): ((fa: ReadonlyArray) => Separated, ReadonlyArray>) => + partitionWithIndex((_, b) => predicate(b)) /** * @category FilterableWithIndex @@ -1630,9 +1632,9 @@ export const partitionMapWithIndex = (f: (i: number, a: A) => Either(refinementWithIndex: RefinementWithIndex): (fa: ReadonlyArray) => ReadonlyArray - (predicateWithIndex: PredicateWithIndex): (fa: ReadonlyArray) => ReadonlyArray -} = (predicateWithIndex: PredicateWithIndex) => (fa: ReadonlyArray): ReadonlyArray => - fa.filter((a, i) => predicateWithIndex(i, a)) + (predicateWithIndex: PredicateWithIndex): (fb: ReadonlyArray) => ReadonlyArray +} = (predicateWithIndex: PredicateWithIndex) => (fb: ReadonlyArray): ReadonlyArray => + fb.filter((b, i) => predicateWithIndex(i, b)) /** * @category Extend diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index e95682edc..75161da94 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -643,8 +643,8 @@ export const compact = (fa: ReadonlyMap>): ReadonlyMap */ export const filter: { (refinement: Refinement): (fa: ReadonlyMap) => ReadonlyMap - (predicate: Predicate): (fa: ReadonlyMap) => ReadonlyMap -} = (predicate: Predicate) => (fa: ReadonlyMap) => _filter(fa, predicate) + (predicate: Predicate): (fb: ReadonlyMap) => ReadonlyMap +} = (predicate: Predicate) => (fb: ReadonlyMap) => _filter(fb, predicate) /** * @category Filterable @@ -679,8 +679,10 @@ export const partition: { (refinement: Refinement): ( fa: ReadonlyMap ) => Separated, ReadonlyMap> - (predicate: Predicate): (fa: ReadonlyMap) => Separated, ReadonlyMap> -} = (predicate: Predicate) => (fa: ReadonlyMap) => _partition(fa, predicate) + (predicate: Predicate): ( + fb: ReadonlyMap + ) => Separated, ReadonlyMap> +} = (predicate: Predicate) => (fb: ReadonlyMap) => _partition(fb, predicate) /** * @category Filterable diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 9fda85e1e..afdeef60e 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1061,9 +1061,9 @@ const _traverseWithIndex = (O: Ord) => ( */ export const filter: { (refinement: Refinement): (fa: ReadonlyRecord) => ReadonlyRecord - (predicate: Predicate): (fa: ReadonlyRecord) => ReadonlyRecord -} = (predicate: Predicate): ((fa: ReadonlyRecord) => ReadonlyRecord) => - filterWithIndex((_, a) => predicate(a)) + (predicate: Predicate): (fb: ReadonlyRecord) => ReadonlyRecord +} = (predicate: Predicate): ((fb: ReadonlyRecord) => ReadonlyRecord) => + filterWithIndex((_, b) => predicate(b)) /** * @category Filterable @@ -1081,13 +1081,13 @@ export const partition: { (refinement: Refinement): ( fa: ReadonlyRecord ) => Separated, ReadonlyRecord> - (predicate: Predicate): ( - fa: ReadonlyRecord - ) => Separated, ReadonlyRecord> + (predicate: Predicate): ( + fb: ReadonlyRecord + ) => Separated, ReadonlyRecord> } = ( predicate: Predicate -): ((fa: ReadonlyRecord) => Separated, ReadonlyRecord>) => - partitionWithIndex((_, a) => predicate(a)) +): ((fb: ReadonlyRecord) => Separated, ReadonlyRecord>) => + partitionWithIndex((_, b) => predicate(b)) /** * @category Filterable diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index ca0627f06..e45b6a012 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -104,12 +104,12 @@ export function chain(E: Eq): (f: (x: A) => ReadonlySet) => (set: Re * @since 2.5.0 */ export function filter(refinement: Refinement): (set: ReadonlySet) => ReadonlySet -export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet -export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet { - return (set) => { +export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet +export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet { + return (set: ReadonlySet) => { const values = set.values() - let e: Next - const r = new Set() + let e: Next + const r = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { const value = e.value @@ -129,15 +129,15 @@ export function partition( ): (set: ReadonlySet) => Separated, ReadonlySet> export function partition( predicate: Predicate -): (set: ReadonlySet) => Separated, ReadonlySet> +): (set: ReadonlySet) => Separated, ReadonlySet> export function partition( predicate: Predicate -): (set: ReadonlySet) => Separated, ReadonlySet> { - return (set) => { +): (set: ReadonlySet) => Separated, ReadonlySet> { + return (set: ReadonlySet) => { const values = set.values() - let e: Next - const right = new Set() - const left = new Set() + let e: Next + const right = new Set() + const left = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { const value = e.value diff --git a/src/Record.ts b/src/Record.ts index f7836a9f2..0a3236aac 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -650,7 +650,7 @@ const _traverseWithIndex = (O: Ord) => ( */ export const filter: { (refinement: Refinement): (fa: Record) => Record - (predicate: Predicate): (fa: Record) => Record + (predicate: Predicate): (fb: Record) => Record } = RR.filter /** @@ -667,7 +667,7 @@ export const partition: { (refinement: Refinement): ( fa: Record ) => Separated, Record> - (predicate: Predicate): (fa: Record) => Separated, Record> + (predicate: Predicate): (fb: Record) => Separated, Record> } = RR.partition /** diff --git a/src/Set.ts b/src/Set.ts index a4b024ee3..e618a6e72 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -79,12 +79,12 @@ interface Next { * @since 2.0.0 */ export function filter(refinement: Refinement): (set: Set) => Set -export function filter(predicate: Predicate): (set: Set) => Set -export function filter(predicate: Predicate): (set: Set) => Set { - return (set) => { +export function filter(predicate: Predicate): (set: Set) => Set +export function filter(predicate: Predicate): (set: Set) => Set { + return (set: Set) => { const values = set.values() - let e: Next - const r = new Set() + let e: Next + const r = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { const value = e.value @@ -100,13 +100,13 @@ export function filter(predicate: Predicate): (set: Set) => Set { * @since 2.0.0 */ export function partition(refinement: Refinement): (set: Set) => Separated, Set> -export function partition(predicate: Predicate): (set: Set) => Separated, Set> -export function partition(predicate: Predicate): (set: Set) => Separated, Set> { - return (set) => { +export function partition(predicate: Predicate): (set: Set) => Separated, Set> +export function partition(predicate: Predicate): (set: Set) => Separated, Set> { + return (set: Set) => { const values = set.values() - let e: Next - const right = new Set() - const left = new Set() + let e: Next + const right = new Set() + const left = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { const value = e.value diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index b11a3b820..3b0390f3e 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -907,9 +907,9 @@ export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): ( ma: StateReaderTaskEither ) => StateReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E): ( - ma: StateReaderTaskEither - ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + mb: StateReaderTaskEither + ) => StateReaderTaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -924,9 +924,9 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: StateReaderTaskEither ) => StateReaderTaskEither - (predicate: Predicate, onFalse: (a: A) => E2): ( - ma: StateReaderTaskEither - ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: StateReaderTaskEither + ) => StateReaderTaskEither } = filterOrElse /** diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 372b6521c..ce183bf5c 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -938,7 +938,7 @@ export const fromPredicate: { */ export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither - (predicate: Predicate, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (mb: TaskEither) => TaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -953,7 +953,9 @@ export const filterOrElseW: { (refinement: Refinement, onFalse: (a: A) => E2): ( ma: TaskEither ) => TaskEither - (predicate: Predicate, onFalse: (a: A) => E2): (ma: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + mb: TaskEither + ) => TaskEither } = filterOrElse /** diff --git a/src/TaskOption.ts b/src/TaskOption.ts index da45f6aa6..1dc25206a 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -74,7 +74,7 @@ export const fromOption: (ma: Option) => TaskOption = T.of */ export const fromPredicate: { (refinement: Refinement): (a: A) => TaskOption - (predicate: Predicate): (a: A) => TaskOption + (predicate: Predicate): (b: B) => TaskOption } = /*#__PURE__*/ OT.fromPredicate(T.Pointed) @@ -362,7 +362,7 @@ export const separate: Compactable1['separate'] = * @category Filterable * @since 2.10.0 */ -export const filter: (predicate: Predicate) => (fga: TaskOption) => TaskOption = +export const filter: (predicate: Predicate) => (fb: TaskOption) => TaskOption = /*#__PURE__*/ filter_(T.Functor, O.Filterable) @@ -380,7 +380,7 @@ export const filterMap: (f: (a: A) => Option) => (fga: TaskOption) = */ export const partition: ( predicate: Predicate -) => (fga: TaskOption) => Separated, TaskOption> = +) => (fb: TaskOption) => Separated, TaskOption> = /*#__PURE__*/ partition_(T.Functor, O.Filterable) From b9eac58c8d810f394d523a048a5262d24bd58579 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 23 Apr 2021 11:54:02 +0200 Subject: [PATCH 095/162] improve signatures involving Predicate - Set / ReadonlySet - filter - partition - Map / ReadonlyMap - filter - filterWithIndex - partition - partitionWithIndex - Option - fromPredicate - OptionT - fromPredicate - filterOrElseW - Either - IOEither - ReaderEither - ReaderTaskEither - StateReaderTaskEither - TaskEither - Array / ReadonlyArray - filter - filterWithIndex - partition - partitionWithIndex - takeLeftWhile - dropLeftWhile - spanLeft - findFirst - findLast - FromEither - fromPredicate - filterOrElse --- docs/modules/Array.ts.md | 5 +- docs/modules/Map.ts.md | 12 +- docs/modules/Option.ts.md | 4 +- docs/modules/ReadonlyArray.ts.md | 11 +- docs/modules/ReadonlyMap.ts.md | 16 ++- docs/modules/TaskOption.ts.md | 12 +- dtslint/ts3.5/Array.ts | 185 +++++++++++++++++++++++-------- dtslint/ts3.5/Either.ts | 71 ++++++++++-- dtslint/ts3.5/Map.ts | 103 +++++++++++++++-- dtslint/ts3.5/Option.ts | 65 ++++++++++- dtslint/ts3.5/ReadonlyArray.ts | 82 +++++++++++--- dtslint/ts3.5/ReadonlyMap.ts | 103 +++++++++++++++-- dtslint/ts3.5/ReadonlySet.ts | 38 +++++-- dtslint/ts3.5/Set.ts | 49 ++++++++ dtslint/ts3.5/TaskOption.ts | 72 ++++++++++++ src/Array.ts | 27 +++-- src/Map.ts | 62 ++++++----- src/Option.ts | 4 +- src/ReadonlyArray.ts | 36 +++--- src/ReadonlyMap.ts | 68 +++++++----- src/TaskOption.ts | 12 +- 21 files changed, 821 insertions(+), 216 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 29747f1a2..5895e42f4 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -401,7 +401,7 @@ Added in v2.0.0 ```ts export declare const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): (fa: A[]) => Separated - (predicateWithIndex: PredicateWithIndex): (fa: A[]) => Separated + (predicateWithIndex: PredicateWithIndex): (fb: B[]) => Separated } ``` @@ -851,7 +851,8 @@ Remove the longest initial subarray for which all element satisfy the specified **Signature** ```ts -export declare const dropLeftWhile: (predicate: Predicate) => (bs: B[]) => B[] +export declare function dropLeftWhile(refinement: Refinement): (as: Array) => Array +export declare function dropLeftWhile(predicate: Predicate): (bs: Array) => Array ``` **Example** diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index ff92b7fd5..ce1958093 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -212,7 +212,8 @@ Added in v2.10.0 **Signature** ```ts -export declare const filterWithIndex: (p: (k: K, a: A) => boolean) => (m: Map) => Map +export declare function filterWithIndex(p: (k: K, a: A) => a is B): (m: Map) => Map +export declare function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map ``` Added in v2.10.0 @@ -246,9 +247,12 @@ Added in v2.10.0 **Signature** ```ts -export declare const partitionWithIndex: ( - p: (k: K, a: A) => boolean -) => (fa: Map) => Separated, Map> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (fa: Map) => Separated, Map> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (fa: Map) => Separated, Map> ``` Added in v2.10.0 diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index a9ba35c6c..3caf23b7a 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -282,7 +282,7 @@ Added in v2.0.0 ```ts export declare const partition: { (refinement: Refinement): (fa: Option) => Separated, Option> - (predicate: Predicate): (fa: Option) => Separated, Option> + (predicate: Predicate): (fb: Option) => Separated, Option> } ``` @@ -566,7 +566,7 @@ Returns a _smart constructor_ based on the given predicate. ```ts export declare function fromPredicate(refinement: Refinement): (a: A) => Option -export declare function fromPredicate(predicate: Predicate): (a: A) => Option +export declare function fromPredicate(predicate: Predicate): (b: B) => Option ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index e67c59c20..d17eb2982 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -407,7 +407,9 @@ export declare const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( fa: readonly A[] ) => Separated - (predicateWithIndex: PredicateWithIndex): (fa: readonly A[]) => Separated + (predicateWithIndex: PredicateWithIndex): ( + fb: readonly B[] + ) => Separated } ``` @@ -857,7 +859,12 @@ Remove the longest initial subarray for which all element satisfy the specified **Signature** ```ts -export declare const dropLeftWhile: (predicate: Predicate) => (bs: readonly B[]) => readonly B[] +export declare function dropLeftWhile( + refinement: Refinement +): (as: ReadonlyArray) => ReadonlyArray +export declare function dropLeftWhile( + predicate: Predicate +): (bs: ReadonlyArray) => ReadonlyArray ``` **Example** diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index cfc37fe1f..c7cc120b3 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -225,7 +225,12 @@ Added in v2.10.0 **Signature** ```ts -export declare const filterWithIndex: (p: (k: K, a: A) => boolean) => (m: ReadonlyMap) => ReadonlyMap +export declare function filterWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (m: ReadonlyMap) => ReadonlyMap +export declare function filterWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => ReadonlyMap ``` Added in v2.10.0 @@ -259,9 +264,12 @@ Added in v2.10.0 **Signature** ```ts -export declare const partitionWithIndex: ( - p: (k: K, a: A) => boolean -) => (fa: ReadonlyMap) => Separated, ReadonlyMap> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (m: ReadonlyMap) => Separated, ReadonlyMap> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => Separated, ReadonlyMap> ``` Added in v2.10.0 diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 7e00d528c..a53b8b58a 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -182,7 +182,10 @@ Added in v2.10.0 **Signature** ```ts -export declare const filter: (predicate: Predicate) => (fb: TaskOption) => TaskOption +export declare const filter: { + (refinement: Refinement): (fb: TaskOption) => TaskOption + (predicate: Predicate): (fb: TaskOption) => TaskOption +} ``` Added in v2.10.0 @@ -202,9 +205,10 @@ Added in v2.10.0 **Signature** ```ts -export declare const partition: ( - predicate: Predicate -) => (fb: TaskOption) => Separated, TaskOption> +export declare const partition: { + (refinement: Refinement): (fb: TaskOption) => Separated, TaskOption> + (predicate: Predicate): (fb: TaskOption) => Separated, TaskOption> +} ``` Added in v2.10.0 diff --git a/dtslint/ts3.5/Array.ts b/dtslint/ts3.5/Array.ts index e2524f904..377c083fc 100644 --- a/dtslint/ts3.5/Array.ts +++ b/dtslint/ts3.5/Array.ts @@ -38,55 +38,6 @@ _.zipWith(ns, ss, (n, s) => [n, s] as const) // $ExpectType (readonly [number, s _.unzip(tns) // $ExpectType [number[], string[]] pipe(tns, _.unzip) // $ExpectType [number[], string[]] -// -// Filterable overlodings -// - -declare function isStringWithIndex(i: number, x: unknown): x is string - -_.array.filterWithIndex([] as Array, isStringWithIndex) // $ExpectType string[] -_.array.partitionWithIndex([] as Array, isStringWithIndex) // $ExpectType Separated<(string | number)[], string[]> - -// -// filter -// - -// $ExpectType number[] -pipe( - us, - _.filter((u: unknown): u is number => typeof u === 'number') -) - -// -// filterWithIndex -// - -// $ExpectType number[] -pipe( - us, - _.filterWithIndex((_, u: unknown): u is number => typeof u === 'number') -) - -// -// partition -// - -// $ExpectType Separated -pipe( - us, - _.partition((u: unknown): u is number => typeof u === 'number') -) - -// -// partitionWithIndex -// - -// $ExpectType Separated -pipe( - us, - _.partitionWithIndex((_, u: unknown): u is number => typeof u === 'number') -) - // // spanLeft // @@ -199,3 +150,139 @@ pipe( identity ) ) + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const prns: Array +declare const prsns: Array +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean +declare const isStringWithIndex: (i: number, u: unknown) => u is string +declare const isNumberWithIndex: (i: number, sn: string | number) => sn is number +declare const predicateWithIndex: (i: number, sn: string | number) => boolean + +// +// filter +// + +// $ExpectType string[] +pipe(prsns, _.filter(isString)) +// $ExpectType number[] +pipe(prns, _.filter(predicate)) +// $ExpectType number[] +pipe( + prns, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// filterWithIndex +// + +// $ExpectType string[] +pipe(prsns, _.filterWithIndex(isStringWithIndex)) +// $ExpectType number[] +pipe(prns, _.filterWithIndex(predicateWithIndex)) +// $ExpectType number[] +pipe( + prns, + _.filterWithIndex( + ( + i, // $ExpectType number + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated +pipe(prsns, _.partition(isString)) +// $ExpectType Separated +pipe(prns, _.partition(predicate)) +// $ExpectType Separated<(string | number)[], number[]> +pipe(prsns, _.partition(isNumber)) +// $ExpectType Separated +pipe( + prns, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partitionWithIndex +// + +// $ExpectType Separated +pipe(prsns, _.partitionWithIndex(isStringWithIndex)) +// $ExpectType Separated +pipe(prns, _.partitionWithIndex(predicateWithIndex)) +// $ExpectType Separated<(string | number)[], number[]> +pipe(prsns, _.partitionWithIndex(isNumberWithIndex)) +// $ExpectType Separated +pipe( + prns, + _.partitionWithIndex( + ( + i, // $ExpectType number + x // $ExpectType number + ) => true + ) +) + +// +// takeLeftWhile +// + +// $ExpectType string[] +pipe(prsns, _.takeLeftWhile(isString)) +// $ExpectType number[] +pipe(prns, _.takeLeftWhile(predicate)) + +// +// dropLeftWhile +// + +// $ExpectType string[] +pipe(prsns, _.dropLeftWhile(isString)) +// $ExpectType number[] +pipe(prns, _.dropLeftWhile(predicate)) + +// +// spanLeft +// + +// $ExpectType Spanned +pipe(prsns, _.spanLeft(isString)) +// $ExpectType Spanned +pipe(prns, _.spanLeft(predicate)) + +// +// findFirst +// + +// $ExpectType Option +pipe(prsns, _.findFirst(isString)) +// $ExpectType Option +pipe(prns, _.findFirst(predicate)) + +// +// findLast +// + +// $ExpectType Option +pipe(prsns, _.findLast(isString)) +// $ExpectType Option +pipe(prns, _.findLast(predicate)) diff --git a/dtslint/ts3.5/Either.ts b/dtslint/ts3.5/Either.ts index 6d3d9200b..809dbf18d 100644 --- a/dtslint/ts3.5/Either.ts +++ b/dtslint/ts3.5/Either.ts @@ -34,16 +34,6 @@ declare const f: (key: K) => D[K] // $ExpectType Either flow(f, _.fromNullable('error'))('foo') -// -// Witherable overlodings -// - -declare function isString(x: unknown): x is string -const W = _.getWitherable(B.MonoidAll) - -W.filter(_.right(1), isString) // $ExpectType Either -W.partition(_.right(1), isString) // $ExpectType Separated, Either> - // // do notation // @@ -96,3 +86,64 @@ pipe( _.right('a'), _.chainFirst((s) => _.stringifyJSON(s, identity)) ) + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const n: number +declare const sn: string | number +declare const en: _.Either +declare const esn: _.Either +declare const isString: (u: unknown) => u is string +declare const predicate: (sn: string | number) => boolean + +// +// fromPredicate +// + +// $ExpectType Either +pipe( + sn, + _.fromPredicate(isString, () => false) +) +// $ExpectType Either +pipe( + n, + _.fromPredicate(predicate, () => false) +) +// $ExpectType Either +pipe( + n, + _.fromPredicate( + ( + n // $ExpectType number + ) => true, + () => false + ) +) + +// +// filterOrElse +// + +// $ExpectType Either +pipe( + esn, + _.filterOrElse(isString, () => false) +) +// $ExpectType Either +pipe( + en, + _.filterOrElse(predicate, () => false) +) +// $ExpectType Either +pipe( + en, + _.filterOrElse( + ( + n // $ExpectType number + ) => true, + () => false + ) +) diff --git a/dtslint/ts3.5/Map.ts b/dtslint/ts3.5/Map.ts index faa42e17d..1730d11c2 100644 --- a/dtslint/ts3.5/Map.ts +++ b/dtslint/ts3.5/Map.ts @@ -1,17 +1,7 @@ import * as _ from '../../src/Map' import * as S from '../../src/string' import * as N from '../../src/number' - -// -// FilterableWithIndex overloadings -// - -const FWI = _.getFilterableWithIndex<'a' | 'b'>() - -declare function isStringWithKey(i: 'a' | 'b', x: unknown): x is string - -FWI.filterWithIndex(_.empty as Map<'a' | 'b', string | number>, isStringWithKey) // $ExpectType Map<"a" | "b", string> -FWI.partitionWithIndex(_.empty as Map<'a' | 'b', string | number>, isStringWithKey) // $ExpectType Separated, Map<"a" | "b", string>> +import { pipe } from '../../src/function' // // member @@ -47,3 +37,94 @@ _.lookupWithKey(S.Eq)('a') // $ExpectType (m: Map) => Option<[stri _.isSubmap(S.Eq, N.Eq)(new Map([['a', 1]]), new Map([['a', 1]])) // $ExpectType boolean _.isSubmap(S.Eq, N.Eq)(new Map([['a', 1]])) // $ExpectType (me: Map) => boolean + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const prns: Map +declare const prsns: Map +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean +declare const isStringWithIndex: (k: string, u: unknown) => u is string +declare const isNumberWithIndex: (k: string, sn: string | number) => sn is number +declare const predicateWithIndex: (k: string, sn: string | number) => boolean + +// +// filter +// + +// $ExpectType Map +pipe(prsns, _.filter(isString)) +// $ExpectType Map +pipe(prns, _.filter(predicate)) +// $ExpectType Map +pipe( + prns, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// filterWithIndex +// + +// $ExpectType Map +pipe(prsns, _.filterWithIndex(isStringWithIndex)) +// $ExpectType Map +pipe(prns, _.filterWithIndex(predicateWithIndex)) +// $ExpectType Map +pipe( + prns, + _.filterWithIndex( + ( + k, // $ExpectType string + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated, Map> +pipe(prsns, _.partition(isString)) +// $ExpectType Separated, Map> +pipe(prns, _.partition(predicate)) +// $ExpectType Separated, Map> +pipe(prsns, _.partition(isNumber)) +// $ExpectType Separated, Map> +pipe( + prns, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partitionWithIndex +// + +// $ExpectType Separated, Map> +pipe(prsns, _.partitionWithIndex(isStringWithIndex)) +// $ExpectType Separated, Map> +pipe(prns, _.partitionWithIndex(predicateWithIndex)) +// $ExpectType Separated, Map> +pipe(prsns, _.partitionWithIndex(isNumberWithIndex)) +// $ExpectType Separated, Map> +pipe( + prns, + _.partitionWithIndex( + ( + k, // $ExpectType string + x // $ExpectType number + ) => true + ) +) diff --git a/dtslint/ts3.5/Option.ts b/dtslint/ts3.5/Option.ts index 42bba82b8..f53af7f58 100644 --- a/dtslint/ts3.5/Option.ts +++ b/dtslint/ts3.5/Option.ts @@ -36,17 +36,70 @@ pipe( _.bind('b', () => _.of('b')) ) +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const n: number +declare const sn: string | number +declare const on: _.Option +declare const osn: _.Option +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean + // // filter // -declare function isString(x: unknown): x is string - -_.option.filter(_.some('a'), isString) // $ExpectType Option -pipe(_.some('a'), _.filter(isString)) // $ExpectType Option +// $ExpectType Option +pipe(osn, _.filter(isString)) +// $ExpectType Option +pipe(on, _.filter(predicate)) +// $ExpectType Option +pipe( + on, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) // // partition // -_.option.partition(_.some('a'), isString) // $ExpectType Separated, Option> -pipe(_.some('a'), _.partition(isString)) // $ExpectType Separated, Option> + +// $ExpectType Separated, Option> +pipe(osn, _.partition(isString)) +// $ExpectType Separated, Option> +pipe(on, _.partition(predicate)) +// $ExpectType Separated, Option> +pipe(osn, _.partition(isNumber)) +// $ExpectType Separated, Option> +pipe( + on, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// fromPredicate +// + +// $ExpectType Option +pipe(sn, _.fromPredicate(isString)) +// $ExpectType Option +pipe(n, _.fromPredicate(predicate)) +// $ExpectType Option +pipe( + n, + _.fromPredicate( + ( + n // $ExpectType number + ) => true + ) +) diff --git a/dtslint/ts3.5/ReadonlyArray.ts b/dtslint/ts3.5/ReadonlyArray.ts index c1c062777..a70b2ac50 100644 --- a/dtslint/ts3.5/ReadonlyArray.ts +++ b/dtslint/ts3.5/ReadonlyArray.ts @@ -155,24 +155,26 @@ pipe( // Predicate-based APIs // ------------------------------------------------------------------------------------- -declare const rsns: ReadonlyArray +declare const prns: ReadonlyArray +declare const prsns: ReadonlyArray declare const isString: (u: unknown) => u is string declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean declare const isStringWithIndex: (i: number, u: unknown) => u is string declare const isNumberWithIndex: (i: number, sn: string | number) => sn is number +declare const predicateWithIndex: (i: number, sn: string | number) => boolean // // filter // +// $ExpectType readonly string[] +pipe(prsns, _.filter(isString)) // $ExpectType readonly number[] -pipe( - rsns, - _.filter((u: unknown): u is number => typeof u === 'number') -) +pipe(prns, _.filter(predicate)) // $ExpectType readonly number[] pipe( - rns, + prns, _.filter( ( x // $ExpectType number @@ -184,14 +186,13 @@ pipe( // filterWithIndex // +// $ExpectType readonly string[] +pipe(prsns, _.filterWithIndex(isStringWithIndex)) // $ExpectType readonly number[] -pipe( - rsns, - _.filterWithIndex((_, u: unknown): u is number => typeof u === 'number') -) +pipe(prns, _.filterWithIndex(predicateWithIndex)) // $ExpectType readonly number[] pipe( - rns, + prns, _.filterWithIndex( ( i, // $ExpectType number @@ -205,9 +206,11 @@ pipe( // // $ExpectType Separated -pipe(rsns, _.partition(isString)) +pipe(prsns, _.partition(isString)) +// $ExpectType Separated +pipe(prns, _.partition(predicate)) // $ExpectType Separated -pipe(rsns, _.partition(isNumber)) +pipe(prsns, _.partition(isNumber)) // $ExpectType Separated pipe( rns, @@ -223,12 +226,14 @@ pipe( // // $ExpectType Separated -pipe(rsns, _.partitionWithIndex(isStringWithIndex)) +pipe(prsns, _.partitionWithIndex(isStringWithIndex)) +// $ExpectType Separated +pipe(prns, _.partitionWithIndex(predicateWithIndex)) // $ExpectType Separated -pipe(rsns, _.partitionWithIndex(isNumberWithIndex)) +pipe(prsns, _.partitionWithIndex(isNumberWithIndex)) // $ExpectType Separated pipe( - rns, + prns, _.partitionWithIndex( ( i, // $ExpectType number @@ -236,3 +241,48 @@ pipe( ) => true ) ) + +// +// takeLeftWhile +// + +// $ExpectType readonly string[] +pipe(prsns, _.takeLeftWhile(isString)) +// $ExpectType readonly number[] +pipe(prns, _.takeLeftWhile(predicate)) + +// +// dropLeftWhile +// + +// $ExpectType readonly string[] +pipe(prsns, _.dropLeftWhile(isString)) +// $ExpectType readonly number[] +pipe(prns, _.dropLeftWhile(predicate)) + +// +// spanLeft +// + +// $ExpectType Spanned +pipe(prsns, _.spanLeft(isString)) +// $ExpectType Spanned +pipe(prns, _.spanLeft(predicate)) + +// +// findFirst +// + +// $ExpectType Option +pipe(prsns, _.findFirst(isString)) +// $ExpectType Option +pipe(prns, _.findFirst(predicate)) + +// +// findLast +// + +// $ExpectType Option +pipe(prsns, _.findLast(isString)) +// $ExpectType Option +pipe(prns, _.findLast(predicate)) diff --git a/dtslint/ts3.5/ReadonlyMap.ts b/dtslint/ts3.5/ReadonlyMap.ts index eb67dd032..e83b41314 100644 --- a/dtslint/ts3.5/ReadonlyMap.ts +++ b/dtslint/ts3.5/ReadonlyMap.ts @@ -1,17 +1,7 @@ import * as _ from '../../src/ReadonlyMap' import * as N from '../../src/number' import * as S from '../../src/string' - -// -// FilterableWithIndex overloadings -// - -const FWI = _.getFilterableWithIndex<'a' | 'b'>() - -declare function isStringWithKey(i: 'a' | 'b', x: unknown): x is string - -FWI.filterWithIndex(_.empty as ReadonlyMap<'a' | 'b', string | number>, isStringWithKey) // $ExpectType ReadonlyMap<"a" | "b", string> -FWI.partitionWithIndex(_.empty as ReadonlyMap<'a' | 'b', string | number>, isStringWithKey) // $ExpectType Separated, ReadonlyMap<"a" | "b", string>> +import { pipe } from '../../src/function' // // member @@ -47,3 +37,94 @@ _.lookupWithKey(S.Eq)('a') // $ExpectType (m: ReadonlyMap) => Opti _.isSubmap(S.Eq, N.Eq)(new Map([['a', 1]]), new Map([['a', 1]])) // $ExpectType boolean _.isSubmap(S.Eq, N.Eq)(new Map([['a', 1]])) // $ExpectType (me: ReadonlyMap) => boolean + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const prns: ReadonlyMap +declare const prsns: ReadonlyMap +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean +declare const isStringWithIndex: (k: string, u: unknown) => u is string +declare const isNumberWithIndex: (k: string, sn: string | number) => sn is number +declare const predicateWithIndex: (k: string, sn: string | number) => boolean + +// +// filter +// + +// $ExpectType ReadonlyMap +pipe(prsns, _.filter(isString)) +// $ExpectType ReadonlyMap +pipe(prns, _.filter(predicate)) +// $ExpectType ReadonlyMap +pipe( + prns, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// filterWithIndex +// + +// $ExpectType ReadonlyMap +pipe(prsns, _.filterWithIndex(isStringWithIndex)) +// $ExpectType ReadonlyMap +pipe(prns, _.filterWithIndex(predicateWithIndex)) +// $ExpectType ReadonlyMap +pipe( + prns, + _.filterWithIndex( + ( + k, // $ExpectType string + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated, ReadonlyMap> +pipe(prsns, _.partition(isString)) +// $ExpectType Separated, ReadonlyMap> +pipe(prns, _.partition(predicate)) +// $ExpectType Separated, ReadonlyMap> +pipe(prsns, _.partition(isNumber)) +// $ExpectType Separated, ReadonlyMap> +pipe( + prns, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partitionWithIndex +// + +// $ExpectType Separated, ReadonlyMap> +pipe(prsns, _.partitionWithIndex(isStringWithIndex)) +// $ExpectType Separated, ReadonlyMap> +pipe(prns, _.partitionWithIndex(predicateWithIndex)) +// $ExpectType Separated, ReadonlyMap> +pipe(prsns, _.partitionWithIndex(isNumberWithIndex)) +// $ExpectType Separated, ReadonlyMap> +pipe( + prns, + _.partitionWithIndex( + ( + k, // $ExpectType string + x // $ExpectType number + ) => true + ) +) diff --git a/dtslint/ts3.5/ReadonlySet.ts b/dtslint/ts3.5/ReadonlySet.ts index 1a86c8074..a6c380674 100644 --- a/dtslint/ts3.5/ReadonlySet.ts +++ b/dtslint/ts3.5/ReadonlySet.ts @@ -39,20 +39,30 @@ _.intersection(N.Eq)(me) // $ExpectType (me: ReadonlySet) => ReadonlySet _.difference(N.Eq)(me, me) // $ExpectType ReadonlySet _.difference(N.Eq)(me) // $ExpectType (me: ReadonlySet) => ReadonlySet +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const prns: ReadonlySet +declare const prsns: ReadonlySet +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean + // // filter // -declare const refinement: (u: unknown) => u is string -pipe(new Set(), _.filter(refinement)) // $ExpectType ReadonlySet -declare const predicate: (u: unknown) => boolean -pipe(new Set(), _.filter(predicate)) // $ExpectType ReadonlySet - +// $ExpectType ReadonlySet +pipe(prsns, _.filter(isString)) +// $ExpectType ReadonlySet +pipe(prns, _.filter(predicate)) +// $ExpectType ReadonlySet pipe( - new Set(), + prns, _.filter( ( - a // $ExpectType string | number + x // $ExpectType number ) => true ) ) @@ -61,14 +71,18 @@ pipe( // partition // -pipe(new Set(), _.partition(refinement)) // $ExpectType Separated, ReadonlySet> -pipe(new Set(), _.partition(predicate)) // $ExpectType Separated, ReadonlySet> - +// $ExpectType Separated, ReadonlySet> +pipe(prsns, _.partition(isString)) +// $ExpectType Separated, ReadonlySet> +pipe(prns, _.partition(predicate)) +// $ExpectType Separated, ReadonlySet> +pipe(prsns, _.partition(isNumber)) +// $ExpectType Separated, ReadonlySet> pipe( - new Set(), + prns, _.partition( ( - a // $ExpectType string | number + x // $ExpectType number ) => true ) ) diff --git a/dtslint/ts3.5/Set.ts b/dtslint/ts3.5/Set.ts index 0821aa28b..9c94bf449 100644 --- a/dtslint/ts3.5/Set.ts +++ b/dtslint/ts3.5/Set.ts @@ -1,5 +1,6 @@ import * as _ from '../../src/Set' import * as N from '../../src/number' +import { pipe } from '../../src/function' declare const me: Set @@ -37,3 +38,51 @@ _.intersection(N.Eq)(me) // $ExpectType (me: Set) => Set _.difference(N.Eq)(me, me) // $ExpectType Set _.difference(N.Eq)(me) // $ExpectType (me: Set) => Set + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const prns: Set +declare const prsns: Set +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean + +// +// filter +// + +// $ExpectType Set +pipe(prsns, _.filter(isString)) +// $ExpectType Set +pipe(prns, _.filter(predicate)) +// $ExpectType Set +pipe( + prns, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated, Set> +pipe(prsns, _.partition(isString)) +// $ExpectType Separated, Set> +pipe(prns, _.partition(predicate)) +// $ExpectType Separated, Set> +pipe(prsns, _.partition(isNumber)) +// $ExpectType Separated, Set> +pipe( + prns, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) diff --git a/dtslint/ts3.5/TaskOption.ts b/dtslint/ts3.5/TaskOption.ts index 7026f1bab..97c1b689b 100644 --- a/dtslint/ts3.5/TaskOption.ts +++ b/dtslint/ts3.5/TaskOption.ts @@ -9,3 +9,75 @@ declare const tesn: TE.TaskEither // pipe(tesn, _.fromTaskEither) // $ExpectType TaskOption + +// ------------------------------------------------------------------------------------- +// Predicate-based APIs +// ------------------------------------------------------------------------------------- + +declare const n: number +declare const sn: string | number +declare const on: _.TaskOption +declare const osn: _.TaskOption +declare const isString: (u: unknown) => u is string +declare const isNumber: (sn: string | number) => sn is number +declare const predicate: (sn: string | number) => boolean + +// +// filter +// + +// $ExpectType TaskOption +pipe(osn, _.filter(isString)) +// $ExpectType TaskOption +pipe(on, _.filter(predicate)) +// $ExpectType TaskOption +pipe( + on, + _.filter( + ( + x // $ExpectType number + ) => true + ) +) + +// +// partition +// + +// $ExpectType Separated, TaskOption> +pipe(osn, _.partition(isString)) +// $ExpectType Separated, TaskOption> +pipe(on, _.partition(predicate)) +// $ExpectType Separated, TaskOption> +pipe(osn, _.partition(isNumber)) +// $ExpectType Separated, TaskOption> +pipe( + on, + _.partition( + ( + x // $ExpectType number + ) => true + ) +) + +// +// fromPredicate +// + +// $ExpectType TaskOption +pipe(sn, _.fromPredicate(isString)) +// $ExpectType TaskOption +pipe(n, _.fromPredicate(predicate)) +// $ExpectType TaskOption +pipe( + n, + _.fromPredicate( + ( + n // $ExpectType number + ) => true + ) +) + +// +// filterOrElse +// diff --git a/src/Array.ts b/src/Array.ts index 967e3ae9b..855cacc6f 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -561,8 +561,11 @@ export const dropRight = (n: number) => (as: Array): Array => * @category combinators * @since 2.0.0 */ -export const dropLeftWhile = (predicate: Predicate) => (bs: Array): Array => - bs.slice(spanLeftIndex(bs, predicate)) +export function dropLeftWhile(refinement: Refinement): (as: Array) => Array +export function dropLeftWhile(predicate: Predicate): (bs: Array) => Array +export function dropLeftWhile(predicate: Predicate): (bs: Array) => Array { + return (bs) => bs.slice(spanLeftIndex(bs, predicate)) +} /** * Find the first index for which a predicate holds @@ -1468,16 +1471,18 @@ export const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( fa: Array ) => Separated, Array> - (predicateWithIndex: PredicateWithIndex): (fa: Array) => Separated, Array> -} = (predicateWithIndex: PredicateWithIndex) => (fa: Array): Separated, Array> => { - const left: Array = [] - const right: Array = [] - for (let i = 0; i < fa.length; i++) { - const a = fa[i] - if (predicateWithIndex(i, a)) { - right.push(a) + (predicateWithIndex: PredicateWithIndex): (fb: Array) => Separated, Array> +} = (predicateWithIndex: PredicateWithIndex) => ( + fb: Array +): Separated, Array> => { + const left: Array = [] + const right: Array = [] + for (let i = 0; i < fb.length; i++) { + const b = fb[i] + if (predicateWithIndex(i, b)) { + right.push(b) } else { - left.push(a) + left.push(b) } } return separated(left, right) diff --git a/src/Map.ts b/src/Map.ts index 4fbfb5a10..c0cb386f0 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -408,23 +408,31 @@ export const partitionMapWithIndex = (f: (k: K, a: A) => Either(p: (k: K, a: A) => boolean) => ( - fa: Map -): Separated, Map> => { - const left = new Map() - const right = new Map() - const entries = fa.entries() - let e: Next<[K, A]> - // tslint:disable-next-line: strict-boolean-expressions - while (!(e = entries.next()).done) { - const [k, a] = e.value - if (p(k, a)) { - right.set(k, a) - } else { - left.set(k, a) +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (fa: Map) => Separated, Map> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (fa: Map) => Separated, Map> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (fa: Map) => Separated, Map> { + return (fa: Map) => { + const left = new Map() + const right = new Map() + const entries = fa.entries() + let e: Next<[K, B]> + // tslint:disable-next-line: strict-boolean-expressions + while (!(e = entries.next()).done) { + const [k, b] = e.value + if (predicateWithIndex(k, b)) { + right.set(k, b) + } else { + left.set(k, b) + } } + return separated(left, right) } - return separated(left, right) } /** @@ -450,18 +458,22 @@ export const filterMapWithIndex = (f: (k: K, a: A) => Option) => (fa * @category combinators * @since 2.10.0 */ -export const filterWithIndex = (p: (k: K, a: A) => boolean) => (m: Map): Map => { - const out = new Map() - const entries = m.entries() - let e: Next<[K, A]> - // tslint:disable-next-line: strict-boolean-expressions - while (!(e = entries.next()).done) { - const [k, a] = e.value - if (p(k, a)) { - out.set(k, a) +export function filterWithIndex(p: (k: K, a: A) => a is B): (m: Map) => Map +export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map +export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map { + return (m: Map) => { + const out = new Map() + const entries = m.entries() + let e: Next<[K, B]> + // tslint:disable-next-line: strict-boolean-expressions + while (!(e = entries.next()).done) { + const [k, b] = e.value + if (p(k, b)) { + out.set(k, b) + } } + return out } - return out } // ------------------------------------------------------------------------------------- diff --git a/src/Option.ts b/src/Option.ts index d1dc6170c..3d463c81c 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -141,8 +141,8 @@ export const some: (a: A) => Option = _.some * @since 2.0.0 */ export function fromPredicate(refinement: Refinement): (a: A) => Option -export function fromPredicate(predicate: Predicate): (a: A) => Option -export function fromPredicate(predicate: Predicate): (a: A) => Option { +export function fromPredicate(predicate: Predicate): (b: B) => Option +export function fromPredicate(predicate: Predicate): (b: B) => Option { return (a) => (predicate(a) ? some(a) : none) } diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 3319c0f64..f266b2361 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -586,9 +586,13 @@ export const dropRight = (n: number) => (as: ReadonlyArray): ReadonlyArray * @category combinators * @since 2.5.0 */ -export const dropLeftWhile = (predicate: Predicate) => (bs: ReadonlyArray): ReadonlyArray => { - const i = spanLeftIndex(bs, predicate) - return i === 0 ? bs : i === bs.length ? empty : bs.slice(i) +export function dropLeftWhile(refinement: Refinement): (as: ReadonlyArray) => ReadonlyArray +export function dropLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray +export function dropLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray { + return (bs) => { + const i = spanLeftIndex(bs, predicate) + return i === 0 ? bs : i === bs.length ? empty : bs.slice(i) + } } /** @@ -1578,20 +1582,20 @@ export const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( fa: ReadonlyArray ) => Separated, ReadonlyArray> - (predicateWithIndex: PredicateWithIndex): ( - fa: ReadonlyArray - ) => Separated, ReadonlyArray> -} = (predicateWithIndex: PredicateWithIndex) => ( - fa: ReadonlyArray -): Separated, ReadonlyArray> => { - const left: Array = [] - const right: Array = [] - for (let i = 0; i < fa.length; i++) { - const a = fa[i] - if (predicateWithIndex(i, a)) { - right.push(a) + (predicateWithIndex: PredicateWithIndex): ( + fb: ReadonlyArray + ) => Separated, ReadonlyArray> +} = (predicateWithIndex: PredicateWithIndex) => ( + fb: ReadonlyArray +): Separated, ReadonlyArray> => { + const left: Array = [] + const right: Array = [] + for (let i = 0; i < fb.length; i++) { + const b = fb[i] + if (predicateWithIndex(i, b)) { + right.push(b) } else { - left.push(a) + left.push(b) } } return separated(left, right) diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 75161da94..3898b246f 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -539,23 +539,31 @@ export const partitionMapWithIndex = (f: (k: K, a: A) => Either(p: (k: K, a: A) => boolean) => ( - fa: ReadonlyMap -): Separated, ReadonlyMap> => { - const left = new Map() - const right = new Map() - const entries = fa.entries() - let e: Next - // tslint:disable-next-line: strict-boolean-expressions - while (!(e = entries.next()).done) { - const [k, a] = e.value - if (p(k, a)) { - right.set(k, a) - } else { - left.set(k, a) +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (m: ReadonlyMap) => Separated, ReadonlyMap> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => Separated, ReadonlyMap> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => Separated, ReadonlyMap> { + return (m: ReadonlyMap) => { + const left = new Map() + const right = new Map() + const entries = m.entries() + let e: Next + // tslint:disable-next-line: strict-boolean-expressions + while (!(e = entries.next()).done) { + const [k, b] = e.value + if (predicateWithIndex(k, b)) { + right.set(k, b) + } else { + left.set(k, b) + } } + return separated(left, right) } - return separated(left, right) } /** @@ -583,18 +591,28 @@ export const filterMapWithIndex = (f: (k: K, a: A) => Option) => ( * @category combinators * @since 2.10.0 */ -export const filterWithIndex = (p: (k: K, a: A) => boolean) => (m: ReadonlyMap): ReadonlyMap => { - const out = new Map() - const entries = m.entries() - let e: Next - // tslint:disable-next-line: strict-boolean-expressions - while (!(e = entries.next()).done) { - const [k, a] = e.value - if (p(k, a)) { - out.set(k, a) +export function filterWithIndex( + predicateWithIndex: (k: K, a: A) => a is B +): (m: ReadonlyMap) => ReadonlyMap +export function filterWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => ReadonlyMap +export function filterWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => ReadonlyMap { + return (m: ReadonlyMap) => { + const out = new Map() + const entries = m.entries() + let e: Next + // tslint:disable-next-line: strict-boolean-expressions + while (!(e = entries.next()).done) { + const [k, b] = e.value + if (predicateWithIndex(k, b)) { + out.set(k, b) + } } + return out } - return out } // ------------------------------------------------------------------------------------- diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 1dc25206a..77e2de258 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -362,7 +362,10 @@ export const separate: Compactable1['separate'] = * @category Filterable * @since 2.10.0 */ -export const filter: (predicate: Predicate) => (fb: TaskOption) => TaskOption = +export const filter: { + (refinement: Refinement): (fb: TaskOption) => TaskOption + (predicate: Predicate): (fb: TaskOption) => TaskOption +} = /*#__PURE__*/ filter_(T.Functor, O.Filterable) @@ -378,9 +381,10 @@ export const filterMap: (f: (a: A) => Option) => (fga: TaskOption) = * @category Filterable * @since 2.10.0 */ -export const partition: ( - predicate: Predicate -) => (fb: TaskOption) => Separated, TaskOption> = +export const partition: { + (refinement: Refinement): (fb: TaskOption) => Separated, TaskOption> + (predicate: Predicate): (fb: TaskOption) => Separated, TaskOption> +} = /*#__PURE__*/ partition_(T.Functor, O.Filterable) From 22f7f77c174c124b0dea4e8691c8ccf4684af14c Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 23 Apr 2021 12:07:20 +0200 Subject: [PATCH 096/162] Filterable: add overloadings to filter / partition --- docs/modules/Filterable.ts.md | 64 +++++++++++++++++++++++++---------- src/Filterable.ts | 64 +++++++++++++++++++++++++---------- 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/docs/modules/Filterable.ts.md b/docs/modules/Filterable.ts.md index 080e04eca..299713f4e 100644 --- a/docs/modules/Filterable.ts.md +++ b/docs/modules/Filterable.ts.md @@ -69,19 +69,31 @@ Added in v2.0.0 export declare function filter( F: Functor2, G: Filterable2C -): (predicate: Predicate) => (fgb: Kind2>) => Kind2> +): { + (refinement: Refinement): (fga: Kind2>) => Kind2> + (predicate: Predicate): (fgb: Kind2>) => Kind2> +} export declare function filter( F: Functor1, G: Filterable2C -): (predicate: Predicate) => (fgb: Kind>) => Kind> +): { + (refinement: Refinement): (fga: Kind>) => Kind> + (predicate: Predicate): (fgb: Kind>) => Kind> +} export declare function filter( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fgb: Kind>) => Kind> +): { + (refinement: Refinement): (fga: Kind>) => Kind> + (predicate: Predicate): (fgb: Kind>) => Kind> +} export declare function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fgb: HKT>) => HKT> +): { + (refinement: Refinement): (fga: HKT>) => HKT> + (predicate: Predicate): (fgb: HKT>) => HKT> +} ``` Added in v2.10.0 @@ -123,29 +135,45 @@ Added in v2.10.0 export declare function partition( F: Functor2, G: Filterable2C -): ( - predicate: Predicate -) => ( - fgb: Kind2> -) => Separated>, Kind2>> +): { + (refinement: Refinement): ( + fga: Kind2> + ) => Separated>, Kind2>> + (predicate: Predicate): ( + fgb: Kind2> + ) => Separated>, Kind2>> +} export declare function partition( F: Functor1, G: Filterable2C -): ( - predicate: Predicate -) => (fgb: Kind>) => Separated>, Kind>> +): { + (refinement: Refinement): ( + fga: Kind> + ) => Separated>, Kind>> + (predicate: Predicate): ( + fgb: Kind> + ) => Separated>, Kind>> +} export declare function partition( F: Functor1, G: Filterable1 -): ( - predicate: Predicate -) => (fgb: Kind>) => Separated>, Kind>> +): { + (refinement: Refinement): ( + fga: Kind> + ) => Separated>, Kind>> + (predicate: Predicate): ( + fgb: Kind> + ) => Separated>, Kind>> +} export declare function partition( F: Functor, G: Filterable -): ( - predicate: Predicate -) => (fgb: HKT>) => Separated>, HKT>> +): { + (refinement: Refinement): ( + fga: HKT> + ) => Separated>, HKT>> + (predicate: Predicate): (fgb: HKT>) => Separated>, HKT>> +} ``` Added in v2.10.0 diff --git a/src/Filterable.ts b/src/Filterable.ts index 6c5d20db7..0e9be07fd 100644 --- a/src/Filterable.ts +++ b/src/Filterable.ts @@ -290,19 +290,31 @@ export interface Filterable4 extends Functor4, Compactable4< export function filter( F: Functor2, G: Filterable2C -): (predicate: Predicate) => (fgb: Kind2>) => Kind2> +): { + (refinement: Refinement): (fga: Kind2>) => Kind2> + (predicate: Predicate): (fgb: Kind2>) => Kind2> +} export function filter( F: Functor1, G: Filterable2C -): (predicate: Predicate) => (fgb: Kind>) => Kind> +): { + (refinement: Refinement): (fga: Kind>) => Kind> + (predicate: Predicate): (fgb: Kind>) => Kind> +} export function filter( F: Functor1, G: Filterable1 -): (predicate: Predicate) => (fgb: Kind>) => Kind> +): { + (refinement: Refinement): (fga: Kind>) => Kind> + (predicate: Predicate): (fgb: Kind>) => Kind> +} export function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fgb: HKT>) => HKT> +): { + (refinement: Refinement): (fga: HKT>) => HKT> + (predicate: Predicate): (fgb: HKT>) => HKT> +} export function filter( F: Functor, G: Filterable @@ -348,29 +360,45 @@ export function filterMap( export function partition( F: Functor2, G: Filterable2C -): ( - predicate: Predicate -) => ( - fgb: Kind2> -) => Separated>, Kind2>> +): { + (refinement: Refinement): ( + fga: Kind2> + ) => Separated>, Kind2>> + (predicate: Predicate): ( + fgb: Kind2> + ) => Separated>, Kind2>> +} export function partition( F: Functor1, G: Filterable2C -): ( - predicate: Predicate -) => (fgb: Kind>) => Separated>, Kind>> +): { + (refinement: Refinement): ( + fga: Kind> + ) => Separated>, Kind>> + (predicate: Predicate): ( + fgb: Kind> + ) => Separated>, Kind>> +} export function partition( F: Functor1, G: Filterable1 -): ( - predicate: Predicate -) => (fgb: Kind>) => Separated>, Kind>> +): { + (refinement: Refinement): ( + fga: Kind> + ) => Separated>, Kind>> + (predicate: Predicate): ( + fgb: Kind> + ) => Separated>, Kind>> +} export function partition( F: Functor, G: Filterable -): ( - predicate: Predicate -) => (fgb: HKT>) => Separated>, HKT>> +): { + (refinement: Refinement): ( + fga: HKT> + ) => Separated>, HKT>> + (predicate: Predicate): (fgb: HKT>) => Separated>, HKT>> +} export function partition( F: Functor, G: Filterable From 50b6e9ef2c388b8f1d6b7f7592eb530e21a2b2f6 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Sat, 24 Apr 2021 18:21:31 +0200 Subject: [PATCH 097/162] Add FromReader.chainFirstReaderK --- docs/modules/FromReader.ts.md | 30 +++++++++++++++++++++ docs/modules/ReaderEither.ts.md | 28 ++++++++++++++++++++ docs/modules/ReaderTask.ts.md | 28 ++++++++++++++++++++ docs/modules/ReaderTaskEither.ts.md | 28 ++++++++++++++++++++ docs/modules/StateReaderTaskEither.ts.md | 28 ++++++++++++++++++++ src/FromReader.ts | 33 +++++++++++++++++++++++- src/ReaderEither.ts | 21 +++++++++++++++ src/ReaderTask.ts | 19 ++++++++++++++ src/ReaderTaskEither.ts | 21 +++++++++++++++ src/StateReaderTaskEither.ts | 23 +++++++++++++++++ test/ReaderEither.ts | 11 ++++++++ 11 files changed, 269 insertions(+), 1 deletion(-) diff --git a/docs/modules/FromReader.ts.md b/docs/modules/FromReader.ts.md index a171d30ec..0aeb7ae27 100644 --- a/docs/modules/FromReader.ts.md +++ b/docs/modules/FromReader.ts.md @@ -15,6 +15,7 @@ Added in v2.11.0

Table of contents

- [combinators](#combinators) + - [chainFirstReaderK](#chainfirstreaderk) - [chainReaderK](#chainreaderk) - [fromReaderK](#fromreaderk) - [constructors](#constructors) @@ -31,6 +32,35 @@ Added in v2.11.0 # combinators +## chainFirstReaderK + +**Signature** + +```ts +export declare function chainFirstReaderK( + F: FromReader4, + M: Chain4 +): (f: (a: A) => Reader) => (ma: Kind4) => Kind4 +export declare function chainFirstReaderK( + F: FromReader3, + M: Chain3 +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export declare function chainFirstReaderK( + F: FromReader3C, + M: Chain3C +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export declare function chainFirstReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 +export declare function chainFirstReaderK( + F: FromReader, + M: Chain +): (f: (a: A) => Reader) => (ma: HKT2) => HKT2 +``` + +Added in v2.11.0 + ## chainReaderK **Signature** diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 72152fd10..ce5ccff36 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -38,6 +38,8 @@ Added in v2.0.0 - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstW](#chainfirstw) - [chainOptionK](#chainoptionk) - [chainReaderK](#chainreaderk) @@ -375,6 +377,32 @@ export declare const chainFirst: ( Added in v2.0.0 +## chainFirstReaderK + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Less strict version of [`chainReaderK`](#chainReaderK). + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + ## chainFirstW Less strict version of [`chainFirst`](#chainfirst) diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 44918f59b..db14703ff 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -29,6 +29,8 @@ Added in v2.3.0 - [asksEW](#asksew) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainIOK](#chainiok) @@ -249,6 +251,32 @@ export declare const chainFirstIOK: (f: (a: A) => IO) => (first: Rea Added in v2.10.0 +## chainFirstReaderK + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainFirstTaskK **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index bd84f0902..1d96a3d90 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -39,6 +39,8 @@ Added in v2.0.0 - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainIOEitherK](#chainioeitherk) @@ -429,6 +431,32 @@ export declare const chainFirstIOK: ( Added in v2.10.0 +## chainFirstReaderK + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index f63e2e8af..7609857ee 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -39,6 +39,8 @@ Added in v2.0.0 - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainIOEitherK](#chainioeitherk) @@ -414,6 +416,32 @@ export declare const chainFirstIOK: ( Added in v2.10.0 +## chainFirstReaderK + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK **Signature** diff --git a/src/FromReader.ts b/src/FromReader.ts index cb4d14349..f0476c144 100644 --- a/src/FromReader.ts +++ b/src/FromReader.ts @@ -3,7 +3,7 @@ * * @since 2.11.0 */ -import { Chain, Chain2, Chain3, Chain3C, Chain4 } from './Chain' +import { Chain, Chain2, Chain3, Chain3C, Chain4, chainFirst } from './Chain' import { flow } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' import * as R from './Reader' @@ -150,3 +150,34 @@ export function chainReaderK( const fromReaderKF = fromReaderK(F) return (f) => (ma) => M.chain(ma, fromReaderKF(f)) } + +/** + * @category combinators + * @since 2.11.0 + */ +export function chainFirstReaderK( + F: FromReader4, + M: Chain4 +): (f: (a: A) => Reader) => (ma: Kind4) => Kind4 +export function chainFirstReaderK( + F: FromReader3, + M: Chain3 +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export function chainFirstReaderK( + F: FromReader3C, + M: Chain3C +): (f: (a: A) => Reader) => (ma: Kind3) => Kind3 +export function chainFirstReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 +export function chainFirstReaderK( + F: FromReader, + M: Chain +): (f: (a: A) => Reader) => (ma: HKT2) => HKT2 +export function chainFirstReaderK( + F: FromReader2, + M: Chain2 +): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 { + return flow(fromReaderK(F), chainFirst(M)) +} diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index e1a9020eb..c59cb07e7 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -37,6 +37,7 @@ import { ask as ask_, asks as asks_, chainReaderK as chainReaderK_, + chainFirstReaderK as chainFirstReaderK_, FromReader3, fromReaderK as fromReaderK_ } from './FromReader' @@ -718,6 +719,26 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderK: ( + f: (a: A) => Reader +) => (ma: ReaderEither) => ReaderEither = + /*#__PURE__*/ + chainFirstReaderK_(FromReader, Chain) + +/** + * Less strict version of [`chainReaderK`](#chainReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderKW: ( + f: (a: A) => Reader +) => (ma: ReaderEither) => ReaderEither = chainFirstReaderK as any + /** * @category instances * @since 2.7.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index d7d12a647..636ccec20 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -15,6 +15,7 @@ import { ask as ask_, asks as asks_, chainReaderK as chainReaderK_, + chainFirstReaderK as chainFirstReaderK_, FromReader2, fromReaderK as fromReaderK_ } from './FromReader' @@ -460,6 +461,24 @@ export const chainReaderK = /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderK = + /*#__PURE__*/ + chainFirstReaderK_(FromReader, Chain) + +/** + * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask = chainFirstReaderK as any + /** * @category instances * @since 2.10.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c40e404ac..ca3025c4e 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -39,6 +39,7 @@ import { ask as ask_, asks as asks_, chainReaderK as chainReaderK_, + chainFirstReaderK as chainFirstReaderK_, FromReader3, fromReaderK as fromReaderK_ } from './FromReader' @@ -942,6 +943,26 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither = + /*#__PURE__*/ + chainFirstReaderK_(FromReader, Chain) + +/** + * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainFirstReaderK as any + /** * @category combinators * @since 2.11.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 3b0390f3e..58a4f830b 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -23,6 +23,7 @@ import { ask as ask_, asks as asks_, chainReaderK as chainReaderK_, + chainFirstReaderK as chainFirstReaderK_, FromReader4, fromReaderK as fromReaderK_ } from './FromReader' @@ -833,6 +834,28 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderK: ( + f: (a: A) => Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = + /*#__PURE__*/ + chainFirstReaderK_(FromReader, Chain) + +/** + * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderKW: ( + f: (a: A) => Reader +) => ( + ma: StateReaderTaskEither +) => StateReaderTaskEither = chainFirstReaderK as any + /** * @category instances * @since 2.10.0 diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index bc5660b1b..8d8c5173b 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -304,4 +304,15 @@ describe('ReaderEither', () => { U.deepStrictEqual(pipe(_.right(3), f)(2), E.right(6)) U.deepStrictEqual(pipe(_.left('a'), f)(2), E.left('a')) }) + + it('chainFirstReaderK', () => { + const f = _.chainFirstReaderK((n: number): R.Reader => (c) => n * c) + U.deepStrictEqual(pipe(_.right(3), f)(2), E.right(3)) + U.deepStrictEqual(pipe(_.left('a'), f)(2), E.left('a')) + }) + + it('chainFirstReaderKW', () => { + const f = _.chainFirstReaderKW((): R.Reader => () => 2) + U.deepStrictEqual(pipe(_.right<{}, never, number>(3), f)({}), E.right(3)) + }) }) From 6760744404e0e9bea8c79c299fa36d3d21d20c1b Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Sat, 24 Apr 2021 18:27:58 +0200 Subject: [PATCH 098/162] Add some chainReaderKW --- docs/modules/ReaderEither.ts.md | 15 +++++++++++++++ docs/modules/ReaderTask.ts.md | 15 +++++++++++++++ docs/modules/ReaderTaskEither.ts.md | 15 +++++++++++++++ docs/modules/StateReaderTaskEither.ts.md | 15 +++++++++++++++ src/ReaderEither.ts | 10 ++++++++++ src/ReaderTask.ts | 10 ++++++++++ src/ReaderTaskEither.ts | 10 ++++++++++ src/StateReaderTaskEither.ts | 12 ++++++++++++ test/ReaderEither.ts | 5 +++++ 9 files changed, 107 insertions(+) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index ce5ccff36..86fd864e8 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -43,6 +43,7 @@ Added in v2.0.0 - [chainFirstW](#chainfirstw) - [chainOptionK](#chainoptionk) - [chainReaderK](#chainreaderk) + - [chainReaderKW](#chainreaderkw) - [filterOrElse](#filterorelse) - [filterOrElseW](#filterorelsew) - [flap](#flap) @@ -443,6 +444,20 @@ export declare const chainReaderK: ( Added in v2.11.0 +## chainReaderKW + +Less strict version of [`chainReaderK`](#chainReaderK). + +**Signature** + +```ts +export declare const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + ## filterOrElse **Signature** diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index db14703ff..1c266bc4e 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -35,6 +35,7 @@ Added in v2.3.0 - [chainFirstW](#chainfirstw) - [chainIOK](#chainiok) - [chainReaderK](#chainreaderk) + - [chainReaderKW](#chainreaderkw) - [chainTaskK](#chaintaskk) - [flap](#flap) - [flatten](#flatten) @@ -323,6 +324,20 @@ export declare const chainReaderK: (f: (a: A) => R.Reader) => (ma Added in v2.11.0 +## chainReaderKW + +Less strict version of [`chainReaderK`](#chainReaderK). + +**Signature** + +```ts +export declare const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainTaskK **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 1d96a3d90..49f4a4f59 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -48,6 +48,7 @@ Added in v2.0.0 - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) - [chainReaderK](#chainreaderk) + - [chainReaderKW](#chainreaderkw) - [chainReaderTaskK](#chainreadertaskk) - [chainReaderTaskKW](#chainreadertaskkw) - [chainTaskEitherK](#chaintaskeitherk) @@ -547,6 +548,20 @@ export declare const chainReaderK: ( Added in v2.11.0 +## chainReaderKW + +Less strict version of [`chainReaderK`](#chainReaderK). + +**Signature** + +```ts +export declare const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainReaderTaskK **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 7609857ee..5d7f02921 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -48,6 +48,7 @@ Added in v2.0.0 - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) - [chainReaderK](#chainreaderk) + - [chainReaderKW](#chainreaderkw) - [chainReaderTaskEitherK](#chainreadertaskeitherk) - [chainReaderTaskEitherKW](#chainreadertaskeitherkw) - [chainStateK](#chainstatek) @@ -534,6 +535,20 @@ export declare const chainReaderK: ( Added in v2.11.0 +## chainReaderKW + +Less strict version of [`chainReaderK`](#chainReaderK). + +**Signature** + +```ts +export declare const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainReaderTaskEitherK **Signature** diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index c59cb07e7..59a1e7217 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -719,6 +719,16 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * Less strict version of [`chainReaderK`](#chainReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderKW: ( + f: (a: A) => Reader +) => (ma: ReaderEither) => ReaderEither = chainReaderK as any + /** * @category combinators * @since 2.11.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 636ccec20..8d0ca0467 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -461,6 +461,16 @@ export const chainReaderK = /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * Less strict version of [`chainReaderK`](#chainReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask = chainReaderK as any + /** * @category combinators * @since 2.11.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index ca3025c4e..0b5edd06f 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -943,6 +943,16 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * Less strict version of [`chainReaderK`](#chainReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderK as any + /** * @category combinators * @since 2.11.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 58a4f830b..2b58be5d9 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -834,6 +834,18 @@ export const chainReaderK: ( /*#__PURE__*/ chainReaderK_(FromReader, Chain) +/** + * Less strict version of [`chainReaderK`](#chainReaderK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderKW: ( + f: (a: A) => Reader +) => ( + ma: StateReaderTaskEither +) => StateReaderTaskEither = chainReaderK as any + /** * @category combinators * @since 2.11.0 diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 8d8c5173b..8e1311f9a 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -305,6 +305,11 @@ describe('ReaderEither', () => { U.deepStrictEqual(pipe(_.left('a'), f)(2), E.left('a')) }) + it('chainReaderKW', () => { + const f = _.chainReaderKW((): R.Reader => () => 2) + U.deepStrictEqual(pipe(_.right<{}, never, number>(3), f)({}), E.right(2)) + }) + it('chainFirstReaderK', () => { const f = _.chainFirstReaderK((n: number): R.Reader => (c) => n * c) U.deepStrictEqual(pipe(_.right(3), f)(2), E.right(3)) From 28925b310021b6e8da2008f098f2bdec0bd7629e Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Sat, 24 Apr 2021 18:33:06 +0200 Subject: [PATCH 099/162] Add RTE.chainFirstReaderTaskK(W) --- docs/modules/ReaderTaskEither.ts.md | 28 ++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 19 +++++++++++++++++++ test/ReaderTaskEither.ts | 5 +++++ 3 files changed, 52 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 49f4a4f59..108daaa89 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -41,6 +41,8 @@ Added in v2.0.0 - [chainFirstIOK](#chainfirstiok) - [chainFirstReaderK](#chainfirstreaderk) - [chainFirstReaderKW](#chainfirstreaderkw) + - [chainFirstReaderTaskK](#chainfirstreadertaskk) + - [chainFirstReaderTaskKW](#chainfirstreadertaskkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainIOEitherK](#chainioeitherk) @@ -458,6 +460,32 @@ export declare const chainFirstReaderKW: ( Added in v2.11.0 +## chainFirstReaderTaskK + +**Signature** + +```ts +export declare const chainFirstReaderTaskK: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstReaderTaskKW + +Less strict version of [`chainFirstReaderTaskK`](#chainFirstReaderTaskK). + +**Signature** + +```ts +export declare const chainFirstReaderTaskKW: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 0b5edd06f..7a544859c 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1000,6 +1000,25 @@ export const chainReaderTaskK: ( f: (a: A) => RT.ReaderTask ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderTaskKW +/** + * Less strict version of [`chainFirstReaderTaskK`](#chainFirstReaderTaskK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderTaskKW: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => + chainFirstW(fromReaderTaskK(f)) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderTaskK: ( + f: (a: A) => RT.ReaderTask +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainFirstReaderTaskKW + /** * @category instances * @since 2.10.0 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 0f0d6363e..2aa884043 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -423,6 +423,11 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainReaderTaskKW(f))({})(), E.right(1)) }) + it('chainFirstReaderTaskKW', async () => { + const f = flow(S.size, RT.of) + U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainFirstReaderTaskKW(f))({})(), E.right('a')) + }) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- From ee2aa062380ec8950440fdb480335be81a4daea6 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Sat, 24 Apr 2021 18:35:23 +0200 Subject: [PATCH 100/162] Fix a bad version number for Array.matchLeftW --- docs/modules/Array.ts.md | 2 +- src/Array.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 5895e42f4..7d6225321 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -1851,7 +1851,7 @@ export declare const matchLeftW: ( ) => (as: A[]) => B | C ``` -Added in v3.0.0 +Added in v2.11.0 ## matchRight diff --git a/src/Array.ts b/src/Array.ts index 855cacc6f..a3784b366 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -201,7 +201,7 @@ export const match: (onEmpty: Lazy, onNonEmpty: (as: NonEmptyArray
) * Less strict version of [`matchLeft`](#matchLeft). * * @category destructors - * @since 3.0.0 + * @since 2.11.0 */ export const matchLeftW = (onEmpty: Lazy, onNonEmpty: (head: A, tail: Array) => C) => ( as: Array From 851f7f280262862b8d28d171b220a3ea945e8676 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Sat, 24 Apr 2021 18:42:40 +0200 Subject: [PATCH 101/162] Update changelog --- CHANGELOG.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 326c4c905..50e8f07fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,15 +140,21 @@ high state of flux, you're at risk of it changing without notice. - add `asksEW`, `asksW` - add `orElseFirst` / `orElseFirstW` - add `orLeft` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` - `ReaderTask` - add `asksEW`, `asksW` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` - `ReaderTaskEither` - add `asksEW`, `asksW` - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `fromReaderTaskK` - - add `chainReaderTaskK` - - add `chainReaderTaskKW` + - add `chainReaderKW` + - add `chainReaderTaskK`, `chainReaderTaskKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `chainFirstReaderTaskK`, `chainFirstReaderTaskKW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -216,6 +222,8 @@ high state of flux, you're at risk of it changing without notice. - add `chainStateK` - add `local` - add `asksEW`, `asksW` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` - `string` - add `toUpperCase` - `struct` From ee7d76e51aafc669520ef0890429dc358e4a142d Mon Sep 17 00:00:00 2001 From: gcanti Date: Sun, 25 Apr 2021 09:13:45 +0200 Subject: [PATCH 102/162] Option: deprecate getFirstMonoid, getLastMonoid --- CHANGELOG.md | 2 + docs/modules/Option.ts.md | 152 ++++++++++++++++++++++---------------- src/Option.ts | 134 ++++++++++++++++++--------------- test/Option.ts | 6 +- 4 files changed, 167 insertions(+), 127 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e8f07fc..4c2870906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ high state of flux, you're at risk of it changing without notice. - deprecate `groupSort` (it's just `sort` followed by `group`) - `Option` - deprecate `getRefinement`, use `Refinement` module instead. + - deprecate `getFirstMonoid`, use `getMonoid` module instead. + - deprecate `getLastMonoid`, use `getMonoid` module instead. - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 3caf23b7a..aadecf7dd 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -102,13 +102,13 @@ Added in v2.0.0 - [URI (type alias)](#uri-type-alias) - [Witherable](#witherable-1) - [getEq](#geteq) - - [getFirstMonoid](#getfirstmonoid) - - [getLastMonoid](#getlastmonoid) - [getMonoid](#getmonoid) - [getOrd](#getord) - [getShow](#getshow) - [~~getApplyMonoid~~](#getapplymonoid) - [~~getApplySemigroup~~](#getapplysemigroup) + - [~~getFirstMonoid~~](#getfirstmonoid) + - [~~getLastMonoid~~](#getlastmonoid) - [~~option~~](#option) - [interop](#interop) - [chainNullableK](#chainnullablek) @@ -982,68 +982,6 @@ assert.strictEqual(E.equals(some(1), some(1)), true) Added in v2.0.0 -## getFirstMonoid - -Monoid returning the left-most non-`None` value - -| x | y | concat(x, y) | -| ------- | ------- | ------------ | -| none | none | none | -| some(a) | none | some(a) | -| none | some(a) | some(a) | -| some(a) | some(b) | some(a) | - -**Signature** - -```ts -export declare function getFirstMonoid(): Monoid> -``` - -**Example** - -```ts -import { getFirstMonoid, some, none } from 'fp-ts/Option' - -const M = getFirstMonoid() -assert.deepStrictEqual(M.concat(none, none), none) -assert.deepStrictEqual(M.concat(some(1), none), some(1)) -assert.deepStrictEqual(M.concat(none, some(1)), some(1)) -assert.deepStrictEqual(M.concat(some(1), some(2)), some(1)) -``` - -Added in v2.0.0 - -## getLastMonoid - -Monoid returning the right-most non-`None` value - -| x | y | concat(x, y) | -| ------- | ------- | ------------ | -| none | none | none | -| some(a) | none | some(a) | -| none | some(a) | some(a) | -| some(a) | some(b) | some(b) | - -**Signature** - -```ts -export declare function getLastMonoid(): Monoid> -``` - -**Example** - -```ts -import { getLastMonoid, some, none } from 'fp-ts/Option' - -const M = getLastMonoid() -assert.deepStrictEqual(M.concat(none, none), none) -assert.deepStrictEqual(M.concat(some(1), none), some(1)) -assert.deepStrictEqual(M.concat(none, some(1)), some(1)) -assert.deepStrictEqual(M.concat(some(1), some(2)), some(2)) -``` - -Added in v2.0.0 - ## getMonoid Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are @@ -1053,7 +991,7 @@ concatenated using the provided `Semigroup` | ------- | ------- | ------------------ | | none | none | none | | some(a) | none | some(a) | -| none | some(a) | some(a) | +| none | some(b) | some(b) | | some(a) | some(b) | some(concat(a, b)) | **Signature** @@ -1141,6 +1079,90 @@ export declare const getApplySemigroup: (S: Semigroup) => Semigroup() => Monoid> +``` + +**Example** + +```ts +import { getLastMonoid, some, none } from 'fp-ts/Option' + +const M = getLastMonoid() +assert.deepStrictEqual(M.concat(none, none), none) +assert.deepStrictEqual(M.concat(some(1), none), some(1)) +assert.deepStrictEqual(M.concat(none, some(2)), some(2)) +assert.deepStrictEqual(M.concat(some(1), some(2)), some(2)) +``` + +Added in v2.0.0 + ## ~~option~~ Use small, specific instances instead. diff --git a/src/Option.ts b/src/Option.ts index 3d463c81c..4619ba751 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -41,7 +41,7 @@ import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { not, Predicate } from './Predicate' import { Refinement } from './Refinement' -import { Semigroup } from './Semigroup' +import { Semigroup, first, last } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' @@ -808,64 +808,6 @@ export function getOrd(O: Ord): Ord> { } } -/** - * Monoid returning the left-most non-`None` value - * - * | x | y | concat(x, y) | - * | ------- | ------- | ------------ | - * | none | none | none | - * | some(a) | none | some(a) | - * | none | some(a) | some(a) | - * | some(a) | some(b) | some(a) | - * - * @example - * import { getFirstMonoid, some, none } from 'fp-ts/Option' - * - * const M = getFirstMonoid() - * assert.deepStrictEqual(M.concat(none, none), none) - * assert.deepStrictEqual(M.concat(some(1), none), some(1)) - * assert.deepStrictEqual(M.concat(none, some(1)), some(1)) - * assert.deepStrictEqual(M.concat(some(1), some(2)), some(1)) - * - * @category instances - * @since 2.0.0 - */ -export function getFirstMonoid(): Monoid> { - return { - concat: (x, y) => (isNone(x) ? y : x), - empty: none - } -} - -/** - * Monoid returning the right-most non-`None` value - * - * | x | y | concat(x, y) | - * | ------- | ------- | ------------ | - * | none | none | none | - * | some(a) | none | some(a) | - * | none | some(a) | some(a) | - * | some(a) | some(b) | some(b) | - * - * @example - * import { getLastMonoid, some, none } from 'fp-ts/Option' - * - * const M = getLastMonoid() - * assert.deepStrictEqual(M.concat(none, none), none) - * assert.deepStrictEqual(M.concat(some(1), none), some(1)) - * assert.deepStrictEqual(M.concat(none, some(1)), some(1)) - * assert.deepStrictEqual(M.concat(some(1), some(2)), some(2)) - * - * @category instances - * @since 2.0.0 - */ -export function getLastMonoid(): Monoid> { - return { - concat: (x, y) => (isNone(y) ? x : y), - empty: none - } -} - /** * Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are * concatenated using the provided `Semigroup` @@ -874,7 +816,7 @@ export function getLastMonoid(): Monoid> { * | ------- | ------- | ------------------ | * | none | none | none | * | some(a) | none | some(a) | - * | none | some(a) | some(a) | + * | none | some(b) | some(b) | * | some(a) | some(b) | some(concat(a, b)) | * * @example @@ -1370,3 +1312,75 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup> = export const getApplyMonoid: (M: Monoid) => Monoid> = /*#__PURE__*/ getApplicativeMonoid(Applicative) + +/** + * Use + * + * ```ts + * import { first } from 'fp-ts/Semigroup' + * import { getMonoid } from 'fp-ts/Option' + * + * getMonoid(first()) + * ``` + * + * instead. + * + * Monoid returning the left-most non-`None` value + * + * | x | y | concat(x, y) | + * | ------- | ------- | ------------ | + * | none | none | none | + * | some(a) | none | some(a) | + * | none | some(b) | some(b) | + * | some(a) | some(b) | some(a) | + * + * @example + * import { getFirstMonoid, some, none } from 'fp-ts/Option' + * + * const M = getFirstMonoid() + * assert.deepStrictEqual(M.concat(none, none), none) + * assert.deepStrictEqual(M.concat(some(1), none), some(1)) + * assert.deepStrictEqual(M.concat(none, some(2)), some(2)) + * assert.deepStrictEqual(M.concat(some(1), some(2)), some(1)) + * + * @category instances + * @since 2.0.0 + * @deprecated + */ +export const getFirstMonoid = (): Monoid> => getMonoid(first()) + +/** + * Use + * + * ```ts + * import { last } from 'fp-ts/Semigroup' + * import { getMonoid } from 'fp-ts/Option' + * + * getMonoid(last()) + * ``` + * + * instead. + * + * Monoid returning the right-most non-`None` value + * + * | x | y | concat(x, y) | + * | ------- | ------- | ------------ | + * | none | none | none | + * | some(a) | none | some(a) | + * | none | some(b) | some(b) | + * | some(a) | some(b) | some(b) | + * + * @example + * import { getLastMonoid, some, none } from 'fp-ts/Option' + * + * const M = getLastMonoid() + * assert.deepStrictEqual(M.concat(none, none), none) + * assert.deepStrictEqual(M.concat(some(1), none), some(1)) + * assert.deepStrictEqual(M.concat(none, some(2)), some(2)) + * assert.deepStrictEqual(M.concat(some(1), some(2)), some(2)) + * + * @category instances + * @since 2.0.0 + * @deprecated + */ +export const getLastMonoid = (): Monoid> => getMonoid(last()) diff --git a/test/Option.ts b/test/Option.ts index 049636591..125f0a763 100644 --- a/test/Option.ts +++ b/test/Option.ts @@ -370,18 +370,20 @@ describe('Option', () => { }) it('getFirstMonoid', () => { + // tslint:disable-next-line: deprecation const M = _.getFirstMonoid() U.deepStrictEqual(M.concat(_.none, _.none), _.none) U.deepStrictEqual(M.concat(_.some(1), _.none), _.some(1)) - U.deepStrictEqual(M.concat(_.none, _.some(1)), _.some(1)) + U.deepStrictEqual(M.concat(_.none, _.some(2)), _.some(2)) U.deepStrictEqual(M.concat(_.some(1), _.some(2)), _.some(1)) }) it('getLastMonoid', () => { + // tslint:disable-next-line: deprecation const M = _.getLastMonoid() U.deepStrictEqual(M.concat(_.none, _.none), _.none) U.deepStrictEqual(M.concat(_.some(1), _.none), _.some(1)) - U.deepStrictEqual(M.concat(_.none, _.some(1)), _.some(1)) + U.deepStrictEqual(M.concat(_.none, _.some(2)), _.some(2)) U.deepStrictEqual(M.concat(_.some(1), _.some(2)), _.some(2)) }) From 6d4bab52625ecabff6f9081ba3e5daf1d1c1f923 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 27 Apr 2021 14:38:07 +0200 Subject: [PATCH 103/162] Add RTE.fromReaderEitherK and RTE.chainReaderEitherK(W) --- docs/modules/ReaderTaskEither.ts.md | 41 +++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 27 +++++++++++++++++++ test/ReaderTaskEither.ts | 5 ++++ 3 files changed, 73 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 108daaa89..d33e52f1d 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -49,6 +49,8 @@ Added in v2.0.0 - [chainIOEitherKW](#chainioeitherkw) - [chainIOK](#chainiok) - [chainOptionK](#chainoptionk) + - [chainReaderEitherK](#chainreadereitherk) + - [chainReaderEitherKW](#chainreadereitherkw) - [chainReaderK](#chainreaderk) - [chainReaderKW](#chainreaderkw) - [chainReaderTaskK](#chainreadertaskk) @@ -64,6 +66,7 @@ Added in v2.0.0 - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) + - [fromReaderEitherK](#fromreadereitherk) - [fromReaderK](#fromreaderk) - [fromReaderTaskK](#fromreadertaskk) - [fromTaskEitherK](#fromtaskeitherk) @@ -564,6 +567,32 @@ export declare const chainOptionK: ( Added in v2.10.0 +## chainReaderEitherK + +**Signature** + +```ts +export declare const chainReaderEitherK: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainReaderEitherKW + +Less strict version of [`chainReaderEitherK`](#chainReaderEitherK). + +**Signature** + +```ts +export declare const chainReaderEitherKW: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainReaderK **Signature** @@ -762,6 +791,18 @@ export declare const fromOptionK: ( Added in v2.10.0 +## fromReaderEitherK + +**Signature** + +```ts +export declare const fromReaderEitherK: ( + f: (...a: A) => ReaderEither +) => (...a: A) => ReaderTaskEither +``` + +Added in v2.11.0 + ## fromReaderK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 7a544859c..4d7c91b8e 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -453,6 +453,33 @@ export const chainTaskEitherK: ( f: (a: A) => TaskEither ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainTaskEitherKW +/** + * @category combinators + * @since 2.11.0 + */ +export const fromReaderEitherK = , B>( + f: (...a: A) => ReaderEither +): ((...a: A) => ReaderTaskEither) => flow(f, fromReaderEither) + +/** + * Less strict version of [`chainReaderEitherK`](#chainReaderEitherK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainReaderEitherKW: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => + chainW(fromReaderEitherK(f)) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainReaderEitherK: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderEitherKW + // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 2aa884043..28e084297 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -428,6 +428,11 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainFirstReaderTaskKW(f))({})(), E.right('a')) }) + it('chainReaderEitherKW', async () => { + const f = (s: string) => RE.right(s.length) + U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainReaderEitherKW(f))({})(), E.right(1)) + }) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- From 6c1f0499b03ede4e5512359e711a4b474d519301 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 27 Apr 2021 14:47:17 +0200 Subject: [PATCH 104/162] Add RTE.chainFirstReaderEitherK(W) --- docs/modules/ReaderTaskEither.ts.md | 28 ++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 19 +++++++++++++++++++ test/ReaderTaskEither.ts | 5 +++++ 3 files changed, 52 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index d33e52f1d..b31855929 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -39,6 +39,8 @@ Added in v2.0.0 - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderEitherK](#chainfirstreadereitherk) + - [chainFirstReaderEitherKW](#chainfirstreadereitherkw) - [chainFirstReaderK](#chainfirstreaderk) - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstReaderTaskK](#chainfirstreadertaskk) @@ -437,6 +439,32 @@ export declare const chainFirstIOK: ( Added in v2.10.0 +## chainFirstReaderEitherK + +**Signature** + +```ts +export declare const chainFirstReaderEitherK: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstReaderEitherKW + +Less strict version of [`chainFirstReaderEitherK`](#chainFirstReaderEitherK). + +**Signature** + +```ts +export declare const chainFirstReaderEitherKW: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstReaderK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 4d7c91b8e..2a86e4921 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -480,6 +480,25 @@ export const chainReaderEitherK: ( f: (a: A) => ReaderEither ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderEitherKW +/** + * Less strict version of [`chainFirstReaderEitherK`](#chainFirstReaderEitherK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderEitherKW: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => + chainFirstW(fromReaderEitherK(f)) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstReaderEitherK: ( + f: (a: A) => ReaderEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainFirstReaderEitherKW + // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 28e084297..14b21d61a 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -433,6 +433,11 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainReaderEitherKW(f))({})(), E.right(1)) }) + it('chainFirstReaderEitherKW', async () => { + const f = (s: string) => RE.right(s.length) + U.deepStrictEqual(await pipe(_.right<{}, never, string>('a'), _.chainFirstReaderEitherKW(f))({})(), E.right('a')) + }) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- From 82a3987ba72b5b3151e0bfc920379cbcf59dd1f2 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Tue, 27 Apr 2021 14:47:31 +0200 Subject: [PATCH 105/162] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c2870906..d266251ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -153,10 +153,13 @@ high state of flux, you're at risk of it changing without notice. - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `fromReaderTaskK` + - add `fromReaderEitherK` - add `chainReaderKW` - add `chainReaderTaskK`, `chainReaderTaskKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `chainFirstReaderTaskK`, `chainFirstReaderTaskKW` + - add `chainReaderEitherK`, `chainReaderEitherKW` + - add `chainFirstReaderEitherK`, `chainFirstReaderEitherKW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` From 03e7d25a7758f612dc7fc18354b2760194709742 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Sun, 25 Apr 2021 19:53:17 +0300 Subject: [PATCH 106/162] Add Either.flattenW -- less strict version of Either.flatten --- docs/modules/Either.ts.md | 13 +++++++++++++ src/Either.ts | 10 ++++++++++ test/Either.ts | 4 ++++ 3 files changed, 27 insertions(+) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 273c4b373..35eee7cf4 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -62,6 +62,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromOptionK](#fromoptionk) - [orElse](#orelse) - [orElseW](#orelsew) @@ -620,6 +621,18 @@ assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e')) Added in v2.0.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: (mma: Either>) => Either +``` + +Added in v2.11.0 + ## fromOptionK **Signature** diff --git a/src/Either.ts b/src/Either.ts index fdd5219c2..cb1b703fc 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -482,6 +482,16 @@ export const flatten: (mma: Either>) => Either = /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: Either>) => Either = + /*#__PURE__*/ + chainW(identity) + /** * Less strict version of [`alt`](#alt). * diff --git a/test/Either.ts b/test/Either.ts index 215ddab23..08f51bb94 100644 --- a/test/Either.ts +++ b/test/Either.ts @@ -114,6 +114,10 @@ describe('Either', () => { U.deepStrictEqual(pipe(_.right(_.right('a')), _.flatten), _.right('a')) }) + it('flattenW', () => { + U.deepStrictEqual(pipe(_.right<'left1', _.Either<'left2', 'a'>>(_.right('a')), _.flattenW), _.right('a')) + }) + it('bimap', () => { const f = (s: string): number => s.length const g = (n: number): boolean => n > 2 From ab640a4c737d1d167d1283d263e9a2ec060e817e Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 17:56:48 +0300 Subject: [PATCH 107/162] Add IOEither.flattenW --- docs/modules/IOEither.ts.md | 13 +++++++++++++ src/IOEither.ts | 10 ++++++++++ test/IOEither.ts | 4 ++++ 3 files changed, 27 insertions(+) diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 52153536d..3c3047ef9 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -47,6 +47,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromEitherK](#fromeitherk) - [fromIOK](#fromiok) - [fromOptionK](#fromoptionk) @@ -444,6 +445,18 @@ export declare const flatten: (mma: IOEither>) => IOEith Added in v2.0.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: (mma: IOEither>) => IOEither +``` + +Added in v2.11.0 + ## fromEitherK **Signature** diff --git a/src/IOEither.ts b/src/IOEither.ts index 2886ea0c4..b120bcd75 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -387,6 +387,16 @@ export const flatten: (mma: IOEither>) => IOEither /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: IOEither>) => IOEither = + /*#__PURE__*/ + chainW(identity) + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/test/IOEither.ts b/test/IOEither.ts index 0211c4cae..c84f54e49 100644 --- a/test/IOEither.ts +++ b/test/IOEither.ts @@ -117,6 +117,10 @@ describe('IOEither', () => { U.deepStrictEqual(pipe(_.right(_.right('a')), _.flatten)(), E.right('a')) }) + it('flattenW', () => { + U.deepStrictEqual(pipe(_.right<'left1', _.IOEither<'left2', 'a'>>(_.right('a')), _.flattenW)(), E.right('a')) + }) + it('bimap', () => { const f = (s: string): number => s.length const g = (n: number): boolean => n > 2 From f00cc11ea0f3870c370e3ce4bc0ff4ee3d6538e1 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 17:57:14 +0300 Subject: [PATCH 108/162] add Reader.flattenW --- docs/modules/Reader.ts.md | 13 +++++++++++++ src/Reader.ts | 10 ++++++++++ test/Reader.ts | 9 ++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index d4ec2af17..ef75ec05e 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -43,6 +43,7 @@ Added in v2.0.0 - [chainFirstW](#chainfirstw) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [local](#local) - [constructors](#constructors) - [ask](#ask) @@ -346,6 +347,18 @@ export declare const flatten: (mma: Reader>) => Reader(mma: Reader>) => Reader +``` + +Added in v2.11.0 + ## local Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s diff --git a/src/Reader.ts b/src/Reader.ts index ea2c2a80b..03aacf453 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -157,6 +157,16 @@ export const flatten: (mma: Reader>) => Reader = /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: Reader>) => Reader = + /*#__PURE__*/ + chainW(identity) + /** * @category Semigroupoid * @since 2.0.0 diff --git a/test/Reader.ts b/test/Reader.ts index 948ef8b24..c7e6eb0cd 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -41,10 +41,17 @@ describe('Reader', () => { U.deepStrictEqual(pipe(_.of('foo'), _.chainFirstW(f))({}), 'foo') }) - it('chain', () => { + it('flatten', () => { U.deepStrictEqual(pipe(_.of(_.of('a')), _.flatten)({}), 'a') }) + type R1 = { readonly env1: unknown } + type R2 = { readonly env2: unknown } + + it('flattenW', () => { + U.deepStrictEqual(pipe(_.of>(_.of('a')), _.flattenW)({ env1: '', env2: '' }), 'a') + }) + it('compose', () => { U.deepStrictEqual(pipe(U.double, _.compose(S.size))('aaa'), 6) }) From 9f4f484c7c4625b3b65ea87ff7d293da1159c934 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 17:57:44 +0300 Subject: [PATCH 109/162] Add ReaderEither.flattenW --- docs/modules/ReaderEither.ts.md | 15 +++++++++++++++ src/ReaderEither.ts | 12 ++++++++++++ test/ReaderEither.ts | 12 ++++++++++++ 3 files changed, 39 insertions(+) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 86fd864e8..c15fd3b50 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -48,6 +48,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromEitherK](#fromeitherk) - [fromOptionK](#fromoptionk) - [fromReaderK](#fromreaderk) @@ -518,6 +519,20 @@ export declare const flatten: (mma: ReaderEither( + mma: ReaderEither> +) => ReaderEither +``` + +Added in v2.11.0 + ## fromEitherK **Signature** diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 59a1e7217..2552528fd 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -410,6 +410,18 @@ export const flatten: (mma: ReaderEither>) /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: ( + mma: ReaderEither> +) => ReaderEither = + /*#__PURE__*/ + chainW(identity) + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 8e1311f9a..1dde92a53 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -58,6 +58,18 @@ describe('ReaderEither', () => { U.deepStrictEqual(pipe(_.right(_.right('a')), _.flatten)({}), E.right('a')) }) + type R1 = { readonly env1: unknown } + type R2 = { readonly env2: unknown } + type E1 = { readonly left1: unknown } + type E2 = { readonly left2: unknown } + + it('flattenW', () => { + U.deepStrictEqual( + pipe(_.right>(_.right('a')), _.flattenW)({ env1: '', env2: '' }), + E.right('a') + ) + }) + it('mapLeft', () => { U.deepStrictEqual(pipe(_.right(1), _.mapLeft(S.size))({}), E.right(1)) U.deepStrictEqual(pipe(_.left('aa'), _.mapLeft(S.size))({}), E.left(2)) From d08397271befced7a95bdf617a4e7249e134bee6 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 17:58:13 +0300 Subject: [PATCH 110/162] Add ReaderTask.flattenW --- docs/modules/ReaderTask.ts.md | 13 +++++++++++++ src/ReaderTask.ts | 10 ++++++++++ test/ReaderTask.ts | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 1c266bc4e..ae5b64464 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -39,6 +39,7 @@ Added in v2.3.0 - [chainTaskK](#chaintaskk) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromIOK](#fromiok) - [fromReaderK](#fromreaderk) - [fromTaskK](#fromtaskk) @@ -372,6 +373,18 @@ export declare const flatten: (mma: ReaderTask>) => Re Added in v2.3.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: (mma: ReaderTask>) => ReaderTask +``` + +Added in v2.11.0 + ## fromIOK **Signature** diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 8d0ca0467..5d801ef22 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -192,6 +192,16 @@ export const flatten: (mma: ReaderTask>) => ReaderTask /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: ReaderTask>) => ReaderTask = + /*#__PURE__*/ + chainW(identity) + // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index 0248ebb18..eec36e757 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -50,6 +50,13 @@ describe('ReaderTask', () => { U.deepStrictEqual(await pipe(_.of(_.of('a')), _.flatten)({})(), 'a') }) + type R1 = { readonly env1: unknown } + type R2 = { readonly env2: unknown } + + it('flattenW', async () => { + U.deepStrictEqual(await pipe(_.of>(_.of('a')), _.flattenW)({ env1: '', env2: '' })(), 'a') + }) + it('of', async () => { U.deepStrictEqual(await _.fromReader(R.of(1))({})(), 1) }) From e85e7e42e2e33ca81525be8eca313bae3b019286 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 17:58:45 +0300 Subject: [PATCH 111/162] Add ReaderTaskEither.flattenW --- docs/modules/ReaderTaskEither.ts.md | 15 +++++++++++++++ src/ReaderTaskEither.ts | 12 ++++++++++++ test/ReaderTaskEither.ts | 15 +++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index b31855929..8cf34ffaa 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -64,6 +64,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromEitherK](#fromeitherk) - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) @@ -773,6 +774,20 @@ export declare const flatten: ( Added in v2.0.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: ( + mma: ReaderTaskEither> +) => ReaderTaskEither +``` + +Added in v2.11.0 + ## fromEitherK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 2a86e4921..d403a289b 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -617,6 +617,18 @@ export const flatten: (mma: ReaderTaskEither( + mma: ReaderTaskEither> +) => ReaderTaskEither = + /*#__PURE__*/ + chainW(identity) + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 14b21d61a..31b3ee0cc 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -53,6 +53,21 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right(_.right('a')), _.flatten)({})(), E.right('a')) }) + type R1 = { readonly env1: unknown } + type R2 = { readonly env2: unknown } + type E1 = { readonly left1: unknown } + type E2 = { readonly left2: unknown } + + it('flattenW', async () => { + U.deepStrictEqual( + await pipe( + _.right>(_.right('a')), + _.flattenW + )({ env1: '', env2: '' })(), + E.right('a') + ) + }) + it('bimap', async () => { const f = (s: string): number => s.length const g = (n: number): boolean => n > 2 From 228da9fb0fb7f1c3113113c75ba30d50ebcb6f0e Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 18:01:02 +0300 Subject: [PATCH 112/162] Add StateReaderTaskEither.flattenW --- docs/modules/StateReaderTaskEither.ts.md | 15 +++++++++++++++ src/StateReaderTaskEither.ts | 12 ++++++++++++ test/StateReaderTaskEither.ts | 17 ++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 5d7f02921..2e25ef7f0 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -59,6 +59,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromEitherK](#fromeitherk) - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) @@ -689,6 +690,20 @@ export declare const flatten: ( Added in v2.0.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: ( + mma: StateReaderTaskEither> +) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## fromEitherK **Signature** diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 2b58be5d9..3976470ad 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -478,6 +478,18 @@ export const flatten: ( /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: ( + mma: StateReaderTaskEither> +) => StateReaderTaskEither = + /*#__PURE__*/ + chainW(identity) + /** * Less strict version of [`alt`](#alt). * diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 4febdaf94..8017e3f05 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -76,11 +76,26 @@ describe('StateReaderTaskEither', () => { U.deepStrictEqual(e, E.right('aaa')) }) - it('chainFirst', async () => { + it('flatten', async () => { const e = await pipe(_.right(_.right('a')), _.flatten, _.evaluate(state))({})() U.deepStrictEqual(e, E.right('a')) }) + type S = unknown + type R1 = { readonly env1: unknown } + type R2 = { readonly env2: unknown } + type E1 = { readonly left1: unknown } + type E2 = { readonly left2: unknown } + + it('flattenW', async () => { + const e = await pipe( + _.right>(_.right('a')), + _.flattenW, + _.evaluate(state) + )({ env1: '', env2: '' })() + U.deepStrictEqual(e, E.right('a')) + }) + it('bimap', async () => { const gt2 = (n: number): boolean => n > 2 const e1 = await pipe(_.right('aaa'), _.bimap(gt2, S.size), _.evaluate(state))({})() From 6439151fb2ab0ae6ffdd948e2ba2ae0867d249fa Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Mon, 26 Apr 2021 18:01:30 +0300 Subject: [PATCH 113/162] Add TaskEither.flattenW --- docs/modules/TaskEither.ts.md | 13 +++++++++++++ src/TaskEither.ts | 10 ++++++++++ test/TaskEither.ts | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 93384d6cf..566ad14ae 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -56,6 +56,7 @@ Added in v2.0.0 - [filterOrElseW](#filterorelsew) - [flap](#flap) - [flatten](#flatten) + - [flattenW](#flattenw) - [fromEitherK](#fromeitherk) - [fromIOEitherK](#fromioeitherk) - [fromIOK](#fromiok) @@ -563,6 +564,18 @@ export declare const flatten: (mma: TaskEither>) => Ta Added in v2.0.0 +## flattenW + +Less strict version of [`flatten`](#flatten). + +**Signature** + +```ts +export declare const flattenW: (mma: TaskEither>) => TaskEither +``` + +Added in v2.11.0 + ## fromEitherK **Signature** diff --git a/src/TaskEither.ts b/src/TaskEither.ts index ce183bf5c..910c421a8 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -503,6 +503,16 @@ export const flatten: (mma: TaskEither>) => TaskEither /*#__PURE__*/ chain(identity) +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: TaskEither>) => TaskEither = + /*#__PURE__*/ + chainW(identity) + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/test/TaskEither.ts b/test/TaskEither.ts index ef024e71b..f56cd19fb 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -87,6 +87,13 @@ describe('TaskEither', () => { U.deepStrictEqual(await pipe(_.right(_.right('a')), _.flatten)(), E.right('a')) }) + it('flattenW', async () => { + U.deepStrictEqual( + await pipe(_.right<'left1', _.TaskEither<'left2', 'a'>>(_.right('a')), _.flattenW)(), + E.right('a') + ) + }) + it('bimap', async () => { const f = (s: string): number => s.length const g = (n: number): boolean => n > 2 From dea5852b464ad211d48040d56bc5894e21c1505e Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Wed, 28 Apr 2021 09:41:27 +0300 Subject: [PATCH 114/162] Update changelog to include mention of flattenW --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d266251ee..b5475d6c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ high state of flux, you're at risk of it changing without notice. - add `isBoolean` - `Either` - add `chainOptionK` + - add `flattenW` - `EitherT` - add `orElseFirst` - add `orLeft` @@ -98,6 +99,7 @@ high state of flux, you're at risk of it changing without notice. - `IOEither` - add `orElseFirst` / `orElseFirstW` - add `orLeft` + - add `flattenW` - `Magma` - add `reverse` - add `filterFirst` @@ -138,16 +140,19 @@ high state of flux, you're at risk of it changing without notice. - add `equals` - `Reader` - add `asksEW`, `asksW` + - add `flattenW` - `ReaderEither` - add `asksEW`, `asksW` - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` - `ReaderTask` - add `asksEW`, `asksW` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` - `ReaderTaskEither` - add `asksEW`, `asksW` - add `orElseFirst` / `orElseFirstW` @@ -160,6 +165,7 @@ high state of flux, you're at risk of it changing without notice. - add `chainFirstReaderTaskK`, `chainFirstReaderTaskKW` - add `chainReaderEitherK`, `chainReaderEitherKW` - add `chainFirstReaderEitherK`, `chainFirstReaderEitherKW` + - add `flattenW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -229,6 +235,7 @@ high state of flux, you're at risk of it changing without notice. - add `asksEW`, `asksW` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` - `string` - add `toUpperCase` - `struct` @@ -239,6 +246,7 @@ high state of flux, you're at risk of it changing without notice. - add `chainTaskOptionK` - add `orElseFirst` / `orElseFirstW` - add `orLeft` + - add `flattenW` - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) - `Witherable` From 2be727427e0a7a28e82f7508f6d0b9a67bc6a697 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 28 Apr 2021 16:36:00 +0200 Subject: [PATCH 115/162] add dpdm --- package-lock.json | 860 ++++++++++++++++++++++++---------------------- package.json | 6 +- 2 files changed, 456 insertions(+), 410 deletions(-) diff --git a/package-lock.json b/package-lock.json index d245bf8cd..2501131ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "fp-ts", - "version": "2.10.0-rc.6", + "version": "2.11.0-rc.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1072,27 +1072,6 @@ "@sinonjs/commons": "^1.7.0" } }, - "@textlint/ast-node-types": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/@textlint/ast-node-types/-/ast-node-types-4.2.1.tgz", - "integrity": "sha512-Pqg1LTJpF929Ovi/lCaPqlyz8yDwBFbQulC0jyQcbRAoTxYS4AZMc48Ug2yk0so5hISQXKrlAxyVBmBVl9EKGA==", - "dev": true - }, - "@textlint/markdown-to-ast": { - "version": "6.0.9", - "resolved": "https://registry.npmjs.org/@textlint/markdown-to-ast/-/markdown-to-ast-6.0.9.tgz", - "integrity": "sha512-hfAWBvTeUGh5t5kTn2U3uP3qOSM1BSrxzl1jF3nn0ywfZXpRBZr5yRjXnl4DzIYawCtZOshmRi/tI3/x4TE1jQ==", - "dev": true, - "requires": { - "@textlint/ast-node-types": "^4.0.3", - "debug": "^2.1.3", - "remark-frontmatter": "^1.2.0", - "remark-parse": "^5.0.0", - "structured-source": "^3.0.2", - "traverse": "^0.6.6", - "unified": "^6.1.6" - } - }, "@ts-morph/common": { "version": "0.7.5", "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.7.5.tgz", @@ -1174,6 +1153,15 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, + "@types/fs-extra": { + "version": "9.0.11", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.11.tgz", + "integrity": "sha512-mZsifGG4QeQ7hlkhO56u7zt/ycBgGxSVsFI/6lGTU34VtwkiqrrSDgw0+ygs8kFGWcXnFQWMrzF2h7TtDFNixA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/glob": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", @@ -1309,15 +1297,6 @@ "uri-js": "^4.2.2" } }, - "anchor-markdown-header": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/anchor-markdown-header/-/anchor-markdown-header-0.5.7.tgz", - "integrity": "sha1-BFBj125qH5zTJ6V6ASaqD97Dcac=", - "dev": true, - "requires": { - "emoji-regex": "~6.1.0" - } - }, "ansi-escapes": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.1.tgz", @@ -1459,6 +1438,12 @@ "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", "dev": true }, + "at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", + "dev": true + }, "atob": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", @@ -1652,12 +1637,6 @@ "babel-preset-current-node-syntax": "^0.1.3" } }, - "bail": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/bail/-/bail-1.0.3.tgz", - "integrity": "sha512-1X8CnjFVQ+a+KW36uBNMTU5s8+v5FzeqrP7hTG5aTb4aPreSbZJlhwPon9VKMuEVgV++JM+SQrALY3kr7eswdg==", - "dev": true - }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -1731,6 +1710,12 @@ } } }, + "base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -1750,11 +1735,35 @@ "platform": "^1.3.3" } }, - "boundary": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/boundary/-/boundary-1.0.1.tgz", - "integrity": "sha1-TWfcJgLAzBbdm85+v4fpSCkPWBI=", - "dev": true + "bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "dev": true, + "requires": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + }, + "dependencies": { + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "dev": true, + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + } + } }, "brace-expansion": { "version": "1.1.8", @@ -1816,6 +1825,16 @@ "node-int64": "^0.4.0" } }, + "buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "dev": true, + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "buffer-from": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.0.0.tgz", @@ -1897,24 +1916,6 @@ "integrity": "sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==", "dev": true }, - "character-entities": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/character-entities/-/character-entities-1.2.2.tgz", - "integrity": "sha512-sMoHX6/nBiy3KKfC78dnEalnpn0Az0oSNvqUWYTtYrhRI5iUIYsROU48G+E+kMFQzqXaJ8kHJZ85n7y6/PHgwQ==", - "dev": true - }, - "character-entities-legacy": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/character-entities-legacy/-/character-entities-legacy-1.1.2.tgz", - "integrity": "sha512-9NB2VbXtXYWdXzqrvAHykE/f0QJxzaKIpZ5QzNZrrgQ7Iyxr2vnfS8fCBNVW9nUEZE0lo57nxKRqnzY/dKrwlA==", - "dev": true - }, - "character-reference-invalid": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/character-reference-invalid/-/character-reference-invalid-1.1.2.tgz", - "integrity": "sha512-7I/xceXfKyUJmSAn/jw8ve/9DyOP7XxufNYLI9Px7CmsKgEUaZLUTax6nZxGQtaoiZCjpu6cHPj20xC/vqRReQ==", - "dev": true - }, "ci-info": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", @@ -1950,6 +1951,21 @@ } } }, + "cli-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", + "integrity": "sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==", + "dev": true, + "requires": { + "restore-cursor": "^3.1.0" + } + }, + "cli-spinners": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.6.0.tgz", + "integrity": "sha512-t+4/y50K/+4xcCRosKkA7W4gTr1MySvLV0q+PxmG7FJ5g+66ChKurYjxBCjHggHH3HA5Hh9cy+lcUGWDqVH+4Q==", + "dev": true + }, "cliui": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", @@ -1978,6 +1994,12 @@ } } }, + "clone": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", + "integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=", + "dev": true + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -1996,12 +2018,6 @@ "integrity": "sha512-fLeEhqwymYat/MpTPUjSKHVYYl0ec2mOyALEMLmzr5i1isuG+6jfI2j2d5oBO3VIzgUXgBVIcOT9uH1TFxBckw==", "dev": true }, - "collapse-white-space": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/collapse-white-space/-/collapse-white-space-1.0.4.tgz", - "integrity": "sha512-YfQ1tAUZm561vpYD+5eyWN8+UsceQbSrqqlc/6zDY2gtAE+uZLSdkkovhnGpmCThsvKBFakq4EdY/FF93E8XIw==", - "dev": true - }, "collect-v8-coverage": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/collect-v8-coverage/-/collect-v8-coverage-1.0.1.tgz", @@ -2205,6 +2221,15 @@ "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", "dev": true }, + "defaults": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.3.tgz", + "integrity": "sha1-xlYFHpgX2f8I7YgUd/P+QBnz730=", + "dev": true, + "requires": { + "clone": "^1.0.2" + } + }, "define-property": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/define-property/-/define-property-2.0.2.tgz", @@ -2389,28 +2414,6 @@ } } }, - "doctoc": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/doctoc/-/doctoc-1.4.0.tgz", - "integrity": "sha512-8IAq3KdMkxhXCUF+xdZxdJxwuz8N2j25sMgqiu4U4JWluN9tRKMlAalxGASszQjlZaBprdD2YfXpL3VPWUD4eg==", - "dev": true, - "requires": { - "@textlint/markdown-to-ast": "~6.0.9", - "anchor-markdown-header": "^0.5.5", - "htmlparser2": "~3.9.2", - "minimist": "~1.2.0", - "underscore": "~1.8.3", - "update-section": "^0.3.0" - }, - "dependencies": { - "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true - } - } - }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -2420,22 +2423,6 @@ "esutils": "^2.0.2" } }, - "dom-serializer": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.1.tgz", - "integrity": "sha512-l0IU0pPzLWSHBcieZbpOKgkIn3ts3vAh7ZuFyXNwJxJXk/c4Gwj9xaTJwIDVQCXawWD0qb3IzMGH5rglQaO0XA==", - "dev": true, - "requires": { - "domelementtype": "^1.3.0", - "entities": "^1.1.1" - } - }, - "domelementtype": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz", - "integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==", - "dev": true - }, "domexception": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/domexception/-/domexception-2.0.1.tgz", @@ -2453,23 +2440,186 @@ } } }, - "domhandler": { - "version": "2.4.2", - "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.2.tgz", - "integrity": "sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==", - "dev": true, - "requires": { - "domelementtype": "1" - } - }, - "domutils": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz", - "integrity": "sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==", + "dpdm": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/dpdm/-/dpdm-3.6.0.tgz", + "integrity": "sha512-+DRT3o/5mIbjKENtKUEDX0qOk0xIBDllDITtvHRH+5Yppz2aJAHQkozj3oiIPLWWfup8CuuBl3xWvk5yD5oaOg==", "dev": true, "requires": { - "dom-serializer": "0", - "domelementtype": "1" + "@types/fs-extra": "^9.0.6", + "@types/glob": "^7.1.3", + "@types/yargs": "^15.0.12", + "chalk": "^4.1.0", + "fs-extra": "^9.0.1", + "glob": "^7.1.6", + "ora": "^5.2.0", + "tslib": "^2.1.0", + "typescript": "^4.1.3", + "yargs": "^16.2.0" + }, + "dependencies": { + "@types/yargs": { + "version": "15.0.13", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.13.tgz", + "integrity": "sha512-kQ5JNTrbDv3Rp5X2n/iUu37IJBDU2gsZ5R/g1/KHOOEc5IKfUFjXT6DENPGduh08I/pamwtEq4oul7gUqKTQDQ==", + "dev": true, + "requires": { + "@types/yargs-parser": "*" + } + }, + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "dev": true, + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, + "graceful-fs": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", + "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==", + "dev": true + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true + }, + "jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dev": true, + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + }, + "tslib": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.2.0.tgz", + "integrity": "sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==", + "dev": true + }, + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "dev": true + }, + "wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "requires": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + } + }, + "y18n": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", + "dev": true + }, + "yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dev": true, + "requires": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + } + }, + "yargs-parser": { + "version": "20.2.7", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.7.tgz", + "integrity": "sha512-FiNkvbeHzB/syOjIUxFDCnhSfzAL8R5vs40MgLFBorXACCOAEaWu0gRZl14vG8MR9AOJIZbmkjhusqBYZ3HTHw==", + "dev": true + } } }, "dtslint": { @@ -2579,12 +2729,6 @@ "integrity": "sha512-d34LN4L6h18Bzz9xpoku2nPwKxCPlPMr3EEKTkoEBi+1/+b0lcRkRJ1UVyyZaKNeqGR3swcGl6s390DNO4YVgQ==", "dev": true }, - "emoji-regex": { - "version": "6.1.3", - "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-6.1.3.tgz", - "integrity": "sha1-7HmjlpsC0uzytyJUJ5v5m8eoOTI=", - "dev": true - }, "end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -2594,12 +2738,6 @@ "once": "^1.4.0" } }, - "entities": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz", - "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==", - "dev": true - }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -2609,6 +2747,12 @@ "is-arrayish": "^0.2.1" } }, + "escalade": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", + "dev": true + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -2929,15 +3073,6 @@ "reusify": "^1.0.4" } }, - "fault": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/fault/-/fault-1.0.2.tgz", - "integrity": "sha512-o2eo/X2syzzERAtN5LcGbiVQ0WwZSlN3qLtadwAz3X8Bu+XWD16dja/KMsjZLiQr+BLGPDnHGkc4yUJf1Xpkpw==", - "dev": true, - "requires": { - "format": "^0.2.2" - } - }, "fb-watchman": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.1.tgz", @@ -2993,12 +3128,6 @@ "mime-types": "^2.1.12" } }, - "format": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz", - "integrity": "sha1-1hcBB+nv3E7TDJ3DkBbflCtctYs=", - "dev": true - }, "fp-ts": { "version": "2.9.5", "resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-2.9.5.tgz", @@ -3281,20 +3410,6 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, - "htmlparser2": { - "version": "3.9.2", - "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.9.2.tgz", - "integrity": "sha1-G9+HrMoPP55T+k/M6w9LTLsAszg=", - "dev": true, - "requires": { - "domelementtype": "^1.3.0", - "domhandler": "^2.3.0", - "domutils": "^1.5.1", - "entities": "^1.1.1", - "inherits": "^2.0.1", - "readable-stream": "^2.0.2" - } - }, "http-signature": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", @@ -3321,6 +3436,12 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "dev": true + }, "import-local": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.0.2.tgz", @@ -3384,22 +3505,6 @@ "kind-of": "^3.0.2" } }, - "is-alphabetical": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-alphabetical/-/is-alphabetical-1.0.2.tgz", - "integrity": "sha512-V0xN4BYezDHcBSKb1QHUFMlR4as/XEuCZBzMJUU4n7+Cbt33SmUnSol+pnXFvLxSHNq2CemUXNdaXV6Flg7+xg==", - "dev": true - }, - "is-alphanumerical": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-alphanumerical/-/is-alphanumerical-1.0.2.tgz", - "integrity": "sha512-pyfU/0kHdISIgslFfZN9nfY1Gk3MquQgUm1mJTjdkEPpkAKNWuBTSqFwewOpR7N351VkErCiyV71zX7mlQQqsg==", - "dev": true, - "requires": { - "is-alphabetical": "^1.0.0", - "is-decimal": "^1.0.0" - } - }, "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", @@ -3430,12 +3535,6 @@ "kind-of": "^3.0.2" } }, - "is-decimal": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-decimal/-/is-decimal-1.0.2.tgz", - "integrity": "sha512-TRzl7mOCchnhchN+f3ICUCzYvL9ul7R+TYOsZ8xia++knyZAJfv/uA1FvQXsAnYIl1T3B2X5E/J7Wb1QXiIBXg==", - "dev": true - }, "is-descriptor": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/is-descriptor/-/is-descriptor-0.1.6.tgz", @@ -3495,10 +3594,10 @@ "is-extglob": "^2.1.1" } }, - "is-hexadecimal": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-hexadecimal/-/is-hexadecimal-1.0.2.tgz", - "integrity": "sha512-but/G3sapV3MNyqiDBLrOi4x8uCIw0RY3o/Vb5GT0sMFHrVV7731wFSVy41T5FO1og7G0gXLJh0MkgPRouko/A==", + "is-interactive": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", + "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", "dev": true }, "is-negated-glob": { @@ -3516,12 +3615,6 @@ "kind-of": "^3.0.2" } }, - "is-plain-obj": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz", - "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=", - "dev": true - }, "is-plain-object": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", @@ -3575,10 +3668,10 @@ "unc-path-regex": "^0.1.2" } }, - "is-whitespace-character": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-whitespace-character/-/is-whitespace-character-1.0.2.tgz", - "integrity": "sha512-SzM+T5GKUCtLhlHFKt2SDAX2RFzfS6joT91F2/WSi9LxgFdsnhfPK/UIA+JhRR2xuyLdrCys2PiFDrtn1fU5hQ==", + "is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", "dev": true }, "is-windows": { @@ -3587,12 +3680,6 @@ "integrity": "sha1-0YUOuXkezRjmGCzhKjDzlmNLsZ0=", "dev": true }, - "is-word-character": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-word-character/-/is-word-character-1.0.2.tgz", - "integrity": "sha512-T3FlsX8rCHAH8e7RE7PfOPZVFQlcV3XRF9eOOBQ1uf70OxO7CjjSOjeImMPCADBdYWcStAbVbYvJ1m2D3tb+EA==", - "dev": true - }, "is-wsl": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz", @@ -5315,6 +5402,67 @@ "lodash._reinterpolate": "^3.0.0" } }, + "log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dev": true, + "requires": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "dependencies": { + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, "logging-ts": { "version": "0.3.4", "resolved": "https://registry.npmjs.org/logging-ts/-/logging-ts-0.3.4.tgz", @@ -5368,12 +5516,6 @@ "object-visit": "^1.0.0" } }, - "markdown-escapes": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/markdown-escapes/-/markdown-escapes-1.0.2.tgz", - "integrity": "sha512-lbRZ2mE3Q9RtLjxZBZ9+IMl68DKIXaVAhwvwn9pmjnPLS0h/6kyBMgNhqi1xFJ/2yv6cSyv0jbiZavZv93JkkA==", - "dev": true - }, "markdown-link": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/markdown-link/-/markdown-link-0.1.1.tgz", @@ -5832,6 +5974,89 @@ "word-wrap": "~1.2.3" } }, + "ora": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.0.tgz", + "integrity": "sha512-1StwyXQGoU6gdjYkyVcqOLnVlbKj+6yPNNOxJVgpt9t4eksKjiriiHuxktLYkgllwk+D6MbC4ihH84L1udRXPg==", + "dev": true, + "requires": { + "bl": "^4.1.0", + "chalk": "^4.1.0", + "cli-cursor": "^3.1.0", + "cli-spinners": "^2.5.0", + "is-interactive": "^1.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", + "strip-ansi": "^6.0.0", + "wcwidth": "^1.0.1" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, "p-each-series": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-2.1.0.tgz", @@ -5868,20 +6093,6 @@ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", "dev": true }, - "parse-entities": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-1.2.1.tgz", - "integrity": "sha512-NBWYLQm1KSoDKk7GAHyioLTvCZ5QjdH/ASBBQTD3iLiAWJXS5bg1jEWI8nIJ+vgVvsceBVBcDGRWSo0KVQBvvg==", - "dev": true, - "requires": { - "character-entities": "^1.0.0", - "character-entities-legacy": "^1.0.0", - "character-reference-invalid": "^1.0.0", - "is-alphanumerical": "^1.0.0", - "is-decimal": "^1.0.0", - "is-hexadecimal": "^1.0.0" - } - }, "parse-json": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.1.0.tgz", @@ -6178,39 +6389,6 @@ "safe-regex": "^1.1.0" } }, - "remark-frontmatter": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/remark-frontmatter/-/remark-frontmatter-1.3.1.tgz", - "integrity": "sha512-Zj/fDMYnSVgMCeKp8fXIhtMoZq4G6E1dnwfMoO8fVXrm/+oVSiN8YMREtwN2cctgK9EsnYSeS1ExX2hcX/fE1A==", - "dev": true, - "requires": { - "fault": "^1.0.1", - "xtend": "^4.0.1" - } - }, - "remark-parse": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/remark-parse/-/remark-parse-5.0.0.tgz", - "integrity": "sha512-b3iXszZLH1TLoyUzrATcTQUZrwNl1rE70rVdSruJFlDaJ9z5aMkhrG43Pp68OgfHndL/ADz6V69Zow8cTQu+JA==", - "dev": true, - "requires": { - "collapse-white-space": "^1.0.2", - "is-alphabetical": "^1.0.0", - "is-decimal": "^1.0.0", - "is-whitespace-character": "^1.0.0", - "is-word-character": "^1.0.0", - "markdown-escapes": "^1.0.0", - "parse-entities": "^1.1.0", - "repeat-string": "^1.5.4", - "state-toggle": "^1.0.0", - "trim": "0.0.1", - "trim-trailing-lines": "^1.0.0", - "unherit": "^1.0.4", - "unist-util-remove-position": "^1.0.0", - "vfile-location": "^2.0.0", - "xtend": "^4.0.1" - } - }, "remarkable": { "version": "1.7.4", "resolved": "https://registry.npmjs.org/remarkable/-/remarkable-1.7.4.tgz", @@ -6239,12 +6417,6 @@ "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=", "dev": true }, - "replace-ext": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/replace-ext/-/replace-ext-1.0.0.tgz", - "integrity": "sha1-3mMSg3P8v3w8z6TeWkgMRaZ5WOs=", - "dev": true - }, "request": { "version": "2.88.2", "resolved": "https://registry.npmjs.org/request/-/request-2.88.2.tgz", @@ -6365,6 +6537,16 @@ "integrity": "sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=", "dev": true }, + "restore-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", + "integrity": "sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==", + "dev": true, + "requires": { + "onetime": "^5.1.0", + "signal-exit": "^3.0.2" + } + }, "ret": { "version": "0.1.15", "resolved": "https://registry.npmjs.org/ret/-/ret-0.1.15.tgz", @@ -6904,12 +7086,6 @@ } } }, - "state-toggle": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/state-toggle/-/state-toggle-1.0.1.tgz", - "integrity": "sha512-Qe8QntFrrpWTnHwvwj2FZTgv+PKIsp0B9VxLzLLbSpPXWOgRgc5LVj/aTiSfK1RqIeF9jeC1UeOH8Q8y60A7og==", - "dev": true - }, "static-extend": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/static-extend/-/static-extend-0.1.2.tgz", @@ -7046,15 +7222,6 @@ "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", "dev": true }, - "structured-source": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/structured-source/-/structured-source-3.0.2.tgz", - "integrity": "sha1-3YAkJeD1PcSm56yjdSkBoczaevU=", - "dev": true, - "requires": { - "boundary": "^1.0.1" - } - }, "supports-color": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz", @@ -7210,30 +7377,6 @@ "punycode": "^2.1.1" } }, - "traverse": { - "version": "0.6.6", - "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz", - "integrity": "sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc=", - "dev": true - }, - "trim": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/trim/-/trim-0.0.1.tgz", - "integrity": "sha1-WFhUf2spB1fulczMZm+1AITEYN0=", - "dev": true - }, - "trim-trailing-lines": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/trim-trailing-lines/-/trim-trailing-lines-1.1.1.tgz", - "integrity": "sha512-bWLv9BbWbbd7mlqqs2oQYnLD/U/ZqeJeJwbO0FG2zA1aTq+HTvxfHNKFa/HGCVyJpDiioUYaBhfiT6rgk+l4mg==", - "dev": true - }, - "trough": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/trough/-/trough-1.0.3.tgz", - "integrity": "sha512-fwkLWH+DimvA4YCy+/nvJd61nWQQ2liO/nF/RjkTpiOGi+zxZzVkhb1mvbHIIW4b/8nDsYI8uTmAlc0nNkRMOw==", - "dev": true - }, "ts-jest": { "version": "26.3.0", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-26.3.0.tgz", @@ -7750,36 +7893,6 @@ "integrity": "sha1-5z3T17DXxe2G+6xrCufYxqadUPo=", "dev": true }, - "underscore": { - "version": "1.8.3", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", - "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=", - "dev": true - }, - "unherit": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/unherit/-/unherit-1.1.1.tgz", - "integrity": "sha512-+XZuV691Cn4zHsK0vkKYwBEwB74T3IZIcxrgn2E4rKwTfFyI1zCh7X7grwh9Re08fdPlarIdyWgI8aVB3F5A5g==", - "dev": true, - "requires": { - "inherits": "^2.0.1", - "xtend": "^4.0.1" - } - }, - "unified": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/unified/-/unified-6.2.0.tgz", - "integrity": "sha512-1k+KPhlVtqmG99RaTbAv/usu85fcSRu3wY8X+vnsEhIxNP5VbVIDiXnLqyKIG+UMdyTg0ZX9EI6k2AfjJkHPtA==", - "dev": true, - "requires": { - "bail": "^1.0.0", - "extend": "^3.0.0", - "is-plain-obj": "^1.1.0", - "trough": "^1.0.0", - "vfile": "^2.0.0", - "x-is-string": "^0.1.0" - } - }, "union-value": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz", @@ -7792,45 +7905,6 @@ "set-value": "^2.0.1" } }, - "unist-util-is": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-2.1.2.tgz", - "integrity": "sha512-YkXBK/H9raAmG7KXck+UUpnKiNmUdB+aBGrknfQ4EreE1banuzrKABx3jP6Z5Z3fMSPMQQmeXBlKpCbMwBkxVw==", - "dev": true - }, - "unist-util-remove-position": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/unist-util-remove-position/-/unist-util-remove-position-1.1.2.tgz", - "integrity": "sha512-XxoNOBvq1WXRKXxgnSYbtCF76TJrRoe5++pD4cCBsssSiWSnPEktyFrFLE8LTk3JW5mt9hB0Sk5zn4x/JeWY7Q==", - "dev": true, - "requires": { - "unist-util-visit": "^1.1.0" - } - }, - "unist-util-stringify-position": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-1.1.2.tgz", - "integrity": "sha512-pNCVrk64LZv1kElr0N1wPiHEUoXNVFERp+mlTg/s9R5Lwg87f9bM/3sQB99w+N9D/qnM9ar3+AKDBwo/gm/iQQ==", - "dev": true - }, - "unist-util-visit": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-1.4.0.tgz", - "integrity": "sha512-FiGu34ziNsZA3ZUteZxSFaczIjGmksfSgdKqBfOejrrfzyUy5b7YrlzT1Bcvi+djkYDituJDy2XB7tGTeBieKw==", - "dev": true, - "requires": { - "unist-util-visit-parents": "^2.0.0" - } - }, - "unist-util-visit-parents": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-2.0.1.tgz", - "integrity": "sha512-6B0UTiMfdWql4cQ03gDTCSns+64Zkfo2OCbK31Ov0uMizEz+CJeAp0cgZVb5Fhmcd7Bct2iRNywejT0orpbqUA==", - "dev": true, - "requires": { - "unist-util-is": "^2.1.2" - } - }, "universalify": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.1.tgz", @@ -7883,12 +7957,6 @@ } } }, - "update-section": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/update-section/-/update-section-0.3.3.tgz", - "integrity": "sha1-RY8Xgg03gg3GDiC4bZQ5GwASMVg=", - "dev": true - }, "uri-js": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.0.tgz", @@ -7963,33 +8031,6 @@ "extsprintf": "^1.2.0" } }, - "vfile": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/vfile/-/vfile-2.3.0.tgz", - "integrity": "sha512-ASt4mBUHcTpMKD/l5Q+WJXNtshlWxOogYyGYYrg4lt/vuRjC1EFQtlAofL5VmtVNIZJzWYFJjzGWZ0Gw8pzW1w==", - "dev": true, - "requires": { - "is-buffer": "^1.1.4", - "replace-ext": "1.0.0", - "unist-util-stringify-position": "^1.0.0", - "vfile-message": "^1.0.0" - } - }, - "vfile-location": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/vfile-location/-/vfile-location-2.0.4.tgz", - "integrity": "sha512-KRL5uXQPoUKu+NGvQVL4XLORw45W62v4U4gxJ3vRlDfI9QsT4ZN1PNXn/zQpKUulqGDpYuT0XDfp5q9O87/y/w==", - "dev": true - }, - "vfile-message": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-1.1.1.tgz", - "integrity": "sha512-1WmsopSGhWt5laNir+633LszXvZ+Z/lxveBf6yhGsqnQIhlhzooZae7zV6YVM1Sdkw68dtAW3ow0pOdPANugvA==", - "dev": true, - "requires": { - "unist-util-stringify-position": "^1.1.1" - } - }, "w3c-hr-time": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz", @@ -8017,6 +8058,15 @@ "makeerror": "1.0.x" } }, + "wcwidth": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", + "integrity": "sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=", + "dev": true, + "requires": { + "defaults": "^1.0.3" + } + }, "webidl-conversions": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-6.1.0.tgz", @@ -8147,12 +8197,6 @@ "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==", "dev": true }, - "x-is-string": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/x-is-string/-/x-is-string-0.1.0.tgz", - "integrity": "sha1-R0tQhlrzpJqcRlfwWs0UVFj3fYI=", - "dev": true - }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index 76d3ae955..f5d6104e6 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prettier": "prettier --list-different \"./{src,test,examples,scripts}/**/*.ts\"", "fix-prettier": "prettier --write \"./{src,test,examples,scripts}/**/*.ts\"", "jest": "jest", - "test": "npm run lint && npm run prettier && npm run dtslint && npm run jest-clear-cache && npm run jest && npm run docs", + "test": "npm run dpdm && npm run lint && npm run prettier && npm run dtslint && npm run jest-clear-cache && npm run jest && npm run docs", "clean": "rimraf ./dist", "prebuild": "npm run clean", "build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.build-es6.json && ts-node scripts/build", @@ -22,7 +22,8 @@ "dtslint": "dtslint dtslint", "docs": "docs-ts", "prerelease": "npm run build", - "release": "ts-node scripts/release" + "release": "ts-node scripts/release", + "dpdm": "dpdm --warning=false --tree=false --exit-code circular:1 -T src/index.ts" }, "repository": { "type": "git", @@ -42,6 +43,7 @@ "@types/prettier": "1.10.0", "benchmark": "2.1.4", "docs-ts": "^0.6.7", + "dpdm": "^3.6.0", "dtslint": "github:gcanti/dtslint", "fast-check": "^1.25.1", "glob": "^7.1.6", From 4057b0144200624dbc99af899e9bafdb07cf44c0 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 29 Apr 2021 15:45:50 +0200 Subject: [PATCH 116/162] derive flatten from flattenW --- package.json | 2 +- src/Either.ts | 24 +++++++++++------------- src/IOEither.ts | 16 +++++++--------- src/Reader.ts | 16 +++++++--------- src/ReaderEither.ts | 18 ++++++++---------- src/ReaderTask.ts | 16 +++++++--------- src/ReaderTaskEither.ts | 20 ++++++++++---------- src/StateReaderTaskEither.ts | 22 ++++++++++------------ src/TaskEither.ts | 16 +++++++--------- 9 files changed, 68 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index f5d6104e6..6e074b120 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prettier": "prettier --list-different \"./{src,test,examples,scripts}/**/*.ts\"", "fix-prettier": "prettier --write \"./{src,test,examples,scripts}/**/*.ts\"", "jest": "jest", - "test": "npm run dpdm && npm run lint && npm run prettier && npm run dtslint && npm run jest-clear-cache && npm run jest && npm run docs", + "test": "npm run lint && npm run prettier && npm run dtslint && npm run jest-clear-cache && npm run jest && npm run docs", "clean": "rimraf ./dist", "prebuild": "npm run clean", "build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.build-es6.json && ts-node scripts/build", diff --git a/src/Either.ts b/src/Either.ts index cb1b703fc..f94958bda 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -463,6 +463,16 @@ export const chainW = (f: (a: A) => Either) => (ma: Either< */ export const chain: (f: (a: A) => Either) => (ma: Either) => Either = chainW +/** + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 + */ +export const flattenW: (mma: Either>) => Either = + /*#__PURE__*/ + chainW(identity) + /** * The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level. * @@ -478,19 +488,7 @@ export const chain: (f: (a: A) => Either) => (ma: Either) = * @category combinators * @since 2.0.0 */ -export const flatten: (mma: Either>) => Either = - /*#__PURE__*/ - chain(identity) - -/** - * Less strict version of [`flatten`](#flatten). - * - * @category combinators - * @since 2.11.0 - */ -export const flattenW: (mma: Either>) => Either = - /*#__PURE__*/ - chainW(identity) +export const flatten: (mma: Either>) => Either = flattenW /** * Less strict version of [`alt`](#alt). diff --git a/src/IOEither.ts b/src/IOEither.ts index b120bcd75..d22910dfb 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -378,24 +378,22 @@ export const chainW: ( ) => (ma: IOEither) => IOEither = chain as any /** - * Derivable from `Chain`. + * Less strict version of [`flatten`](#flatten). * * @category combinators - * @since 2.0.0 + * @since 2.11.0 */ -export const flatten: (mma: IOEither>) => IOEither = +export const flattenW: (mma: IOEither>) => IOEither = /*#__PURE__*/ - chain(identity) + chainW(identity) /** - * Less strict version of [`flatten`](#flatten). + * Derivable from `Chain`. * * @category combinators - * @since 2.11.0 + * @since 2.0.0 */ -export const flattenW: (mma: IOEither>) => IOEither = - /*#__PURE__*/ - chainW(identity) +export const flatten: (mma: IOEither>) => IOEither = flattenW /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to diff --git a/src/Reader.ts b/src/Reader.ts index 03aacf453..72fdda0af 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -148,24 +148,22 @@ export const chainW: (f: (a: A) => Reader) => (ma: Reader(f: (a: A) => Reader) => (ma: Reader) => Reader = chainW /** - * Derivable from `Chain`. + * Less strict version of [`flatten`](#flatten). * * @category combinators - * @since 2.0.0 + * @since 2.11.0 */ -export const flatten: (mma: Reader>) => Reader = +export const flattenW: (mma: Reader>) => Reader = /*#__PURE__*/ - chain(identity) + chainW(identity) /** - * Less strict version of [`flatten`](#flatten). + * Derivable from `Chain`. * * @category combinators - * @since 2.11.0 + * @since 2.0.0 */ -export const flattenW: (mma: Reader>) => Reader = - /*#__PURE__*/ - chainW(identity) +export const flatten: (mma: Reader>) => Reader = flattenW /** * @category Semigroupoid diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 2552528fd..84656465b 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -400,16 +400,6 @@ export const chainW: ( f: (a: A) => ReaderEither ) => (ma: ReaderEither) => ReaderEither = chain as any -/** - * Derivable from `Chain`. - * - * @category combinators - * @since 2.0.0 - */ -export const flatten: (mma: ReaderEither>) => ReaderEither = - /*#__PURE__*/ - chain(identity) - /** * Less strict version of [`flatten`](#flatten). * @@ -422,6 +412,14 @@ export const flattenW: ( /*#__PURE__*/ chainW(identity) +/** + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const flatten: (mma: ReaderEither>) => ReaderEither = flattenW + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 5d801ef22..62a2597e7 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -183,24 +183,22 @@ export const chainW: ( ) => (ma: ReaderTask) => ReaderTask = chain as any /** - * Derivable from `Chain`. + * Less strict version of [`flatten`](#flatten). * * @category combinators - * @since 2.3.0 + * @since 2.11.0 */ -export const flatten: (mma: ReaderTask>) => ReaderTask = +export const flattenW: (mma: ReaderTask>) => ReaderTask = /*#__PURE__*/ - chain(identity) + chainW(identity) /** - * Less strict version of [`flatten`](#flatten). + * Derivable from `Chain`. * * @category combinators - * @since 2.11.0 + * @since 2.3.0 */ -export const flattenW: (mma: ReaderTask>) => ReaderTask = - /*#__PURE__*/ - chainW(identity) +export const flatten: (mma: ReaderTask>) => ReaderTask = flattenW // ------------------------------------------------------------------------------------- // instances diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index d403a289b..05b5aef8c 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -607,16 +607,6 @@ export const chainW: ( f: (a: A) => ReaderTaskEither ) => (ma: ReaderTaskEither) => ReaderTaskEither = chain as any -/** - * Derivable from `Chain`. - * - * @category combinators - * @since 2.0.0 - */ -export const flatten: (mma: ReaderTaskEither>) => ReaderTaskEither = - /*#__PURE__*/ - chain(identity) - /** * Less strict version of [`flatten`](#flatten). * @@ -629,6 +619,16 @@ export const flattenW: ( /*#__PURE__*/ chainW(identity) +/** + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const flatten: ( + mma: ReaderTaskEither> +) => ReaderTaskEither = flattenW + /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to * types of kind `* -> *`. diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 3976470ad..ee22d2ee1 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -466,18 +466,6 @@ export const chainW: ( f: (a: A) => StateReaderTaskEither ) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chain as any -/** - * Derivable from `Chain`. - * - * @category combinators - * @since 2.0.0 - */ -export const flatten: ( - mma: StateReaderTaskEither> -) => StateReaderTaskEither = - /*#__PURE__*/ - chain(identity) - /** * Less strict version of [`flatten`](#flatten). * @@ -490,6 +478,16 @@ export const flattenW: ( /*#__PURE__*/ chainW(identity) +/** + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const flatten: ( + mma: StateReaderTaskEither> +) => StateReaderTaskEither = flattenW + /** * Less strict version of [`alt`](#alt). * diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 910c421a8..eee3d7bec 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -494,24 +494,22 @@ export const chainW: ( ) => (ma: TaskEither) => TaskEither = chain as any /** - * Derivable from `Chain`. + * Less strict version of [`flatten`](#flatten). * * @category combinators - * @since 2.0.0 + * @since 2.11.0 */ -export const flatten: (mma: TaskEither>) => TaskEither = +export const flattenW: (mma: TaskEither>) => TaskEither = /*#__PURE__*/ - chain(identity) + chainW(identity) /** - * Less strict version of [`flatten`](#flatten). + * Derivable from `Chain`. * * @category combinators - * @since 2.11.0 + * @since 2.0.0 */ -export const flattenW: (mma: TaskEither>) => TaskEither = - /*#__PURE__*/ - chainW(identity) +export const flatten: (mma: TaskEither>) => TaskEither = flattenW /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to From 3d5e2a1cf44c26d37fe5a56e50c3bb0b68f35a95 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 29 Apr 2021 17:16:12 +0200 Subject: [PATCH 117/162] remove dpdm --- package-lock.json | 474 +--------------------------------------------- package.json | 1 - 2 files changed, 7 insertions(+), 468 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2501131ca..dd6274cb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1153,15 +1153,6 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, - "@types/fs-extra": { - "version": "9.0.11", - "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.11.tgz", - "integrity": "sha512-mZsifGG4QeQ7hlkhO56u7zt/ycBgGxSVsFI/6lGTU34VtwkiqrrSDgw0+ygs8kFGWcXnFQWMrzF2h7TtDFNixA==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/glob": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", @@ -1438,12 +1429,6 @@ "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", "dev": true }, - "at-least-node": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", - "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", - "dev": true - }, "atob": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", @@ -1710,12 +1695,6 @@ } } }, - "base64-js": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", - "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", - "dev": true - }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -1735,36 +1714,6 @@ "platform": "^1.3.3" } }, - "bl": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", - "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", - "dev": true, - "requires": { - "buffer": "^5.5.0", - "inherits": "^2.0.4", - "readable-stream": "^3.4.0" - }, - "dependencies": { - "inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true - }, - "readable-stream": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", - "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", - "dev": true, - "requires": { - "inherits": "^2.0.3", - "string_decoder": "^1.1.1", - "util-deprecate": "^1.0.1" - } - } - } - }, "brace-expansion": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", @@ -1825,16 +1774,6 @@ "node-int64": "^0.4.0" } }, - "buffer": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", - "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", - "dev": true, - "requires": { - "base64-js": "^1.3.1", - "ieee754": "^1.1.13" - } - }, "buffer-from": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.0.0.tgz", @@ -1951,21 +1890,6 @@ } } }, - "cli-cursor": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", - "integrity": "sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==", - "dev": true, - "requires": { - "restore-cursor": "^3.1.0" - } - }, - "cli-spinners": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.6.0.tgz", - "integrity": "sha512-t+4/y50K/+4xcCRosKkA7W4gTr1MySvLV0q+PxmG7FJ5g+66ChKurYjxBCjHggHH3HA5Hh9cy+lcUGWDqVH+4Q==", - "dev": true - }, "cliui": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", @@ -1994,12 +1918,6 @@ } } }, - "clone": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", - "integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=", - "dev": true - }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -2221,15 +2139,6 @@ "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", "dev": true }, - "defaults": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.3.tgz", - "integrity": "sha1-xlYFHpgX2f8I7YgUd/P+QBnz730=", - "dev": true, - "requires": { - "clone": "^1.0.2" - } - }, "define-property": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/define-property/-/define-property-2.0.2.tgz", @@ -2440,188 +2349,6 @@ } } }, - "dpdm": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/dpdm/-/dpdm-3.6.0.tgz", - "integrity": "sha512-+DRT3o/5mIbjKENtKUEDX0qOk0xIBDllDITtvHRH+5Yppz2aJAHQkozj3oiIPLWWfup8CuuBl3xWvk5yD5oaOg==", - "dev": true, - "requires": { - "@types/fs-extra": "^9.0.6", - "@types/glob": "^7.1.3", - "@types/yargs": "^15.0.12", - "chalk": "^4.1.0", - "fs-extra": "^9.0.1", - "glob": "^7.1.6", - "ora": "^5.2.0", - "tslib": "^2.1.0", - "typescript": "^4.1.3", - "yargs": "^16.2.0" - }, - "dependencies": { - "@types/yargs": { - "version": "15.0.13", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.13.tgz", - "integrity": "sha512-kQ5JNTrbDv3Rp5X2n/iUu37IJBDU2gsZ5R/g1/KHOOEc5IKfUFjXT6DENPGduh08I/pamwtEq4oul7gUqKTQDQ==", - "dev": true, - "requires": { - "@types/yargs-parser": "*" - } - }, - "ansi-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", - "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", - "dev": true - }, - "ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, - "requires": { - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", - "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", - "dev": true, - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "cliui": { - "version": "7.0.4", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", - "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", - "dev": true, - "requires": { - "string-width": "^4.2.0", - "strip-ansi": "^6.0.0", - "wrap-ansi": "^7.0.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true - }, - "fs-extra": { - "version": "9.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", - "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", - "dev": true, - "requires": { - "at-least-node": "^1.0.0", - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - } - }, - "graceful-fs": { - "version": "4.2.6", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", - "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==", - "dev": true - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true - }, - "jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.1.6", - "universalify": "^2.0.0" - } - }, - "strip-ansi": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", - "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", - "dev": true, - "requires": { - "ansi-regex": "^5.0.0" - } - }, - "supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, - "requires": { - "has-flag": "^4.0.0" - } - }, - "tslib": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.2.0.tgz", - "integrity": "sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==", - "dev": true - }, - "universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true - }, - "wrap-ansi": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", - "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", - "dev": true, - "requires": { - "ansi-styles": "^4.0.0", - "string-width": "^4.1.0", - "strip-ansi": "^6.0.0" - } - }, - "y18n": { - "version": "5.0.8", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", - "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", - "dev": true - }, - "yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", - "dev": true, - "requires": { - "cliui": "^7.0.2", - "escalade": "^3.1.1", - "get-caller-file": "^2.0.5", - "require-directory": "^2.1.1", - "string-width": "^4.2.0", - "y18n": "^5.0.5", - "yargs-parser": "^20.2.2" - } - }, - "yargs-parser": { - "version": "20.2.7", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.7.tgz", - "integrity": "sha512-FiNkvbeHzB/syOjIUxFDCnhSfzAL8R5vs40MgLFBorXACCOAEaWu0gRZl14vG8MR9AOJIZbmkjhusqBYZ3HTHw==", - "dev": true - } - } - }, "dtslint": { "version": "github:gcanti/dtslint#824af7cf7780df9840b87d126835a40da740c064", "from": "github:gcanti/dtslint", @@ -2631,7 +2358,7 @@ "parsimmon": "^1.12.0", "strip-json-comments": "^2.0.1", "tslint": "^5.12.0", - "typescript": "^4.3.0-dev.20210310" + "typescript": "^4.3.0-dev.20210429" }, "dependencies": { "ansi-styles": { @@ -2706,9 +2433,9 @@ } }, "typescript": { - "version": "4.3.0-dev.20210310", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.0-dev.20210310.tgz", - "integrity": "sha512-k/FbuOXh6ASEGg8R468bfD6JJH9wX3woe+NtxcRHk8tXfH4lsU7RR4LZgEKa9ctfq5Df2Kvj2WyyTcHV2XYPsQ==", + "version": "4.3.0-dev.20210429", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.0-dev.20210429.tgz", + "integrity": "sha512-rBkCCg6odkGs1Xc2pKHhjwCCYm2wrx6LvqgbWAquRg7yRVFz6lNtRLfqc+q0Oc5N9BjnlcyWTAi+mqOZzIZUDA==", "dev": true } } @@ -2747,12 +2474,6 @@ "is-arrayish": "^0.2.1" } }, - "escalade": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", - "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", - "dev": true - }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -3436,12 +3157,6 @@ "safer-buffer": ">= 2.1.2 < 3" } }, - "ieee754": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", - "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", - "dev": true - }, "import-local": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.0.2.tgz", @@ -3594,12 +3309,6 @@ "is-extglob": "^2.1.1" } }, - "is-interactive": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", - "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", - "dev": true - }, "is-negated-glob": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-negated-glob/-/is-negated-glob-1.0.0.tgz", @@ -3668,12 +3377,6 @@ "unc-path-regex": "^0.1.2" } }, - "is-unicode-supported": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", - "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", - "dev": true - }, "is-windows": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", @@ -5402,67 +5105,6 @@ "lodash._reinterpolate": "^3.0.0" } }, - "log-symbols": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", - "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", - "dev": true, - "requires": { - "chalk": "^4.1.0", - "is-unicode-supported": "^0.1.0" - }, - "dependencies": { - "ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, - "requires": { - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", - "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", - "dev": true, - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true - }, - "supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, - "requires": { - "has-flag": "^4.0.0" - } - } - } - }, "logging-ts": { "version": "0.3.4", "resolved": "https://registry.npmjs.org/logging-ts/-/logging-ts-0.3.4.tgz", @@ -5974,89 +5616,6 @@ "word-wrap": "~1.2.3" } }, - "ora": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.0.tgz", - "integrity": "sha512-1StwyXQGoU6gdjYkyVcqOLnVlbKj+6yPNNOxJVgpt9t4eksKjiriiHuxktLYkgllwk+D6MbC4ihH84L1udRXPg==", - "dev": true, - "requires": { - "bl": "^4.1.0", - "chalk": "^4.1.0", - "cli-cursor": "^3.1.0", - "cli-spinners": "^2.5.0", - "is-interactive": "^1.0.0", - "is-unicode-supported": "^0.1.0", - "log-symbols": "^4.1.0", - "strip-ansi": "^6.0.0", - "wcwidth": "^1.0.1" - }, - "dependencies": { - "ansi-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", - "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", - "dev": true - }, - "ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, - "requires": { - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", - "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", - "dev": true, - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true - }, - "strip-ansi": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", - "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", - "dev": true, - "requires": { - "ansi-regex": "^5.0.0" - } - }, - "supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, - "requires": { - "has-flag": "^4.0.0" - } - } - } - }, "p-each-series": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-2.1.0.tgz", @@ -6112,9 +5671,9 @@ "dev": true }, "parsimmon": { - "version": "1.16.0", - "resolved": "https://registry.npmjs.org/parsimmon/-/parsimmon-1.16.0.tgz", - "integrity": "sha512-tekGDz2Lny27SQ/5DzJdIK0lqsWwZ667SCLFIDCxaZM7VNgQjyKLbaL7FYPKpbjdxNAXFV/mSxkq5D2fnkW4pA==", + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/parsimmon/-/parsimmon-1.17.0.tgz", + "integrity": "sha512-gp5yNYs0Lyv5Mp6hj+JMzsHaM4Mel0WuK2iHYKX32ActYAQdsSq+t4nVsqlOpUCiMYdTX1wFISLvugrAl9harg==", "dev": true }, "pascalcase": { @@ -6537,16 +6096,6 @@ "integrity": "sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=", "dev": true }, - "restore-cursor": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", - "integrity": "sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==", - "dev": true, - "requires": { - "onetime": "^5.1.0", - "signal-exit": "^3.0.2" - } - }, "ret": { "version": "0.1.15", "resolved": "https://registry.npmjs.org/ret/-/ret-0.1.15.tgz", @@ -8058,15 +7607,6 @@ "makeerror": "1.0.x" } }, - "wcwidth": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", - "integrity": "sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=", - "dev": true, - "requires": { - "defaults": "^1.0.3" - } - }, "webidl-conversions": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-6.1.0.tgz", diff --git a/package.json b/package.json index 6e074b120..6a4f97c56 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ "@types/prettier": "1.10.0", "benchmark": "2.1.4", "docs-ts": "^0.6.7", - "dpdm": "^3.6.0", "dtslint": "github:gcanti/dtslint", "fast-check": "^1.25.1", "glob": "^7.1.6", From 3412ca063535cc126a52aae1b43b80391672a7ed Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 30 Apr 2021 12:03:22 +0200 Subject: [PATCH 118/162] add `NaturalTransformation` module --- CHANGELOG.md | 1 + docs/modules/Array.ts.md | 49 ++-- docs/modules/Either.ts.md | 71 +++--- docs/modules/Eq.ts.md | 2 +- docs/modules/FromEither.ts.md | 22 +- docs/modules/FromIO.ts.md | 12 +- docs/modules/FromReader.ts.md | 8 +- docs/modules/FromState.ts.md | 8 +- docs/modules/FromTask.ts.md | 12 +- docs/modules/FromThese.ts.md | 10 +- docs/modules/IO.ts.md | 2 +- docs/modules/IOEither.ts.md | 69 +++--- docs/modules/NaturalTransformation.ts.md | 287 +++++++++++++++++++++++ docs/modules/NonEmptyArray.ts.md | 2 +- docs/modules/Option.ts.md | 35 +-- docs/modules/OptionT.ts.md | 2 +- docs/modules/Ord.ts.md | 2 +- docs/modules/Ordering.ts.md | 2 +- docs/modules/Pointed.ts.md | 2 +- docs/modules/Predicate.ts.md | 2 +- docs/modules/Profunctor.ts.md | 2 +- docs/modules/Random.ts.md | 2 +- docs/modules/Reader.ts.md | 2 +- docs/modules/ReaderEither.ts.md | 71 +++--- docs/modules/ReaderT.ts.md | 33 ++- docs/modules/ReaderTask.ts.md | 71 +++--- docs/modules/ReaderTaskEither.ts.md | 161 ++++++------- docs/modules/ReadonlyArray.ts.md | 53 +++-- docs/modules/ReadonlyMap.ts.md | 49 ++-- docs/modules/ReadonlyNonEmptyArray.ts.md | 2 +- docs/modules/ReadonlyRecord.ts.md | 6 +- docs/modules/ReadonlySet.ts.md | 51 +++- docs/modules/ReadonlyTuple.ts.md | 2 +- docs/modules/Record.ts.md | 2 +- docs/modules/Refinement.ts.md | 2 +- docs/modules/Ring.ts.md | 2 +- docs/modules/Semigroup.ts.md | 2 +- docs/modules/Semigroupoid.ts.md | 2 +- docs/modules/Semiring.ts.md | 2 +- docs/modules/Separated.ts.md | 2 +- docs/modules/Set.ts.md | 2 +- docs/modules/Show.ts.md | 2 +- docs/modules/State.ts.md | 2 +- docs/modules/StateReaderTaskEither.ts.md | 213 ++++++++--------- docs/modules/StateT.ts.md | 2 +- docs/modules/Store.ts.md | 2 +- docs/modules/Strong.ts.md | 2 +- docs/modules/Task.ts.md | 32 +-- docs/modules/TaskEither.ts.md | 117 ++++----- docs/modules/TaskOption.ts.md | 115 ++++----- docs/modules/TaskThese.ts.md | 117 ++++----- docs/modules/These.ts.md | 6 +- docs/modules/TheseT.ts.md | 2 +- docs/modules/Traced.ts.md | 2 +- docs/modules/Traversable.ts.md | 2 +- docs/modules/TraversableWithIndex.ts.md | 2 +- docs/modules/Tree.ts.md | 2 +- docs/modules/Tuple.ts.md | 2 +- docs/modules/Unfoldable.ts.md | 2 +- docs/modules/ValidationT.ts.md | 2 +- docs/modules/Witherable.ts.md | 2 +- docs/modules/Writer.ts.md | 2 +- docs/modules/WriterT.ts.md | 2 +- docs/modules/index.ts.md | 11 + docs/modules/number.ts.md | 2 +- docs/modules/pipeable.ts.md | 2 +- docs/modules/string.ts.md | 2 +- docs/modules/struct.ts.md | 2 +- docs/modules/void.ts.md | 2 +- dtslint/ts3.5/ReaderT.ts | 23 ++ src/Array.ts | 17 +- src/Either.ts | 2 +- src/Eq.ts | 8 +- src/FromEither.ts | 37 ++- src/FromIO.ts | 22 +- src/FromReader.ts | 14 +- src/FromState.ts | 14 +- src/FromTask.ts | 22 +- src/FromThese.ts | 19 +- src/IO.ts | 14 +- src/IOEither.ts | 13 +- src/NaturalTransformation.ts | 152 ++++++++++++ src/Option.ts | 10 +- src/ReaderEither.ts | 21 +- src/ReaderT.ts | 36 +++ src/ReaderTask.ts | 13 +- src/ReaderTaskEither.ts | 60 ++--- src/ReadonlyArray.ts | 15 +- src/ReadonlyMap.ts | 10 +- src/ReadonlyRecord.ts | 8 +- src/ReadonlySet.ts | 196 +++++++++------- src/StateReaderTaskEither.ts | 102 ++++---- src/Task.ts | 18 +- src/TaskEither.ts | 46 ++-- src/TaskOption.ts | 32 ++- src/TaskThese.ts | 41 ++-- src/These.ts | 5 +- src/index.ts | 5 + test/ReaderT.ts | 14 ++ 99 files changed, 1744 insertions(+), 1019 deletions(-) create mode 100644 docs/modules/NaturalTransformation.ts.md create mode 100644 dtslint/ts3.5/ReaderT.ts create mode 100644 src/NaturalTransformation.ts create mode 100644 test/ReaderT.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b5475d6c1..d799c89d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ high state of flux, you're at risk of it changing without notice. - add `FromThese` module - add `void` module - add `FromReader` module + - add `NaturalTransformation` module - `Alt` - add `altAll` - `Alternative` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 7d6225321..84e59ab10 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -106,8 +106,6 @@ Added in v2.0.0 - [constructors](#constructors) - [append](#append) - [appendW](#appendw) - - [fromEither](#fromeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [makeBy](#makeby) - [prepend](#prepend) @@ -169,6 +167,9 @@ Added in v2.0.0 - [getUnionMonoid](#getunionmonoid) - [getUnionSemigroup](#getunionsemigroup) - [~~array~~](#array) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromOption](#fromoption) - [refinements](#refinements) - [isEmpty](#isempty) - [isNonEmpty](#isnonempty) @@ -1439,28 +1440,6 @@ export declare const appendW: (end: B) => (init: A[]) => NEA.NonEmptyArray Added in v2.11.0 -## fromEither - -Transforms an `Either` to a `Array`. - -**Signature** - -```ts -export declare const fromEither: (e: Either) => A[] -``` - -Added in v2.11.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (ma: Option) => A[] -``` - -Added in v2.11.0 - ## fromPredicate **Signature** @@ -2336,6 +2315,28 @@ export declare const array: FunctorWithIndex1<'Array', number> & Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation21<'Either', 'Array'> +``` + +Added in v2.11.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: NaturalTransformation11<'Option', 'Array'> +``` + +Added in v2.11.0 + # refinements ## isEmpty diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 35eee7cf4..8f5aa4aa6 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -68,7 +68,6 @@ Added in v2.0.0 - [orElseW](#orelsew) - [swap](#swap) - [constructors](#constructors) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [left](#left) - [right](#right) @@ -123,6 +122,8 @@ Added in v2.0.0 - [Either (type alias)](#either-type-alias) - [Left (interface)](#left-interface) - [Right (interface)](#right-interface) +- [natural transformations](#natural-transformations) + - [fromOption](#fromoption) - [refinements](#refinements) - [isLeft](#isleft) - [isRight](#isright) @@ -683,39 +684,6 @@ Added in v2.0.0 # constructors -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => Either -``` - -**Example** - -```ts -import * as E from 'fp-ts/Either' -import { pipe } from 'fp-ts/function' -import * as O from 'fp-ts/Option' - -assert.deepStrictEqual( - pipe( - O.some(1), - E.fromOption(() => 'error') - ), - E.right(1) -) -assert.deepStrictEqual( - pipe( - O.none, - E.fromOption(() => 'error') - ), - E.left('error') -) -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -1427,6 +1395,41 @@ export interface Right { Added in v2.0.0 +# natural transformations + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'Either', E> +``` + +**Example** + +```ts +import * as E from 'fp-ts/Either' +import { pipe } from 'fp-ts/function' +import * as O from 'fp-ts/Option' + +assert.deepStrictEqual( + pipe( + O.some(1), + E.fromOption(() => 'error') + ), + E.right(1) +) +assert.deepStrictEqual( + pipe( + O.none, + E.fromOption(() => 'error') + ), + E.left('error') +) +``` + +Added in v2.0.0 + # refinements ## isLeft diff --git a/docs/modules/Eq.ts.md b/docs/modules/Eq.ts.md index fae691ee3..7a5590c2e 100644 --- a/docs/modules/Eq.ts.md +++ b/docs/modules/Eq.ts.md @@ -132,7 +132,7 @@ Added in v2.0.0 **Signature** ```ts -export declare function fromEquals(equals: (x: A, y: A) => boolean): Eq +export declare const fromEquals: (equals: (x: A, y: A) => boolean) => Eq ``` Added in v2.0.0 diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index b9047488a..5c5af4780 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -239,19 +239,19 @@ Added in v2.10.0 ```ts export declare function fromOption( F: FromEither4 -): (onNone: Lazy) => (ma: Option) => Kind4 +): (onNone: Lazy) => NaturalTransformation14C export declare function fromOption( F: FromEither3 -): (onNone: Lazy) => (ma: Option) => Kind3 +): (onNone: Lazy) => NaturalTransformation13C export declare function fromOption( F: FromEither3C -): (onNone: Lazy) => (ma: Option) => Kind3 +): (onNone: Lazy) => NaturalTransformation13C export declare function fromOption( F: FromEither2 -): (onNone: Lazy) => (ma: Option) => Kind2 +): (onNone: Lazy) => NaturalTransformation12C export declare function fromOption( F: FromEither2C -): (onNone: Lazy) => (ma: Option) => Kind2 +): (onNone: Lazy) => NaturalTransformation12C export declare function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 ``` @@ -324,7 +324,7 @@ Added in v2.10.0 ```ts export interface FromEither1 { readonly URI: F - readonly fromEither: (e: Either) => Kind + readonly fromEither: NaturalTransformation21 } ``` @@ -337,7 +337,7 @@ Added in v2.11.0 ```ts export interface FromEither2 { readonly URI: F - readonly fromEither: (e: Either) => Kind2 + readonly fromEither: NaturalTransformation22 } ``` @@ -351,7 +351,7 @@ Added in v2.10.0 export interface FromEither2C { readonly URI: F readonly _E: E - readonly fromEither: (e: Either) => Kind2 + readonly fromEither: NaturalTransformation22C } ``` @@ -364,7 +364,7 @@ Added in v2.10.0 ```ts export interface FromEither3 { readonly URI: F - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: NaturalTransformation23 } ``` @@ -378,7 +378,7 @@ Added in v2.10.0 export interface FromEither3C { readonly URI: F readonly _E: E - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: NaturalTransformation23C } ``` @@ -391,7 +391,7 @@ Added in v2.10.0 ```ts export interface FromEither4 { readonly URI: F - readonly fromEither: (e: Either) => Kind4 + readonly fromEither: NaturalTransformation24 } ``` diff --git a/docs/modules/FromIO.ts.md b/docs/modules/FromIO.ts.md index ac383a8bf..431a980d4 100644 --- a/docs/modules/FromIO.ts.md +++ b/docs/modules/FromIO.ts.md @@ -157,7 +157,7 @@ Added in v2.10.0 ```ts export interface FromIO1 { readonly URI: F - readonly fromIO: (fa: IO) => Kind + readonly fromIO: NaturalTransformation11 } ``` @@ -170,7 +170,7 @@ Added in v2.10.0 ```ts export interface FromIO2 { readonly URI: F - readonly fromIO: (fa: IO) => Kind2 + readonly fromIO: NaturalTransformation12 } ``` @@ -184,7 +184,7 @@ Added in v2.10.0 export interface FromIO2C { readonly URI: F readonly _E: E - readonly fromIO: (fa: IO) => Kind2 + readonly fromIO: NaturalTransformation12C } ``` @@ -197,7 +197,7 @@ Added in v2.10.0 ```ts export interface FromIO3 { readonly URI: F - readonly fromIO: (fa: IO) => Kind3 + readonly fromIO: NaturalTransformation13 } ``` @@ -211,7 +211,7 @@ Added in v2.10.0 export interface FromIO3C { readonly URI: F readonly _E: E - readonly fromIO: (fa: IO) => Kind3 + readonly fromIO: NaturalTransformation13C } ``` @@ -224,7 +224,7 @@ Added in v2.10.0 ```ts export interface FromIO4 { readonly URI: F - readonly fromIO: (fa: IO) => Kind4 + readonly fromIO: NaturalTransformation14 } ``` diff --git a/docs/modules/FromReader.ts.md b/docs/modules/FromReader.ts.md index 0aeb7ae27..e71afbacd 100644 --- a/docs/modules/FromReader.ts.md +++ b/docs/modules/FromReader.ts.md @@ -166,7 +166,7 @@ Added in v2.11.0 ```ts export interface FromReader2 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind2 + readonly fromReader: NaturalTransformation22 } ``` @@ -179,7 +179,7 @@ Added in v2.11.0 ```ts export interface FromReader3 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind3 + readonly fromReader: NaturalTransformation23R } ``` @@ -193,7 +193,7 @@ Added in v2.11.0 export interface FromReader3C { readonly URI: F readonly _E: E - readonly fromReader: (fa: Reader) => Kind3 + readonly fromReader: NaturalTransformation23RC } ``` @@ -206,7 +206,7 @@ Added in v2.11.0 ```ts export interface FromReader4 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind4 + readonly fromReader: NaturalTransformation24R } ``` diff --git a/docs/modules/FromState.ts.md b/docs/modules/FromState.ts.md index fbe27e6e4..122dc599b 100644 --- a/docs/modules/FromState.ts.md +++ b/docs/modules/FromState.ts.md @@ -166,7 +166,7 @@ Added in v2.11.0 ```ts export interface FromState2 { readonly URI: F - readonly fromState: (fa: State) => Kind2 + readonly fromState: NaturalTransformation22 } ``` @@ -179,7 +179,7 @@ Added in v2.11.0 ```ts export interface FromState3 { readonly URI: F - readonly fromState: (fa: State) => Kind3 + readonly fromState: NaturalTransformation23R } ``` @@ -193,7 +193,7 @@ Added in v2.11.0 export interface FromState3C { readonly URI: F readonly _E: E - readonly fromState: (fa: State) => Kind3 + readonly fromState: NaturalTransformation23RC } ``` @@ -206,7 +206,7 @@ Added in v2.11.0 ```ts export interface FromState4 { readonly URI: F - readonly fromState: (fa: State) => Kind4 + readonly fromState: NaturalTransformation24S } ``` diff --git a/docs/modules/FromTask.ts.md b/docs/modules/FromTask.ts.md index c1f68f198..67bc379be 100644 --- a/docs/modules/FromTask.ts.md +++ b/docs/modules/FromTask.ts.md @@ -155,7 +155,7 @@ Added in v2.10.0 ```ts export interface FromTask1 extends FromIO1 { - readonly fromTask: (fa: Task) => Kind + readonly fromTask: NaturalTransformation11 } ``` @@ -167,7 +167,7 @@ Added in v2.10.0 ```ts export interface FromTask2 extends FromIO2 { - readonly fromTask: (fa: Task) => Kind2 + readonly fromTask: NaturalTransformation12 } ``` @@ -179,7 +179,7 @@ Added in v2.10.0 ```ts export interface FromTask2C extends FromIO2C { - readonly fromTask: (fa: Task) => Kind2 + readonly fromTask: NaturalTransformation12C } ``` @@ -191,7 +191,7 @@ Added in v2.10.0 ```ts export interface FromTask3 extends FromIO3 { - readonly fromTask: (fa: Task) => Kind3 + readonly fromTask: NaturalTransformation13 } ``` @@ -203,7 +203,7 @@ Added in v2.10.0 ```ts export interface FromTask3C extends FromIO3C { - readonly fromTask: (fa: Task) => Kind3 + readonly fromTask: NaturalTransformation13C } ``` @@ -215,7 +215,7 @@ Added in v2.10.0 ```ts export interface FromTask4 extends FromIO4 { - readonly fromTask: (fa: Task) => Kind4 + readonly fromTask: NaturalTransformation14 } ``` diff --git a/docs/modules/FromThese.ts.md b/docs/modules/FromThese.ts.md index 69d434c0b..41ee328a0 100644 --- a/docs/modules/FromThese.ts.md +++ b/docs/modules/FromThese.ts.md @@ -77,7 +77,7 @@ Added in v2.11.0 ```ts export interface FromThese2 { readonly URI: F - readonly fromThese: (e: These) => Kind2 + readonly fromThese: NaturalTransformation22 } ``` @@ -91,7 +91,7 @@ Added in v2.11.0 export interface FromThese2C { readonly URI: F readonly _E: E - readonly fromThese: (e: These) => Kind2 + readonly fromThese: NaturalTransformation22C } ``` @@ -104,7 +104,7 @@ Added in v2.11.0 ```ts export interface FromThese3 { readonly URI: F - readonly fromThese: (e: These) => Kind3 + readonly fromThese: NaturalTransformation23 } ``` @@ -118,7 +118,7 @@ Added in v2.11.0 export interface FromThese3C { readonly URI: F readonly _E: E - readonly fromThese: (e: These) => Kind3 + readonly fromThese: NaturalTransformation23C } ``` @@ -131,7 +131,7 @@ Added in v2.11.0 ```ts export interface FromThese4 { readonly URI: F - readonly fromThese: (e: These) => Kind4 + readonly fromThese: NaturalTransformation24 } ``` diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index df3300212..ddb8762e7 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -197,7 +197,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const fromIO: (fa: IO) => IO +export declare const fromIO: NaturalTransformation11<'IO', 'IO'> ``` Added in v2.7.0 diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 3c3047ef9..f25093bce 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -58,9 +58,6 @@ Added in v2.0.0 - [orLeft](#orleft) - [swap](#swap) - [constructors](#constructors) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [left](#left) - [leftIO](#leftio) @@ -107,6 +104,10 @@ Added in v2.0.0 - [tryCatchK](#trycatchk) - [model](#model) - [IOEither (interface)](#ioeither-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromOption](#fromoption) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -559,36 +560,6 @@ Added in v2.0.0 # constructors -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: E.Either) => IOEither -``` - -Added in v2.0.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: I.IO) => IOEither -``` - -Added in v2.7.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => IOEither -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -1072,6 +1043,38 @@ export interface IOEither extends IO> {} Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation22<'Either', 'IOEither'> +``` + +Added in v2.0.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation12<'IO', 'IOEither'> +``` + +Added in v2.7.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'IOEither', E> +``` + +Added in v2.0.0 + # utils ## Do diff --git a/docs/modules/NaturalTransformation.ts.md b/docs/modules/NaturalTransformation.ts.md new file mode 100644 index 000000000..5494652e9 --- /dev/null +++ b/docs/modules/NaturalTransformation.ts.md @@ -0,0 +1,287 @@ +--- +title: NaturalTransformation.ts +nav_order: 65 +parent: Modules +--- + +## NaturalTransformation overview + +A type for natural transformations. + +A natural transformation is a mapping between type constructors of kind `* -> *` where the mapping +operation has no ability to manipulate the inner values. + +The definition of a natural transformation in category theory states that `F` and `G` should be functors, +but the `Functor` constraint is not enforced here; that the types are of kind `* -> *` is enough for our purposes. + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [utils](#utils) + - [NaturalTransformation (interface)](#naturaltransformation-interface) + - [NaturalTransformation11 (interface)](#naturaltransformation11-interface) + - [NaturalTransformation12 (interface)](#naturaltransformation12-interface) + - [NaturalTransformation12C (interface)](#naturaltransformation12c-interface) + - [NaturalTransformation13 (interface)](#naturaltransformation13-interface) + - [NaturalTransformation13C (interface)](#naturaltransformation13c-interface) + - [NaturalTransformation14 (interface)](#naturaltransformation14-interface) + - [NaturalTransformation14C (interface)](#naturaltransformation14c-interface) + - [NaturalTransformation21 (interface)](#naturaltransformation21-interface) + - [NaturalTransformation22 (interface)](#naturaltransformation22-interface) + - [NaturalTransformation22C (interface)](#naturaltransformation22c-interface) + - [NaturalTransformation23 (interface)](#naturaltransformation23-interface) + - [NaturalTransformation23C (interface)](#naturaltransformation23c-interface) + - [NaturalTransformation23R (interface)](#naturaltransformation23r-interface) + - [NaturalTransformation23RC (interface)](#naturaltransformation23rc-interface) + - [NaturalTransformation24 (interface)](#naturaltransformation24-interface) + - [NaturalTransformation24R (interface)](#naturaltransformation24r-interface) + - [NaturalTransformation24S (interface)](#naturaltransformation24s-interface) + - [NaturalTransformation33 (interface)](#naturaltransformation33-interface) + - [NaturalTransformation34 (interface)](#naturaltransformation34-interface) + +--- + +# utils + +## NaturalTransformation (interface) + +**Signature** + +```ts +export interface NaturalTransformation { +
(fa: HKT): HKT +} +``` + +Added in v2.11.0 + +## NaturalTransformation11 (interface) + +**Signature** + +```ts +export interface NaturalTransformation11 { + (fa: Kind): Kind +} +``` + +Added in v2.11.0 + +## NaturalTransformation12 (interface) + +**Signature** + +```ts +export interface NaturalTransformation12 { + (fa: Kind): Kind2 +} +``` + +Added in v2.11.0 + +## NaturalTransformation12C (interface) + +**Signature** + +```ts +export interface NaturalTransformation12C { + (fa: Kind): Kind2 +} +``` + +Added in v2.11.0 + +## NaturalTransformation13 (interface) + +**Signature** + +```ts +export interface NaturalTransformation13 { + (fa: Kind): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation13C (interface) + +**Signature** + +```ts +export interface NaturalTransformation13C { + (fa: Kind): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation14 (interface) + +**Signature** + +```ts +export interface NaturalTransformation14 { + (fa: Kind): Kind4 +} +``` + +Added in v2.11.0 + +## NaturalTransformation14C (interface) + +**Signature** + +```ts +export interface NaturalTransformation14C { + (fa: Kind): Kind4 +} +``` + +Added in v2.11.0 + +## NaturalTransformation21 (interface) + +**Signature** + +```ts +export interface NaturalTransformation21 { + (fa: Kind2): Kind +} +``` + +Added in v2.11.0 + +## NaturalTransformation22 (interface) + +**Signature** + +```ts +export interface NaturalTransformation22 { + (fa: Kind2): Kind2 +} +``` + +Added in v2.11.0 + +## NaturalTransformation22C (interface) + +**Signature** + +```ts +export interface NaturalTransformation22C { + (fa: Kind2): Kind2 +} +``` + +Added in v2.11.0 + +## NaturalTransformation23 (interface) + +**Signature** + +```ts +export interface NaturalTransformation23 { + (fa: Kind2): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation23C (interface) + +**Signature** + +```ts +export interface NaturalTransformation23C { + (fa: Kind2): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation23R (interface) + +**Signature** + +```ts +export interface NaturalTransformation23R { + (fa: Kind2): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation23RC (interface) + +**Signature** + +```ts +export interface NaturalTransformation23RC { + (fa: Kind2): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation24 (interface) + +**Signature** + +```ts +export interface NaturalTransformation24 { + (fa: Kind2): Kind4 +} +``` + +Added in v2.11.0 + +## NaturalTransformation24R (interface) + +**Signature** + +```ts +export interface NaturalTransformation24R { + (fa: Kind2): Kind4 +} +``` + +Added in v2.11.0 + +## NaturalTransformation24S (interface) + +**Signature** + +```ts +export interface NaturalTransformation24S { + (fa: Kind2): Kind4 +} +``` + +Added in v2.11.0 + +## NaturalTransformation33 (interface) + +**Signature** + +```ts +export interface NaturalTransformation33 { + (fa: Kind3): Kind3 +} +``` + +Added in v2.11.0 + +## NaturalTransformation34 (interface) + +**Signature** + +```ts +export interface NaturalTransformation34 { + (fa: Kind3): Kind4 +} +``` + +Added in v2.11.0 diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 45acdebbe..ec85f418e 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -1,6 +1,6 @@ --- title: NonEmptyArray.ts -nav_order: 65 +nav_order: 66 parent: Modules --- diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index aadecf7dd..a83709a8b 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1,6 +1,6 @@ --- title: Option.ts -nav_order: 67 +nav_order: 68 parent: Modules --- @@ -69,7 +69,6 @@ Added in v2.0.0 - [fromEitherK](#fromeitherk) - [~~mapNullable~~](#mapnullable) - [constructors](#constructors) - - [fromEither](#fromeither) - [fromPredicate](#frompredicate) - [getLeft](#getleft) - [getRight](#getright) @@ -122,6 +121,8 @@ Added in v2.0.0 - [None (interface)](#none-interface) - [Option (type alias)](#option-type-alias) - [Some (interface)](#some-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) - [refinements](#refinements) - [isNone](#isnone) - [isSome](#issome) @@ -544,20 +545,6 @@ Added in v2.0.0 # constructors -## fromEither - -Transforms an `Either` to an `Option` discarding the error. - -Alias of [getRight](#getright) - -**Signature** - -```ts -export declare const fromEither: (ma: Either) => Option -``` - -Added in v2.0.0 - ## fromPredicate Returns a _smart constructor_ based on the given predicate. @@ -1413,6 +1400,22 @@ export interface Some { Added in v2.0.0 +# natural transformations + +## fromEither + +Transforms an `Either` to an `Option` discarding the error. + +Alias of [getRight](#getright) + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation21<'Either', 'Option'> +``` + +Added in v2.0.0 + # refinements ## isNone diff --git a/docs/modules/OptionT.ts.md b/docs/modules/OptionT.ts.md index e2ad94820..fa62f23bd 100644 --- a/docs/modules/OptionT.ts.md +++ b/docs/modules/OptionT.ts.md @@ -1,6 +1,6 @@ --- title: OptionT.ts -nav_order: 68 +nav_order: 69 parent: Modules --- diff --git a/docs/modules/Ord.ts.md b/docs/modules/Ord.ts.md index c5be4b861..c820d4ac7 100644 --- a/docs/modules/Ord.ts.md +++ b/docs/modules/Ord.ts.md @@ -1,6 +1,6 @@ --- title: Ord.ts -nav_order: 69 +nav_order: 70 parent: Modules --- diff --git a/docs/modules/Ordering.ts.md b/docs/modules/Ordering.ts.md index 57c87feba..b2d67e09d 100644 --- a/docs/modules/Ordering.ts.md +++ b/docs/modules/Ordering.ts.md @@ -1,6 +1,6 @@ --- title: Ordering.ts -nav_order: 70 +nav_order: 71 parent: Modules --- diff --git a/docs/modules/Pointed.ts.md b/docs/modules/Pointed.ts.md index e8b6b0744..1ad51985a 100644 --- a/docs/modules/Pointed.ts.md +++ b/docs/modules/Pointed.ts.md @@ -1,6 +1,6 @@ --- title: Pointed.ts -nav_order: 72 +nav_order: 73 parent: Modules --- diff --git a/docs/modules/Predicate.ts.md b/docs/modules/Predicate.ts.md index 8ece26016..fad120be6 100644 --- a/docs/modules/Predicate.ts.md +++ b/docs/modules/Predicate.ts.md @@ -1,6 +1,6 @@ --- title: Predicate.ts -nav_order: 73 +nav_order: 74 parent: Modules --- diff --git a/docs/modules/Profunctor.ts.md b/docs/modules/Profunctor.ts.md index 2a854bbdc..9ca9d754d 100644 --- a/docs/modules/Profunctor.ts.md +++ b/docs/modules/Profunctor.ts.md @@ -1,6 +1,6 @@ --- title: Profunctor.ts -nav_order: 74 +nav_order: 75 parent: Modules --- diff --git a/docs/modules/Random.ts.md b/docs/modules/Random.ts.md index 7d7ff3534..c87b9fccb 100644 --- a/docs/modules/Random.ts.md +++ b/docs/modules/Random.ts.md @@ -1,6 +1,6 @@ --- title: Random.ts -nav_order: 75 +nav_order: 76 parent: Modules --- diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index ef75ec05e..905f989c5 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -1,6 +1,6 @@ --- title: Reader.ts -nav_order: 76 +nav_order: 77 parent: Modules --- diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index c15fd3b50..9e61a907b 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderEither.ts -nav_order: 77 +nav_order: 78 parent: Modules --- @@ -62,10 +62,7 @@ Added in v2.0.0 - [constructors](#constructors) - [ask](#ask) - [asks](#asks) - - [fromEither](#fromeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromReader](#fromreader) - [left](#left) - [leftReader](#leftreader) - [right](#right) @@ -106,6 +103,10 @@ Added in v2.0.0 - [toUnion](#tounion) - [model](#model) - [ReaderEither (interface)](#readereither-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromOption](#fromoption) + - [fromReader](#fromreader) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -680,26 +681,6 @@ export declare const asks: (f: (r: R) => A) => ReaderEither(e: E.Either) => ReaderEither -``` - -Added in v2.0.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => ReaderEither -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -713,16 +694,6 @@ export declare const fromPredicate: { Added in v2.0.0 -## fromReader - -**Signature** - -```ts -export declare const fromReader: (ma: R.Reader) => ReaderEither -``` - -Added in v2.11.0 - ## left **Signature** @@ -1138,6 +1109,38 @@ export interface ReaderEither extends Reader> {} Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation23<'Either', 'ReaderEither'> +``` + +Added in v2.0.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation13C<'Option', 'ReaderEither', E> +``` + +Added in v2.0.0 + +## fromReader + +**Signature** + +```ts +export declare const fromReader: NaturalTransformation23R<'Reader', 'ReaderEither'> +``` + +Added in v2.11.0 + # utils ## Do diff --git a/docs/modules/ReaderT.ts.md b/docs/modules/ReaderT.ts.md index cbd4df628..9876700ea 100644 --- a/docs/modules/ReaderT.ts.md +++ b/docs/modules/ReaderT.ts.md @@ -1,6 +1,6 @@ --- title: ReaderT.ts -nav_order: 78 +nav_order: 79 parent: Modules --- @@ -12,6 +12,8 @@ Added in v2.0.0

Table of contents

+- [constructors](#constructors) + - [fromNaturalTransformation](#fromnaturaltransformation) - [model](#model) - [~~ReaderT1~~ (interface)](#readert1-interface) - [~~ReaderT2~~ (interface)](#readert2-interface) @@ -32,6 +34,35 @@ Added in v2.0.0 --- +# constructors + +## fromNaturalTransformation + +**Signature** + +```ts +export declare function fromNaturalTransformation( + nt: NaturalTransformation24S +): (f: (r: R) => Kind2) => Reader> +export declare function fromNaturalTransformation( + nt: NaturalTransformation23R +): (f: (r: R) => Kind2) => Reader> +export declare function fromNaturalTransformation( + nt: NaturalTransformation22 +): (f: (r: R) => Kind2) => Reader> +export declare function fromNaturalTransformation( + nt: NaturalTransformation12 +): (f: (r: R) => Kind) => Reader> +export declare function fromNaturalTransformation( + nt: NaturalTransformation11 +): (f: (r: R) => Kind) => Reader> +export declare function fromNaturalTransformation( + nt: NaturalTransformation +): (f: (r: R) => HKT) => Reader> +``` + +Added in v2.11.0 + # model ## ~~ReaderT1~~ (interface) diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index ae5b64464..64c9b66a6 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTask.ts -nav_order: 79 +nav_order: 80 parent: Modules --- @@ -47,9 +47,6 @@ Added in v2.3.0 - [constructors](#constructors) - [ask](#ask) - [asks](#asks) - - [fromIO](#fromio) - - [fromReader](#fromreader) - - [fromTask](#fromtask) - [instances](#instances) - [ApplicativePar](#applicativepar) - [ApplicativeSeq](#applicativeseq) @@ -72,6 +69,10 @@ Added in v2.3.0 - [~~readerTask~~](#readertask) - [model](#model) - [ReaderTask (interface)](#readertask-interface) +- [natural transformations](#natural-transformations) + - [fromIO](#fromio) + - [fromReader](#fromreader) + - [fromTask](#fromtask) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -454,36 +455,6 @@ export declare const asks: (f: (r: R) => A) => ReaderTask Added in v2.3.0 -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO
) => ReaderTask -``` - -Added in v2.3.0 - -## fromReader - -**Signature** - -```ts -export declare const fromReader: (ma: R.Reader) => ReaderTask -``` - -Added in v2.3.0 - -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: T.Task) => ReaderTask -``` - -Added in v2.3.0 - # instances ## ApplicativePar @@ -698,6 +669,38 @@ export interface ReaderTask { Added in v2.3.0 +# natural transformations + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation12<'IO', 'ReaderTask'> +``` + +Added in v2.3.0 + +## fromReader + +**Signature** + +```ts +export declare const fromReader: NaturalTransformation22<'Reader', 'ReaderTask'> +``` + +Added in v2.3.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation12<'Task', 'ReaderTask'> +``` + +Added in v2.3.0 + # utils ## Do diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 8cf34ffaa..0e2049ff3 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: ReaderTaskEither.ts -nav_order: 80 +nav_order: 81 parent: Modules --- @@ -84,15 +84,8 @@ Added in v2.0.0 - [constructors](#constructors) - [ask](#ask) - [asks](#asks) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromIOEither](#fromioeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromReader](#fromreader) - [fromReaderEither](#fromreadereither) - - [fromTask](#fromtask) - - [fromTaskEither](#fromtaskeither) - [left](#left) - [leftIO](#leftio) - [leftReader](#leftreader) @@ -146,6 +139,14 @@ Added in v2.0.0 - [toUnion](#tounion) - [model](#model) - [ReaderTaskEither (interface)](#readertaskeither-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromIOEither](#fromioeither) + - [fromOption](#fromoption) + - [fromReader](#fromreader) + - [fromTask](#fromtask) + - [fromTaskEither](#fromtaskeither) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -1005,46 +1006,6 @@ export declare const asks: (f: (r: R) => A) => ReaderTaskEither Added in v2.0.0 -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: E.Either) => ReaderTaskEither -``` - -Added in v2.0.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => ReaderTaskEither -``` - -Added in v2.0.0 - -## fromIOEither - -**Signature** - -```ts -export declare const fromIOEither: (ma: IOEither) => ReaderTaskEither -``` - -Added in v2.0.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => ReaderTaskEither -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -1058,42 +1019,12 @@ export declare const fromPredicate: { Added in v2.0.0 -## fromReader - -**Signature** - -```ts -export declare const fromReader: (fa: R.Reader) => ReaderTaskEither -``` - -Added in v2.11.0 - ## fromReaderEither **Signature** ```ts -export declare const fromReaderEither: (ma: ReaderEither) => ReaderTaskEither -``` - -Added in v2.0.0 - -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: T.Task) => ReaderTaskEither -``` - -Added in v2.0.0 - -## fromTaskEither - -**Signature** - -```ts -export declare const fromTaskEither: (ma: TE.TaskEither) => ReaderTaskEither +export declare const fromReaderEither: NaturalTransformation33<'ReaderEither', 'ReaderTaskEither'> ``` Added in v2.0.0 @@ -1655,6 +1586,78 @@ export interface ReaderTaskEither { Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation23<'Either', 'ReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation13<'IO', 'ReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromIOEither + +**Signature** + +```ts +export declare const fromIOEither: NaturalTransformation23<'IOEither', 'ReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation13C<'Option', 'ReaderTaskEither', E> +``` + +Added in v2.0.0 + +## fromReader + +**Signature** + +```ts +export declare const fromReader: NaturalTransformation23R<'Reader', 'ReaderTaskEither'> +``` + +Added in v2.11.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation13<'Task', 'ReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromTaskEither + +**Signature** + +```ts +export declare const fromTaskEither: NaturalTransformation23<'TaskEither', 'ReaderTaskEither'> +``` + +Added in v2.0.0 + # utils ## Do diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index d17eb2982..dc779f85a 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyArray.ts -nav_order: 81 +nav_order: 82 parent: Modules --- @@ -105,8 +105,6 @@ Added in v2.5.0 - [constructors](#constructors) - [append](#append) - [appendW](#appendw) - - [fromEither](#fromeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [makeBy](#makeby) - [prepend](#prepend) @@ -162,6 +160,9 @@ Added in v2.5.0 - [interop](#interop) - [fromArray](#fromarray) - [toArray](#toarray) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromOption](#fromoption) - [refinements](#refinements) - [isEmpty](#isempty) - [isNonEmpty](#isnonempty) @@ -1478,28 +1479,6 @@ export declare const appendW: (end: B) => (init: readonly A[]) => RNEA.Rea Added in v2.11.0 -## fromEither - -Transforms an `Either` to a `ReadonlyArray`. - -**Signature** - -```ts -export declare const fromEither: (e: Either) => readonly A[] -``` - -Added in v2.11.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (ma: Option) => readonly A[] -``` - -Added in v2.11.0 - ## fromPredicate **Signature** @@ -2174,6 +2153,30 @@ export declare const toArray: (as: readonly A[]) => A[] Added in v2.5.0 +# natural transformations + +## fromEither + +Transforms an `Either` to a `ReadonlyArray`. + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation21<'Either', 'ReadonlyArray'> +``` + +Added in v2.11.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: NaturalTransformation11<'Option', 'ReadonlyArray'> +``` + +Added in v2.11.0 + # refinements ## isEmpty diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index c7cc120b3..84f9a6ee5 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyMap.ts -nav_order: 82 +nav_order: 83 parent: Modules --- @@ -35,10 +35,8 @@ Added in v2.5.0 - [~~insertAt~~](#insertat) - [constructors](#constructors) - [fromFoldable](#fromfoldable) - - [fromMap](#frommap) - [singleton](#singleton) - [destructors](#destructors) - - [toMap](#tomap) - [toUnfoldable](#tounfoldable) - [instances](#instances) - [Compactable](#compactable-1) @@ -61,6 +59,9 @@ Added in v2.5.0 - [getUnionSemigroup](#getunionsemigroup) - [getWitherable](#getwitherable) - [~~readonlyMap~~](#readonlymap) +- [interop](#interop) + - [fromMap](#frommap) + - [toMap](#tomap) - [utils](#utils) - [collect](#collect) - [difference](#difference) @@ -332,16 +333,6 @@ export declare function fromFoldable( Added in v2.5.0 -## fromMap - -**Signature** - -```ts -export declare function fromMap(m: Map): ReadonlyMap -``` - -Added in v2.5.0 - ## singleton Create a map with one key/value pair @@ -356,16 +347,6 @@ Added in v2.5.0 # destructors -## toMap - -**Signature** - -```ts -export declare function toMap(m: ReadonlyMap): Map -``` - -Added in v2.5.0 - ## toUnfoldable Unfolds a map into a list of key/value pairs @@ -591,6 +572,28 @@ export declare const readonlyMap: Filterable2<'ReadonlyMap'> Added in v2.5.0 +# interop + +## fromMap + +**Signature** + +```ts +export declare const fromMap: (m: Map) => ReadonlyMap +``` + +Added in v2.5.0 + +## toMap + +**Signature** + +```ts +export declare function toMap(m: ReadonlyMap): Map +``` + +Added in v2.5.0 + # utils ## collect diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 96341783a..02d652b53 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyNonEmptyArray.ts -nav_order: 83 +nav_order: 84 parent: Modules --- diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 898d91e75..c8e87aab0 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyRecord.ts -nav_order: 84 +nav_order: 85 parent: Modules --- @@ -694,7 +694,7 @@ Added in v2.5.0 **Signature** ```ts -export declare function fromRecord(r: Record): ReadonlyRecord +export declare const fromRecord: (r: Record) => Readonly> ``` Added in v2.5.0 @@ -704,7 +704,7 @@ Added in v2.5.0 **Signature** ```ts -export declare function toRecord(r: ReadonlyRecord): Record +export declare const toRecord: (r: Readonly>) => Record ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 98c09760f..cac462735 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlySet.ts -nav_order: 85 +nav_order: 86 parent: Modules --- @@ -26,18 +26,21 @@ Added in v2.5.0 - [union](#union) - [constructors](#constructors) - [fromReadonlyArray](#fromreadonlyarray) - - [fromSet](#fromset) - [singleton](#singleton) - [~~fromArray~~](#fromarray) - [destructors](#destructors) - [toSet](#toset) - [instances](#instances) + - [URI](#uri) + - [URI (type alias)](#uri-type-alias) - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getIntersectionSemigroup](#getintersectionsemigroup) - [getShow](#getshow) - [getUnionMonoid](#getunionmonoid) - [getUnionSemigroup](#getunionsemigroup) +- [interop](#interop) + - [fromSet](#fromset) - [utils](#utils) - [elem](#elem) - [empty](#empty) @@ -214,7 +217,7 @@ Added in v2.5.0 ## fromReadonlyArray -Create a set from an array +Create a `ReadonlySet` from a `ReadonlyArray` **Signature** @@ -224,16 +227,6 @@ export declare const fromReadonlyArray: (E: Eq) => (as: readonly A[]) => R Added in v2.10.0 -## fromSet - -**Signature** - -```ts -export declare function fromSet(s: Set): ReadonlySet -``` - -Added in v2.5.0 - ## singleton Create a set with one element @@ -272,6 +265,26 @@ Added in v2.5.0 # instances +## URI + +**Signature** + +```ts +export declare const URI: 'ReadonlySet' +``` + +Added in v2.11.0 + +## URI (type alias) + +**Signature** + +```ts +export type URI = typeof URI +``` + +Added in v2.11.0 + ## getDifferenceMagma **Signature** @@ -332,6 +345,18 @@ export declare const getUnionSemigroup: (E: Eq) => Semigroup(s: Set) => ReadonlySet +``` + +Added in v2.5.0 + # utils ## elem diff --git a/docs/modules/ReadonlyTuple.ts.md b/docs/modules/ReadonlyTuple.ts.md index 783b2db9c..a3834ef05 100644 --- a/docs/modules/ReadonlyTuple.ts.md +++ b/docs/modules/ReadonlyTuple.ts.md @@ -1,6 +1,6 @@ --- title: ReadonlyTuple.ts -nav_order: 86 +nav_order: 87 parent: Modules --- diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index f33edbbbe..79216bf89 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -1,6 +1,6 @@ --- title: Record.ts -nav_order: 87 +nav_order: 88 parent: Modules --- diff --git a/docs/modules/Refinement.ts.md b/docs/modules/Refinement.ts.md index ada4c1ee0..6672377d0 100644 --- a/docs/modules/Refinement.ts.md +++ b/docs/modules/Refinement.ts.md @@ -1,6 +1,6 @@ --- title: Refinement.ts -nav_order: 88 +nav_order: 89 parent: Modules --- diff --git a/docs/modules/Ring.ts.md b/docs/modules/Ring.ts.md index 888d095ae..00be1e3e6 100644 --- a/docs/modules/Ring.ts.md +++ b/docs/modules/Ring.ts.md @@ -1,6 +1,6 @@ --- title: Ring.ts -nav_order: 89 +nav_order: 90 parent: Modules --- diff --git a/docs/modules/Semigroup.ts.md b/docs/modules/Semigroup.ts.md index a98fd8d66..9193ee3d7 100644 --- a/docs/modules/Semigroup.ts.md +++ b/docs/modules/Semigroup.ts.md @@ -1,6 +1,6 @@ --- title: Semigroup.ts -nav_order: 90 +nav_order: 91 parent: Modules --- diff --git a/docs/modules/Semigroupoid.ts.md b/docs/modules/Semigroupoid.ts.md index 6737fb0c8..3324c3671 100644 --- a/docs/modules/Semigroupoid.ts.md +++ b/docs/modules/Semigroupoid.ts.md @@ -1,6 +1,6 @@ --- title: Semigroupoid.ts -nav_order: 91 +nav_order: 92 parent: Modules --- diff --git a/docs/modules/Semiring.ts.md b/docs/modules/Semiring.ts.md index 76cc78af2..a08adeccf 100644 --- a/docs/modules/Semiring.ts.md +++ b/docs/modules/Semiring.ts.md @@ -1,6 +1,6 @@ --- title: Semiring.ts -nav_order: 92 +nav_order: 93 parent: Modules --- diff --git a/docs/modules/Separated.ts.md b/docs/modules/Separated.ts.md index 40697b649..2674767f6 100644 --- a/docs/modules/Separated.ts.md +++ b/docs/modules/Separated.ts.md @@ -1,6 +1,6 @@ --- title: Separated.ts -nav_order: 93 +nav_order: 94 parent: Modules --- diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 0e7be1fe8..15bae8116 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -1,6 +1,6 @@ --- title: Set.ts -nav_order: 94 +nav_order: 95 parent: Modules --- diff --git a/docs/modules/Show.ts.md b/docs/modules/Show.ts.md index e2500bd2f..7d825f609 100644 --- a/docs/modules/Show.ts.md +++ b/docs/modules/Show.ts.md @@ -1,6 +1,6 @@ --- title: Show.ts -nav_order: 95 +nav_order: 96 parent: Modules --- diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index e0932aa01..7adcab45d 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -1,6 +1,6 @@ --- title: State.ts -nav_order: 96 +nav_order: 97 parent: Modules --- diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 2e25ef7f0..a79aa28ee 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -1,6 +1,6 @@ --- title: StateReaderTaskEither.ts -nav_order: 97 +nav_order: 98 parent: Modules --- @@ -73,17 +73,8 @@ Added in v2.0.0 - [constructors](#constructors) - [ask](#ask) - [asks](#asks) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromIOEither](#fromioeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromReader](#fromreader) - - [fromReaderEither](#fromreadereither) - [fromReaderTaskEither](#fromreadertaskeither) - - [fromState](#fromstate) - - [fromTask](#fromtask) - - [fromTaskEither](#fromtaskeither) - [get](#get) - [gets](#gets) - [left](#left) @@ -121,6 +112,16 @@ Added in v2.0.0 - [~~stateReaderTaskEither~~](#statereadertaskeither) - [model](#model) - [StateReaderTaskEither (interface)](#statereadertaskeither-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromIOEither](#fromioeither) + - [fromOption](#fromoption) + - [fromReader](#fromreader) + - [fromReaderEither](#fromreadereither) + - [fromState](#fromstate) + - [fromTask](#fromtask) + - [fromTaskEither](#fromtaskeither) - [utils](#utils) - [apS](#aps) - [apSW](#apsw) @@ -767,9 +768,9 @@ Added in v2.11.0 **Signature** ```ts -export declare function fromReaderTaskEitherK, B>( - f: (...a: A) => ReaderTaskEither -): (...a: A) => StateReaderTaskEither +export declare const fromReaderTaskEitherK: ( + f: (...a: A) => RTE.ReaderTaskEither +) => (...a: A) => StateReaderTaskEither ``` Added in v2.4.0 @@ -851,46 +852,6 @@ export declare const asks: (f: (r: R) => A) => StateReaderTa Added in v2.11.0 -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: E.Either) => StateReaderTaskEither -``` - -Added in v2.0.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => StateReaderTaskEither -``` - -Added in v2.7.0 - -## fromIOEither - -**Signature** - -```ts -export declare function fromIOEither(ma: IOEither): StateReaderTaskEither -``` - -Added in v2.0.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => StateReaderTaskEither -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -906,64 +867,12 @@ export declare const fromPredicate: { Added in v2.4.4 -## fromReader - -**Signature** - -```ts -export declare const fromReader: (ma: R.Reader) => StateReaderTaskEither -``` - -Added in v2.11.0 - -## fromReaderEither - -**Signature** - -```ts -export declare function fromReaderEither(ma: ReaderEither): StateReaderTaskEither -``` - -Added in v2.0.0 - ## fromReaderTaskEither **Signature** ```ts -export declare const fromReaderTaskEither: ( - ma: RTE.ReaderTaskEither -) => StateReaderTaskEither -``` - -Added in v2.0.0 - -## fromState - -**Signature** - -```ts -export declare const fromState: (sa: State) => StateReaderTaskEither -``` - -Added in v2.10.0 - -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: Task) => StateReaderTaskEither -``` - -Added in v2.7.0 - -## fromTaskEither - -**Signature** - -```ts -export declare function fromTaskEither(ma: TaskEither): StateReaderTaskEither +export declare const fromReaderTaskEither: NaturalTransformation34<'ReaderTaskEither', 'StateReaderTaskEither'> ``` Added in v2.0.0 @@ -1344,6 +1253,98 @@ export interface StateReaderTaskEither { Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation24<'Either', 'StateReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation14<'IO', 'StateReaderTaskEither'> +``` + +Added in v2.7.0 + +## fromIOEither + +**Signature** + +```ts +export declare const fromIOEither: NaturalTransformation24<'IOEither', 'StateReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation14C<'Option', 'StateReaderTaskEither', E> +``` + +Added in v2.0.0 + +## fromReader + +**Signature** + +```ts +export declare const fromReader: NaturalTransformation24R<'Reader', 'StateReaderTaskEither'> +``` + +Added in v2.11.0 + +## fromReaderEither + +**Signature** + +```ts +export declare const fromReaderEither: NaturalTransformation34<'ReaderEither', 'StateReaderTaskEither'> +``` + +Added in v2.0.0 + +## fromState + +**Signature** + +```ts +export declare const fromState: NaturalTransformation24S<'State', 'StateReaderTaskEither'> +``` + +Added in v2.10.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation14<'Task', 'StateReaderTaskEither'> +``` + +Added in v2.7.0 + +## fromTaskEither + +**Signature** + +```ts +export declare const fromTaskEither: NaturalTransformation24<'TaskEither', 'StateReaderTaskEither'> +``` + +Added in v2.0.0 + # utils ## apS diff --git a/docs/modules/StateT.ts.md b/docs/modules/StateT.ts.md index 20c3ae813..ac1d2106e 100644 --- a/docs/modules/StateT.ts.md +++ b/docs/modules/StateT.ts.md @@ -1,6 +1,6 @@ --- title: StateT.ts -nav_order: 98 +nav_order: 99 parent: Modules --- diff --git a/docs/modules/Store.ts.md b/docs/modules/Store.ts.md index af00114f4..a8e43179b 100644 --- a/docs/modules/Store.ts.md +++ b/docs/modules/Store.ts.md @@ -1,6 +1,6 @@ --- title: Store.ts -nav_order: 99 +nav_order: 100 parent: Modules --- diff --git a/docs/modules/Strong.ts.md b/docs/modules/Strong.ts.md index 5d919c32f..c331116dd 100644 --- a/docs/modules/Strong.ts.md +++ b/docs/modules/Strong.ts.md @@ -1,6 +1,6 @@ --- title: Strong.ts -nav_order: 101 +nav_order: 102 parent: Modules --- diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index b8ce7e90e..5a667b241 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -1,6 +1,6 @@ --- title: Task.ts -nav_order: 103 +nav_order: 104 parent: Modules --- @@ -41,8 +41,6 @@ Added in v2.0.0 - [flap](#flap) - [flatten](#flatten) - [fromIOK](#fromiok) -- [constructors](#constructors) - - [fromIO](#fromio) - [instances](#instances) - [ApplicativePar](#applicativepar) - [ApplicativeSeq](#applicativeseq) @@ -65,6 +63,8 @@ Added in v2.0.0 - [~~task~~](#task) - [model](#model) - [Task (interface)](#task-interface) +- [natural transformations](#natural-transformations) + - [fromIO](#fromio) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -101,7 +101,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const fromTask: (fa: Task) => Task +export declare const fromTask: NaturalTransformation11<'Task', 'Task'> ``` Added in v2.7.0 @@ -281,18 +281,6 @@ export declare const fromIOK: (f: (...a: A) => IO) => (...a: A) => Task Added in v2.4.0 -# constructors - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => Task -``` - -Added in v2.0.0 - # instances ## ApplicativePar @@ -528,6 +516,18 @@ export interface Task { Added in v2.0.0 +# natural transformations + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation11<'IO', 'Task'> +``` + +Added in v2.0.0 + # utils ## Do diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 566ad14ae..87c53539f 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -1,6 +1,6 @@ --- title: TaskEither.ts -nav_order: 104 +nav_order: 105 parent: Modules --- @@ -70,12 +70,7 @@ Added in v2.0.0 - [orLeft](#orleft) - [swap](#swap) - [constructors](#constructors) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromIOEither](#fromioeither) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromTask](#fromtask) - [fromTaskOption](#fromtaskoption) - [left](#left) - [leftIO](#leftio) @@ -127,6 +122,12 @@ Added in v2.0.0 - [tryCatchK](#trycatchk) - [model](#model) - [TaskEither (interface)](#taskeither-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromIOEither](#fromioeither) + - [fromOption](#fromoption) + - [fromTask](#fromtask) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -736,46 +737,6 @@ Added in v2.0.0 # constructors -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: E.Either) => TaskEither -``` - -Added in v2.0.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => TaskEither -``` - -Added in v2.7.0 - -## fromIOEither - -**Signature** - -```ts -export declare const fromIOEither: (fa: IOEither) => TaskEither -``` - -Added in v2.0.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => TaskEither -``` - -Added in v2.0.0 - ## fromPredicate **Signature** @@ -789,22 +750,12 @@ export declare const fromPredicate: { Added in v2.0.0 -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: T.Task) => TaskEither -``` - -Added in v2.7.0 - ## fromTaskOption **Signature** ```ts -export declare const fromTaskOption: (onNone: Lazy) => (e: TaskOption) => TaskEither +export declare const fromTaskOption: (onNone: Lazy) => NaturalTransformation12C<'TaskOption', 'TaskEither', E> ``` Added in v2.11.0 @@ -1349,6 +1300,58 @@ export interface TaskEither extends Task> {} Added in v2.0.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation22<'Either', 'TaskEither'> +``` + +Added in v2.0.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation12<'IO', 'TaskEither'> +``` + +Added in v2.7.0 + +## fromIOEither + +**Signature** + +```ts +export declare const fromIOEither: NaturalTransformation22<'IOEither', 'TaskEither'> +``` + +Added in v2.0.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'TaskEither', E> +``` + +Added in v2.0.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation12<'Task', 'TaskEither'> +``` + +Added in v2.7.0 + # utils ## Do diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index a53b8b58a..b7a12eef7 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -1,6 +1,6 @@ --- title: TaskOption.ts -nav_order: 105 +nav_order: 106 parent: Modules --- @@ -48,12 +48,7 @@ Added in v2.10.0 - [fromOptionK](#fromoptionk) - [fromTaskK](#fromtaskk) - [constructors](#constructors) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromTask](#fromtask) - - [fromTaskEither](#fromtaskeither) - [none](#none) - [some](#some) - [destructors](#destructors) @@ -91,6 +86,12 @@ Added in v2.10.0 - [tryCatchK](#trycatchk) - [model](#model) - [TaskOption (interface)](#taskoption-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromOption](#fromoption) + - [fromTask](#fromtask) + - [fromTaskEither](#fromtaskeither) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -417,36 +418,6 @@ Added in v2.10.0 # constructors -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: Either) => TaskOption -``` - -Added in v2.10.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => TaskOption -``` - -Added in v2.10.0 - -## fromOption - -**Signature** - -```ts -export declare const fromOption: (ma: O.Option) => TaskOption -``` - -Added in v2.10.0 - ## fromPredicate **Signature** @@ -460,26 +431,6 @@ export declare const fromPredicate: { Added in v2.10.0 -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: T.Task) => TaskOption -``` - -Added in v2.10.0 - -## fromTaskEither - -**Signature** - -```ts -export declare const fromTaskEither: (e: TaskEither) => TaskOption -``` - -Added in v2.11.0 - ## none **Signature** @@ -854,6 +805,58 @@ export interface TaskOption extends Task> {} Added in v2.10.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation21<'Either', 'TaskOption'> +``` + +Added in v2.10.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation11<'IO', 'TaskOption'> +``` + +Added in v2.10.0 + +## fromOption + +**Signature** + +```ts +export declare const fromOption: NaturalTransformation11<'Option', 'TaskOption'> +``` + +Added in v2.10.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation11<'Task', 'TaskOption'> +``` + +Added in v2.10.0 + +## fromTaskEither + +**Signature** + +```ts +export declare const fromTaskEither: NaturalTransformation21<'TaskEither', 'TaskOption'> +``` + +Added in v2.11.0 + # utils ## Do diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index 63849f9ab..04e938d02 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -1,6 +1,6 @@ --- title: TaskThese.ts -nav_order: 106 +nav_order: 107 parent: Modules --- @@ -28,13 +28,8 @@ Added in v2.4.0 - [swap](#swap) - [constructors](#constructors) - [both](#both) - - [fromEither](#fromeither) - - [fromIO](#fromio) - - [fromIOEither](#fromioeither) - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - - [fromTask](#fromtask) - - [fromThese](#fromthese) - [left](#left) - [leftIO](#leftio) - [leftTask](#lefttask) @@ -68,6 +63,12 @@ Added in v2.4.0 - [~~taskThese~~](#taskthese) - [model](#model) - [TaskThese (interface)](#taskthese-interface) +- [natural transformations](#natural-transformations) + - [fromEither](#fromeither) + - [fromIO](#fromio) + - [fromIOEither](#fromioeither) + - [fromTask](#fromtask) + - [fromThese](#fromthese) - [utils](#utils) - [toTuple2](#totuple2) - [~~toTuple~~](#totuple) @@ -205,42 +206,12 @@ export declare const both: (e: E, a: A) => TaskThese Added in v2.4.0 -## fromEither - -**Signature** - -```ts -export declare const fromEither: (e: Either) => TaskThese -``` - -Added in v2.10.0 - -## fromIO - -**Signature** - -```ts -export declare const fromIO: (fa: IO) => TaskThese -``` - -Added in v2.7.0 - -## fromIOEither - -**Signature** - -```ts -export declare const fromIOEither: (fa: IOEither) => TaskThese -``` - -Added in v2.4.0 - ## fromOption **Signature** ```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => TaskThese +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'TaskThese', E> ``` Added in v2.10.0 @@ -258,26 +229,6 @@ export declare const fromPredicate: { Added in v2.10.0 -## fromTask - -**Signature** - -```ts -export declare const fromTask: (fa: T.Task) => TaskThese -``` - -Added in v2.7.0 - -## fromThese - -**Signature** - -```ts -export declare const fromThese: (e: TH.These) => TaskThese -``` - -Added in v2.11.0 - ## left **Signature** @@ -624,6 +575,58 @@ export interface TaskThese extends Task> {} Added in v2.4.0 +# natural transformations + +## fromEither + +**Signature** + +```ts +export declare const fromEither: NaturalTransformation22<'Either', 'TaskThese'> +``` + +Added in v2.10.0 + +## fromIO + +**Signature** + +```ts +export declare const fromIO: NaturalTransformation12<'IO', 'TaskThese'> +``` + +Added in v2.7.0 + +## fromIOEither + +**Signature** + +```ts +export declare const fromIOEither: NaturalTransformation22<'IOEither', 'TaskThese'> +``` + +Added in v2.4.0 + +## fromTask + +**Signature** + +```ts +export declare const fromTask: NaturalTransformation12<'Task', 'TaskThese'> +``` + +Added in v2.7.0 + +## fromThese + +**Signature** + +```ts +export declare const fromThese: NaturalTransformation22<'These', 'TaskThese'> +``` + +Added in v2.11.0 + # utils ## toTuple2 diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index fbdeec2be..8fcec12d2 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -1,6 +1,6 @@ --- title: These.ts -nav_order: 107 +nav_order: 108 parent: Modules --- @@ -231,7 +231,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const fromOption: (onNone: Lazy) => (ma: Option) => These +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'These', E> ``` Added in v2.10.0 @@ -243,7 +243,7 @@ Takes a pair of `Option`s and attempts to create a `These` from them **Signature** ```ts -export declare function fromOptions(fe: Option, fa: Option): Option> +export declare const fromOptions: (fe: Option, fa: Option) => Option> ``` **Example** diff --git a/docs/modules/TheseT.ts.md b/docs/modules/TheseT.ts.md index bd2699be6..b4d5191da 100644 --- a/docs/modules/TheseT.ts.md +++ b/docs/modules/TheseT.ts.md @@ -1,6 +1,6 @@ --- title: TheseT.ts -nav_order: 108 +nav_order: 109 parent: Modules --- diff --git a/docs/modules/Traced.ts.md b/docs/modules/Traced.ts.md index fb09d1f11..a05b493b2 100644 --- a/docs/modules/Traced.ts.md +++ b/docs/modules/Traced.ts.md @@ -1,6 +1,6 @@ --- title: Traced.ts -nav_order: 109 +nav_order: 110 parent: Modules --- diff --git a/docs/modules/Traversable.ts.md b/docs/modules/Traversable.ts.md index 8b3e8ff41..2eb144792 100644 --- a/docs/modules/Traversable.ts.md +++ b/docs/modules/Traversable.ts.md @@ -1,6 +1,6 @@ --- title: Traversable.ts -nav_order: 110 +nav_order: 111 parent: Modules --- diff --git a/docs/modules/TraversableWithIndex.ts.md b/docs/modules/TraversableWithIndex.ts.md index 4929c6bca..ca3c5efa8 100644 --- a/docs/modules/TraversableWithIndex.ts.md +++ b/docs/modules/TraversableWithIndex.ts.md @@ -1,6 +1,6 @@ --- title: TraversableWithIndex.ts -nav_order: 111 +nav_order: 112 parent: Modules --- diff --git a/docs/modules/Tree.ts.md b/docs/modules/Tree.ts.md index c3e53da57..2058340d5 100644 --- a/docs/modules/Tree.ts.md +++ b/docs/modules/Tree.ts.md @@ -1,6 +1,6 @@ --- title: Tree.ts -nav_order: 112 +nav_order: 113 parent: Modules --- diff --git a/docs/modules/Tuple.ts.md b/docs/modules/Tuple.ts.md index 3d344c3b9..90d4eb8a1 100644 --- a/docs/modules/Tuple.ts.md +++ b/docs/modules/Tuple.ts.md @@ -1,6 +1,6 @@ --- title: Tuple.ts -nav_order: 113 +nav_order: 114 parent: Modules --- diff --git a/docs/modules/Unfoldable.ts.md b/docs/modules/Unfoldable.ts.md index 235667351..5f87adc14 100644 --- a/docs/modules/Unfoldable.ts.md +++ b/docs/modules/Unfoldable.ts.md @@ -1,6 +1,6 @@ --- title: Unfoldable.ts -nav_order: 114 +nav_order: 115 parent: Modules --- diff --git a/docs/modules/ValidationT.ts.md b/docs/modules/ValidationT.ts.md index 561871a5f..4214f5ea6 100644 --- a/docs/modules/ValidationT.ts.md +++ b/docs/modules/ValidationT.ts.md @@ -1,6 +1,6 @@ --- title: ValidationT.ts -nav_order: 115 +nav_order: 116 parent: Modules --- diff --git a/docs/modules/Witherable.ts.md b/docs/modules/Witherable.ts.md index d7ab94a16..fa116092d 100644 --- a/docs/modules/Witherable.ts.md +++ b/docs/modules/Witherable.ts.md @@ -1,6 +1,6 @@ --- title: Witherable.ts -nav_order: 117 +nav_order: 118 parent: Modules --- diff --git a/docs/modules/Writer.ts.md b/docs/modules/Writer.ts.md index c653ea8b6..892b9657f 100644 --- a/docs/modules/Writer.ts.md +++ b/docs/modules/Writer.ts.md @@ -1,6 +1,6 @@ --- title: Writer.ts -nav_order: 118 +nav_order: 119 parent: Modules --- diff --git a/docs/modules/WriterT.ts.md b/docs/modules/WriterT.ts.md index f0d9981bb..5f9e57f0e 100644 --- a/docs/modules/WriterT.ts.md +++ b/docs/modules/WriterT.ts.md @@ -1,6 +1,6 @@ --- title: WriterT.ts -nav_order: 119 +nav_order: 120 parent: Modules --- diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index 26dcc748a..3011840c1 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -75,6 +75,7 @@ Added in v2.0.0 - [monadTask](#monadtask) - [monadThrow](#monadthrow) - [monoid](#monoid) + - [naturalTransformation](#naturaltransformation) - [nonEmptyArray](#nonemptyarray) - [number](#number) - [option](#option) @@ -755,6 +756,16 @@ export declare const monoid: typeof monoid Added in v2.0.0 +## naturalTransformation + +**Signature** + +```ts +export declare const naturalTransformation: typeof naturalTransformation +``` + +Added in v2.11.0 + ## nonEmptyArray **Signature** diff --git a/docs/modules/number.ts.md b/docs/modules/number.ts.md index 839f3091a..bef10770a 100644 --- a/docs/modules/number.ts.md +++ b/docs/modules/number.ts.md @@ -1,6 +1,6 @@ --- title: number.ts -nav_order: 66 +nav_order: 67 parent: Modules --- diff --git a/docs/modules/pipeable.ts.md b/docs/modules/pipeable.ts.md index 94dd2bb61..9b726e944 100644 --- a/docs/modules/pipeable.ts.md +++ b/docs/modules/pipeable.ts.md @@ -1,6 +1,6 @@ --- title: pipeable.ts -nav_order: 71 +nav_order: 72 parent: Modules --- diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index d28c6204c..7eb9ffc6a 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -1,6 +1,6 @@ --- title: string.ts -nav_order: 100 +nav_order: 101 parent: Modules --- diff --git a/docs/modules/struct.ts.md b/docs/modules/struct.ts.md index 171a55cd6..a85036956 100644 --- a/docs/modules/struct.ts.md +++ b/docs/modules/struct.ts.md @@ -1,6 +1,6 @@ --- title: struct.ts -nav_order: 102 +nav_order: 103 parent: Modules --- diff --git a/docs/modules/void.ts.md b/docs/modules/void.ts.md index c9a58f4ab..9e6bcb394 100644 --- a/docs/modules/void.ts.md +++ b/docs/modules/void.ts.md @@ -1,6 +1,6 @@ --- title: void.ts -nav_order: 116 +nav_order: 117 parent: Modules --- diff --git a/dtslint/ts3.5/ReaderT.ts b/dtslint/ts3.5/ReaderT.ts new file mode 100644 index 000000000..27a1b24fa --- /dev/null +++ b/dtslint/ts3.5/ReaderT.ts @@ -0,0 +1,23 @@ +import * as _ from '../../src/ReaderT' +import * as TE from '../../src/TaskEither' +import * as TTH from '../../src/TaskThese' +import * as RTE from '../../src/ReaderTaskEither' +import * as SRTE from '../../src/StateReaderTaskEither' + +// $ExpectType (f: (r: R) => Either) => Reader> +_.fromNaturalTransformation(TE.fromEither) + +// $ExpectType (f: (r: R) => IO) => Reader> +_.fromNaturalTransformation(TE.fromIO) + +// $ExpectType (f: (r: R) => Reader) => Reader> +_.fromNaturalTransformation(RTE.fromReader) + +// $ExpectType (f: (r: R) => State) => Reader> +_.fromNaturalTransformation(SRTE.fromState) + +// $ExpectType (f: (r: R) => Task) => Reader> +_.fromNaturalTransformation(TE.fromTask) + +// // $ExpectType (f: (r: R) => These) => Reader> +// _.fromNaturalTransformation(TTH.fromThese) diff --git a/src/Array.ts b/src/Array.ts index a3784b366..297f623a0 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -24,8 +24,9 @@ import * as _ from './internal' import { Magma } from './Magma' import { Monad1 } from './Monad' import { Monoid } from './Monoid' +import { NaturalTransformation11 } from './NaturalTransformation' import * as NEA from './NonEmptyArray' -import { Option } from './Option' +import { Option, URI as OURI } from './Option' import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' @@ -162,19 +163,21 @@ export function fromPredicate(predicate: Predicate): (a: A) => Array { return (a) => (predicate(a) ? [a] : []) } +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromOption = (ma: Option): Array => (_.isNone(ma) ? [] : [ma.value]) +export const fromOption: NaturalTransformation11 = (ma) => (_.isNone(ma) ? [] : [ma.value]) /** - * Transforms an `Either` to a `Array`. - * - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromEither = (e: Either): Array => (_.isLeft(e) ? [] : [e.right]) +export const fromEither: FromEither1['fromEither'] = (e) => (_.isLeft(e) ? [] : [e.right]) // ------------------------------------------------------------------------------------- // destructors diff --git a/src/Either.ts b/src/Either.ts index f94958bda..180e7c1cc 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1097,7 +1097,7 @@ export const FromEither: FromEither2 = { * E.left('error') * ) * - * @category constructors + * @category natural transformations * @since 2.0.0 */ export const fromOption = diff --git a/src/Eq.ts b/src/Eq.ts index b7734bd48..2e7f7b5e4 100644 --- a/src/Eq.ts +++ b/src/Eq.ts @@ -35,11 +35,9 @@ export interface Eq { * @category constructors * @since 2.0.0 */ -export function fromEquals(equals: (x: A, y: A) => boolean): Eq { - return { - equals: (x, y) => x === y || equals(x, y) - } -} +export const fromEquals = (equals: (x: A, y: A) => boolean): Eq => ({ + equals: (x, y) => x === y || equals(x, y) +}) // ------------------------------------------------------------------------------------- // combinators diff --git a/src/FromEither.ts b/src/FromEither.ts index 07c994bf7..0aa22c4c5 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -5,11 +5,22 @@ */ import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4 } from './Chain' -import { Either } from './Either' +import { Either, URI as EURI } from './Either' import { flow, Lazy } from './function' import { HKT2, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import * as _ from './internal' -import { Option } from './Option' +import { + NaturalTransformation12C, + NaturalTransformation13C, + NaturalTransformation14C, + NaturalTransformation21, + NaturalTransformation22, + NaturalTransformation22C, + NaturalTransformation23, + NaturalTransformation23C, + NaturalTransformation24 +} from './NaturalTransformation' +import { Option, URI as OURI } from './Option' import { Predicate } from './Predicate' import { Refinement } from './Refinement' @@ -32,7 +43,7 @@ export interface FromEither { */ export interface FromEither1 { readonly URI: F - readonly fromEither: (e: Either) => Kind + readonly fromEither: NaturalTransformation21 } /** @@ -41,7 +52,7 @@ export interface FromEither1 { */ export interface FromEither2 { readonly URI: F - readonly fromEither: (e: Either) => Kind2 + readonly fromEither: NaturalTransformation22 } /** @@ -51,7 +62,7 @@ export interface FromEither2 { export interface FromEither2C { readonly URI: F readonly _E: E - readonly fromEither: (e: Either) => Kind2 + readonly fromEither: NaturalTransformation22C } /** @@ -60,7 +71,7 @@ export interface FromEither2C { */ export interface FromEither3 { readonly URI: F - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: NaturalTransformation23 } /** @@ -70,7 +81,7 @@ export interface FromEither3 { export interface FromEither3C { readonly URI: F readonly _E: E - readonly fromEither: (e: Either) => Kind3 + readonly fromEither: NaturalTransformation23C } /** @@ -79,7 +90,7 @@ export interface FromEither3C { */ export interface FromEither4 { readonly URI: F - readonly fromEither: (e: Either) => Kind4 + readonly fromEither: NaturalTransformation24 } // ------------------------------------------------------------------------------------- @@ -92,19 +103,19 @@ export interface FromEither4 { */ export function fromOption( F: FromEither4 -): (onNone: Lazy) => (ma: Option) => Kind4 +): (onNone: Lazy) => NaturalTransformation14C export function fromOption( F: FromEither3 -): (onNone: Lazy) => (ma: Option) => Kind3 +): (onNone: Lazy) => NaturalTransformation13C export function fromOption( F: FromEither3C -): (onNone: Lazy) => (ma: Option) => Kind3 +): (onNone: Lazy) => NaturalTransformation13C export function fromOption( F: FromEither2 -): (onNone: Lazy) => (ma: Option) => Kind2 +): (onNone: Lazy) => NaturalTransformation12C export function fromOption( F: FromEither2C -): (onNone: Lazy) => (ma: Option) => Kind2 +): (onNone: Lazy) => NaturalTransformation12C export function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 export function fromOption(F: FromEither): (onNone: Lazy) => (ma: Option) => HKT2 { return (onNone) => (ma) => F.fromEither(_.isNone(ma) ? _.left(onNone()) : _.right(ma.value)) diff --git a/src/FromIO.ts b/src/FromIO.ts index 3517a510e..a3482d857 100644 --- a/src/FromIO.ts +++ b/src/FromIO.ts @@ -6,7 +6,15 @@ import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4, chainFirst } from './Chain' import { flow } from './function' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' -import { IO } from './IO' +import { IO, URI } from './IO' +import { + NaturalTransformation11, + NaturalTransformation12, + NaturalTransformation12C, + NaturalTransformation13, + NaturalTransformation13C, + NaturalTransformation14 +} from './NaturalTransformation' // ------------------------------------------------------------------------------------- // model @@ -27,7 +35,7 @@ export interface FromIO { */ export interface FromIO1 { readonly URI: F - readonly fromIO: (fa: IO) => Kind + readonly fromIO: NaturalTransformation11 } /** @@ -36,7 +44,7 @@ export interface FromIO1 { */ export interface FromIO2 { readonly URI: F - readonly fromIO: (fa: IO) => Kind2 + readonly fromIO: NaturalTransformation12 } /** @@ -46,7 +54,7 @@ export interface FromIO2 { export interface FromIO2C { readonly URI: F readonly _E: E - readonly fromIO: (fa: IO) => Kind2 + readonly fromIO: NaturalTransformation12C } /** @@ -55,7 +63,7 @@ export interface FromIO2C { */ export interface FromIO3 { readonly URI: F - readonly fromIO: (fa: IO) => Kind3 + readonly fromIO: NaturalTransformation13 } /** @@ -65,7 +73,7 @@ export interface FromIO3 { export interface FromIO3C { readonly URI: F readonly _E: E - readonly fromIO: (fa: IO) => Kind3 + readonly fromIO: NaturalTransformation13C } /** @@ -74,7 +82,7 @@ export interface FromIO3C { */ export interface FromIO4 { readonly URI: F - readonly fromIO: (fa: IO) => Kind4 + readonly fromIO: NaturalTransformation14 } // ------------------------------------------------------------------------------------- diff --git a/src/FromReader.ts b/src/FromReader.ts index f0476c144..e02a44394 100644 --- a/src/FromReader.ts +++ b/src/FromReader.ts @@ -6,6 +6,12 @@ import { Chain, Chain2, Chain3, Chain3C, Chain4, chainFirst } from './Chain' import { flow } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import { + NaturalTransformation22, + NaturalTransformation23R, + NaturalTransformation23RC, + NaturalTransformation24R +} from './NaturalTransformation' import * as R from './Reader' import Reader = R.Reader @@ -29,7 +35,7 @@ export interface FromReader { */ export interface FromReader2 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind2 + readonly fromReader: NaturalTransformation22 } /** @@ -38,7 +44,7 @@ export interface FromReader2 { */ export interface FromReader3 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind3 + readonly fromReader: NaturalTransformation23R } /** @@ -48,7 +54,7 @@ export interface FromReader3 { export interface FromReader3C { readonly URI: F readonly _E: E - readonly fromReader: (fa: Reader) => Kind3 + readonly fromReader: NaturalTransformation23RC } /** @@ -57,7 +63,7 @@ export interface FromReader3C { */ export interface FromReader4 { readonly URI: F - readonly fromReader: (fa: Reader) => Kind4 + readonly fromReader: NaturalTransformation24R } // ------------------------------------------------------------------------------------- diff --git a/src/FromState.ts b/src/FromState.ts index 5fa12b924..6b00ae916 100644 --- a/src/FromState.ts +++ b/src/FromState.ts @@ -7,6 +7,12 @@ import { Chain, Chain2, Chain3, Chain4 } from './Chain' import { Endomorphism } from './Endomorphism' import { flow } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' +import { + NaturalTransformation22, + NaturalTransformation23R, + NaturalTransformation23RC, + NaturalTransformation24S +} from './NaturalTransformation' import * as S from './State' import State = S.State @@ -30,7 +36,7 @@ export interface FromState { */ export interface FromState2 { readonly URI: F - readonly fromState: (fa: State) => Kind2 + readonly fromState: NaturalTransformation22 } /** @@ -39,7 +45,7 @@ export interface FromState2 { */ export interface FromState3 { readonly URI: F - readonly fromState: (fa: State) => Kind3 + readonly fromState: NaturalTransformation23R } /** @@ -49,7 +55,7 @@ export interface FromState3 { export interface FromState3C { readonly URI: F readonly _E: E - readonly fromState: (fa: State) => Kind3 + readonly fromState: NaturalTransformation23RC } /** @@ -58,7 +64,7 @@ export interface FromState3C { */ export interface FromState4 { readonly URI: F - readonly fromState: (fa: State) => Kind4 + readonly fromState: NaturalTransformation24S } // ------------------------------------------------------------------------------------- diff --git a/src/FromTask.ts b/src/FromTask.ts index 55f067a69..f702e3090 100644 --- a/src/FromTask.ts +++ b/src/FromTask.ts @@ -7,7 +7,15 @@ import { Chain, Chain1, Chain2, Chain2C, Chain3, Chain3C, Chain4, chainFirst } f import { FromIO, FromIO1, FromIO2, FromIO2C, FromIO3, FromIO3C, FromIO4 } from './FromIO' import { flow } from './function' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' -import { Task } from './Task' +import { + NaturalTransformation11, + NaturalTransformation12, + NaturalTransformation12C, + NaturalTransformation13, + NaturalTransformation13C, + NaturalTransformation14 +} from './NaturalTransformation' +import { Task, URI } from './Task' // ------------------------------------------------------------------------------------- // model @@ -26,7 +34,7 @@ export interface FromTask extends FromIO { * @since 2.10.0 */ export interface FromTask1 extends FromIO1 { - readonly fromTask: (fa: Task) => Kind + readonly fromTask: NaturalTransformation11 } /** @@ -34,7 +42,7 @@ export interface FromTask1 extends FromIO1 { * @since 2.10.0 */ export interface FromTask2 extends FromIO2 { - readonly fromTask: (fa: Task) => Kind2 + readonly fromTask: NaturalTransformation12 } /** @@ -42,7 +50,7 @@ export interface FromTask2 extends FromIO2 { * @since 2.10.0 */ export interface FromTask2C extends FromIO2C { - readonly fromTask: (fa: Task) => Kind2 + readonly fromTask: NaturalTransformation12C } /** @@ -50,7 +58,7 @@ export interface FromTask2C extends FromIO2C { * @since 2.10.0 */ export interface FromTask3 extends FromIO3 { - readonly fromTask: (fa: Task) => Kind3 + readonly fromTask: NaturalTransformation13 } /** @@ -58,7 +66,7 @@ export interface FromTask3 extends FromIO3 { * @since 2.10.0 */ export interface FromTask3C extends FromIO3C { - readonly fromTask: (fa: Task) => Kind3 + readonly fromTask: NaturalTransformation13C } /** @@ -66,7 +74,7 @@ export interface FromTask3C extends FromIO3C { * @since 2.10.0 */ export interface FromTask4 extends FromIO4 { - readonly fromTask: (fa: Task) => Kind4 + readonly fromTask: NaturalTransformation14 } // ------------------------------------------------------------------------------------- diff --git a/src/FromThese.ts b/src/FromThese.ts index 80245bc06..29dfbbe01 100644 --- a/src/FromThese.ts +++ b/src/FromThese.ts @@ -5,7 +5,14 @@ */ import { flow } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' -import { These } from './These' +import { + NaturalTransformation22, + NaturalTransformation22C, + NaturalTransformation23, + NaturalTransformation23C, + NaturalTransformation24 +} from './NaturalTransformation' +import { These, URI } from './These' // ------------------------------------------------------------------------------------- // model @@ -26,7 +33,7 @@ export interface FromThese { */ export interface FromThese2 { readonly URI: F - readonly fromThese: (e: These) => Kind2 + readonly fromThese: NaturalTransformation22 } /** @@ -36,7 +43,7 @@ export interface FromThese2 { export interface FromThese2C { readonly URI: F readonly _E: E - readonly fromThese: (e: These) => Kind2 + readonly fromThese: NaturalTransformation22C } /** @@ -45,7 +52,7 @@ export interface FromThese2C { */ export interface FromThese3 { readonly URI: F - readonly fromThese: (e: These) => Kind3 + readonly fromThese: NaturalTransformation23 } /** @@ -55,7 +62,7 @@ export interface FromThese3 { export interface FromThese3C { readonly URI: F readonly _E: E - readonly fromThese: (e: These) => Kind3 + readonly fromThese: NaturalTransformation23C } /** @@ -64,7 +71,7 @@ export interface FromThese3C { */ export interface FromThese4 { readonly URI: F - readonly fromThese: (e: These) => Kind4 + readonly fromThese: NaturalTransformation24 } // ------------------------------------------------------------------------------------- diff --git a/src/IO.ts b/src/IO.ts index 985a6c3f2..12d1c77bc 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -97,13 +97,6 @@ export const flatten: (mma: IO>) => IO = /*#__PURE__*/ chain(identity) -/** - * @category constructors - * @since 2.7.0 - * @deprecated - */ -export const fromIO: FromIO1['fromIO'] = identity - // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- @@ -235,6 +228,13 @@ export const chainFirst = /*#__PURE__*/ chainFirst_(Chain) +/** + * @category constructors + * @since 2.7.0 + * @deprecated + */ +export const fromIO: FromIO1['fromIO'] = identity + /** * @category instances * @since 2.7.0 diff --git a/src/IOEither.ts b/src/IOEither.ts index d22910dfb..7010c1bdf 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -45,7 +45,6 @@ import { Monad2, Monad2C } from './Monad' import { MonadIO2, MonadIO2C } from './MonadIO' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' -import { Option } from './Option' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' import { Refinement } from './Refinement' @@ -100,14 +99,18 @@ export const leftIO: (me: IO) => IOEither = /*#__PURE__*/ ET.leftF(I.Functor) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ export const fromEither: FromEither2['fromEither'] = I.of /** - * @category constructors + * @category natural transformations * @since 2.7.0 */ export const fromIO: FromIO2['fromIO'] = rightIO @@ -733,10 +736,10 @@ export const FromEither: FromEither2 = { } /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromOption: (onNone: Lazy) => (ma: Option) => IOEither = +export const fromOption = /*#__PURE__*/ fromOption_(FromEither) diff --git a/src/NaturalTransformation.ts b/src/NaturalTransformation.ts new file mode 100644 index 000000000..7ec0370ef --- /dev/null +++ b/src/NaturalTransformation.ts @@ -0,0 +1,152 @@ +/** + * A type for natural transformations. + * + * A natural transformation is a mapping between type constructors of kind `* -> *` where the mapping + * operation has no ability to manipulate the inner values. + * + * The definition of a natural transformation in category theory states that `F` and `G` should be functors, + * but the `Functor` constraint is not enforced here; that the types are of kind `* -> *` is enough for our purposes. + * + * @since 2.11.0 + */ +import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation { + (fa: HKT): HKT +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation11 { + (fa: Kind): Kind +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation12 { + (fa: Kind): Kind2 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation12C { + (fa: Kind): Kind2 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation13 { + (fa: Kind): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation13C { + (fa: Kind): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation14 { + (fa: Kind): Kind4 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation14C { + (fa: Kind): Kind4 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation21 { + (fa: Kind2): Kind +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation22 { + (fa: Kind2): Kind2 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation22C { + (fa: Kind2): Kind2 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation23 { + (fa: Kind2): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation23C { + (fa: Kind2): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation24 { + (fa: Kind2): Kind4 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation23R { + (fa: Kind2): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation23RC { + (fa: Kind2): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation24R { + (fa: Kind2): Kind4 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation24S { + (fa: Kind2): Kind4 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation33 { + (fa: Kind3): Kind3 +} + +/** + * @since 2.11.0 + */ +export interface NaturalTransformation34 { + (fa: Kind3): Kind4 +} diff --git a/src/Option.ts b/src/Option.ts index 4619ba751..9e8c2a5be 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -41,7 +41,7 @@ import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { not, Predicate } from './Predicate' import { Refinement } from './Refinement' -import { Semigroup, first, last } from './Semigroup' +import { first, last, Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' @@ -180,15 +180,19 @@ export function getRight(ma: Either): Option { return ma._tag === 'Left' ? none : some(ma.right) } +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** * Transforms an `Either` to an `Option` discarding the error. * * Alias of [getRight](#getright) * - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromEither: (ma: Either) => Option = getRight +export const fromEither: FromEither1['fromEither'] = getRight // ------------------------------------------------------------------------------------- // destructors diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 84656465b..dc1af2dae 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -36,8 +36,8 @@ import { import { ask as ask_, asks as asks_, - chainReaderK as chainReaderK_, chainFirstReaderK as chainFirstReaderK_, + chainReaderK as chainReaderK_, FromReader3, fromReaderK as fromReaderK_ } from './FromReader' @@ -47,7 +47,8 @@ import * as _ from './internal' import { Monad3, Monad3C } from './Monad' import { MonadThrow3, MonadThrow3C } from './MonadThrow' import { Monoid } from './Monoid' -import { Option } from './Option' +import { NaturalTransformation13C } from './NaturalTransformation' +import { URI as OURI } from './Option' import { Pointed3 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' @@ -103,17 +104,21 @@ export const leftReader: (me: Reader) => ReaderEi /*#__PURE__*/ ET.leftF(R.Functor) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromEither: (e: E.Either) => ReaderEither = R.of +export const fromEither: FromEither3['fromEither'] = R.of /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromReader: (ma: Reader) => ReaderEither = rightReader +export const fromReader: FromReader3['fromReader'] = rightReader // ------------------------------------------------------------------------------------- // destructors @@ -782,10 +787,10 @@ export const FromEither: FromEither3 = { } /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromOption: (onNone: Lazy) => (ma: Option) => ReaderEither = +export const fromOption: (onNone: Lazy) => NaturalTransformation13C = /*#__PURE__*/ fromOption_(FromEither) diff --git a/src/ReaderT.ts b/src/ReaderT.ts index d16f89769..63ee5c5eb 100644 --- a/src/ReaderT.ts +++ b/src/ReaderT.ts @@ -7,6 +7,14 @@ import { flow, pipe } from './function' import { Functor, Functor1, Functor2, Functor2C, Functor3, Functor3C, Functor4 } from './Functor' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' import { Monad, Monad1, Monad2, Monad2C, Monad3 } from './Monad' +import { + NaturalTransformation22, + NaturalTransformation12, + NaturalTransformation11, + NaturalTransformation, + NaturalTransformation23R, + NaturalTransformation24S +} from './NaturalTransformation' import { Pointed, Pointed1, Pointed2, Pointed2C, Pointed3, Pointed3C, Pointed4 } from './Pointed' import { Reader } from './Reader' @@ -150,6 +158,34 @@ export function fromReader(F: Pointed): (ma: Reader) => Reader return (ma) => flow(ma, F.of) } +/** + * @category constructors + * @since 2.11.0 + */ +export function fromNaturalTransformation( + nt: NaturalTransformation24S +): (f: (r: R) => Kind2) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation23R +): (f: (r: R) => Kind2) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation22 +): (f: (r: R) => Kind2) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation12 +): (f: (r: R) => Kind) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation11 +): (f: (r: R) => Kind) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation +): (f: (r: R) => HKT) => Reader> +export function fromNaturalTransformation( + nt: NaturalTransformation +): (f: (r: R) => HKT) => Reader> { + return (f) => flow(f, nt) +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 62a2597e7..ae859b7a5 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -14,8 +14,8 @@ import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIO import { ask as ask_, asks as asks_, - chainReaderK as chainReaderK_, chainFirstReaderK as chainFirstReaderK_, + chainReaderK as chainReaderK_, FromReader2, fromReaderK as fromReaderK_ } from './FromReader' @@ -43,7 +43,6 @@ import * as T from './Task' // ------------------------------------------------------------------------------------- import Task = T.Task -import Reader = R.Reader /** * @category model @@ -54,19 +53,19 @@ export interface ReaderTask { } // ------------------------------------------------------------------------------------- -// constructors +// natural transformations // ------------------------------------------------------------------------------------- /** - * @category constructors + * @category natural transformations * @since 2.3.0 */ -export const fromReader: (ma: Reader) => ReaderTask = +export const fromReader: FromReader2['fromReader'] = /*#__PURE__*/ RT.fromReader(T.Pointed) /** - * @category constructors + * @category natural transformations * @since 2.3.0 */ export const fromTask: FromTask2['fromTask'] = @@ -74,7 +73,7 @@ export const fromTask: FromTask2['fromTask'] = R.of /** - * @category constructors + * @category natural transformations * @since 2.3.0 */ export const fromIO: FromIO2['fromIO'] = diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 05b5aef8c..dd8974397 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -38,8 +38,8 @@ import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO3, fromIO import { ask as ask_, asks as asks_, - chainReaderK as chainReaderK_, chainFirstReaderK as chainFirstReaderK_, + chainReaderK as chainReaderK_, FromReader3, fromReaderK as fromReaderK_ } from './FromReader' @@ -53,17 +53,18 @@ import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { IO } from './IO' -import { IOEither } from './IOEither' +import { IOEither, URI as IEURI } from './IOEither' import { Monad3, Monad3C } from './Monad' import { MonadIO3 } from './MonadIO' import { MonadTask3, MonadTask3C } from './MonadTask' import { MonadThrow3, MonadThrow3C } from './MonadThrow' import { Monoid } from './Monoid' -import { Option } from './Option' +import { NaturalTransformation13C, NaturalTransformation23, NaturalTransformation33 } from './NaturalTransformation' +import { URI as OURI } from './Option' import { Pointed3 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' -import { ReaderEither } from './ReaderEither' +import { ReaderEither, URI as REURI } from './ReaderEither' import * as RT from './ReaderTask' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' @@ -93,10 +94,10 @@ export interface ReaderTaskEither { // ------------------------------------------------------------------------------------- /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromTaskEither: (ma: TaskEither) => ReaderTaskEither = +export const fromTaskEither: NaturalTransformation23 = /*#__PURE__*/ R.of @@ -166,56 +167,59 @@ export const leftReaderTask: (me: ReaderTask) => * @category constructors * @since 2.0.0 */ -export const fromIOEither: (ma: IOEither) => ReaderTaskEither = +export const rightIO: (ma: IO) => ReaderTaskEither = /*#__PURE__*/ - flow(TE.fromIOEither, fromTaskEither) + flow(TE.rightIO, fromTaskEither) /** * @category constructors * @since 2.0.0 */ -export const fromReaderEither = (ma: ReaderEither): ReaderTaskEither => - flow(ma, TE.fromEither) +export const leftIO: (me: IO) => ReaderTaskEither = + /*#__PURE__*/ + flow(TE.leftIO, fromTaskEither) + +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const rightIO: (ma: IO) => ReaderTaskEither = - /*#__PURE__*/ - flow(TE.rightIO, fromTaskEither) +export const fromEither: FromEither3['fromEither'] = RT.of /** - * @category constructors + * @category natural transformations + * @since 2.11.0 + */ +export const fromReader: FromReader3['fromReader'] = rightReader + +/** + * @category natural transformations * @since 2.0.0 */ export const fromIO: FromIO3['fromIO'] = rightIO /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ export const fromTask: FromTask3['fromTask'] = rightTask /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const leftIO: (me: IO) => ReaderTaskEither = +export const fromIOEither: NaturalTransformation23 = /*#__PURE__*/ - flow(TE.leftIO, fromTaskEither) - -/** - * @category constructors - * @since 2.11.0 - */ -export const fromReader: FromReader3['fromReader'] = rightReader + flow(TE.fromIOEither, fromTaskEither) /** * @category constructors * @since 2.0.0 */ -export const fromEither: (e: E.Either) => ReaderTaskEither = RT.of +export const fromReaderEither: NaturalTransformation33 = (ma) => flow(ma, TE.fromEither) // ------------------------------------------------------------------------------------- // destructors @@ -1087,10 +1091,10 @@ export const FromEither: FromEither3 = { } /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromOption: (onNone: Lazy) => (ma: Option) => ReaderTaskEither = +export const fromOption: (onNone: Lazy) => NaturalTransformation13C = /*#__PURE__*/ fromOption_(FromEither) diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index f266b2361..08a42665c 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -24,9 +24,10 @@ import * as _ from './internal' import { Magma } from './Magma' import { Monad1 } from './Monad' import { Monoid } from './Monoid' +import { NaturalTransformation11 } from './NaturalTransformation' import { NonEmptyArray } from './NonEmptyArray' import * as N from './number' -import { Option } from './Option' +import { Option, URI as OURI } from './Option' import { fromCompare, Ord } from './Ord' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' @@ -163,19 +164,23 @@ export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArr return (a) => (predicate(a) ? [a] : empty) } +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromOption = (ma: Option): ReadonlyArray => (_.isNone(ma) ? empty : [ma.value]) +export const fromOption: NaturalTransformation11 = (ma) => (_.isNone(ma) ? empty : [ma.value]) /** * Transforms an `Either` to a `ReadonlyArray`. * - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromEither = (e: Either): ReadonlyArray => (_.isLeft(e) ? empty : [e.right]) +export const fromEither: FromEither1['fromEither'] = (e) => (_.isLeft(e) ? empty : [e.right]) // ------------------------------------------------------------------------------------- // destructors diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 3898b246f..5735e35c5 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -31,19 +31,17 @@ import { wiltDefault, Witherable2C, witherDefault } from './Witherable' import Option = O.Option // ------------------------------------------------------------------------------------- -// model +// interop // ------------------------------------------------------------------------------------- /** - * @category constructors + * @category interop * @since 2.5.0 */ -export function fromMap(m: Map): ReadonlyMap { - return new Map(m) -} +export const fromMap = (m: Map): ReadonlyMap => new Map(m) /** - * @category destructors + * @category interop * @since 2.5.0 */ export function toMap(m: ReadonlyMap): Map { diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index afdeef60e..5d6565213 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -47,17 +47,13 @@ export type ReadonlyRecord = Readonly> * @category interop * @since 2.5.0 */ -export function fromRecord(r: Record): ReadonlyRecord { - return Object.assign({}, r) -} +export const fromRecord = (r: Record): ReadonlyRecord => Object.assign({}, r) /** * @category interop * @since 2.5.0 */ -export function toRecord(r: ReadonlyRecord): Record { - return Object.assign({}, r) -} +export const toRecord = (r: ReadonlyRecord): Record => Object.assign({}, r) /** * Calculate the number of key/value pairs in a `ReadonlyRecord`, diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index e45b6a012..9aa3a8af3 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -14,45 +14,57 @@ import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' import { Show } from './Show' +// ------------------------------------------------------------------------------------- +// interop +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category interop * @since 2.5.0 */ -export function fromSet(s: Set): ReadonlySet { - return new Set(s) -} +export const fromSet = (s: Set): ReadonlySet => new Set(s) + +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- /** - * @category destructors + * Create a set with one element + * + * @category constructors * @since 2.5.0 */ -export function toSet(s: ReadonlySet): Set { - return new Set(s) -} +export const singleton = (a: A): ReadonlySet => new Set([a]) /** - * @category instances - * @since 2.5.0 + * Create a `ReadonlySet` from a `ReadonlyArray` + * + * @category constructors + * @since 2.10.0 */ -export function getShow(S: Show): Show> { - return { - show: (s) => { - const entries: Array = [] - s.forEach((a) => { - entries.push(S.show(a)) - }) - return `new Set([${entries.sort().join(', ')}])` +export const fromReadonlyArray = (E: Eq) => (as: ReadonlyArray): ReadonlySet => { + const len = as.length + const out = new Set() + const has = elem(E) + for (let i = 0; i < len; i++) { + const a = as[i] + if (!has(a, out)) { + out.add(a) } } + return out } +// ------------------------------------------------------------------------------------- +// destructors +// ------------------------------------------------------------------------------------- + /** - * @category instances + * @category destructors * @since 2.5.0 */ -export function getEq(E: Eq): Eq> { - const subsetE = isSubset(E) - return fromEquals((x, y) => subsetE(x, y) && subsetE(y, x)) +export function toSet(s: ReadonlySet): Set { + return new Set(s) } interface Next { @@ -291,39 +303,6 @@ export function difference( } } -/** - * @category instances - * @since 2.11.0 - */ -export const getUnionSemigroup = (E: Eq): Semigroup> => ({ - concat: union(E) -}) - -/** - * @category instances - * @since 2.5.0 - */ -export const getUnionMonoid = (E: Eq): Monoid> => ({ - concat: getUnionSemigroup(E).concat, - empty -}) - -/** - * @category instances - * @since 2.5.0 - */ -export const getIntersectionSemigroup = (E: Eq): Semigroup> => ({ - concat: intersection(E) -}) - -/** - * @category instances - * @since 2.11.0 - */ -export const getDifferenceMagma = (E: Eq): Magma> => ({ - concat: difference(E) -}) - /** * @since 2.5.0 */ @@ -348,14 +327,6 @@ export const reduceRight = (O: Ord): ((b: B, f: (a: A, b: B) => B) => ( return (b, f) => (fa) => toReadonlyArrayO(fa).reduceRight((b, a) => f(a, b), b) } -/** - * Create a set with one element - * - * @category constructors - * @since 2.5.0 - */ -export const singleton = (a: A): ReadonlySet => new Set([a]) - /** * Insert a value into a set * @@ -399,25 +370,6 @@ export const toggle = (E: Eq): ((a: A) => (set: ReadonlySet) => Readonl return (a) => (set) => (elemE(a, set) ? removeE : insertE)(a)(set) } -/** - * Create a set from an array - * - * @category constructors - * @since 2.10.0 - */ -export const fromReadonlyArray = (E: Eq) => (as: ReadonlyArray): ReadonlySet => { - const len = as.length - const out = new Set() - const has = elem(E) - for (let i = 0; i < len; i++) { - const a = as[i] - if (!has(a, out)) { - out.add(a) - } - } - return out -} - /** * @category combinators * @since 2.5.0 @@ -579,6 +531,86 @@ export const toReadonlyArray = (O: Ord) => (set: ReadonlySet): Readonly return out.sort(O.compare) } +// ------------------------------------------------------------------------------------- +// instances +// ------------------------------------------------------------------------------------- + +/** + * @category instances + * @since 2.11.0 + */ +export const URI = 'ReadonlySet' + +/** + * @category instances + * @since 2.11.0 + */ +export type URI = typeof URI + +declare module './HKT' { + interface URItoKind { + readonly [URI]: ReadonlySet + } +} + +/** + * @category instances + * @since 2.5.0 + */ +export function getShow(S: Show): Show> { + return { + show: (s) => { + const entries: Array = [] + s.forEach((a) => { + entries.push(S.show(a)) + }) + return `new Set([${entries.sort().join(', ')}])` + } + } +} + +/** + * @category instances + * @since 2.5.0 + */ +export function getEq(E: Eq): Eq> { + const subsetE = isSubset(E) + return fromEquals((x, y) => subsetE(x, y) && subsetE(y, x)) +} + +/** + * @category instances + * @since 2.11.0 + */ +export const getUnionSemigroup = (E: Eq): Semigroup> => ({ + concat: union(E) +}) + +/** + * @category instances + * @since 2.5.0 + */ +export const getUnionMonoid = (E: Eq): Monoid> => ({ + concat: getUnionSemigroup(E).concat, + empty +}) + +/** + * @category instances + * @since 2.5.0 + */ +export const getIntersectionSemigroup = (E: Eq): Semigroup> => ({ + concat: intersection(E) +}) + +/** + * @category instances + * @since 2.11.0 + */ +export const getDifferenceMagma = (E: Eq): Magma> => ({ + concat: difference(E) +}) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index ee22d2ee1..0f5074240 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -22,8 +22,8 @@ import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO4, fromIO import { ask as ask_, asks as asks_, - chainReaderK as chainReaderK_, chainFirstReaderK as chainFirstReaderK_, + chainReaderK as chainReaderK_, FromReader4, fromReaderK as fromReaderK_ } from './FromReader' @@ -46,22 +46,23 @@ import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor4 } from './Functor' import * as _ from './internal' import { IO } from './IO' -import { IOEither } from './IOEither' +import { IOEither, URI as IEURI } from './IOEither' import { Monad4 } from './Monad' import { MonadIO4 } from './MonadIO' import { MonadTask4 } from './MonadTask' import { MonadThrow4 } from './MonadThrow' -import { Option } from './Option' +import { NaturalTransformation14C, NaturalTransformation24, NaturalTransformation34 } from './NaturalTransformation' +import { URI as OURI } from './Option' import { Pointed4 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' -import { ReaderEither } from './ReaderEither' +import { URI as REURI } from './ReaderEither' import * as RTE from './ReaderTaskEither' import { Refinement } from './Refinement' import { State } from './State' import * as ST from './StateT' import { Task } from './Task' -import { TaskEither } from './TaskEither' +import { TaskEither, URI as TEURI } from './TaskEither' // ------------------------------------------------------------------------------------- // model @@ -113,14 +114,6 @@ export function leftTask(me: Task): StateReaderTa return fromReaderTaskEither(RTE.leftTask(me)) } -/** - * @category constructors - * @since 2.0.0 - */ -export function fromTaskEither(ma: TaskEither): StateReaderTaskEither { - return fromReaderTaskEither(RTE.fromTaskEither(ma)) -} - /** * @category constructors * @since 2.0.0 @@ -137,22 +130,6 @@ export function leftReader(me: Reader): StateR return fromReaderTaskEither(RTE.leftReader(me)) } -/** - * @category constructors - * @since 2.0.0 - */ -export function fromIOEither(ma: IOEither): StateReaderTaskEither { - return fromReaderTaskEither(RTE.fromIOEither(ma)) -} - -/** - * @category constructors - * @since 2.0.0 - */ -export function fromReaderEither(ma: ReaderEither): StateReaderTaskEither { - return fromReaderTaskEither(RTE.fromReaderEither(ma)) -} - /** * @category constructors * @since 2.0.0 @@ -184,48 +161,71 @@ export const leftState: (me: State) => StateRe s ) => RTE.left(me(s)[0]) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromReaderTaskEither: (ma: ReaderTaskEither) => StateReaderTaskEither = +export const fromEither: FromEither4['fromEither'] = /*#__PURE__*/ - ST.fromF(RTE.Functor) + E.match((e) => left(e), right) /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromReader: (ma: Reader) => StateReaderTaskEither = rightReader - -/** - * @category constructors - * @since 2.0.0 - */ -export const fromEither: (e: E.Either) => StateReaderTaskEither = - /*#__PURE__*/ - E.match((e) => left(e), right) +export const fromReader: FromReader4['fromReader'] = rightReader /** - * @category constructors + * @category natural transformations * @since 2.7.0 */ export const fromIO: FromIO4['fromIO'] = rightIO /** - * @category constructors + * @category natural transformations * @since 2.7.0 */ export const fromTask: FromTask4['fromTask'] = rightTask /** - * @category constructors + * @category natural transformations * @since 2.10.0 */ -export const fromState: (sa: State) => StateReaderTaskEither = +export const fromState: FromState4['fromState'] = /*#__PURE__*/ ST.fromState(RTE.Pointed) +/** + * @category natural transformations + * @since 2.0.0 + */ +export const fromTaskEither: NaturalTransformation24 = (ma) => fromReaderTaskEither(RTE.fromTaskEither(ma)) + +/** + * @category natural transformations + * @since 2.0.0 + */ +export const fromIOEither: NaturalTransformation24 = (ma) => fromReaderTaskEither(RTE.fromIOEither(ma)) + +/** + * @category natural transformations + * @since 2.0.0 + */ +export const fromReaderEither: NaturalTransformation34 = (ma) => + fromReaderTaskEither(RTE.fromReaderEither(ma)) + +/** + * @category constructors + * @since 2.0.0 + */ +export const fromReaderTaskEither: NaturalTransformation34 = + /*#__PURE__*/ + ST.fromF(RTE.Functor) + // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- @@ -317,11 +317,9 @@ export const chainTaskEitherK: ( * @category combinators * @since 2.4.0 */ -export function fromReaderTaskEitherK, B>( +export const fromReaderTaskEitherK = , B>( f: (...a: A) => ReaderTaskEither -): (...a: A) => StateReaderTaskEither { - return (...a) => fromReaderTaskEither(f(...a)) -} +): ((...a: A) => StateReaderTaskEither) => (...a) => fromReaderTaskEither(f(...a)) /** * Less strict version of [`chainReaderTaskEitherK`](#chainreadertaskeitherk). @@ -888,10 +886,10 @@ export const FromEither: FromEither4 = { } /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromOption: (onNone: Lazy) => (ma: Option) => StateReaderTaskEither = +export const fromOption: (onNone: Lazy) => NaturalTransformation14C = /*#__PURE__*/ fromOption_(FromEither) diff --git a/src/Task.ts b/src/Task.ts index 483702ac1..d06fe065a 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -44,11 +44,11 @@ export interface Task { } // ------------------------------------------------------------------------------------- -// constructors +// natural transformations // ------------------------------------------------------------------------------------- /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ export const fromIO: FromIO1['fromIO'] = (ma) => () => Promise.resolve(ma()) @@ -153,13 +153,6 @@ export const flatten: (mma: Task>) => Task = /*#__PURE__*/ chain(identity) -/** - * @category FromTask - * @since 2.7.0 - * @deprecated - */ -export const fromTask: FromTask1['fromTask'] = identity - // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- @@ -339,6 +332,13 @@ export const MonadIO: MonadIO1 = { fromIO } +/** + * @category FromTask + * @since 2.7.0 + * @deprecated + */ +export const fromTask: FromTask1['fromTask'] = identity + /** * @category instances * @since 2.10.0 diff --git a/src/TaskEither.ts b/src/TaskEither.ts index eee3d7bec..f64bf114f 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -52,19 +52,19 @@ import { flow, identity, Lazy, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { IO } from './IO' -import { IOEither } from './IOEither' +import { IOEither, URI as IEURI } from './IOEither' import { Monad2, Monad2C } from './Monad' import { MonadIO2 } from './MonadIO' import { MonadTask2, MonadTask2C } from './MonadTask' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' -import { Option } from './Option' +import { NaturalTransformation12C, NaturalTransformation22 } from './NaturalTransformation' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import * as T from './Task' -import { TaskOption } from './TaskOption' +import { TaskOption, URI as TOURI } from './TaskOption' // ------------------------------------------------------------------------------------- // model @@ -131,29 +131,40 @@ export const leftIO: (me: IO) => TaskEither = /*#__PURE__*/ flow(T.fromIO, leftTask) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors - * @since 2.0.0 + * @category natural transformations + * @since 2.7.0 + */ +export const fromIO: FromIO2['fromIO'] = rightIO + +/** + * @category natural transformations + * @since 2.7.0 */ -export const fromIOEither: (fa: IOEither) => TaskEither = T.fromIO +export const fromTask: FromTask2['fromTask'] = rightTask /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ export const fromEither: FromEither2['fromEither'] = T.of /** - * @category constructors - * @since 2.7.0 + * @category natural transformations + * @since 2.0.0 */ -export const fromIO: FromIO2['fromIO'] = rightIO +export const fromIOEither: NaturalTransformation22 = T.fromIO /** * @category constructors - * @since 2.7.0 + * @since 2.11.0 */ -export const fromTask: FromTask2['fromTask'] = rightTask +export const fromTaskOption: (onNone: Lazy) => NaturalTransformation12C = (onNone) => + T.map(E.fromOption(onNone)) // ------------------------------------------------------------------------------------- // destructors @@ -881,10 +892,10 @@ export const FromEither: FromEither2 = { } /** - * @category constructors + * @category natural transformations * @since 2.0.0 */ -export const fromOption: (onNone: Lazy) => (ma: Option) => TaskEither = +export const fromOption = /*#__PURE__*/ fromOption_(FromEither) @@ -904,13 +915,6 @@ export const chainOptionK = /*#__PURE__*/ chainOptionK_(FromEither, Chain) -/** - * @category constructors - * @since 2.11.0 - */ -export const fromTaskOption: (onNone: Lazy) => (e: TaskOption) => TaskEither = (onNone) => - T.map(E.fromOption(onNone)) - /** * @category combinators * @since 2.4.0 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 77e2de258..4b4138442 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -15,6 +15,7 @@ import { partition as partition_, partitionMap as partitionMap_ } from './Filterable' +import { FromEither1 } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO1, fromIOK as fromIOK_ } from './FromIO' import { chainFirstTaskK as chainFirstTaskK_, @@ -28,6 +29,7 @@ import * as _ from './internal' import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { MonadTask1 } from './MonadTask' +import { NaturalTransformation11, NaturalTransformation21 } from './NaturalTransformation' import * as O from './Option' import * as OT from './OptionT' import { Pointed1 } from './Pointed' @@ -35,7 +37,7 @@ import { Predicate } from './Predicate' import { Refinement } from './Refinement' import { Separated } from './Separated' import * as T from './Task' -import { TaskEither } from './TaskEither' +import { URI as TEURI } from './TaskEither' import Task = T.Task import Option = O.Option @@ -62,12 +64,6 @@ export const some: (a: A) => TaskOption = /*#__PURE__*/ OT.some(T.Pointed) -/** - * @category constructors - * @since 2.10.0 - */ -export const fromOption: (ma: Option) => TaskOption = T.of - /** * @category constructors * @since 2.10.0 @@ -79,22 +75,32 @@ export const fromPredicate: { /*#__PURE__*/ OT.fromPredicate(T.Pointed) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors + * @category natural transformations + * @since 2.10.0 + */ +export const fromOption: NaturalTransformation11 = T.of + +/** + * @category natural transformations * @since 2.10.0 */ -export const fromEither: (e: Either) => TaskOption = +export const fromEither: FromEither1['fromEither'] = /*#__PURE__*/ OT.fromEither(T.Pointed) /** - * @category constructors + * @category natural transformations * @since 2.10.0 */ export const fromIO: FromIO1['fromIO'] = (ma) => fromTask(T.fromIO(ma)) /** - * @category constructors + * @category natural transformations * @since 2.10.0 */ export const fromTask: FromTask1['fromTask'] = @@ -102,10 +108,10 @@ export const fromTask: FromTask1['fromTask'] = OT.fromF(T.Functor) /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ -export const fromTaskEither: (e: TaskEither) => TaskOption = +export const fromTaskEither: NaturalTransformation21 = /*#__PURE__*/ T.map(O.fromEither) diff --git a/src/TaskThese.ts b/src/TaskThese.ts index 9bfa1f86e..74db403b8 100644 --- a/src/TaskThese.ts +++ b/src/TaskThese.ts @@ -13,18 +13,19 @@ import { } from './FromEither' import { FromIO2, fromIOK as fromIOK_ } from './FromIO' import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' +import { FromThese2, fromTheseK as fromTheseK_ } from './FromThese' import { flow, Lazy, pipe } from './function' import { flap as flap_, Functor2 } from './Functor' import { IO } from './IO' -import { IOEither } from './IOEither' +import { URI as IEURI } from './IOEither' import { Monad2C } from './Monad' import { MonadTask2C } from './MonadTask' +import { NaturalTransformation22 } from './NaturalTransformation' import { Pointed2 } from './Pointed' import { Semigroup } from './Semigroup' import * as T from './Task' import * as TH from './These' import * as TT from './TheseT' -import { FromThese2, fromTheseK as fromTheseK_ } from './FromThese' import These = TH.These import Task = T.Task @@ -95,37 +96,41 @@ export const leftIO: (me: IO) => TaskThese = /*#__PURE__*/ flow(T.fromIO, leftTask) +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- + /** - * @category constructors - * @since 2.4.0 + * @category natural transformations + * @since 2.10.0 */ -export const fromIOEither: (fa: IOEither) => TaskThese = - /*#__PURE__*/ - T.fromIO +export const fromEither: FromEither2['fromEither'] = T.of /** - * @category constructors - * @since 2.7.0 + * @category natural transformations + * @since 2.11.0 */ -export const fromIO: FromIO2['fromIO'] = rightIO +export const fromThese: FromThese2['fromThese'] = T.of /** - * @category constructors + * @category natural transformations * @since 2.7.0 */ -export const fromTask: FromTask2['fromTask'] = rightTask +export const fromIO: FromIO2['fromIO'] = rightIO /** - * @category constructors - * @since 2.10.0 + * @category natural transformations + * @since 2.4.0 */ -export const fromEither: FromEither2['fromEither'] = T.of +export const fromIOEither: NaturalTransformation22 = + /*#__PURE__*/ + T.fromIO /** - * @category constructors - * @since 2.11.0 + * @category natural transformations + * @since 2.7.0 */ -export const fromThese: FromThese2['fromThese'] = T.of +export const fromTask: FromTask2['fromTask'] = rightTask // ------------------------------------------------------------------------------------- // destructors diff --git a/src/These.ts b/src/These.ts index e814f4a76..79d46ea65 100644 --- a/src/These.ts +++ b/src/These.ts @@ -435,15 +435,14 @@ export function getRightOnly(fa: These): Option { * @category constructors * @since 2.0.0 */ -export function fromOptions(fe: Option, fa: Option): Option> { - return _.isNone(fe) +export const fromOptions = (fe: Option, fa: Option): Option> => + _.isNone(fe) ? _.isNone(fa) ? _.none : _.some(right(fa.value)) : _.isNone(fa) ? _.some(left(fe.value)) : _.some(both(fe.value, fa.value)) -} // ------------------------------------------------------------------------------------- // non-pipeables diff --git a/src/index.ts b/src/index.ts index 50d924046..ca578c3af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,6 +64,7 @@ import * as monadIO from './MonadIO' import * as monadTask from './MonadTask' import * as monadThrow from './MonadThrow' import * as monoid from './Monoid' +import * as naturalTransformation from './NaturalTransformation' import * as nonEmptyArray from './NonEmptyArray' import * as number from './number' import * as option from './Option' @@ -364,6 +365,10 @@ export { * @since 2.0.0 */ monoid, + /** + * @since 2.11.0 + */ + naturalTransformation, /** * @since 2.0.0 */ diff --git a/test/ReaderT.ts b/test/ReaderT.ts new file mode 100644 index 000000000..ae40d96ee --- /dev/null +++ b/test/ReaderT.ts @@ -0,0 +1,14 @@ +import * as E from '../src/Either' +import * as IO from '../src/IO' +import * as _ from '../src/ReaderT' +import * as TE from '../src/TaskEither' +import * as U from './util' + +describe('ReaderT', () => { + it('fromNaturalTransformation', async () => { + const fromReaderIO = _.fromNaturalTransformation(TE.fromIO) + const f = (s: string): IO.IO => IO.of(s.length) + const fa = fromReaderIO(f) + U.deepStrictEqual(await fa('a')(), E.right(1)) + }) +}) From 54b3d32250de9cae99a73727f0b243b211e7e4bf Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 30 Apr 2021 12:51:52 +0200 Subject: [PATCH 119/162] chore --- docs/modules/ReaderT.ts.md | 2 +- docs/modules/TaskEither.ts.md | 22 +++++++++++----------- docs/modules/TaskThese.ts.md | 22 +++++++++++----------- docs/modules/These.ts.md | 25 ++++++++++++++----------- dtslint/ts3.5/ReaderT.ts | 6 +++--- src/ReaderT.ts | 2 +- src/TaskEither.ts | 2 +- src/TaskThese.ts | 2 +- src/These.ts | 2 +- 9 files changed, 44 insertions(+), 41 deletions(-) diff --git a/docs/modules/ReaderT.ts.md b/docs/modules/ReaderT.ts.md index 9876700ea..4c7be6477 100644 --- a/docs/modules/ReaderT.ts.md +++ b/docs/modules/ReaderT.ts.md @@ -49,7 +49,7 @@ export declare function fromNaturalTransformation(f: (r: R) => Kind2) => Reader> export declare function fromNaturalTransformation( nt: NaturalTransformation22 -): (f: (r: R) => Kind2) => Reader> +): (f: (r: R) => Kind2) => Reader> export declare function fromNaturalTransformation( nt: NaturalTransformation12 ): (f: (r: R) => Kind) => Reader> diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 87c53539f..294e8483f 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -71,7 +71,6 @@ Added in v2.0.0 - [swap](#swap) - [constructors](#constructors) - [fromPredicate](#frompredicate) - - [fromTaskOption](#fromtaskoption) - [left](#left) - [leftIO](#leftio) - [leftTask](#lefttask) @@ -128,6 +127,7 @@ Added in v2.0.0 - [fromIOEither](#fromioeither) - [fromOption](#fromoption) - [fromTask](#fromtask) + - [fromTaskOption](#fromtaskoption) - [utils](#utils) - [Do](#do) - [apS](#aps) @@ -750,16 +750,6 @@ export declare const fromPredicate: { Added in v2.0.0 -## fromTaskOption - -**Signature** - -```ts -export declare const fromTaskOption: (onNone: Lazy) => NaturalTransformation12C<'TaskOption', 'TaskEither', E> -``` - -Added in v2.11.0 - ## left **Signature** @@ -1352,6 +1342,16 @@ export declare const fromTask: NaturalTransformation12<'Task', 'TaskEither'> Added in v2.7.0 +## fromTaskOption + +**Signature** + +```ts +export declare const fromTaskOption: (onNone: Lazy) => NaturalTransformation12C<'TaskOption', 'TaskEither', E> +``` + +Added in v2.11.0 + # utils ## Do diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index 04e938d02..165c0a872 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -28,7 +28,6 @@ Added in v2.4.0 - [swap](#swap) - [constructors](#constructors) - [both](#both) - - [fromOption](#fromoption) - [fromPredicate](#frompredicate) - [left](#left) - [leftIO](#leftio) @@ -67,6 +66,7 @@ Added in v2.4.0 - [fromEither](#fromeither) - [fromIO](#fromio) - [fromIOEither](#fromioeither) + - [fromOption](#fromoption) - [fromTask](#fromtask) - [fromThese](#fromthese) - [utils](#utils) @@ -206,16 +206,6 @@ export declare const both: (e: E, a: A) => TaskThese Added in v2.4.0 -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'TaskThese', E> -``` - -Added in v2.10.0 - ## fromPredicate **Signature** @@ -607,6 +597,16 @@ export declare const fromIOEither: NaturalTransformation22<'IOEither', 'TaskThes Added in v2.4.0 +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'TaskThese', E> +``` + +Added in v2.10.0 + ## fromTask **Signature** diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 8fcec12d2..9fdbfac73 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -47,7 +47,6 @@ Added in v2.0.0 - [swap](#swap) - [constructors](#constructors) - [both](#both) - - [fromOption](#fromoption) - [fromOptions](#fromoptions) - [left](#left) - [leftOrBoth](#leftorboth) @@ -83,6 +82,8 @@ Added in v2.0.0 - [model](#model) - [Both (interface)](#both-interface) - [These (type alias)](#these-type-alias) +- [natural transformations](#natural-transformations) + - [fromOption](#fromoption) - [refinements](#refinements) - [isBoth](#isboth) - [isLeft](#isleft) @@ -226,16 +227,6 @@ export declare function both(left: E, right: A): These Added in v2.0.0 -## fromOption - -**Signature** - -```ts -export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'These', E> -``` - -Added in v2.10.0 - ## fromOptions Takes a pair of `Option`s and attempts to create a `These` from them @@ -676,6 +667,18 @@ export type These = Either | Both Added in v2.0.0 +# natural transformations + +## fromOption + +**Signature** + +```ts +export declare const fromOption: (onNone: Lazy) => NaturalTransformation12C<'Option', 'These', E> +``` + +Added in v2.10.0 + # refinements ## isBoth diff --git a/dtslint/ts3.5/ReaderT.ts b/dtslint/ts3.5/ReaderT.ts index 27a1b24fa..13a09f6ed 100644 --- a/dtslint/ts3.5/ReaderT.ts +++ b/dtslint/ts3.5/ReaderT.ts @@ -4,7 +4,7 @@ import * as TTH from '../../src/TaskThese' import * as RTE from '../../src/ReaderTaskEither' import * as SRTE from '../../src/StateReaderTaskEither' -// $ExpectType (f: (r: R) => Either) => Reader> +// $ExpectType (f: (r: R) => Either) => Reader> _.fromNaturalTransformation(TE.fromEither) // $ExpectType (f: (r: R) => IO) => Reader> @@ -19,5 +19,5 @@ _.fromNaturalTransformation(SRTE.fromState) // $ExpectType (f: (r: R) => Task) => Reader> _.fromNaturalTransformation(TE.fromTask) -// // $ExpectType (f: (r: R) => These) => Reader> -// _.fromNaturalTransformation(TTH.fromThese) +// $ExpectType (f: (r: R) => These) => Reader> +_.fromNaturalTransformation(TTH.fromThese) diff --git a/src/ReaderT.ts b/src/ReaderT.ts index 63ee5c5eb..2f4c8393a 100644 --- a/src/ReaderT.ts +++ b/src/ReaderT.ts @@ -170,7 +170,7 @@ export function fromNaturalTransformation( ): (f: (r: R) => Kind2) => Reader> export function fromNaturalTransformation( nt: NaturalTransformation22 -): (f: (r: R) => Kind2) => Reader> +): (f: (r: R) => Kind2) => Reader> export function fromNaturalTransformation( nt: NaturalTransformation12 ): (f: (r: R) => Kind) => Reader> diff --git a/src/TaskEither.ts b/src/TaskEither.ts index f64bf114f..deea94fed 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -160,7 +160,7 @@ export const fromEither: FromEither2['fromEither'] = T.of export const fromIOEither: NaturalTransformation22 = T.fromIO /** - * @category constructors + * @category natural transformations * @since 2.11.0 */ export const fromTaskOption: (onNone: Lazy) => NaturalTransformation12C = (onNone) => diff --git a/src/TaskThese.ts b/src/TaskThese.ts index 74db403b8..b2392c5e0 100644 --- a/src/TaskThese.ts +++ b/src/TaskThese.ts @@ -397,7 +397,7 @@ export const FromEither: FromEither2 = { } /** - * @category constructors + * @category natural transformations * @since 2.10.0 */ export const fromOption = diff --git a/src/These.ts b/src/These.ts index 79d46ea65..775617eb6 100644 --- a/src/These.ts +++ b/src/These.ts @@ -650,7 +650,7 @@ export const FromEither: FromEither2 = { } /** - * @category constructors + * @category natural transformations * @since 2.10.0 */ export const fromOption = From eea1af9a7b5d1e48607ab744af59afcaba6abe04 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Fri, 30 Apr 2021 16:34:23 +0200 Subject: [PATCH 120/162] Add RTE.add-rte-chainFirstTaskEitherK(W) --- docs/modules/ReaderTaskEither.ts.md | 28 ++++++++++++++++++++++++++++ src/ReaderTaskEither.ts | 18 ++++++++++++++++++ test/ReaderTaskEither.ts | 5 +++++ 3 files changed, 51 insertions(+) diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 0e2049ff3..30fea3c20 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -45,6 +45,8 @@ Added in v2.0.0 - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstReaderTaskK](#chainfirstreadertaskk) - [chainFirstReaderTaskKW](#chainfirstreadertaskkw) + - [chainFirstTaskEitherK](#chainfirsttaskeitherk) + - [chainFirstTaskEitherKW](#chainfirsttaskeitherkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainIOEitherK](#chainioeitherk) @@ -519,6 +521,32 @@ export declare const chainFirstReaderTaskKW: ( Added in v2.11.0 +## chainFirstTaskEitherK + +**Signature** + +```ts +export declare const chainFirstTaskEitherK: ( + f: (a: A) => TE.TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstTaskEitherKW + +Less strict version of [`chainFirstTaskEitherK`](#chainFirstTaskEitherK). + +**Signature** + +```ts +export declare const chainFirstTaskEitherKW: ( + f: (a: A) => TE.TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK **Signature** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index dd8974397..c4438b54a 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -457,6 +457,24 @@ export const chainTaskEitherK: ( f: (a: A) => TaskEither ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainTaskEitherKW +/** + * Less strict version of [`chainFirstTaskEitherK`](#chainFirstTaskEitherK). + * + * @category combinators + * @since 2.11.0 + */ +export const chainFirstTaskEitherKW: ( + f: (a: A) => TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => chainFirstW(fromTaskEitherK(f)) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainFirstTaskEitherK: ( + f: (a: A) => TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainFirstTaskEitherKW + /** * @category combinators * @since 2.11.0 diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 31b3ee0cc..49537374e 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -428,6 +428,11 @@ describe('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.right('a'), _.chainTaskEitherK(f))(undefined)(), E.right(1)) }) + it('chainFirstTaskEitherKW', async () => { + const f = (s: string) => TE.right(s.length) + U.deepStrictEqual(await pipe(_.right<{}, number, string>('a'), _.chainFirstTaskEitherKW(f))({})(), E.right('a')) + }) + it('chainReaderTaskK', async () => { const f = flow(S.size, RT.of) U.deepStrictEqual(await pipe(_.right('a'), _.chainReaderTaskK(f))(undefined)(), E.right(1)) From 13c149b9273d3d3f6be0931142f265338b9863b2 Mon Sep 17 00:00:00 2001 From: vinassefranche Date: Fri, 30 Apr 2021 16:34:57 +0200 Subject: [PATCH 121/162] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d799c89d7..630d4cddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,7 @@ high state of flux, you're at risk of it changing without notice. - add `chainFirstReaderTaskK`, `chainFirstReaderTaskKW` - add `chainReaderEitherK`, `chainReaderEitherKW` - add `chainFirstReaderEitherK`, `chainFirstReaderEitherKW` + - add `chainFirstTaskEitherK`, `chainFirstTaskEitherKW` - add `flattenW` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) From f522d3d7db08bc4f702cb8d451fecc7d1840ac74 Mon Sep 17 00:00:00 2001 From: gcanti Date: Mon, 3 May 2021 18:45:38 +0200 Subject: [PATCH 122/162] add Zero module, closes #1462 --- CHANGELOG.md | 9 ++ docs/modules/Alternative.ts.md | 28 ++---- docs/modules/Array.ts.md | 52 +++++++---- docs/modules/Option.ts.md | 52 +++++++---- docs/modules/ReadonlyArray.ts.md | 52 +++++++---- docs/modules/TaskOption.ts.md | 52 +++++++---- docs/modules/Zero.ts.md | 148 +++++++++++++++++++++++++++++++ docs/modules/index.ts.md | 11 +++ src/Alternative.ts | 29 ++---- src/Array.ts | 22 ++++- src/Option.ts | 22 ++++- src/ReadonlyArray.ts | 22 ++++- src/TaskOption.ts | 22 ++++- src/Zero.ts | 93 +++++++++++++++++++ src/index.ts | 7 +- test/Option.ts | 21 +++++ 16 files changed, 531 insertions(+), 111 deletions(-) create mode 100644 docs/modules/Zero.ts.md create mode 100644 src/Zero.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ffb37593..92c816f09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ high state of flux, you're at risk of it changing without notice. - add `void` module - add `FromReader` module - add `NaturalTransformation` module + - add `Zero` module - `Alt` - add `altAll` - `Alternative` @@ -86,6 +87,8 @@ high state of flux, you're at risk of it changing without notice. - add `concat` / `concatW` - add `match`, `matchW`, `matchLeftW`, `matchRightW` - add `fromOptionK` + - add `Zero` instance + - add `guard` constructor - `boolean` - add `isBoolean` - `Either` @@ -136,6 +139,8 @@ high state of flux, you're at risk of it changing without notice. - add `FromEither` instance - add `fromEitherK` - add `chainEitherK` + - add `Zero` instance + - add `guard` constructor - `Ord` - add `trivial` instance - add `equals` @@ -188,6 +193,8 @@ high state of flux, you're at risk of it changing without notice. - add `concat` / `concatW` - add `match`, `matchW`, `matchLeftW`, `matchRightW` - add `fromOptionK` + - add `Zero` instance + - add `guard` constructor - `ReadonlyMap` - add `union` - add `intersection` @@ -251,6 +258,8 @@ high state of flux, you're at risk of it changing without notice. - add `flattenW` - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) + - add `Zero` instance + - add `guard` constructor - `Witherable` - add `filterE`, #1458 (@vinassefranche) - add `wiltDefault` diff --git a/docs/modules/Alternative.ts.md b/docs/modules/Alternative.ts.md index 1c9378b10..1efd9c7fb 100644 --- a/docs/modules/Alternative.ts.md +++ b/docs/modules/Alternative.ts.md @@ -45,9 +45,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative extends Applicative, Alt { - readonly zero: () => HKT -} +export interface Alternative extends Applicative, Alt, Zero {} ``` Added in v2.0.0 @@ -57,9 +55,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative1 extends Applicative1, Alt1 { - readonly zero: () => Kind -} +export interface Alternative1 extends Applicative1, Alt1, Zero1 {} ``` Added in v2.0.0 @@ -69,9 +65,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative2 extends Applicative2, Alt2 { - readonly zero: () => Kind2 -} +export interface Alternative2 extends Applicative2, Alt2, Zero2 {} ``` Added in v2.0.0 @@ -81,9 +75,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative2C extends Applicative2C, Alt2C { - readonly zero: () => Kind2 -} +export interface Alternative2C extends Applicative2C, Alt2C, Zero2C {} ``` Added in v2.0.0 @@ -93,9 +85,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative3 extends Applicative3, Alt3 { - readonly zero: () => Kind3 -} +export interface Alternative3 extends Applicative3, Alt3, Zero3 {} ``` Added in v2.0.0 @@ -105,9 +95,7 @@ Added in v2.0.0 **Signature** ```ts -export interface Alternative3C extends Applicative3C, Alt3C { - readonly zero: () => Kind3 -} +export interface Alternative3C extends Applicative3C, Alt3C, Zero3C {} ``` Added in v2.10.0 @@ -117,9 +105,7 @@ Added in v2.10.0 **Signature** ```ts -export interface Alternative4 extends Applicative4, Alt4 { - readonly zero: () => Kind4 -} +export interface Alternative4 extends Applicative4, Alt4, Zero4 {} ``` Added in v2.10.0 diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 84e59ab10..4345a215e 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -15,8 +15,6 @@ Added in v2.0.0 - [Alt](#alt) - [alt](#alt) - [altW](#altw) -- [Alternative](#alternative) - - [zero](#zero) - [Apply](#apply) - [ap](#ap) - [ChainRec](#chainrec) @@ -63,6 +61,8 @@ Added in v2.0.0 - [Witherable](#witherable) - [wilt](#wilt) - [wither](#wither) +- [Zero](#zero) + - [zero](#zero) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -107,6 +107,7 @@ Added in v2.0.0 - [append](#append) - [appendW](#appendw) - [fromPredicate](#frompredicate) + - [guard](#guard) - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) @@ -134,7 +135,7 @@ Added in v2.0.0 - [tail](#tail) - [instances](#instances) - [Alt](#alt-1) - - [Alternative](#alternative-1) + - [Alternative](#alternative) - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) @@ -157,6 +158,7 @@ Added in v2.0.0 - [URI (type alias)](#uri-type-alias) - [Unfoldable](#unfoldable-1) - [Witherable](#witherable-1) + - [Zero](#zero-1) - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getIntersectionSemigroup](#getintersectionsemigroup) @@ -228,18 +230,6 @@ export declare const altW: (that: Lazy) => (fa: A[]) => (B | A)[] Added in v2.9.0 -# Alternative - -## zero - -**Signature** - -```ts -export declare const zero: () => A[] -``` - -Added in v2.7.0 - # Apply ## ap @@ -593,6 +583,18 @@ export declare const wither: PipeableWither1<'Array'> Added in v2.6.5 +# Zero + +## zero + +**Signature** + +```ts +export declare const zero: () => A[] +``` + +Added in v2.7.0 + # combinators ## apFirst @@ -1451,6 +1453,16 @@ export declare function fromPredicate(predicate: Predicate): (a: A) => Arr Added in v2.11.0 +## guard + +**Signature** + +```ts +export declare const guard: (b: boolean) => void[] +``` + +Added in v2.11.0 + ## makeBy Return a `Array` of length `n` with element `i` initialized with `f(i)`. @@ -2162,6 +2174,16 @@ export declare const Witherable: Witherable1<'Array'> Added in v2.7.0 +## Zero + +**Signature** + +```ts +export declare const Zero: Zero1<'Array'> +``` + +Added in v2.11.0 + ## getDifferenceMagma **Signature** diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index a83709a8b..812c0d3b7 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -26,8 +26,6 @@ Added in v2.0.0 - [Alt](#alt) - [alt](#alt) - [altW](#altw) -- [Alternative](#alternative) - - [zero](#zero) - [Apply](#apply) - [ap](#ap) - [Compactable](#compactable) @@ -58,6 +56,8 @@ Added in v2.0.0 - [Witherable](#witherable) - [wilt](#wilt) - [wither](#wither) +- [Zero](#zero) + - [zero](#zero) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -72,6 +72,7 @@ Added in v2.0.0 - [fromPredicate](#frompredicate) - [getLeft](#getleft) - [getRight](#getright) + - [guard](#guard) - [none](#none) - [some](#some) - [destructors](#destructors) @@ -83,7 +84,7 @@ Added in v2.0.0 - [matchW](#matchw) - [instances](#instances) - [Alt](#alt-1) - - [Alternative](#alternative-1) + - [Alternative](#alternative) - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) @@ -100,6 +101,7 @@ Added in v2.0.0 - [URI](#uri) - [URI (type alias)](#uri-type-alias) - [Witherable](#witherable-1) + - [Zero](#zero-1) - [getEq](#geteq) - [getMonoid](#getmonoid) - [getOrd](#getord) @@ -191,18 +193,6 @@ export declare const altW: (that: Lazy>) => (fa: Option) => O Added in v2.9.0 -# Alternative - -## zero - -**Signature** - -```ts -export declare const zero: () => Option -``` - -Added in v2.7.0 - # Apply ## ap @@ -430,6 +420,18 @@ export declare const wither: PipeableWither1<'Option'> Added in v2.6.5 +# Zero + +## zero + +**Signature** + +```ts +export declare const zero: () => Option +``` + +Added in v2.7.0 + # combinators ## apFirst @@ -613,6 +615,16 @@ assert.deepStrictEqual(getRight(left('a')), none) Added in v2.0.0 +## guard + +**Signature** + +```ts +export declare const guard: (b: boolean) => Option +``` + +Added in v2.11.0 + ## none `None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value. @@ -945,6 +957,16 @@ export declare const Witherable: Witherable1<'Option'> Added in v2.7.0 +## Zero + +**Signature** + +```ts +export declare const Zero: Zero1<'Option'> +``` + +Added in v2.11.0 + ## getEq **Signature** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index dc779f85a..d4dbfd674 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -15,8 +15,6 @@ Added in v2.5.0 - [Alt](#alt) - [alt](#alt) - [altW](#altw) -- [Alternative](#alternative) - - [zero](#zero) - [Apply](#apply) - [ap](#ap) - [ChainRec](#chainrec) @@ -63,6 +61,8 @@ Added in v2.5.0 - [Witherable](#witherable) - [wilt](#wilt) - [wither](#wither) +- [Zero](#zero) + - [zero](#zero) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -106,6 +106,7 @@ Added in v2.5.0 - [append](#append) - [appendW](#appendw) - [fromPredicate](#frompredicate) + - [guard](#guard) - [makeBy](#makeby) - [prepend](#prepend) - [prependW](#prependw) @@ -124,7 +125,7 @@ Added in v2.5.0 - [matchW](#matchw) - [instances](#instances) - [Alt](#alt-1) - - [Alternative](#alternative-1) + - [Alternative](#alternative) - [Applicative](#applicative) - [Apply](#apply-1) - [Chain](#chain) @@ -147,6 +148,7 @@ Added in v2.5.0 - [URI (type alias)](#uri-type-alias) - [Unfoldable](#unfoldable-1) - [Witherable](#witherable-1) + - [Zero](#zero-1) - [getDifferenceMagma](#getdifferencemagma) - [getEq](#geteq) - [getIntersectionSemigroup](#getintersectionsemigroup) @@ -230,18 +232,6 @@ export declare const altW: (that: Lazy) => (fa: readonly A[] Added in v2.9.0 -# Alternative - -## zero - -**Signature** - -```ts -export declare const zero: () => readonly A[] -``` - -Added in v2.7.0 - # Apply ## ap @@ -601,6 +591,18 @@ export declare const wither: PipeableWither1<'ReadonlyArray'> Added in v2.6.5 +# Zero + +## zero + +**Signature** + +```ts +export declare const zero: () => readonly A[] +``` + +Added in v2.7.0 + # combinators ## apFirst @@ -1490,6 +1492,16 @@ export declare function fromPredicate(predicate: Predicate): (a: A) => Rea Added in v2.11.0 +## guard + +**Signature** + +```ts +export declare const guard: (b: boolean) => readonly void[] +``` + +Added in v2.11.0 + ## makeBy Return a `ReadonlyArray` of length `n` with element `i` initialized with `f(i)`. @@ -1978,6 +1990,16 @@ export declare const Witherable: Witherable1<'ReadonlyArray'> Added in v2.7.0 +## Zero + +**Signature** + +```ts +export declare const Zero: Zero1<'ReadonlyArray'> +``` + +Added in v2.11.0 + ## getDifferenceMagma **Signature** diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index b7a12eef7..4bf96370d 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -15,8 +15,6 @@ Added in v2.10.0 - [Alt](#alt) - [alt](#alt) - [altW](#altw) -- [Alternative](#alternative) - - [zero](#zero) - [Apply](#apply) - [ap](#ap) - [Compactable](#compactable) @@ -33,6 +31,8 @@ Added in v2.10.0 - [chain](#chain) - [Pointed](#pointed) - [of](#of) +- [Zero](#zero) + - [zero](#zero) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -49,6 +49,7 @@ Added in v2.10.0 - [fromTaskK](#fromtaskk) - [constructors](#constructors) - [fromPredicate](#frompredicate) + - [guard](#guard) - [none](#none) - [some](#some) - [destructors](#destructors) @@ -62,7 +63,7 @@ Added in v2.10.0 - [matchW](#matchw) - [instances](#instances) - [Alt](#alt-1) - - [Alternative](#alternative-1) + - [Alternative](#alternative) - [ApplicativePar](#applicativepar) - [ApplicativeSeq](#applicativeseq) - [ApplyPar](#applypar) @@ -78,6 +79,7 @@ Added in v2.10.0 - [MonadTask](#monadtask) - [Pointed](#pointed-1) - [URI (type alias)](#uri-type-alias) + - [Zero](#zero-1) - [interop](#interop) - [chainNullableK](#chainnullablek) - [fromNullable](#fromnullable) @@ -130,18 +132,6 @@ export declare const altW: (second: Lazy>) => (first: TaskOp Added in v2.10.0 -# Alternative - -## zero - -**Signature** - -```ts -export declare const zero: () => TaskOption -``` - -Added in v2.10.0 - # Apply ## ap @@ -265,6 +255,18 @@ export declare const of: (a: A) => TaskOption Added in v2.10.0 +# Zero + +## zero + +**Signature** + +```ts +export declare const zero: () => TaskOption +``` + +Added in v2.10.0 + # combinators ## apFirst @@ -431,6 +433,16 @@ export declare const fromPredicate: { Added in v2.10.0 +## guard + +**Signature** + +```ts +export declare const guard: (b: boolean) => TaskOption +``` + +Added in v2.11.0 + ## none **Signature** @@ -727,6 +739,16 @@ export type URI = typeof URI Added in v2.10.0 +## Zero + +**Signature** + +```ts +export declare const Zero: Zero1<'TaskOption'> +``` + +Added in v2.11.0 + # interop ## chainNullableK diff --git a/docs/modules/Zero.ts.md b/docs/modules/Zero.ts.md new file mode 100644 index 000000000..a218ad433 --- /dev/null +++ b/docs/modules/Zero.ts.md @@ -0,0 +1,148 @@ +--- +title: Zero.ts +nav_order: 121 +parent: Modules +--- + +## Zero overview + +Added in v2.11.0 + +--- + +

Table of contents

+ +- [constructors](#constructors) + - [guard](#guard) +- [type classes](#type-classes) + - [Zero (interface)](#zero-interface) + - [Zero1 (interface)](#zero1-interface) + - [Zero2 (interface)](#zero2-interface) + - [Zero2C (interface)](#zero2c-interface) + - [Zero3 (interface)](#zero3-interface) + - [Zero3C (interface)](#zero3c-interface) + - [Zero4 (interface)](#zero4-interface) + +--- + +# constructors + +## guard + +**Signature** + +```ts +export declare function guard( + F: Zero4, + P: Pointed4 +): (b: boolean) => Kind4 +export declare function guard(F: Zero3, P: Pointed3): (b: boolean) => Kind3 +export declare function guard( + F: Zero3C, + P: Pointed3C +): (b: boolean) => Kind3 +export declare function guard(F: Zero2, P: Pointed2): (b: boolean) => Kind2 +export declare function guard( + F: Zero2C, + P: Pointed2C +): (b: boolean) => Kind2 +export declare function guard(F: Zero1, P: Pointed1): (b: boolean) => Kind +export declare function guard(F: Zero, P: Pointed): (b: boolean) => HKT +``` + +Added in v2.11.0 + +# type classes + +## Zero (interface) + +**Signature** + +```ts +export interface Zero { + readonly URI: F + readonly zero:
() => HKT +} +``` + +Added in v2.11.0 + +## Zero1 (interface) + +**Signature** + +```ts +export interface Zero1 { + readonly URI: F + readonly zero: () => Kind +} +``` + +Added in v2.11.0 + +## Zero2 (interface) + +**Signature** + +```ts +export interface Zero2 { + readonly URI: F + readonly zero: () => Kind2 +} +``` + +Added in v2.11.0 + +## Zero2C (interface) + +**Signature** + +```ts +export interface Zero2C { + readonly URI: F + readonly _E: E + readonly zero: () => Kind2 +} +``` + +Added in v2.11.0 + +## Zero3 (interface) + +**Signature** + +```ts +export interface Zero3 { + readonly URI: F + readonly zero: () => Kind3 +} +``` + +Added in v2.11.0 + +## Zero3C (interface) + +**Signature** + +```ts +export interface Zero3C { + readonly URI: F + readonly _E: E + readonly zero: () => Kind3 +} +``` + +Added in v2.11.0 + +## Zero4 (interface) + +**Signature** + +```ts +export interface Zero4 { + readonly URI: F + readonly zero: () => Kind4 +} +``` + +Added in v2.11.0 diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index 3011840c1..3dcca3664 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -131,6 +131,7 @@ Added in v2.0.0 - [witherable](#witherable) - [writer](#writer) - [writerT](#writert) + - [zero](#zero) --- @@ -1315,3 +1316,13 @@ export declare const writerT: typeof writerT ``` Added in v2.4.0 + +## zero + +**Signature** + +```ts +export declare const zero: typeof zero +``` + +Added in v2.11.0 diff --git a/src/Alternative.ts b/src/Alternative.ts index b15e13a50..69d151870 100644 --- a/src/Alternative.ts +++ b/src/Alternative.ts @@ -25,6 +25,7 @@ import { Applicative4 } from './Applicative' import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' +import { Zero, Zero1, Zero2, Zero2C, Zero3, Zero3C, Zero4 } from './Zero' // ------------------------------------------------------------------------------------- // model @@ -34,57 +35,43 @@ import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT * @category type classes * @since 2.0.0 */ -export interface Alternative extends Applicative, Alt { - readonly zero: () => HKT -} +export interface Alternative extends Applicative, Alt, Zero {} /** * @category type classes * @since 2.0.0 */ -export interface Alternative1 extends Applicative1, Alt1 { - readonly zero: () => Kind -} +export interface Alternative1 extends Applicative1, Alt1, Zero1 {} /** * @category type classes * @since 2.0.0 */ -export interface Alternative2 extends Applicative2, Alt2 { - readonly zero: () => Kind2 -} +export interface Alternative2 extends Applicative2, Alt2, Zero2 {} /** * @category type classes * @since 2.0.0 */ -export interface Alternative2C extends Applicative2C, Alt2C { - readonly zero: () => Kind2 -} +export interface Alternative2C extends Applicative2C, Alt2C, Zero2C {} /** * @category type classes * @since 2.0.0 */ -export interface Alternative3 extends Applicative3, Alt3 { - readonly zero: () => Kind3 -} +export interface Alternative3 extends Applicative3, Alt3, Zero3 {} /** * @category type classes * @since 2.10.0 */ -export interface Alternative3C extends Applicative3C, Alt3C { - readonly zero: () => Kind3 -} +export interface Alternative3C extends Applicative3C, Alt3C, Zero3C {} /** * @category type classes * @since 2.10.0 */ -export interface Alternative4 extends Applicative4, Alt4 { - readonly zero: () => Kind4 -} +export interface Alternative4 extends Applicative4, Alt4, Zero4 {} // ------------------------------------------------------------------------------------- // utils diff --git a/src/Array.ts b/src/Array.ts index 297f623a0..62689c41f 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -46,6 +46,7 @@ import { Witherable1, witherDefault } from './Witherable' +import { Zero1, guard as guard_ } from './Zero' import NonEmptyArray = NEA.NonEmptyArray @@ -1349,10 +1350,10 @@ const _chainRecBreadthFirst: ChainRec1['chainRec'] = RA._chainRecBreadthFir export const of: Pointed1['of'] = NEA.of /** - * @category Alternative + * @category Zero * @since 2.7.0 */ -export const zero: Alternative1['zero'] = () => [] +export const zero: Zero1['zero'] = () => [] /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types @@ -1954,6 +1955,23 @@ export const Alt: Alt1 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const Zero: Zero1 = { + URI, + zero +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const guard = + /*#__PURE__*/ + guard_(Zero, Pointed) + /** * @category instances * @since 2.7.0 diff --git a/src/Option.ts b/src/Option.ts index 9e8c2a5be..d48d7dd21 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -46,6 +46,7 @@ import { Separated, separated } from './Separated' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable' +import { Zero1, guard as guard_ } from './Zero' // ------------------------------------------------------------------------------------- // model @@ -592,10 +593,10 @@ export const altW: (that: Lazy>) => (fa: Option) => Option(that: Lazy>) => (fa: Option) => Option = altW /** - * @category Alternative + * @category Zero * @since 2.7.0 */ -export const zero: Alternative1['zero'] = () => none +export const zero: Zero1['zero'] = () => none /** * @category MonadThrow @@ -973,6 +974,23 @@ export const Alt: Alt1 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const Zero: Zero1 = { + URI, + zero +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const guard = + /*#__PURE__*/ + guard_(Zero, Pointed) + /** * @category instances * @since 2.7.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 08a42665c..2cf6ccc1b 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -47,6 +47,7 @@ import { Witherable1, witherDefault } from './Witherable' +import { Zero1, guard as guard_ } from './Zero' import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray @@ -1435,10 +1436,10 @@ export const _chainRecBreadthFirst: ChainRec1['chainRec'] = (a, f) => pipe( export const of: Pointed1['of'] = RNEA.of /** - * @category Alternative + * @category Zero * @since 2.7.0 */ -export const zero: Alternative1['zero'] = () => empty +export const zero: Zero1['zero'] = () => empty /** * Less strict version of [`alt`](#alt). @@ -2088,6 +2089,23 @@ export const Alt: Alt1 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const Zero: Zero1 = { + URI, + zero +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const guard = + /*#__PURE__*/ + guard_(Zero, Pointed) + /** * @category instances * @since 2.7.0 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 4b4138442..76ed670d8 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -38,6 +38,7 @@ import { Refinement } from './Refinement' import { Separated } from './Separated' import * as T from './Task' import { URI as TEURI } from './TaskEither' +import { Zero1, guard as guard_ } from './Zero' import Task = T.Task import Option = O.Option @@ -333,10 +334,10 @@ export const alt: (second: Lazy>) => (first: TaskOption) => export const altW: (second: Lazy>) => (first: TaskOption) => TaskOption = alt as any /** - * @category Alternative + * @category Zero * @since 2.10.0 */ -export const zero: Alternative1['zero'] = +export const zero: Zero1['zero'] = /*#__PURE__*/ OT.zero(T.Pointed) @@ -577,6 +578,23 @@ export const Alt: Alt1 = { alt: _alt } +/** + * @category instances + * @since 2.11.0 + */ +export const Zero: Zero1 = { + URI, + zero +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const guard = + /*#__PURE__*/ + guard_(Zero, Pointed) + /** * @category instances * @since 2.10.0 diff --git a/src/Zero.ts b/src/Zero.ts new file mode 100644 index 000000000..97cc73670 --- /dev/null +++ b/src/Zero.ts @@ -0,0 +1,93 @@ +/** + * @since 2.11.0 + */ +import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT' +import { Pointed, Pointed1, Pointed2, Pointed2C, Pointed3, Pointed3C, Pointed4 } from './Pointed' + +// ------------------------------------------------------------------------------------- +// model +// ------------------------------------------------------------------------------------- + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero { + readonly URI: F + readonly zero: () => HKT +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero1 { + readonly URI: F + readonly zero: () => Kind +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero2 { + readonly URI: F + readonly zero: () => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero2C { + readonly URI: F + readonly _E: E + readonly zero: () => Kind2 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero3 { + readonly URI: F + readonly zero: () => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero3C { + readonly URI: F + readonly _E: E + readonly zero: () => Kind3 +} + +/** + * @category type classes + * @since 2.11.0 + */ +export interface Zero4 { + readonly URI: F + readonly zero: () => Kind4 +} + +// ------------------------------------------------------------------------------------- +// constructors +// ------------------------------------------------------------------------------------- + +/** + * @category constructors + * @since 2.11.0 + */ +export function guard(F: Zero4, P: Pointed4): (b: boolean) => Kind4 +export function guard(F: Zero3, P: Pointed3): (b: boolean) => Kind3 +export function guard(F: Zero3C, P: Pointed3C): (b: boolean) => Kind3 +export function guard(F: Zero2, P: Pointed2): (b: boolean) => Kind2 +export function guard(F: Zero2C, P: Pointed2C): (b: boolean) => Kind2 +export function guard(F: Zero1, P: Pointed1): (b: boolean) => Kind +export function guard(F: Zero, P: Pointed): (b: boolean) => HKT +export function guard(F: Zero, P: Pointed): (b: boolean) => HKT { + return (b) => (b ? P.of(undefined) : F.zero()) +} diff --git a/src/index.ts b/src/index.ts index ca578c3af..6d1fb9f91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -120,6 +120,7 @@ import * as void_ from './void' import * as witherable from './Witherable' import * as writer from './Writer' import * as writerT from './WriterT' +import * as zero from './Zero' export { /** * @since 2.0.0 @@ -592,5 +593,9 @@ export { /** * @since 2.4.0 */ - writerT + writerT, + /** + * @since 2.11.0 + */ + zero } diff --git a/test/Option.ts b/test/Option.ts index 125f0a763..14b14c3b1 100644 --- a/test/Option.ts +++ b/test/Option.ts @@ -493,4 +493,25 @@ describe('Option', () => { U.deepStrictEqual(f('a'), _.some(1)) U.deepStrictEqual(f(''), _.none) }) + + it('guard', () => { + U.deepStrictEqual( + pipe( + _.Do, + _.bind('x', () => _.some('a')), + _.bind('y', () => _.some('a')), + _.chainFirst(({ x, y }) => _.guard(x === y)) + ), + _.some({ x: 'a', y: 'a' }) + ) + U.deepStrictEqual( + pipe( + _.Do, + _.bind('x', () => _.some('a')), + _.bind('y', () => _.some('b')), + _.chainFirst(({ x, y }) => _.guard(x === y)) + ), + _.none + ) + }) }) From 693e081827480cd6392eabf152c1ac020ba88a3d Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 12:59:29 +0200 Subject: [PATCH 123/162] rename asksE, asksEW --- CHANGELOG.md | 10 ++++---- docs/modules/Array.ts.md | 4 ++-- docs/modules/Either.ts.md | 4 ++-- docs/modules/Reader.ts.md | 16 ++++++------- docs/modules/ReaderEither.ts.md | 20 +++++++++------- docs/modules/ReaderTask.ts.md | 20 ++++++++-------- docs/modules/ReaderTaskEither.ts.md | 30 +++++++++++++----------- docs/modules/ReadonlyArray.ts.md | 4 ++-- docs/modules/StateReaderTaskEither.ts.md | 14 +++++------ src/Array.ts | 4 ++-- src/Either.ts | 4 ++-- src/Reader.ts | 8 +++---- src/ReaderEither.ts | 13 ++++++---- src/ReaderTask.ts | 12 +++++----- src/ReaderTaskEither.ts | 25 +++++++++++--------- src/ReadonlyArray.ts | 4 ++-- src/StateReaderTaskEither.ts | 8 +++---- test/Reader.ts | 4 ++-- test/StateReaderTaskEither.ts | 4 ++-- 19 files changed, 109 insertions(+), 99 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c816f09..b57e8e3a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -145,22 +145,22 @@ high state of flux, you're at risk of it changing without notice. - add `trivial` instance - add `equals` - `Reader` - - add `asksEW`, `asksW` + - add `asksReaderW`, `asksReader` - add `flattenW` - `ReaderEither` - - add `asksEW`, `asksW` + - add `asksReaderEitherW`, `asksReaderEither` - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` - `ReaderTask` - - add `asksEW`, `asksW` + - add `asksReaderTaskW`, `asksReaderTask` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` - `ReaderTaskEither` - - add `asksEW`, `asksW` + - add `asksReaderTaskEitherW`, `asksReaderTaskEither` - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `fromReaderTaskK` @@ -241,7 +241,7 @@ high state of flux, you're at risk of it changing without notice. - add `fromStateK` - add `chainStateK` - add `local` - - add `asksEW`, `asksW` + - add `asksStateReaderTaskEitherW`, `asksStateReaderTaskEither` - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 4345a215e..a801da8eb 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -1831,7 +1831,7 @@ Added in v2.10.0 ## matchLeftW -Less strict version of [`matchLeft`](#matchLeft). +Less strict version of [`matchLeft`](#matchleft). **Signature** @@ -1858,7 +1858,7 @@ Added in v2.10.0 ## matchRightW -Less strict version of [`matchRight`](#matchRight). +Less strict version of [`matchRight`](#matchright). **Signature** diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 8f5aa4aa6..a2384cbcd 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -571,7 +571,7 @@ Added in v2.0.0 ## filterOrElseW -Less strict version of [`filterOrElse`](#filterOrElse). +Less strict version of [`filterOrElse`](#filterorelse). **Signature** @@ -791,7 +791,7 @@ Added in v2.0.0 ## foldW -Alias of [`matchW`](#matchww). +Alias of [`matchW`](#matchw). **Signature** diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 905f989c5..c8835247b 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -37,8 +37,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) - - [asksE](#askse) - - [asksEW](#asksew) + - [asksReader](#asksreader) + - [asksReaderW](#asksreaderw) - [chainFirst](#chainfirst) - [chainFirstW](#chainfirstw) - [flap](#flap) @@ -268,26 +268,26 @@ export declare const apSecond: (second: Reader) => (first: Reader Added in v2.0.0 -## asksE +## asksReader Effectfully accesses the environment. **Signature** ```ts -export declare const asksE: (f: (r: R) => Reader) => Reader +export declare const asksReader: (f: (r: R) => Reader) => Reader ``` Added in v2.11.0 -## asksEW +## asksReaderW -Less strict version of [`asksE`](#asksE). +Less strict version of [`asksReader`](#asksreader). **Signature** ```ts -export declare const asksEW: (f: (r1: R1) => Reader) => Reader +export declare const asksReaderW: (f: (r1: R1) => Reader) => Reader ``` Added in v2.11.0 @@ -309,7 +309,7 @@ Added in v2.0.0 ## chainFirstW -Less strict version of [`chainFirst`](#chainFirst). +Less strict version of [`chainFirst`](#chainfirst). Derivable from `Chain`. diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 9e61a907b..8597357a4 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -33,8 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) - - [asksE](#askse) - - [asksEW](#asksew) + - [asksReaderEither](#asksreadereither) + - [asksReaderEitherW](#asksreadereitherw) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -313,26 +313,28 @@ export declare const apSecond: ( Added in v2.0.0 -## asksE +## asksReaderEither Effectfully accesses the environment. **Signature** ```ts -export declare const asksE: (f: (r: R) => ReaderEither) => ReaderEither +export declare const asksReaderEither: (f: (r: R) => ReaderEither) => ReaderEither ``` Added in v2.11.0 -## asksEW +## asksReaderEitherW -Less strict version of [`asksE`](#asksE). +Less strict version of [`asksReaderEither`](#asksreadereither). **Signature** ```ts -export declare const asksEW: (f: (r1: R1) => ReaderEither) => ReaderEither +export declare const asksReaderEitherW: ( + f: (r1: R1) => ReaderEither +) => ReaderEither ``` Added in v2.11.0 @@ -394,7 +396,7 @@ Added in v2.11.0 ## chainFirstReaderKW -Less strict version of [`chainReaderK`](#chainReaderK). +Less strict version of [`chainReaderK`](#chainreaderk). **Signature** @@ -448,7 +450,7 @@ Added in v2.11.0 ## chainReaderKW -Less strict version of [`chainReaderK`](#chainReaderK). +Less strict version of [`chainReaderK`](#chainreaderk). **Signature** diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 64c9b66a6..721bc466d 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -25,8 +25,8 @@ Added in v2.3.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) - - [asksE](#askse) - - [asksEW](#asksew) + - [asksReaderTask](#asksreadertask) + - [asksReaderTaskW](#asksreadertaskw) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) - [chainFirstReaderK](#chainfirstreaderk) @@ -203,26 +203,26 @@ export declare const apSecond: (second: ReaderTask) => (first: Re Added in v2.3.0 -## asksE +## asksReaderTask Effectfully accesses the environment. **Signature** ```ts -export declare const asksE: (f: (r: R) => ReaderTask) => ReaderTask +export declare const asksReaderTask: (f: (r: R) => ReaderTask) => ReaderTask ``` Added in v2.11.0 -## asksEW +## asksReaderTaskW -Less strict version of [`asksE`](#asksE). +Less strict version of [`asksReaderTask`](#asksreadertask). **Signature** ```ts -export declare const asksEW: (f: (r1: R1) => ReaderTask) => ReaderTask +export declare const asksReaderTaskW: (f: (r1: R1) => ReaderTask) => ReaderTask ``` Added in v2.11.0 @@ -268,7 +268,7 @@ Added in v2.11.0 ## chainFirstReaderKW -Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). +Less strict version of [`chainFirstReaderK`](#chainfirstReaderk). **Signature** @@ -292,7 +292,7 @@ Added in v2.10.0 ## chainFirstW -Less strict version of [`chainFirst`](#chainFirst). +Less strict version of [`chainFirst`](#chainfirst). Derivable from `Chain`. @@ -328,7 +328,7 @@ Added in v2.11.0 ## chainReaderKW -Less strict version of [`chainReaderK`](#chainReaderK). +Less strict version of [`chainReaderK`](#chainreaderk). **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 30fea3c20..377eb8094 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -33,8 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) - - [asksE](#askse) - - [asksEW](#asksew) + - [asksReaderTaskEither](#asksreadertaskeither) + - [asksReaderTaskEitherW](#asksreadertaskeitherw) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -362,26 +362,28 @@ export declare const apSecond: ( Added in v2.0.0 -## asksE +## asksReaderTaskEither Effectfully accesses the environment. **Signature** ```ts -export declare const asksE: (f: (r: R) => ReaderTaskEither) => ReaderTaskEither +export declare const asksReaderTaskEither: ( + f: (r: R) => ReaderTaskEither +) => ReaderTaskEither ``` Added in v2.11.0 -## asksEW +## asksReaderTaskEitherW -Less strict version of [`asksE`](#asksE). +Less strict version of [`asksReaderTaskEither`](#asksreadertaskeither). **Signature** ```ts -export declare const asksEW: ( +export declare const asksReaderTaskEitherW: ( f: (r1: R1) => ReaderTaskEither ) => ReaderTaskEither ``` @@ -457,7 +459,7 @@ Added in v2.11.0 ## chainFirstReaderEitherKW -Less strict version of [`chainFirstReaderEitherK`](#chainFirstReaderEitherK). +Less strict version of [`chainFirstReaderEitherK`](#chainfirstreadereitherk). **Signature** @@ -483,7 +485,7 @@ Added in v2.11.0 ## chainFirstReaderKW -Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). +Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). **Signature** @@ -509,7 +511,7 @@ Added in v2.11.0 ## chainFirstReaderTaskKW -Less strict version of [`chainFirstReaderTaskK`](#chainFirstReaderTaskK). +Less strict version of [`chainFirstReaderTaskK`](#chainfirstreadertaskk). **Signature** @@ -535,7 +537,7 @@ Added in v2.11.0 ## chainFirstTaskEitherKW -Less strict version of [`chainFirstTaskEitherK`](#chainFirstTaskEitherK). +Less strict version of [`chainFirstTaskEitherK`](#chainfirsttaskeitherk). **Signature** @@ -639,7 +641,7 @@ Added in v2.11.0 ## chainReaderEitherKW -Less strict version of [`chainReaderEitherK`](#chainReaderEitherK). +Less strict version of [`chainReaderEitherK`](#chainreadereitherk). **Signature** @@ -665,7 +667,7 @@ Added in v2.11.0 ## chainReaderKW -Less strict version of [`chainReaderK`](#chainReaderK). +Less strict version of [`chainReaderK`](#chainreaderk). **Signature** @@ -691,7 +693,7 @@ Added in v2.11.0 ## chainReaderTaskKW -Less strict version of [`chainReaderTaskK`](#chainReaderTaskK). +Less strict version of [`chainReaderTaskK`](#chainreadertaskk). **Signature** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index d4dbfd674..800227058 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -1690,7 +1690,7 @@ Added in v2.10.0 ## matchLeftW -Less strict version of [`matchLeft`](#matchLeft). +Less strict version of [`matchLeft`](#matchleft). **Signature** @@ -1720,7 +1720,7 @@ Added in v2.10.0 ## matchRightW -Less strict version of [`matchRight`](#matchRight). +Less strict version of [`matchRight`](#matchright). **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index a79aa28ee..a47a3b996 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -33,8 +33,8 @@ Added in v2.0.0 - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) - - [asksE](#askse) - - [asksEW](#asksew) + - [asksStateReaderTaskEither](#asksstatereadertaskeither) + - [asksStateReaderTaskEitherW](#asksstatereadertaskeitherw) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) @@ -336,28 +336,28 @@ export declare const apSecond: ( Added in v2.0.0 -## asksE +## asksStateReaderTaskEither Effectfully accesses the environment. **Signature** ```ts -export declare const asksE: ( +export declare const asksStateReaderTaskEither: ( f: (r: R) => StateReaderTaskEither ) => StateReaderTaskEither ``` Added in v2.11.0 -## asksEW +## asksStateReaderTaskEitherW -Less strict version of [`asksE`](#asksE). +Less strict version of [`asksStateReaderTaskEither`](#asksstatereadertaskeither). **Signature** ```ts -export declare const asksEW: ( +export declare const asksStateReaderTaskEitherW: ( f: (r1: R1) => StateReaderTaskEither ) => StateReaderTaskEither ``` diff --git a/src/Array.ts b/src/Array.ts index 62689c41f..7ba36a818 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -202,7 +202,7 @@ export const matchW = (onEmpty: Lazy, onNonEmpty: (as: NonEmptyArray export const match: (onEmpty: Lazy, onNonEmpty: (as: NonEmptyArray) => B) => (as: Array) => B = matchW /** - * Less strict version of [`matchLeft`](#matchLeft). + * Less strict version of [`matchLeft`](#matchleft). * * @category destructors * @since 2.11.0 @@ -240,7 +240,7 @@ export const foldLeft: ( ) => (as: Array) => B = matchLeft /** - * Less strict version of [`matchRight`](#matchRight). + * Less strict version of [`matchRight`](#matchright). * * @category destructors * @since 2.11.0 diff --git a/src/Either.ts b/src/Either.ts index 180e7c1cc..fe8d277ac 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -138,7 +138,7 @@ export const matchW = (onLeft: (e: E) => B, onRight: (a: A) => C) => isLeft(ma) ? onLeft(ma.left) : onRight(ma.right) /** - * Alias of [`matchW`](#matchww). + * Alias of [`matchW`](#matchw). * * @category destructors * @since 2.10.0 @@ -1197,7 +1197,7 @@ export const filterOrElse = filterOrElse_(FromEither, Chain) /** - * Less strict version of [`filterOrElse`](#filterOrElse). + * Less strict version of [`filterOrElse`](#filterorelse). * * @category combinators * @since 2.9.0 diff --git a/src/Reader.ts b/src/Reader.ts index 72fdda0af..51a7c3763 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -64,12 +64,12 @@ export const local: (f: (r2: R2) => R1) => (ma: Reader) => Rea ma(f(r2)) /** - * Less strict version of [`asksE`](#asksE). + * Less strict version of [`asksReader`](#asksreader). * * @category combinators * @since 2.11.0 */ -export const asksEW = (f: (r1: R1) => Reader): Reader => (r) => f(r)(r) +export const asksReaderW = (f: (r1: R1) => Reader): Reader => (r) => f(r)(r) /** * Effectfully accesses the environment. @@ -77,7 +77,7 @@ export const asksEW = (f: (r1: R1) => Reader): Reader(f: (r: R) => Reader) => Reader = asksEW +export const asksReader: (f: (r: R) => Reader) => Reader = asksReaderW // ------------------------------------------------------------------------------------- // non-pipeables @@ -341,7 +341,7 @@ export const chainFirst = chainFirst_(Chain) /** - * Less strict version of [`chainFirst`](#chainFirst). + * Less strict version of [`chainFirst`](#chainfirst). * * Derivable from `Chain`. * diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index dc1af2dae..32b94100d 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -229,12 +229,13 @@ export const local: (f: (r2: R2) => R1) => (ma: ReaderEither(f: (r1: R1) => ReaderEither) => ReaderEither = R.asksEW +export const asksReaderEitherW: (f: (r1: R1) => ReaderEither) => ReaderEither = + R.asksReaderW /** * Effectfully accesses the environment. @@ -242,7 +243,9 @@ export const asksEW: (f: (r1: R1) => ReaderEither) => Re * @category combinators * @since 2.11.0 */ -export const asksE: (f: (r: R) => ReaderEither) => ReaderEither = asksEW +export const asksReaderEither: ( + f: (r: R) => ReaderEither +) => ReaderEither = asksReaderEitherW /** * @category combinators @@ -735,7 +738,7 @@ export const chainReaderK: ( chainReaderK_(FromReader, Chain) /** - * Less strict version of [`chainReaderK`](#chainReaderK). + * Less strict version of [`chainReaderK`](#chainreaderk). * * @category combinators * @since 2.11.0 @@ -755,7 +758,7 @@ export const chainFirstReaderK: ( chainFirstReaderK_(FromReader, Chain) /** - * Less strict version of [`chainReaderK`](#chainReaderK). + * Less strict version of [`chainReaderK`](#chainreaderk). * * @category combinators * @since 2.11.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index ae859b7a5..590bdf5f0 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -94,12 +94,12 @@ export const fromIO: FromIO2['fromIO'] = export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local /** - * Less strict version of [`asksE`](#asksE). + * Less strict version of [`asksReaderTask`](#asksreadertask). * * @category combinators * @since 2.11.0 */ -export const asksEW: (f: (r1: R1) => ReaderTask) => ReaderTask = R.asksEW +export const asksReaderTaskW: (f: (r1: R1) => ReaderTask) => ReaderTask = R.asksReaderW /** * Effectfully accesses the environment. @@ -107,7 +107,7 @@ export const asksEW: (f: (r1: R1) => ReaderTask) => ReaderTask * @category combinators * @since 2.11.0 */ -export const asksE: (f: (r: R) => ReaderTask) => ReaderTask = asksEW +export const asksReaderTask: (f: (r: R) => ReaderTask) => ReaderTask = asksReaderTaskW // ------------------------------------------------------------------------------------- // type class members @@ -379,7 +379,7 @@ export const chainFirst = chainFirst_(Chain) /** - * Less strict version of [`chainFirst`](#chainFirst). + * Less strict version of [`chainFirst`](#chainfirst). * * Derivable from `Chain`. * @@ -469,7 +469,7 @@ export const chainReaderK = chainReaderK_(FromReader, Chain) /** - * Less strict version of [`chainReaderK`](#chainReaderK). + * Less strict version of [`chainReaderK`](#chainreaderk). * * @category combinators * @since 2.11.0 @@ -487,7 +487,7 @@ export const chainFirstReaderK = chainFirstReaderK_(FromReader, Chain) /** - * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + * Less strict version of [`chainFirstReaderK`](#chainfirstReaderk). * * @category combinators * @since 2.11.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c4438b54a..3b7727dc4 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -333,13 +333,14 @@ export const local: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local /** - * Less strict version of [`asksE`](#asksE). + * Less strict version of [`asksReaderTaskEither`](#asksreadertaskeither). * * @category combinators * @since 2.11.0 */ -export const asksEW: (f: (r1: R1) => ReaderTaskEither) => ReaderTaskEither = - R.asksEW +export const asksReaderTaskEitherW: ( + f: (r1: R1) => ReaderTaskEither +) => ReaderTaskEither = R.asksReaderW /** * Effectfully accesses the environment. @@ -347,7 +348,9 @@ export const asksEW: (f: (r1: R1) => ReaderTaskEither) = * @category combinators * @since 2.11.0 */ -export const asksE: (f: (r: R) => ReaderTaskEither) => ReaderTaskEither = asksEW +export const asksReaderTaskEither: ( + f: (r: R) => ReaderTaskEither +) => ReaderTaskEither = asksReaderTaskEitherW /** * @category combinators @@ -458,7 +461,7 @@ export const chainTaskEitherK: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainTaskEitherKW /** - * Less strict version of [`chainFirstTaskEitherK`](#chainFirstTaskEitherK). + * Less strict version of [`chainFirstTaskEitherK`](#chainfirsttaskeitherk). * * @category combinators * @since 2.11.0 @@ -484,7 +487,7 @@ export const fromReaderEitherK = , B>( ): ((...a: A) => ReaderTaskEither) => flow(f, fromReaderEither) /** - * Less strict version of [`chainReaderEitherK`](#chainReaderEitherK). + * Less strict version of [`chainReaderEitherK`](#chainreadereitherk). * * @category combinators * @since 2.11.0 @@ -503,7 +506,7 @@ export const chainReaderEitherK: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderEitherKW /** - * Less strict version of [`chainFirstReaderEitherK`](#chainFirstReaderEitherK). + * Less strict version of [`chainFirstReaderEitherK`](#chainfirstreadereitherk). * * @category combinators * @since 2.11.0 @@ -1024,7 +1027,7 @@ export const chainReaderK: ( chainReaderK_(FromReader, Chain) /** - * Less strict version of [`chainReaderK`](#chainReaderK). + * Less strict version of [`chainReaderK`](#chainreaderk). * * @category combinators * @since 2.11.0 @@ -1044,7 +1047,7 @@ export const chainFirstReaderK: ( chainFirstReaderK_(FromReader, Chain) /** - * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). + * Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). * * @category combinators * @since 2.11.0 @@ -1062,7 +1065,7 @@ export const fromReaderTaskK = , R, B>( ): ((...a: A) => ReaderTaskEither) => (...a) => rightReaderTask(f(...a)) /** - * Less strict version of [`chainReaderTaskK`](#chainReaderTaskK). + * Less strict version of [`chainReaderTaskK`](#chainreadertaskk). * * @category combinators * @since 2.11.0 @@ -1081,7 +1084,7 @@ export const chainReaderTaskK: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderTaskKW /** - * Less strict version of [`chainFirstReaderTaskK`](#chainFirstReaderTaskK). + * Less strict version of [`chainFirstReaderTaskK`](#chainfirstreadertaskk). * * @category combinators * @since 2.11.0 diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 2cf6ccc1b..70e71cd6e 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -207,7 +207,7 @@ export const match: ( ) => (as: ReadonlyArray) => B = matchW /** - * Less strict version of [`matchLeft`](#matchLeft). + * Less strict version of [`matchLeft`](#matchleft). * * @category destructors * @since 2.11.0 @@ -245,7 +245,7 @@ export const foldLeft: ( ) => (as: ReadonlyArray) => B = matchLeft /** - * Less strict version of [`matchRight`](#matchRight). + * Less strict version of [`matchRight`](#matchright). * * @category destructors * @since 2.11.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 0f5074240..0be15433f 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -242,12 +242,12 @@ export const local = (f: (r2: R2) => R1) => ( ): StateReaderTaskEither => flow(ma, R.local(f)) /** - * Less strict version of [`asksE`](#asksE). + * Less strict version of [`asksStateReaderTaskEither`](#asksstatereadertaskeither). * * @category combinators * @since 2.11.0 */ -export const asksEW = ( +export const asksStateReaderTaskEitherW = ( f: (r1: R1) => StateReaderTaskEither ): StateReaderTaskEither => (s) => (r) => f(r)(s)(r) @@ -257,9 +257,9 @@ export const asksEW = ( * @category combinators * @since 2.11.0 */ -export const asksE: ( +export const asksStateReaderTaskEither: ( f: (r: R) => StateReaderTaskEither -) => StateReaderTaskEither = asksEW +) => StateReaderTaskEither = asksStateReaderTaskEitherW /** * @category combinators diff --git a/test/Reader.ts b/test/Reader.ts index c7e6eb0cd..e890a8c8d 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -141,9 +141,9 @@ describe('Reader', () => { U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray)(undefined), [1, 2]) }) - it('asksE', () => { + it('asksReader', () => { const e: Env = { count: 0 } const f = (e: Env) => _.of(e.count + 1) - U.deepStrictEqual(_.asksE(f)(e), 1) + U.deepStrictEqual(_.asksReader(f)(e), 1) }) }) diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 8017e3f05..1b0c179dc 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -394,13 +394,13 @@ describe('StateReaderTaskEither', () => { ) }) - it('asksE', async () => { + it('asksStateReaderTaskEither', async () => { interface Env { readonly count: number } const e: Env = { count: 0 } const f = (e: Env) => _.of(e.count + 1) - U.deepStrictEqual(await _.asksE(f)({})(e)(), E.right(tuple(1, {}))) + U.deepStrictEqual(await _.asksStateReaderTaskEither(f)({})(e)(), E.right(tuple(1, {}))) }) it('#1486', async () => { From e072807e1b891b9d0f53d323f0ead31b2b98fcca Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 14:47:22 +0200 Subject: [PATCH 124/162] These: add elem, exists --- CHANGELOG.md | 3 +++ docs/modules/Either.ts.md | 2 +- docs/modules/These.ts.md | 22 ++++++++++++++++++++++ src/Either.ts | 5 ++--- src/These.ts | 19 ++++++++++++++++--- test/These.ts | 17 +++++++++++++++++ 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b57e8e3a6..b941644c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -260,6 +260,9 @@ high state of flux, you're at risk of it changing without notice. - add `fromTaskEither` (@thewilkybarkid) - add `Zero` instance - add `guard` constructor + - `These` + - add `elem` + - add `exists` - `Witherable` - add `filterE`, #1458 (@vinassefranche) - add `wiltDefault` diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index a2384cbcd..9ee8a0645 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1547,7 +1547,7 @@ Returns `false` if `Left` or returns the result of the application of the given **Signature** ```ts -export declare function exists(predicate: Predicate): (ma: Either) => boolean +export declare const exists: (predicate: Predicate) => (ma: Either) => boolean ``` **Example** diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 9fdbfac73..1d1569d28 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -89,6 +89,8 @@ Added in v2.0.0 - [isLeft](#isleft) - [isRight](#isright) - [utils](#utils) + - [elem](#elem) + - [exists](#exists) - [sequence](#sequence) - [toTuple2](#totuple2) - [traverse](#traverse) @@ -719,6 +721,26 @@ Added in v2.0.0 # utils +## elem + +**Signature** + +```ts +export declare const elem: (E: Eq) => (a: A) => (ma: These) => boolean +``` + +Added in v2.11.0 + +## exists + +**Signature** + +```ts +export declare const exists: (predicate: Predicate) => (ma: These) => boolean +``` + +Added in v2.11.0 + ## sequence **Signature** diff --git a/src/Either.ts b/src/Either.ts index fe8d277ac..d6fed6130 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1242,9 +1242,8 @@ export const elem = (E: Eq) => (a: A, ma: Either): boolean => * * @since 2.0.0 */ -export function exists(predicate: Predicate): (ma: Either) => boolean { - return (ma) => (isLeft(ma) ? false : predicate(ma.right)) -} +export const exists = (predicate: Predicate) => (ma: Either): boolean => + isLeft(ma) ? false : predicate(ma.right) // ------------------------------------------------------------------------------------- // do notation diff --git a/src/These.ts b/src/These.ts index 775617eb6..fedd66716 100644 --- a/src/These.ts +++ b/src/These.ts @@ -37,6 +37,7 @@ import { MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { Option } from './Option' import { Pointed2 } from './Pointed' +import { Predicate } from './Predicate' import { Semigroup } from './Semigroup' import { Show } from './Show' import { PipeableTraverse2, Traversable2 } from './Traversable' @@ -507,21 +508,21 @@ export const map: (f: (a: A) => B) => (fa: These) => These * @since 2.0.0 */ export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: These) => B = (b, f) => (fa) => - isLeft(fa) ? b : isRight(fa) ? f(b, fa.right) : f(b, fa.right) + isLeft(fa) ? b : f(b, fa.right) /** * @category Foldable * @since 2.0.0 */ export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: These) => M = (M) => (f) => (fa) => - isLeft(fa) ? M.empty : isRight(fa) ? f(fa.right) : f(fa.right) + isLeft(fa) ? M.empty : f(fa.right) /** * @category Foldable * @since 2.0.0 */ export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: These) => B = (b, f) => (fa) => - isLeft(fa) ? b : isRight(fa) ? f(fa.right, b) : f(fa.right, b) + isLeft(fa) ? b : f(fa.right, b) /** * @since 2.6.3 @@ -669,6 +670,18 @@ export const fromOptionK = // utils // ------------------------------------------------------------------------------------- +/** + * @since 2.11.0 + */ +export const elem = (E: Eq) => (a: A) => (ma: These): boolean => + isLeft(ma) ? false : E.equals(a, ma.right) + +/** + * @since 2.11.0 + */ +export const exists = (predicate: Predicate) => (ma: These): boolean => + isLeft(ma) ? false : predicate(ma.right) + /** * @example * import { toTuple2, left, right, both } from 'fp-ts/These' diff --git a/test/These.ts b/test/These.ts index 790932aa7..4310f5676 100644 --- a/test/These.ts +++ b/test/These.ts @@ -243,4 +243,21 @@ describe('These', () => { U.deepStrictEqual(_.swap(_.right('a')), _.left('a')) U.deepStrictEqual(_.swap(_.both('a', 1)), _.both(1, 'a')) }) + + it('exists', () => { + const gt2 = _.exists((n: number) => n > 2) + U.deepStrictEqual(gt2(_.left('a')), false) + U.deepStrictEqual(gt2(_.right(1)), false) + U.deepStrictEqual(gt2(_.right(3)), true) + U.deepStrictEqual(gt2(_.both('a', 1)), false) + U.deepStrictEqual(gt2(_.both('a', 3)), true) + }) + + it('elem', () => { + U.deepStrictEqual(_.elem(N.Eq)(2)(_.left('a')), false) + U.deepStrictEqual(_.elem(N.Eq)(2)(_.right(2)), true) + U.deepStrictEqual(_.elem(N.Eq)(1)(_.right(2)), false) + U.deepStrictEqual(_.elem(N.Eq)(2)(_.both('a', 2)), true) + U.deepStrictEqual(_.elem(N.Eq)(1)(_.both('a', 2)), false) + }) }) From 1d69ef8a5f2c60e44ae8852a76191d644c47b3b3 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 14:55:48 +0200 Subject: [PATCH 125/162] Tree: add exists --- CHANGELOG.md | 2 ++ docs/modules/Tree.ts.md | 11 +++++++++++ src/Tree.ts | 33 +++++++++++++++++++-------------- test/Tree.ts | 38 ++++++++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b941644c3..09329ccf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -263,6 +263,8 @@ high state of flux, you're at risk of it changing without notice. - `These` - add `elem` - add `exists` + - `Tree` + - add `exists` - `Witherable` - add `filterE`, #1458 (@vinassefranche) - add `wiltDefault` diff --git a/docs/modules/Tree.ts.md b/docs/modules/Tree.ts.md index 2058340d5..1a2459384 100644 --- a/docs/modules/Tree.ts.md +++ b/docs/modules/Tree.ts.md @@ -75,6 +75,7 @@ Added in v2.0.0 - [drawForest](#drawforest) - [drawTree](#drawtree) - [elem](#elem) + - [exists](#exists) - [sequence](#sequence) - [traverse](#traverse) @@ -680,6 +681,16 @@ export declare function elem(E: Eq): (a: A, fa: Tree) => boolean Added in v2.0.0 +## exists + +**Signature** + +```ts +export declare const exists: (predicate: Predicate) => (ma: Tree) => boolean +``` + +Added in v2.11.0 + ## sequence **Signature** diff --git a/src/Tree.ts b/src/Tree.ts index d2194eed8..2e2a41e39 100644 --- a/src/Tree.ts +++ b/src/Tree.ts @@ -22,6 +22,7 @@ import * as _ from './internal' import { Monad as MonadHKT, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad' import { Monoid } from './Monoid' import { Pointed1 } from './Pointed' +import { Predicate } from './Predicate' import { Show } from './Show' import { PipeableTraverse1, Traversable1 } from './Traversable' @@ -218,20 +219,6 @@ export function unfoldForestM( ) } -// TODO: curry in v3 -/** - * @since 2.0.0 - */ -export function elem(E: Eq): (a: A, fa: Tree) => boolean { - const go = (a: A, fa: Tree): boolean => { - if (E.equals(a, fa.value)) { - return true - } - return fa.forest.some((tree) => go(a, tree)) - } - return go -} - /** * Fold a tree into a "summary" value in depth-first order. * @@ -636,6 +623,24 @@ export const apS = /*#__PURE__*/ apS_(Apply) +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- + +/** + * @since 2.0.0 + */ +export function elem(E: Eq): (a: A, fa: Tree) => boolean { + const go = (a: A, fa: Tree): boolean => E.equals(a, fa.value) || fa.forest.some((tree) => go(a, tree)) + return go +} + +/** + * @since 2.11.0 + */ +export const exists = (predicate: Predicate) => (ma: Tree): boolean => + predicate(ma.value) || ma.forest.some(exists(predicate)) + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/Tree.ts b/test/Tree.ts index 6f341188f..54dd16fdf 100644 --- a/test/Tree.ts +++ b/test/Tree.ts @@ -177,20 +177,6 @@ describe('Tree', () => { ) }) - it('elem', () => { - interface User { - readonly id: number - } - const S: Eq.Eq = pipe( - N.Eq, - Eq.contramap((user: User) => user.id) - ) - const users = _.make({ id: 1 }, [_.make({ id: 1 }, [_.make({ id: 3 }), _.make({ id: 4 })]), _.make({ id: 2 })]) - U.deepStrictEqual(_.elem(S)({ id: 1 }, users), true) - U.deepStrictEqual(_.elem(S)({ id: 4 }, users), true) - U.deepStrictEqual(_.elem(S)({ id: 5 }, users), false) - }) - it('getShow', () => { const Sh = _.getShow(S.Show) const t1 = _.make('a') @@ -221,4 +207,28 @@ describe('Tree', () => { it('apS', () => { U.deepStrictEqual(pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b'))), _.make({ a: 1, b: 'b' })) }) + + it('elem', () => { + interface User { + readonly id: number + } + const S: Eq.Eq = pipe( + N.Eq, + Eq.contramap((user: User) => user.id) + ) + const users = _.make({ id: 1 }, [_.make({ id: 1 }, [_.make({ id: 3 }), _.make({ id: 4 })]), _.make({ id: 2 })]) + U.deepStrictEqual(_.elem(S)({ id: 1 }, users), true) + U.deepStrictEqual(_.elem(S)({ id: 4 }, users), true) + U.deepStrictEqual(_.elem(S)({ id: 5 }, users), false) + }) + + it('exists', () => { + interface User { + readonly id: number + } + const users = _.make({ id: 1 }, [_.make({ id: 1 }, [_.make({ id: 3 }), _.make({ id: 4 })]), _.make({ id: 2 })]) + U.deepStrictEqual(_.exists((user: User) => user.id === 1)(users), true) + U.deepStrictEqual(_.exists((user: User) => user.id === 4)(users), true) + U.deepStrictEqual(_.exists((user: User) => user.id === 5)(users), false) + }) }) From fdba49a0f9551f09f757a19abd823feb072039e7 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 14:59:04 +0200 Subject: [PATCH 126/162] ReadonlyArray: add `exists` alias --- CHANGELOG.md | 2 ++ docs/modules/Array.ts.md | 13 +++++++++++++ docs/modules/ReadonlyArray.ts.md | 13 +++++++++++++ src/Array.ts | 7 +++++++ src/ReadonlyArray.ts | 7 +++++++ 5 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09329ccf2..85ed83da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ high state of flux, you're at risk of it changing without notice. - add `fromOptionK` - add `Zero` instance - add `guard` constructor + - add `exists` alias - `boolean` - add `isBoolean` - `Either` @@ -195,6 +196,7 @@ high state of flux, you're at risk of it changing without notice. - add `fromOptionK` - add `Zero` instance - add `guard` constructor + - add `exists` alias - `ReadonlyMap` - add `union` - add `intersection` diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index a801da8eb..7e1412e0c 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -188,6 +188,7 @@ Added in v2.0.0 - [deleteAt](#deleteat) - [elem](#elem) - [every](#every) + - [exists](#exists) - [filterE](#filtere) - [findIndex](#findindex) - [findLastIndex](#findlastindex) @@ -2545,6 +2546,18 @@ export declare const every: (predicate: Predicate) => (as: A[]) => boolean Added in v2.9.0 +## exists + +Alias of [`some`](#some) + +**Signature** + +```ts +export declare const exists: (predicate: Predicate) => (as: A[]) => as is NEA.NonEmptyArray +``` + +Added in v2.11.0 + ## filterE Filter values inside a context. diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 800227058..469eb864a 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -182,6 +182,7 @@ Added in v2.5.0 - [elem](#elem) - [empty](#empty) - [every](#every) + - [exists](#exists) - [filterE](#filtere) - [findFirst](#findfirst) - [findFirstMap](#findfirstmap) @@ -2414,6 +2415,18 @@ assert.deepStrictEqual(pipe([1, 2, -3], every(isPositive)), false) Added in v2.9.0 +## exists + +Alias of [`some`](#some) + +**Signature** + +```ts +export declare const exists: (predicate: Predicate) => (as: readonly A[]) => as is RNEA.ReadonlyNonEmptyArray +``` + +Added in v2.11.0 + ## filterE Filter values inside a context. diff --git a/src/Array.ts b/src/Array.ts index 7ba36a818..aa25c0b5f 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -2230,6 +2230,13 @@ export const every: (predicate: Predicate) => (as: Array) => boolean = */ export const some = (predicate: Predicate) => (as: Array): as is NonEmptyArray => as.some(predicate) +/** + * Alias of [`some`](#some) + * + * @since 2.11.0 + */ +export const exists = some + // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 70e71cd6e..4e1c9a3ce 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -2460,6 +2460,13 @@ export const every = (predicate: Predicate) => (as: ReadonlyArray): boo export const some = (predicate: Predicate) => (as: ReadonlyArray): as is ReadonlyNonEmptyArray => as.some(predicate) +/** + * Alias of [`some`](#some) + * + * @since 2.11.0 + */ +export const exists = some + // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- From e49da899f24f83a2df81946c76f2625497cdfadc Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 17:11:32 +0200 Subject: [PATCH 127/162] `NonEmptyArray` / `ReadonlyNonEmptyArray`: make `concat` pipeable --- CHANGELOG.md | 2 ++ docs/modules/NonEmptyArray.ts.md | 2 ++ docs/modules/ReadonlyNonEmptyArray.ts.md | 6 ++++++ dtslint/ts3.5/NonEmptyArray.ts | 2 ++ dtslint/ts3.5/ReadonlyNonEmptyArray.ts | 2 ++ src/NonEmptyArray.ts | 15 +++++++++------ src/ReadonlyNonEmptyArray.ts | 19 +++++++++++++------ test/NonEmptyArray.ts | 15 +++++++++++++-- test/ReadonlyNonEmptyArray.ts | 15 +++++++++++++-- 9 files changed, 62 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ed83da1..62a090ad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,6 +131,7 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionSemigroup` - add `makeBy` - add `range` + - make `concat` pipeable - `number` - add `MagmaSub` - add `isNumber` @@ -217,6 +218,7 @@ high state of flux, you're at risk of it changing without notice. - add `getUnionSemigroup` - add `makeBy` - add `range` + - make `concat` pipeable - `ReadonlyRecord` - add `union` (@anthonyjoeseph) - add `intersection` (@anthonyjoeseph) diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index ec85f418e..35987906b 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -374,6 +374,8 @@ Added in v2.10.0 **Signature** ```ts +export declare function concat(second: NonEmptyArray): (first: Array) => NonEmptyArray +export declare function concat(second: Array): (first: NonEmptyArray) => NonEmptyArray export declare function concat(first: Array, second: NonEmptyArray): NonEmptyArray export declare function concat(first: NonEmptyArray, second: Array): NonEmptyArray ``` diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 02d652b53..098f70266 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -482,6 +482,12 @@ Added in v2.10.0 **Signature** ```ts +export declare function concat( + second: ReadonlyNonEmptyArray +): (first: ReadonlyArray) => ReadonlyNonEmptyArray +export declare function concat( + second: ReadonlyArray +): (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray export declare function concat(first: ReadonlyArray, second: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray export declare function concat(first: ReadonlyNonEmptyArray, second: ReadonlyArray): ReadonlyNonEmptyArray ``` diff --git a/dtslint/ts3.5/NonEmptyArray.ts b/dtslint/ts3.5/NonEmptyArray.ts index 28124b593..01e1baa74 100644 --- a/dtslint/ts3.5/NonEmptyArray.ts +++ b/dtslint/ts3.5/NonEmptyArray.ts @@ -113,3 +113,5 @@ _.filter(isNumber2)(neasn) _.concat(as, neas) // $ExpectType NonEmptyArray _.concat(neas, as) // $ExpectType NonEmptyArray +_.concat(neas)(as) // $ExpectType NonEmptyArray +_.concat(as)(neas) // $ExpectType NonEmptyArray diff --git a/dtslint/ts3.5/ReadonlyNonEmptyArray.ts b/dtslint/ts3.5/ReadonlyNonEmptyArray.ts index 692bebd7f..5586acb6b 100644 --- a/dtslint/ts3.5/ReadonlyNonEmptyArray.ts +++ b/dtslint/ts3.5/ReadonlyNonEmptyArray.ts @@ -113,3 +113,5 @@ _.filter(isNumber2)(neasn) _.concat(ras, rneas) // $ExpectType ReadonlyNonEmptyArray _.concat(rneas, ras) // $ExpectType ReadonlyNonEmptyArray +_.concat(rneas)(ras) // $ExpectType ReadonlyNonEmptyArray +_.concat(ras)(rneas) // $ExpectType ReadonlyNonEmptyArray diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 7efc71601..93cc79c5a 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -189,7 +189,7 @@ export const sortBy = (ords: Array>): ((as: NonEmptyArray */ export const union = (E: Eq): ((second: NonEmptyArray) => (first: NonEmptyArray) => NonEmptyArray) => { const uniqE = uniq(E) - return (second) => (first) => uniqE(concat(first, second)) + return (second) => (first) => uniqE(pipe(first, concat(second))) } /** @@ -212,7 +212,7 @@ export const rotate = (n: number) => (as: NonEmptyArray): NonEmptyArray } if (m < 0) { const [f, s] = splitAt(-m)(as) - return concat(s, f) + return pipe(s, concat(f)) } else { return rotate(m - len)(as) } @@ -335,15 +335,18 @@ export function concatW(second: Array): (first: NonEmptyArray) => Ar return (first: NonEmptyArray) => first.concat(second) } -// TODO: curry in v3 /** * @category combinators * @since 2.2.0 */ +export function concat(second: NonEmptyArray): (first: Array) => NonEmptyArray +export function concat(second: Array): (first: NonEmptyArray) => NonEmptyArray +/** @deprecated */ export function concat(first: Array, second: NonEmptyArray): NonEmptyArray +/** @deprecated */ export function concat(first: NonEmptyArray, second: Array): NonEmptyArray -export function concat(first: Array, second: Array): Array { - return first.concat(second) +export function concat(x: Array, y?: Array): Array | ((y: NonEmptyArray) => Array) { + return y ? x.concat(y) : (y) => y.concat(x) } /** @@ -670,7 +673,7 @@ const _traverseWithIndex: TraversableWithIndex1['traverseWithIndex' * @since 2.9.0 */ export const altW = (that: Lazy>) => (as: NonEmptyArray): NonEmptyArray => - concat(as as NonEmptyArray, that()) + pipe(as, concatW(that())) /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 5876be33e..de89f672e 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -201,7 +201,7 @@ export const union = ( E: Eq ): ((second: ReadonlyNonEmptyArray) => (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray) => { const uniqE = uniq(E) - return (second) => (first) => uniqE(concat(first, second)) + return (second) => (first) => uniqE(pipe(first, concat(second))) } /** @@ -224,7 +224,7 @@ export const rotate = (n: number) => (as: ReadonlyNonEmptyArray): Readonly } if (m < 0) { const [f, s] = splitAt(-m)(as) - return concat(s, f) + return pipe(s, concat(f)) } else { return rotate(m - len)(as) } @@ -359,10 +359,17 @@ export function concatW(second: ReadonlyArray): (first: ReadonlyNonEmpt * @category combinators * @since 2.5.0 */ +export function concat(second: ReadonlyNonEmptyArray): (first: ReadonlyArray) => ReadonlyNonEmptyArray +export function concat(second: ReadonlyArray): (first: ReadonlyNonEmptyArray) => ReadonlyNonEmptyArray +/** @deprecated */ export function concat(first: ReadonlyArray, second: ReadonlyNonEmptyArray): ReadonlyNonEmptyArray +/** @deprecated */ export function concat(first: ReadonlyNonEmptyArray, second: ReadonlyArray): ReadonlyNonEmptyArray -export function concat(first: ReadonlyArray, second: ReadonlyArray): ReadonlyArray { - return first.concat(second) +export function concat( + x: ReadonlyArray, + y?: ReadonlyArray +): ReadonlyArray | ((y: ReadonlyNonEmptyArray) => ReadonlyArray) { + return y ? x.concat(y) : (y) => y.concat(x) } /** @@ -685,7 +692,7 @@ export const of: Pointed1['of'] = _.singleton */ export const altW = (that: Lazy>) => ( as: ReadonlyNonEmptyArray -): ReadonlyNonEmptyArray => concat(as as ReadonlyNonEmptyArray, that()) +): ReadonlyNonEmptyArray => pipe(as, concatW(that())) /** * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to @@ -1363,7 +1370,7 @@ export function cons( * @since 2.5.0 * @deprecated */ -export const snoc = (init: ReadonlyArray, end: A): ReadonlyNonEmptyArray => concat(init, [end]) +export const snoc = (init: ReadonlyArray, end: A): ReadonlyNonEmptyArray => pipe(init, concat([end])) /** * Use [`insertAt`](./ReadonlyArray.ts.html#insertAt) instead. diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 12b6d3417..11cf38405 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -381,8 +381,7 @@ describe('NonEmptyArray', () => { U.deepStrictEqual(Sh.show(['a', 'b', 'c']), `["a", "b", "c"]`) }) - it('alt / concat', () => { - U.deepStrictEqual(_.concat(['a'], []), ['a']) + it('alt', () => { U.deepStrictEqual( pipe( ['a'], @@ -550,4 +549,16 @@ describe('NonEmptyArray', () => { it('concatW', () => { U.deepStrictEqual(pipe(['a'], _.concatW(['b'])), ['a', 'b']) }) + + it('concat', () => { + U.deepStrictEqual(pipe(['a'], _.concat(['b'])), ['a', 'b']) + U.deepStrictEqual(pipe([], _.concat(['b'])), ['b']) + U.deepStrictEqual(pipe(['a'], _.concat([])), ['a']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat(['a'], ['b']), ['a', 'b']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat(['a'], []), ['a']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat([], ['b']), ['b']) + }) }) diff --git a/test/ReadonlyNonEmptyArray.ts b/test/ReadonlyNonEmptyArray.ts index b24759987..b9c957758 100644 --- a/test/ReadonlyNonEmptyArray.ts +++ b/test/ReadonlyNonEmptyArray.ts @@ -367,8 +367,7 @@ describe('ReadonlyNonEmptyArray', () => { U.deepStrictEqual(Sh.show(['a', 'b', 'c']), `["a", "b", "c"]`) }) - it('alt / concat', () => { - U.deepStrictEqual(_.concat(['a'], []), ['a']) + it('alt', () => { U.deepStrictEqual( pipe( ['a'], @@ -693,4 +692,16 @@ describe('ReadonlyNonEmptyArray', () => { it('concatW', () => { U.deepStrictEqual(pipe(['a'], _.concatW(['b'])), ['a', 'b']) }) + + it('concat', () => { + U.deepStrictEqual(pipe(['a'], _.concat(['b'])), ['a', 'b']) + U.deepStrictEqual(pipe(_.empty, _.concat(['b'])), ['b']) + U.deepStrictEqual(pipe(['a'], _.concat(_.empty)), ['a']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat(['a'], ['b']), ['a', 'b']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat(['a'], _.empty), ['a']) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.concat(_.empty, ['b']), ['b']) + }) }) From 68fcdbff59e3f80822e9d6a5833314b30e276889 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 17:20:47 +0200 Subject: [PATCH 128/162] These: - add `ApT` - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` --- CHANGELOG.md | 3 +++ docs/modules/These.ts.md | 43 ++++++++++++++++++++++++++++++ src/These.ts | 57 ++++++++++++++++++++++++++++++++++++++++ test/These.ts | 21 +++++++++++++++ 4 files changed, 124 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62a090ad0..c597f9954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -267,6 +267,9 @@ high state of flux, you're at risk of it changing without notice. - `These` - add `elem` - add `exists` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `Tree` - add `exists` - `Witherable` diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 1d1569d28..5ad76e4e6 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -89,11 +89,14 @@ Added in v2.0.0 - [isLeft](#isleft) - [isRight](#isright) - [utils](#utils) + - [ApT](#apt) - [elem](#elem) - [exists](#exists) - [sequence](#sequence) - [toTuple2](#totuple2) - [traverse](#traverse) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~toTuple~~](#totuple) --- @@ -721,6 +724,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: These +``` + +Added in v2.11.0 + ## elem **Signature** @@ -799,6 +812,36 @@ export declare const traverse: PipeableTraverse2<'These'> Added in v2.6.3 +## traverseReadonlyArrayWithIndex + +Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(S))`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndex: ( + S: Semigroup +) => (f: (index: number, a: A) => These) => (as: readonly A[]) => These +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(S))`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + S: Semigroup +) => ( + f: (index: number, a: A) => These +) => (as: ReadonlyNonEmptyArray) => These> +``` + +Added in v2.11.0 + ## ~~toTuple~~ Use [`toTuple2`](#totuple2) instead. diff --git a/src/These.ts b/src/These.ts index fedd66716..a7e62dc1f 100644 --- a/src/These.ts +++ b/src/These.ts @@ -35,9 +35,11 @@ import * as _ from './internal' import { Monad2C } from './Monad' import { MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Option } from './Option' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import { Show } from './Show' import { PipeableTraverse2, Traversable2 } from './Traversable' @@ -711,6 +713,61 @@ export const toTuple = (e: E, a: A): ((fa: These) => [E, A]) => () => a ) as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: These = of(_.emptyReadonlyArray) + +// ------------------------------------------------------------------------------------- +// array utils +// ------------------------------------------------------------------------------------- + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = (S: Semigroup) => ( + f: (index: number, a: A) => These +) => (as: ReadonlyNonEmptyArray): These> => { + let e: Option = _.none + const t = f(0, _.head(as)) + if (isLeft(t)) { + return t + } + if (isBoth(t)) { + e = _.some(t.left) + } + const out: NonEmptyArray = [t.right] + for (let i = 1; i < as.length; i++) { + const t = f(i, as[i]) + if (isLeft(t)) { + return t + } + if (isBoth(t)) { + e = _.isNone(e) ? _.some(t.left) : _.some(S.concat(e.value, t.left)) + } + out.push(t.right) + } + return _.isNone(e) ? right(out) : both(e.value, out) +} + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = (S: Semigroup) => ( + f: (index: number, a: A) => These +): ((as: ReadonlyArray) => These>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(S)(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/These.ts b/test/These.ts index 4310f5676..2934fcd12 100644 --- a/test/These.ts +++ b/test/These.ts @@ -5,6 +5,7 @@ import * as N from '../src/number' import * as O from '../src/Option' import * as S from '../src/string' import * as _ from '../src/These' +import * as RA from '../src/ReadonlyArray' describe('These', () => { describe('pipeables', () => { @@ -244,6 +245,26 @@ describe('These', () => { U.deepStrictEqual(_.swap(_.both('a', 1)), _.both(1, 'a')) }) + // ------------------------------------------------------------------------------------- + // array utils + // ------------------------------------------------------------------------------------- + + it('traverseReadonlyArrayWithIndex', () => { + const f = (i: number, n: number) => (n > 0 ? _.right(n + i) : n === 0 ? _.both('a', 0) : _.left(String(n))) + const standard = RA.traverseWithIndex(_.getApplicative(S.Semigroup))(f) + const optimized = _.traverseReadonlyArrayWithIndex(S.Semigroup)(f) + const assert = (input: ReadonlyArray) => { + U.deepStrictEqual(standard(input), optimized(input)) + } + assert([1, 2, 3]) + assert([0, 2, 3]) + assert([1, 0, 3]) + assert([0, 0, 3]) + assert([-1, 2, 3]) + assert([1, -2, 3]) + assert(RA.empty) + }) + it('exists', () => { const gt2 = _.exists((n: number) => n > 2) U.deepStrictEqual(gt2(_.left('a')), false) From b382ed306628809084ed3b0f42d8ef32a3caae1c Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 17:41:31 +0200 Subject: [PATCH 129/162] Task: - add `ApT` - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` --- CHANGELOG.md | 13 +++++ docs/modules/Task.ts.md | 117 +++++++++++++++++++++++++++++++-------- src/Task.ts | 118 ++++++++++++++++++++++++++++++---------- test/Task.ts | 106 ++++++++++++++++++++++++++---------- 4 files changed, 272 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c597f9954..09ec29770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - `Semigroup` - deprecate `semigroupVoid`, use `void` module instead. + - `Task` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - **New Feature** - add `Endomorphism` module - add `Predicate` module @@ -253,6 +260,12 @@ high state of flux, you're at risk of it changing without notice. - add `toUpperCase` - `struct` - add `evolve` + - `Task` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `TaskEither` - add `fromTaskOption` (@thewilkybarkid) - add `fromTaskOptionK` diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index 5a667b241..bc727d3a8 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -66,17 +66,22 @@ Added in v2.0.0 - [natural transformations](#natural-transformations) - [fromIO](#fromio) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) - [never](#never) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -530,6 +535,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: Task +``` + +Added in v2.11.0 + ## Do **Signature** @@ -588,45 +603,89 @@ export declare const never: Task Added in v2.0.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly Task[]) => Task +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: readonly A[]) => Task ``` -Added in v2.9.0 +Added in v2.11.0 -## sequenceSeqArray +## traverseReadonlyArrayWithIndexSeq -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (arr: readonly Task[]) => Task +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => Task +) => (as: readonly A[]) => Task +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: ReadonlyNonEmptyArray) => Task> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => Task +) => (as: ReadonlyNonEmptyArray) => Task> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly Task[]) => Task ``` Added in v2.9.0 -## traverseArray +## ~~sequenceSeqArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => Task) => (as: readonly A[]) => Task +export declare const sequenceSeqArray: (arr: readonly Task[]) => Task ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -638,21 +697,21 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: (f: (a: A) => Task) => (as: readonly A[]) => Task +export declare const traverseArray: (f: (a: A) => Task) => (as: readonly A[]) => Task ``` Added in v2.9.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -663,3 +722,15 @@ export declare const traverseSeqArrayWithIndex: ( ``` Added in v2.9.0 + +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const traverseSeqArray: (f: (a: A) => Task) => (as: readonly A[]) => Task +``` + +Added in v2.9.0 diff --git a/src/Task.ts b/src/Task.ts index d06fe065a..bcac75e5c 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -28,7 +28,9 @@ import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { MonadTask1 } from './MonadTask' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed1 } from './Pointed' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- @@ -456,78 +458,135 @@ export const apS = /*#__PURE__*/ apS_(ApplyPar) +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: Task = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => Task) => ( + as: ReadonlyNonEmptyArray +): Task> => () => Promise.all(as.map((a, i) => f(i, a)())) as any + /** * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => Task +): ((as: ReadonlyArray) => Task>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = (f: (index: number, a: A) => Task) => ( + as: ReadonlyNonEmptyArray +): Task> => () => + _.tail(as).reduce>>( + (acc, a, i) => + acc.then((bs) => + f(i + 1, a)().then((b) => { + bs.push(b) + return bs + }) + ), + f(0, _.head(as))().then(_.singleton) + ) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => Task +): ((as: ReadonlyArray) => Task>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => Task) => ( - as: ReadonlyArray -): Task> => () => Promise.all(as.map((x, i) => f(i, x)())) +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: ReadonlyArray) => Task> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = (f: (a: A) => Task): ((as: ReadonlyArray) => Task>) => - traverseArrayWithIndex((_, a) => f(a)) + traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Task> = /*#__PURE__*/ traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ -export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => Task) => ( - as: ReadonlyArray -): Task> => () => - as.reduce>>( - (acc, a, i) => - acc.then((bs) => - f(i, a)().then((b) => { - bs.push(b) - return bs - }) - ), - Promise.resolve([]) - ) +export const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: ReadonlyArray) => Task> = traverseReadonlyArrayWithIndexSeq /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArray = (f: (a: A) => Task): ((as: ReadonlyArray) => Task>) => - traverseSeqArrayWithIndex((_, a) => f(a)) + traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => Task> = /*#__PURE__*/ traverseSeqArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** * Use small, specific instances instead. * @@ -552,7 +611,6 @@ export const task: Monad1 & MonadTask1 = { * @since 2.0.0 * @deprecated */ - export const taskSeq: typeof task = { URI, map: _map, diff --git a/test/Task.ts b/test/Task.ts index 83be2bf09..174b7b649 100644 --- a/test/Task.ts +++ b/test/Task.ts @@ -1,10 +1,11 @@ import * as U from './util' -import { pipe } from '../src/function' +import { pipe, SK } from '../src/function' import * as I from '../src/IO' import * as RA from '../src/ReadonlyArray' import * as _ from '../src/Task' import * as assert from 'assert' import * as S from '../src/string' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' const delayReject = (n: number, a: A): _.Task => () => new Promise((_, reject) => { @@ -163,33 +164,80 @@ describe('Task', () => { U.deepStrictEqual(await pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b')))(), { a: 1, b: 'b' }) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const append = (n: number): _.Task => - _.delay(n % 2 === 0 ? 50 : 100)( - _.fromIO(() => { - log.push(n) - return n - }) - ) - const as = RA.makeBy(4, append) - U.deepStrictEqual(await pipe(as, _.sequenceArray)(), [0, 1, 2, 3]) - U.deepStrictEqual(log, [0, 2, 1, 3]) - }) - - it('sequenceSeqArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const append = (n: number): _.Task => - _.delay(n % 2 === 0 ? 50 : 100)( - _.fromIO(() => { - log.push(n) - return n - }) - ) - const as = RA.makeBy(4, append) - U.deepStrictEqual(await pipe(as, _.sequenceSeqArray)(), [0, 1, 2, 3]) - U.deepStrictEqual(log, [0, 1, 2, 3]) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => _.of(a + i)) + U.deepStrictEqual(await pipe(RA.empty, f)(), RA.empty) + U.deepStrictEqual(await pipe(input, f)(), ['a0', 'b1']) + }) + + it('traverseReadonlyArrayWithIndexSeq', async () => { + const f = _.traverseReadonlyArrayWithIndexSeq((i, a: string) => _.of(a + i)) + U.deepStrictEqual(await pipe(RA.empty, f)(), RA.empty) + U.deepStrictEqual(await pipe(input, f)(), ['a0', 'b1']) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const append = (n: number): _.Task => + _.delay(n % 2 === 0 ? 50 : 100)( + _.fromIO(() => { + log.push(n) + return n + }) + ) + const as = RA.makeBy(4, append) + U.deepStrictEqual(await pipe(as, _.traverseReadonlyArrayWithIndex(SK))(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 2, 1, 3]) + }) + + it('sequenceReadonlyArraySeq', async () => { + const log: Array = [] + const append = (n: number): _.Task => + _.delay(n % 2 === 0 ? 50 : 100)( + _.fromIO(() => { + log.push(n) + return n + }) + ) + const as = RA.makeBy(4, append) + U.deepStrictEqual(await pipe(as, _.traverseReadonlyArrayWithIndexSeq(SK))(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 1, 2, 3]) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const append = (n: number): _.Task => + _.delay(n % 2 === 0 ? 50 : 100)( + _.fromIO(() => { + log.push(n) + return n + }) + ) + const as = RA.makeBy(4, append) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe(as, _.sequenceArray)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 2, 1, 3]) + }) + + it('sequenceSeqArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const append = (n: number): _.Task => + _.delay(n % 2 === 0 ? 50 : 100)( + _.fromIO(() => { + log.push(n) + return n + }) + ) + const as = RA.makeBy(4, append) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe(as, _.sequenceSeqArray)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 1, 2, 3]) + }) }) }) From 4b94b5668b06fe5bbd13fea60c4fddd20e312d35 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:14:09 +0200 Subject: [PATCH 130/162] TaskThese - add `ApT` - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` --- CHANGELOG.md | 6 +++ docs/modules/TaskThese.ts.md | 75 +++++++++++++++++++++++++++++++ src/TaskThese.ts | 85 +++++++++++++++++++++++++++++++++++- test/TaskThese.ts | 84 +++++++++++++++++++++++++++++++++-- 4 files changed, 245 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ec29770..60fe5722d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -277,6 +277,12 @@ high state of flux, you're at risk of it changing without notice. - add `fromTaskEither` (@thewilkybarkid) - add `Zero` instance - add `guard` constructor + - `TaskThese` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `These` - add `elem` - add `exists` diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index 165c0a872..99835100f 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -70,7 +70,12 @@ Added in v2.4.0 - [fromTask](#fromtask) - [fromThese](#fromthese) - [utils](#utils) + - [ApT](#apt) - [toTuple2](#totuple2) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - [~~toTuple~~](#totuple) --- @@ -629,6 +634,16 @@ Added in v2.11.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: TaskThese +``` + +Added in v2.11.0 + ## toTuple2 **Signature** @@ -639,6 +654,66 @@ export declare const toTuple2: (e: Lazy, a: Lazy) => (fa: TaskThese< Added in v2.10.0 +## traverseReadonlyArrayWithIndex + +Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndex: ( + S: Semigroup +) => (f: (index: number, a: A) => TaskThese) => (as: readonly A[]) => TaskThese +``` + +Added in v2.11.0 + +## traverseReadonlyArrayWithIndexSeq + +Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndexSeq: ( + S: Semigroup +) => (f: (index: number, a: A) => TaskThese) => (as: readonly A[]) => TaskThese +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + S: Semigroup +) => ( + f: (index: number, a: A) => TaskThese +) => (as: ReadonlyNonEmptyArray) => TaskThese> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + S: Semigroup +) => ( + f: (index: number, a: A) => TaskThese +) => (as: ReadonlyNonEmptyArray) => TaskThese> +``` + +Added in v2.11.0 + ## ~~toTuple~~ Use [`toTuple2`](#totuple2) instead. diff --git a/src/TaskThese.ts b/src/TaskThese.ts index b2392c5e0..85da381e7 100644 --- a/src/TaskThese.ts +++ b/src/TaskThese.ts @@ -14,7 +14,7 @@ import { import { FromIO2, fromIOK as fromIOK_ } from './FromIO' import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' import { FromThese2, fromTheseK as fromTheseK_ } from './FromThese' -import { flow, Lazy, pipe } from './function' +import { flow, Lazy, pipe, SK } from './function' import { flap as flap_, Functor2 } from './Functor' import { IO } from './IO' import { URI as IEURI } from './IOEither' @@ -22,13 +22,16 @@ import { Monad2C } from './Monad' import { MonadTask2C } from './MonadTask' import { NaturalTransformation22 } from './NaturalTransformation' import { Pointed2 } from './Pointed' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import * as T from './Task' import * as TH from './These' import * as TT from './TheseT' +import * as _ from './internal' import These = TH.These import Task = T.Task +import { NonEmptyArray } from './NonEmptyArray' // ------------------------------------------------------------------------------------- // model @@ -483,6 +486,86 @@ export const toTuple2: (e: Lazy, a: Lazy) => (fa: TaskThese) = /*#__PURE__*/ TT.toTuple2(T.Functor) +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: TaskThese = of(_.emptyReadonlyArray) + +// ------------------------------------------------------------------------------------- +// array utils +// ------------------------------------------------------------------------------------- + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + S: Semigroup +): (( + f: (index: number, a: A) => TaskThese +) => (as: ReadonlyNonEmptyArray) => TaskThese>) => { + const g = TH.traverseReadonlyNonEmptyArrayWithIndex(S) + return (f) => flow(T.traverseReadonlyNonEmptyArrayWithIndex(f), T.map(g(SK))) +} + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = (S: Semigroup) => ( + f: (index: number, a: A) => TaskThese +): ((as: ReadonlyArray) => TaskThese>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(S)(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = (S: Semigroup) => ( + f: (index: number, a: A) => TaskThese +) => (as: ReadonlyNonEmptyArray): TaskThese> => () => + _.tail(as).reduce>>>( + (acc, a, i) => + acc.then((ebs) => + TH.isLeft(ebs) + ? acc + : f(i + 1, a)().then((eb) => { + if (TH.isLeft(eb)) { + return eb + } + if (TH.isBoth(eb)) { + const right = ebs.right + right.push(eb.right) + return TH.isBoth(ebs) ? TH.both(S.concat(ebs.left, eb.left), right) : TH.both(eb.left, right) + } + ebs.right.push(eb.right) + return ebs + }) + ), + f(0, _.head(as))().then(TH.map(_.singleton)) + ) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = (S: Semigroup) => ( + f: (index: number, a: A) => TaskThese +): ((as: ReadonlyArray) => TaskThese>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(S)(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- diff --git a/test/TaskThese.ts b/test/TaskThese.ts index a59e8161e..2420e365c 100644 --- a/test/TaskThese.ts +++ b/test/TaskThese.ts @@ -1,11 +1,13 @@ -import * as U from './util' -import { pipe } from '../src/function' +import * as E from '../src/Either' +import { pipe, SK } from '../src/function' import * as IO from '../src/IO' +import * as N from '../src/number' +import * as RA from '../src/ReadonlyArray' +import * as S from '../src/string' import * as T from '../src/Task' import * as _ from '../src/TaskThese' import * as TH from '../src/These' -import * as S from '../src/string' -import * as N from '../src/number' +import * as U from './util' describe('TaskThese', () => { // ------------------------------------------------------------------------------------- @@ -179,4 +181,78 @@ describe('TaskThese', () => { U.deepStrictEqual(await g(0)(), TH.both('zero', 0)) U.deepStrictEqual(await g(1)(), TH.right(1)) }) + + // ------------------------------------------------------------------------------------- + // array utils + // ------------------------------------------------------------------------------------- + + it('traverseReadonlyArrayWithIndex', async () => { + const f = (i: number, n: number) => (n > 0 ? _.right(n + i) : n === 0 ? _.both('a', 0) : _.left(String(n))) + const standard = RA.traverseWithIndex(_.getApplicative(T.ApplicativePar, S.Semigroup))(f) + const optimized = _.traverseReadonlyArrayWithIndex(S.Semigroup)(f) + const assert = async (input: ReadonlyArray) => { + U.deepStrictEqual(await standard(input)(), await optimized(input)()) + } + await assert([1, 2, 3]) + await assert([0, 2, 3]) + await assert([1, 0, 3]) + await assert([0, 0, 3]) + await assert([-1, 2, 3]) + await assert([1, -2, 3]) + await assert(RA.empty) + }) + + it('traverseReadonlyArrayWithIndexSeq', async () => { + const f = (i: number, n: number) => (n > 0 ? _.right(n + i) : n === 0 ? _.both('a', 0) : _.left(String(n))) + const standard = RA.traverseWithIndex(_.getApplicative(T.ApplicativeSeq, S.Semigroup))(f) + const optimized = _.traverseReadonlyArrayWithIndexSeq(S.Semigroup)(f) + const assert = async (input: ReadonlyArray) => { + U.deepStrictEqual(await standard(input)(), await optimized(input)()) + } + await assert([1, 2, 3]) + await assert([0, 2, 3]) + await assert([1, 0, 3]) + await assert([0, 0, 3]) + await assert([-1, 2, 3]) + await assert([1, -2, 3]) + await assert(RA.empty) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const right = (n: number): _.TaskThese => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskThese => + _.leftIO(() => { + log.push(s) + return s + }) + const f = _.traverseReadonlyArrayWithIndex(S.Semigroup) + U.deepStrictEqual(await pipe([right(1), right(2)], f(SK))(), E.right([1, 2])) + U.deepStrictEqual(await pipe([right(3), left('a')], f(SK))(), E.left('a')) + U.deepStrictEqual(await pipe([left('b'), right(4)], f(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceReadonlyArraySeq', async () => { + const log: Array = [] + const right = (n: number): _.TaskThese => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskThese => + _.leftIO(() => { + log.push(s) + return s + }) + const f = _.traverseReadonlyArrayWithIndexSeq(S.Semigroup) + U.deepStrictEqual(await pipe([right(1), right(2)], f(SK))(), E.right([1, 2])) + U.deepStrictEqual(await pipe([right(3), left('a')], f(SK))(), E.left('a')) + U.deepStrictEqual(await pipe([left('b'), right(4)], f(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) }) From 5691709f057ba4bd60860fa3655e7160b92ca590 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:24:22 +0200 Subject: [PATCH 131/162] Either - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 8 +++- docs/modules/Either.ts.md | 92 ++++++++++++++++++++++++++++----------- src/Either.ts | 73 +++++++++++++++++++++++-------- test/Either.ts | 26 ++++++++--- 4 files changed, 149 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60fe5722d..dfa698d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ high state of flux, you're at risk of it changing without notice. - **Deprecation** - `Array` - deprecate `range`, use `NonEmptyArray` module instead. + - `Either` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `function` - deprecate `Endomorphism`, use `Endomorphism` module instead. - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. @@ -102,6 +106,8 @@ high state of flux, you're at risk of it changing without notice. - `Either` - add `chainOptionK` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `EitherT` - add `orElseFirst` - add `orLeft` @@ -407,7 +413,7 @@ high state of flux, you're at risk of it changing without notice. - deprecate `fromIO` - `IOEither` - deprecate `getApplySemigroup` in favour of `Apply.getApplySemigroup` - - deprecate `getApplyMonoid` in favour of `Applicative.getApplicativeMonoid` + - daprecate `getApplyMonoid` in favour of `Applicative.getApplicativeMonoid` - deprecate `getSemigroup` in favour of `Apply.getApplySemigroup` - deprecate `getIOValidation`, use `getApplicativeIOValidation` and `getAltIOValidation` instead - `Map` diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 9ee8a0645..b9903dea0 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -128,6 +128,7 @@ Added in v2.0.0 - [isLeft](#isleft) - [isRight](#isright) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) @@ -136,13 +137,15 @@ Added in v2.0.0 - [bindW](#bindw) - [elem](#elem) - [exists](#exists) - - [sequenceArray](#sequencearray) - [toError](#toerror) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~JsonArray~~ (interface)](#jsonarray-interface) - [~~JsonRecord~~ (interface)](#jsonrecord-interface) - [~~Json~~ (type alias)](#json-type-alias) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -1458,6 +1461,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: Either +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1564,18 +1577,6 @@ assert.strictEqual(gt2(right(3)), true) Added in v2.0.0 -## sequenceArray - -Equivalent to `ReadonlyArray#sequence(Applicative)`. - -**Signature** - -```ts -export declare const sequenceArray: (as: readonly Either[]) => Either -``` - -Added in v2.9.0 - ## toError Default value for the `onError` argument of `tryCatch` @@ -1588,33 +1589,33 @@ export declare function toError(e: unknown): Error Added in v2.0.0 -## traverseArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => Either +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Either ) => (as: readonly A[]) => Either ``` -Added in v2.9.0 +Added in v2.11.0 -## traverseArrayWithIndex +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => Either -) => (as: readonly A[]) => Either +) => (as: ReadonlyNonEmptyArray) => Either> ``` -Added in v2.9.0 +Added in v2.11.0 ## ~~JsonArray~~ (interface) @@ -1636,7 +1637,6 @@ Use [`Json`](./Json.ts.html) module instead. ```ts export interface JsonRecord { - // tslint:disable-next-line: deprecation readonly [key: string]: Json } ``` @@ -1654,3 +1654,43 @@ export type Json = boolean | number | string | null | JsonArray | JsonRecord ``` Added in v2.6.7 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (as: readonly Either[]) => Either +``` + +Added in v2.9.0 + +## ~~traverseArrayWithIndex~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => Either +) => (as: readonly A[]) => Either +``` + +Added in v2.9.0 + +## ~~traverseArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => Either +) => (as: readonly A[]) => Either +``` + +Added in v2.9.0 diff --git a/src/Either.ts b/src/Either.ts index d6fed6130..751c1a66f 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -45,8 +45,10 @@ import * as _ from './internal' import { Monad2, Monad2C } from './Monad' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import { Separated, separated } from './Separated' @@ -1301,20 +1303,33 @@ export const apSW: ( fa: Either ) => Either = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: Either = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. * - * @since 2.9.0 + * @since 2.11.0 */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => Either) => ( - as: ReadonlyArray -): Either> => { - const out = [] - for (let i = 0; i < as.length; i++) { +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => Either) => ( + as: ReadonlyNonEmptyArray +): Either> => { + const e = f(0, _.head(as)) + if (isLeft(e)) { + return e + } + const out: NonEmptyArray = [e.right] + for (let i = 1; i < as.length; i++) { const e = f(i, as[i]) if (isLeft(e)) { return e @@ -1325,34 +1340,59 @@ export const traverseArrayWithIndex = (f: (index: number, a: A) => Eith } /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => Either +): ((as: ReadonlyArray) => Either>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated + */ +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => Either +) => (as: ReadonlyArray) => Either> = traverseReadonlyArrayWithIndex + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * + * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => Either -): ((as: ReadonlyArray) => Either>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => Either>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (as: ReadonlyArray>) => Either> = /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use [`Json`](./Json.ts.html) module instead. * * @since 2.6.7 * @deprecated */ -// tslint:disable-next-line: deprecation export type Json = boolean | number | string | null | JsonArray | JsonRecord /** @@ -1362,7 +1402,6 @@ export type Json = boolean | number | string | null | JsonArray | JsonRecord * @deprecated */ export interface JsonRecord { - // tslint:disable-next-line: deprecation readonly [key: string]: Json } @@ -1372,7 +1411,6 @@ export interface JsonRecord { * @since 2.6.7 * @deprecated */ -// tslint:disable-next-line: deprecation export interface JsonArray extends ReadonlyArray {} /** @@ -1382,7 +1420,6 @@ export interface JsonArray extends ReadonlyArray {} * @since 2.0.0 * @deprecated */ -// tslint:disable-next-line: deprecation export function parseJSON(s: string, onError: (reason: unknown) => E): Either { return tryCatch(() => JSON.parse(s), onError) } diff --git a/test/Either.ts b/test/Either.ts index 08f51bb94..a712818e8 100644 --- a/test/Either.ts +++ b/test/Either.ts @@ -1,12 +1,14 @@ -import * as U from './util' import { sequenceT } from '../src/Apply' import * as _ from '../src/Either' import { identity, pipe } from '../src/function' import * as N from '../src/number' import * as O from '../src/Option' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' +import { separated } from '../src/Separated' import * as S from '../src/string' import * as T from '../src/Task' -import { separated } from '../src/Separated' +import * as U from './util' describe('Either', () => { describe('pipeables', () => { @@ -606,9 +608,23 @@ describe('Either', () => { U.deepStrictEqual(f(_.left('a')), _.left('a')) }) - it('sequenceArray', () => { - U.deepStrictEqual(pipe([_.right(1), _.right(2)], _.sequenceArray), _.right([1, 2])) - U.deepStrictEqual(pipe([_.right(1), _.left('a')], _.sequenceArray), _.left('a')) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(pipe(RA.empty, f), _.right(RA.empty)) + U.deepStrictEqual(pipe(input, f), _.right(['a0', 'b1'])) + U.deepStrictEqual(pipe(['a', ''], f), _.left('e')) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.right(1), _.right(2)], _.sequenceArray), _.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.right(1), _.left('a')], _.sequenceArray), _.left('a')) + }) }) describe('getCompactable', () => { From 996bf025e633a3fcf9d6c84bd1a77d2cd53a94e2 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:31:38 +0200 Subject: [PATCH 132/162] IO - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 7 +++++ docs/modules/IO.ts.md | 65 +++++++++++++++++++++++++++++++++++-------- src/IO.ts | 63 ++++++++++++++++++++++++++++++++++------- test/IO.ts | 34 +++++++++++++++------- 4 files changed, 137 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa698d9f..b0d7c2a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,10 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Predicate`, use `Predicate` module instead. - deprecate `not`, use `Predicate` module instead. - deprecate `Refinement`, use `Refinement` module instead. + - `IO` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `Monoid` - deprecate `monoidVoid`, use `void` module instead. - `NonEmptyArray` @@ -114,6 +118,9 @@ high state of flux, you're at risk of it changing without notice. - `function` - add `SK` (@cdimitroulas) - add `apply` + - `IO` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `IOEither` - add `orElseFirst` / `orElseFirstW` - add `orLeft` diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index ddb8762e7..f6074d405 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -56,13 +56,16 @@ Added in v2.0.0 - [model](#model) - [IO (interface)](#io-interface) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -366,6 +369,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: IO +``` + +Added in v2.11.0 + ## Do **Signature** @@ -412,33 +425,49 @@ export declare const bindTo: (name: N) => (fa: IO) => IO<{ readonly [K Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly IO[]) => IO +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => IO +) => (as: readonly A[]) => IO ``` -Added in v2.9.0 +Added in v2.11.0 -## traverseArray +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => IO) => (as: readonly A[]) => IO +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => IO +) => (as: ReadonlyNonEmptyArray) => IO> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly IO[]) => IO ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -449,3 +478,15 @@ export declare const traverseArrayWithIndex: ( ``` Added in v2.9.0 + +## ~~traverseArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArray: (f: (a: A) => IO) => (as: readonly A[]) => IO +``` + +Added in v2.9.0 diff --git a/src/IO.ts b/src/IO.ts index 12d1c77bc..0eb872f7c 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -22,7 +22,9 @@ import * as _ from './internal' import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed1 } from './Pointed' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- @@ -305,40 +307,81 @@ export const apS = /*#__PURE__*/ apS_(Apply) +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: IO = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => IO) => ( + as: ReadonlyNonEmptyArray +): IO> => () => { + const out: NonEmptyArray = [f(0, _.head(as))()] + for (let i = 1; i < as.length; i++) { + out.push(f(i, as[i])()) + } + return out +} + /** * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => IO +): ((as: ReadonlyArray) => IO>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => IO) => ( - as: ReadonlyArray -): IO> => () => as.map((a, i) => f(i, a)()) +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => IO +) => (as: ReadonlyArray) => IO> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = (f: (a: A) => IO): ((as: ReadonlyArray) => IO>) => - traverseArrayWithIndex((_, a) => f(a)) + traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => IO> = /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use small, specific instances instead. * diff --git a/test/IO.ts b/test/IO.ts index a40198b89..ad7007f45 100644 --- a/test/IO.ts +++ b/test/IO.ts @@ -1,8 +1,10 @@ -import * as U from './util' import * as E from '../src/Either' import { pipe } from '../src/function' import * as _ from '../src/IO' import * as N from '../src/number' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' +import * as U from './util' describe('IO', () => { describe('pipeables', () => { @@ -78,14 +80,26 @@ describe('IO', () => { U.deepStrictEqual(pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b')))(), { a: 1, b: 'b' }) }) - it('sequenceArray', () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const append = (n: number): _.IO => () => { - log.push(n) - return n - } - U.deepStrictEqual(pipe([append(1), append(2)], _.sequenceArray)(), [1, 2]) - U.deepStrictEqual(log, [1, 2]) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => _.of(a + i)) + U.strictEqual(pipe(RA.empty, f)(), RA.empty) + U.deepStrictEqual(pipe(input, f)(), ['a0', 'b1']) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const append = (n: number): _.IO => () => { + log.push(n) + return n + } + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([append(1), append(2)], _.sequenceArray)(), [1, 2]) + U.deepStrictEqual(log, [1, 2]) + }) }) }) From 49b9e349cee0499863195274f50a2f2c0f4d1ef8 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:41:21 +0200 Subject: [PATCH 133/162] IOEither - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` --- CHANGELOG.md | 11 +++ docs/modules/IOEither.ts.md | 121 ++++++++++++++++++++++++++------- src/IOEither.ts | 123 +++++++++++++++++++++++++-------- test/IOEither.ts | 131 ++++++++++++++++++++++++++---------- 4 files changed, 296 insertions(+), 90 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0d7c2a46..841acef17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - `IOEither` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `Monoid` - deprecate `monoidVoid`, use `void` module instead. - `NonEmptyArray` @@ -125,6 +132,10 @@ high state of flux, you're at risk of it changing without notice. - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `Magma` - add `reverse` - add `filterFirst` diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index f25093bce..87dee80d5 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -109,6 +109,7 @@ Added in v2.0.0 - [fromIO](#fromio) - [fromOption](#fromoption) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) @@ -116,12 +117,16 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -1077,6 +1082,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: IOEither +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1168,47 +1183,89 @@ export declare const bracket: ( Added in v2.0.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly IOEither[]) => IOEither +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => IOEither +) => (as: readonly A[]) => IOEither ``` -Added in v2.9.0 +Added in v2.11.0 -## sequenceSeqArray +## traverseReadonlyArrayWithIndexSeq -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (arr: readonly IOEither[]) => IOEither +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => IOEither +) => (as: readonly A[]) => IOEither +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => IOEither +) => (as: ReadonlyNonEmptyArray) => IOEither> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => IOEither +) => (as: ReadonlyNonEmptyArray) => IOEither> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly IOEither[]) => IOEither ``` Added in v2.9.0 -## traverseArray +## ~~sequenceSeqArray~~ -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => IOEither -) => (as: readonly A[]) => IOEither +export declare const sequenceSeqArray: (arr: readonly IOEither[]) => IOEither ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -1220,23 +1277,23 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: ( +export declare const traverseArray: ( f: (a: A) => IOEither ) => (as: readonly A[]) => IOEither ``` Added in v2.9.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -1247,3 +1304,17 @@ export declare const traverseSeqArrayWithIndex: ( ``` Added in v2.9.0 + +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const traverseSeqArray: ( + f: (a: A) => IOEither +) => (as: readonly A[]) => IOEither +``` + +Added in v2.9.0 diff --git a/src/IOEither.ts b/src/IOEither.ts index 7010c1bdf..2942f3d5d 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -37,7 +37,7 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_ } from './FromIO' -import { flow, identity, Lazy, pipe } from './function' +import { flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import * as I from './IO' @@ -45,8 +45,10 @@ import { Monad2, Monad2C } from './Monad' import { MonadIO2, MonadIO2C } from './MonadIO' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' @@ -912,79 +914,142 @@ export const apSW: ( fa: IOEither ) => IOEither = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: IOEither = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => IOEither +) => (as: ReadonlyNonEmptyArray) => IOEither> = (f) => + flow(I.traverseReadonlyNonEmptyArrayWithIndex(f), I.map(E.traverseReadonlyNonEmptyArrayWithIndex(SK))) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => IOEither +): ((as: ReadonlyArray) => IOEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = (f: (index: number, a: A) => IOEither) => ( + as: ReadonlyNonEmptyArray +): IOEither> => () => { + const e = f(0, _.head(as))() + if (_.isLeft(e)) { + return e + } + const out: NonEmptyArray = [e.right] + for (let i = 1; i < as.length; i++) { + const e = f(i, as[i])() + if (_.isLeft(e)) { + return e + } + out.push(e.right) + } + return _.right(out) +} + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => IOEither +): ((as: ReadonlyArray) => IOEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => IOEither -): ((as: ReadonlyArray) => IOEither>) => - flow(I.traverseArrayWithIndex(f), I.map(E.sequenceArray)) +) => (as: ReadonlyArray) => IOEither> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => IOEither -): ((as: ReadonlyArray) => IOEither>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => IOEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => IOEither> = /*#__PURE__*/ traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ -export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => IOEither) => ( - as: ReadonlyArray -): IOEither> => () => { - const out = [] - for (let i = 0; i < as.length; i++) { - const b = f(i, as[i])() - if (E.isLeft(b)) { - return b - } - out.push(b.right) - } - return E.right(out) -} +export const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => IOEither +) => (as: ReadonlyArray) => IOEither> = traverseReadonlyArrayWithIndexSeq /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArray = ( f: (a: A) => IOEither -): ((as: ReadonlyArray) => IOEither>) => traverseSeqArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => IOEither>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => IOEither> = /*#__PURE__*/ traverseSeqArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use [`ApplicativePar`](#applicativepar) instead * diff --git a/test/IOEither.ts b/test/IOEither.ts index c84f54e49..e7d313b47 100644 --- a/test/IOEither.ts +++ b/test/IOEither.ts @@ -1,7 +1,7 @@ import * as U from './util' import { sequenceT } from '../src/Apply' import * as E from '../src/Either' -import { identity, pipe } from '../src/function' +import { identity, pipe, SK } from '../src/function' import * as I from '../src/IO' import * as _ from '../src/IOEither' import * as O from '../src/Option' @@ -10,6 +10,7 @@ import * as RA from '../src/ReadonlyArray' import * as N from '../src/number' import * as S from '../src/string' import { left, right } from '../src/Separated' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' describe('IOEither', () => { describe('pipeables', () => { @@ -486,42 +487,100 @@ describe('IOEither', () => { ) }) - it('sequenceArray', () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.IOEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.IOEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(pipe([right(1), right(2)], _.sequenceArray)(), E.right([1, 2])) - U.deepStrictEqual(pipe([right(3), left('a')], _.sequenceArray)(), E.left('a')) - U.deepStrictEqual(pipe([left('b'), right(4)], _.sequenceArray)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) - }) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] - it('sequenceSeqArray', () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.IOEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.IOEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(pipe([right(1), right(2)], _.sequenceSeqArray)(), E.right([1, 2])) - U.deepStrictEqual(pipe([right(3), left('a')], _.sequenceSeqArray)(), E.left('a')) - U.deepStrictEqual(pipe([left('b'), right(4)], _.sequenceSeqArray)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(pipe(RA.empty, f)(), E.right(RA.empty)) + U.deepStrictEqual(pipe(input, f)(), E.right(['a0', 'b1'])) + U.deepStrictEqual(pipe(['a', ''], f)(), E.left('e')) + }) + + it('sequenceReadonlyArray', () => { + U.deepStrictEqual(pipe(RA.empty, _.traverseReadonlyArrayWithIndex(SK))(), E.right(RA.empty)) + + const log: Array = [] + const right = (n: number): _.IOEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.IOEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual(pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndex(SK))(), E.right([1, 2])) + U.deepStrictEqual(pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndex(SK))(), E.left('a')) + U.deepStrictEqual(pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndex(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceReadonlyArraySeq', () => { + U.deepStrictEqual(pipe(RA.empty, _.traverseReadonlyArrayWithIndexSeq(SK))(), E.right(RA.empty)) + + const log: Array = [] + const right = (n: number): _.IOEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.IOEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual(pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.right([1, 2])) + U.deepStrictEqual(pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.left('a')) + U.deepStrictEqual(pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.IOEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.IOEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([right(1), right(2)], _.sequenceArray)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([right(3), left('a')], _.sequenceArray)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([left('b'), right(4)], _.sequenceArray)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceSeqArray', () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.IOEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.IOEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([right(1), right(2)], _.sequenceSeqArray)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([right(3), left('a')], _.sequenceSeqArray)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([left('b'), right(4)], _.sequenceSeqArray)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) }) it('tryCatchK', () => { From bc129f376e367771c73e95f33731d032c248410b Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:48:06 +0200 Subject: [PATCH 134/162] Option - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 5 +++ docs/modules/Option.ts.md | 73 ++++++++++++++++++++++++++++--------- src/Option.ts | 75 ++++++++++++++++++++++++++++++--------- test/Option.ts | 21 +++++++++-- 4 files changed, 138 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 841acef17..d737dbd3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,9 @@ high state of flux, you're at risk of it changing without notice. - deprecate `getRefinement`, use `Refinement` module instead. - deprecate `getFirstMonoid`, use `getMonoid` module instead. - deprecate `getLastMonoid`, use `getMonoid` module instead. + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -174,6 +177,8 @@ high state of flux, you're at risk of it changing without notice. - add `chainEitherK` - add `Zero` instance - add `guard` constructor + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `Ord` - add `trivial` instance - add `equals` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 812c0d3b7..3cc22a372 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -129,16 +129,19 @@ Added in v2.0.0 - [isNone](#isnone) - [isSome](#issome) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) - [elem](#elem) - [exists](#exists) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~getRefinement~~](#getrefinement) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -1484,6 +1487,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: Option +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1594,33 +1607,61 @@ assert.strictEqual( Added in v2.0.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly Option[]) => Option +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Option +) => (as: readonly A[]) => Option ``` -Added in v2.9.0 +Added in v2.11.0 -## traverseArray +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => Option) => (as: readonly A[]) => Option +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => Option +) => (as: ReadonlyNonEmptyArray) => Option> +``` + +Added in v2.11.0 + +## ~~getRefinement~~ + +Use `Refinement` module instead. + +**Signature** + +```ts +export declare function getRefinement(getOption: (a: A) => Option): Refinement +``` + +Added in v2.0.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly Option[]) => Option ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -1632,14 +1673,14 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## ~~getRefinement~~ +## ~~traverseArray~~ -Use `Refinement` module instead. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare function getRefinement(getOption: (a: A) => Option): Refinement +export declare const traverseArray: (f: (a: A) => Option) => (as: readonly A[]) => Option ``` -Added in v2.0.0 +Added in v2.9.0 diff --git a/src/Option.ts b/src/Option.ts index d48d7dd21..e51b57cff 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -37,9 +37,11 @@ import * as _ from './internal' import { Monad1 } from './Monad' import { MonadThrow1 } from './MonadThrow' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Ord } from './Ord' import { Pointed1 } from './Pointed' import { not, Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { first, last, Semigroup } from './Semigroup' import { Separated, separated } from './Separated' @@ -1213,50 +1215,89 @@ export const apS = /*#__PURE__*/ apS_(Apply) +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: Option = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. * - * @since 2.9.0 + * @since 2.11.0 */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => Option) => ( - as: ReadonlyArray -): Option> => { - const out = [] - for (let i = 0; i < as.length; i++) { - const b = f(i, as[i]) - if (isNone(b)) { +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => Option) => ( + as: ReadonlyNonEmptyArray +): Option> => { + const o = f(0, _.head(as)) + if (isNone(o)) { + return none + } + const out: NonEmptyArray = [o.value] + for (let i = 1; i < as.length; i++) { + const o = f(i, as[i]) + if (isNone(o)) { return none } - out.push(b.value) + out.push(o.value) } return some(out) } /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => Option +): ((as: ReadonlyArray) => Option>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * + * @since 2.9.0 + * @deprecated + */ +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => Option +) => (as: ReadonlyArray) => Option> = traverseReadonlyArrayWithIndex + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = (f: (a: A) => Option): ((as: ReadonlyArray) => Option>) => - traverseArrayWithIndex((_, a) => f(a)) + traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Option> = /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use `Refinement` module instead. * diff --git a/test/Option.ts b/test/Option.ts index 14b14c3b1..d148a2a72 100644 --- a/test/Option.ts +++ b/test/Option.ts @@ -7,6 +7,7 @@ import * as RA from '../src/ReadonlyArray' import * as S from '../src/string' import * as T from '../src/Task' import { separated } from '../src/Separated' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' const p = (n: number): boolean => n > 2 @@ -477,9 +478,23 @@ describe('Option', () => { U.deepStrictEqual(f(-1), _.none) }) - it('sequenceArray', () => { - U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray), _.some([1, 2])) - U.deepStrictEqual(pipe([_.of(1), _.none], _.sequenceArray), _.none) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.some(a + i) : _.none)) + U.deepStrictEqual(pipe(RA.empty, f), _.some(RA.empty)) + U.deepStrictEqual(pipe(input, f), _.some(['a0', 'b1'])) + U.deepStrictEqual(pipe(['a', ''], f), _.none) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray), _.some([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.of(1), _.none], _.sequenceArray), _.none) + }) }) it('tryCatchK', () => { From 7ddaedf298d4a07884a82c37ec3f1c537d1b8420 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:54:02 +0200 Subject: [PATCH 135/162] Reader - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 6 ++++ docs/modules/Reader.ts.md | 67 +++++++++++++++++++++++++++++++-------- src/Reader.ts | 63 ++++++++++++++++++++++++++++++------ test/Reader.ts | 22 ++++++++++--- 4 files changed, 131 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d737dbd3c..121255613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,10 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - `Reader` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -185,6 +189,8 @@ high state of flux, you're at risk of it changing without notice. - `Reader` - add `asksReaderW`, `asksReader` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `ReaderEither` - add `asksReaderEitherW`, `asksReaderEither` - add `orElseFirst` / `orElseFirstW` diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index c8835247b..2aff33fa0 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -67,15 +67,18 @@ Added in v2.0.0 - [model](#model) - [Reader (interface)](#reader-interface) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -576,6 +579,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: Reader +``` + +Added in v2.11.0 + ## Do **Signature** @@ -648,9 +661,37 @@ export declare const bindW: ( Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex + +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Reader +) => (as: readonly A[]) => Reader +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => Reader +) => (as: ReadonlyNonEmptyArray) => Reader> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -660,29 +701,29 @@ export declare const sequenceArray: (arr: readonly Reader[]) => Read Added in v2.9.0 -## traverseArray +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => Reader +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => Reader ) => (as: readonly A[]) => Reader ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => Reader +export declare const traverseArray: ( + f: (a: A) => Reader ) => (as: readonly A[]) => Reader ``` diff --git a/src/Reader.ts b/src/Reader.ts index 51a7c3763..6c17f7549 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -12,8 +12,10 @@ import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { Monad2 } from './Monad' import { Monoid } from './Monoid' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed2 } from './Pointed' import { Profunctor2 } from './Profunctor' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import { Strong2 } from './Strong' @@ -452,41 +454,82 @@ export const apSW: ( fa: Reader ) => Reader = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: Reader = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => Reader) => ( + as: ReadonlyNonEmptyArray +): Reader> => (r) => { + const out: NonEmptyArray = [f(0, _.head(as))(r)] + for (let i = 1; i < as.length; i++) { + out.push(f(i, as[i])(r)) + } + return out +} + /** * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => Reader +): ((as: ReadonlyArray) => Reader>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => Reader) => ( - as: ReadonlyArray -): Reader> => (r) => as.map((x, i) => f(i, x)(r)) +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => Reader +) => (as: ReadonlyArray) => Reader> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => Reader -): ((as: ReadonlyArray) => Reader>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => Reader>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Reader> = /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use small, specific instances instead. * diff --git a/test/Reader.ts b/test/Reader.ts index e890a8c8d..188cc7052 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -1,8 +1,10 @@ -import * as U from './util' import { pipe } from '../src/function' import * as N from '../src/number' -import * as S from '../src/string' import * as _ from '../src/Reader' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' +import * as S from '../src/string' +import * as U from './util' interface Env { readonly count: number @@ -137,8 +139,20 @@ describe('Reader', () => { U.deepStrictEqual(pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b')))(undefined), { a: 1, b: 'b' }) }) - it('sequenceArray', () => { - U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray)(undefined), [1, 2]) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => _.of(a + i)) + U.strictEqual(pipe(RA.empty, f)({}), RA.empty) + U.deepStrictEqual(pipe(input, f)({}), ['a0', 'b1']) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.of(1), _.of(2)], _.sequenceArray)(undefined), [1, 2]) + }) }) it('asksReader', () => { From 9020b34baf0fad3e48a837c20b942ddd1cbcff87 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 18:59:50 +0200 Subject: [PATCH 136/162] ReaderEither - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 6 +++ docs/modules/ReaderEither.ts.md | 67 ++++++++++++++++++++++++++------- src/ReaderEither.ts | 58 ++++++++++++++++++++++------ test/ReaderEither.ts | 28 +++++++++++--- 4 files changed, 129 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 121255613..5ddbf3bfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - `ReaderEither` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -198,6 +202,8 @@ high state of flux, you're at risk of it changing without notice. - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `ReaderTask` - add `asksReaderTaskW`, `asksReaderTask` - add `chainReaderKW` diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 8597357a4..327ee3765 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -108,15 +108,18 @@ Added in v2.0.0 - [fromOption](#fromoption) - [fromReader](#fromreader) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -1145,6 +1148,16 @@ Added in v2.11.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: ReaderEither +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1223,9 +1236,37 @@ export declare const bindW: ( Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex + +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => ReaderEither +) => (as: readonly A[]) => ReaderEither +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => ReaderEither +) => (as: ReadonlyNonEmptyArray) => ReaderEither> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -1235,29 +1276,29 @@ export declare const sequenceArray: (arr: readonly ReaderEither( - f: (a: A) => ReaderEither +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => ReaderEither ) => (as: readonly A[]) => ReaderEither ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => ReaderEither +export declare const traverseArray: ( + f: (a: A) => ReaderEither ) => (as: readonly A[]) => ReaderEither ``` diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 32b94100d..cb28a7052 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -41,7 +41,7 @@ import { FromReader3, fromReaderK as fromReaderK_ } from './FromReader' -import { flow, identity, Lazy, pipe } from './function' +import { flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { Monad3, Monad3C } from './Monad' @@ -52,6 +52,7 @@ import { URI as OURI } from './Option' import { Pointed3 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' @@ -940,33 +941,72 @@ export const apSW: ( fa: ReaderEither ) => ReaderEither = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: ReaderEither = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + f: (index: number, a: A) => ReaderEither +): ((as: ReadonlyNonEmptyArray) => ReaderEither>) => + flow(R.traverseReadonlyNonEmptyArrayWithIndex(f), R.map(E.traverseReadonlyNonEmptyArrayWithIndex(SK))) + /** * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => ReaderEither +): ((as: ReadonlyArray) => ReaderEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderEither -): ((as: ReadonlyArray) => ReaderEither>) => - flow(R.traverseArrayWithIndex(f), R.map(E.sequenceArray)) +) => (as: ReadonlyArray) => ReaderEither> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderEither -): ((as: ReadonlyArray) => ReaderEither>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => ReaderEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -974,10 +1014,6 @@ export const sequenceArray: ( /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use small, specific instances instead. * diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 1dde92a53..51002c437 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -1,13 +1,15 @@ -import * as U from './util' import * as Apply from '../src/Apply' import * as E from '../src/Either' import { pipe } from '../src/function' +import * as N from '../src/number' import * as O from '../src/Option' import * as R from '../src/Reader' import * as _ from '../src/ReaderEither' -import * as S from '../src/string' -import * as N from '../src/number' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' import { left, right } from '../src/Separated' +import * as S from '../src/string' +import * as U from './util' describe('ReaderEither', () => { describe('pipeables', () => { @@ -266,9 +268,23 @@ describe('ReaderEither', () => { ) }) - it('sequenceArray', () => { - U.deepStrictEqual(pipe([_.right(1), _.right(2)], _.sequenceArray)(undefined), E.right([1, 2])) - U.deepStrictEqual(pipe([_.right(1), _.left('a')], _.sequenceArray)(undefined), E.left('a')) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(pipe(RA.empty, f)({}), E.right(RA.empty)) + U.deepStrictEqual(pipe(input, f)({}), E.right(['a0', 'b1'])) + U.deepStrictEqual(pipe(['a', ''], f)({}), E.left('e')) + }) + + // old + it('sequenceArray', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.right(1), _.right(2)], _.sequenceArray)(undefined), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([_.right(1), _.left('a')], _.sequenceArray)(undefined), E.left('a')) + }) }) it('getCompactable', () => { From 40723ceaabccce9ee07f60ed1e6738f056f989df Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 19:07:21 +0200 Subject: [PATCH 137/162] ReaderTask - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` --- CHANGELOG.md | 11 +++ docs/modules/ReaderTask.ts.md | 131 ++++++++++++++++++++++++++-------- src/ReaderTask.ts | 100 ++++++++++++++++++++------ test/ReaderTask.ts | 110 ++++++++++++++++++++-------- 4 files changed, 270 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ddbf3bfc..ca8fcce2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - `ReaderTask` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -209,6 +216,10 @@ high state of flux, you're at risk of it changing without notice. - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `ReaderTaskEither` - add `asksReaderTaskEitherW`, `asksReaderTaskEither` - add `orElseFirst` / `orElseFirstW` diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 721bc466d..d9271426a 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -74,19 +74,24 @@ Added in v2.3.0 - [fromReader](#fromreader) - [fromTask](#fromtask) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - [~~run~~](#run) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -703,6 +708,16 @@ Added in v2.3.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: ReaderTask +``` + +Added in v2.11.0 + ## Do **Signature** @@ -775,47 +790,99 @@ export declare const bindW: ( Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly ReaderTask[]) => ReaderTask +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask ``` -Added in v2.9.0 +Added in v2.11.0 -## sequenceSeqArray +## traverseReadonlyArrayWithIndexSeq -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (arr: readonly ReaderTask[]) => ReaderTask +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask ``` -Added in v2.10.0 +Added in v2.11.0 -## traverseArray +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => ReaderTask -) => (as: readonly A[]) => ReaderTask +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTask +) => (as: ReadonlyNonEmptyArray) => ReaderTask> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => ReaderTask +) => (as: ReadonlyNonEmptyArray) => ReaderTask> +``` + +Added in v2.11.0 + +## ~~run~~ + +**Signature** + +```ts +export declare function run(ma: ReaderTask, r: R): Promise +``` + +Added in v2.4.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly ReaderTask[]) => ReaderTask ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~sequenceSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const sequenceSeqArray: (arr: readonly ReaderTask[]) => ReaderTask +``` + +Added in v2.10.0 -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +## ~~traverseArrayWithIndex~~ + +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -827,23 +894,23 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: ( +export declare const traverseArray: ( f: (a: A) => ReaderTask ) => (as: readonly A[]) => ReaderTask ``` -Added in v2.10.0 +Added in v2.9.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -855,12 +922,16 @@ export declare const traverseSeqArrayWithIndex: ( Added in v2.10.0 -## ~~run~~ +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare function run(ma: ReaderTask, r: R): Promise +export declare const traverseSeqArray: ( + f: (a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask ``` -Added in v2.4.0 +Added in v2.10.0 diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 590bdf5f0..c15a238e4 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -25,7 +25,7 @@ import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, pipe } from './function' +import { flow, identity, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { Monad2 } from './Monad' @@ -35,6 +35,7 @@ import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import * as R from './Reader' import * as RT from './ReaderT' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import * as T from './Task' @@ -586,72 +587,129 @@ export const apSW: ( fa: ReaderTask ) => ReaderTask = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: ReaderTask = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + f: (index: number, a: A) => ReaderTask +): ((as: ReadonlyNonEmptyArray) => ReaderTask>) => + flow(R.traverseReadonlyNonEmptyArrayWithIndex(f), R.map(T.traverseReadonlyNonEmptyArrayWithIndex(SK))) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => ReaderTask +): ((as: ReadonlyArray) => ReaderTask>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = ( + f: (index: number, a: A) => ReaderTask +): ((as: ReadonlyNonEmptyArray) => ReaderTask>) => + flow(R.traverseReadonlyNonEmptyArrayWithIndex(f), R.map(T.traverseReadonlyNonEmptyArrayWithIndexSeq(SK))) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => ReaderTask +): ((as: ReadonlyArray) => ReaderTask>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderTask -): ((as: ReadonlyArray) => ReaderTask>) => - flow(R.traverseArrayWithIndex(f), R.map(T.sequenceArray)) +) => (as: ReadonlyArray) => ReaderTask> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderTask -): ((as: ReadonlyArray) => ReaderTask>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => ReaderTask>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => ReaderTask> = /*#__PURE__*/ traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ -export const traverseSeqArrayWithIndex = ( +export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => ReaderTask -): ((as: ReadonlyArray) => ReaderTask>) => - flow(R.traverseArrayWithIndex(f), R.map(T.sequenceSeqArray)) +) => (as: ReadonlyArray) => ReaderTask> = traverseReadonlyArrayWithIndexSeq /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ export const traverseSeqArray = ( f: (a: A) => ReaderTask -): ((as: ReadonlyArray) => ReaderTask>) => traverseSeqArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => ReaderTask>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => ReaderTask> = /*#__PURE__*/ traverseSeqArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** * Use small, specific instances instead. * diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index eec36e757..54bc3849a 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -1,9 +1,10 @@ -import { pipe } from '../src/function' +import { pipe, SK } from '../src/function' import * as I from '../src/IO' import { monoidString } from '../src/Monoid' import * as R from '../src/Reader' import * as _ from '../src/ReaderTask' import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' import { semigroupString } from '../src/Semigroup' import * as S from '../src/string' import * as T from '../src/Task' @@ -169,37 +170,84 @@ describe('ReaderTask', () => { U.deepStrictEqual(await pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b')))(undefined)(), { a: 1, b: 'b' }) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const append = (n: number): _.ReaderTask => - _.fromTask( - T.delay(n % 2 === 0 ? 50 : 100)( - T.fromIO(() => { - log.push(n) - return n - }) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => _.of(a + i)) + U.deepStrictEqual(await pipe(RA.empty, f)(undefined)(), RA.empty) + U.deepStrictEqual(await pipe(input, f)(undefined)(), ['a0', 'b1']) + }) + + it('sequenceReadonlyArray', async () => { + U.deepStrictEqual(await pipe(RA.empty, _.traverseReadonlyArrayWithIndex(SK))(undefined)(), RA.empty) + const log: Array = [] + const append = (n: number): _.ReaderTask => + _.fromTask( + T.delay(n % 2 === 0 ? 50 : 100)( + T.fromIO(() => { + log.push(n) + return n + }) + ) ) - ) - const as = RA.makeBy(4, append) - U.deepStrictEqual(await pipe(as, _.sequenceArray)(undefined)(), [0, 1, 2, 3]) - U.deepStrictEqual(log, [0, 2, 1, 3]) - }) - - it('sequenceSeqArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const append = (n: number): _.ReaderTask => - _.fromTask( - T.delay(n % 2 === 0 ? 50 : 100)( - T.fromIO(() => { - log.push(n) - return n - }) + const as = RA.makeBy(4, append) + U.deepStrictEqual(await pipe(as, _.traverseReadonlyArrayWithIndex(SK))(undefined)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 2, 1, 3]) + }) + + it('sequenceReadonlyArraySeq', async () => { + U.deepStrictEqual(await pipe(RA.empty, _.traverseReadonlyArrayWithIndexSeq(SK))(undefined)(), RA.empty) + const log: Array = [] + const append = (n: number): _.ReaderTask => + _.fromTask( + T.delay(n % 2 === 0 ? 50 : 100)( + T.fromIO(() => { + log.push(n) + return n + }) + ) ) - ) - const as = RA.makeBy(4, append) - U.deepStrictEqual(await pipe(as, _.sequenceSeqArray)(undefined)(), [0, 1, 2, 3]) - U.deepStrictEqual(log, [0, 1, 2, 3]) + const as = RA.makeBy(4, append) + U.deepStrictEqual(await pipe(as, _.traverseReadonlyArrayWithIndexSeq(SK))(undefined)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 1, 2, 3]) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const append = (n: number): _.ReaderTask => + _.fromTask( + T.delay(n % 2 === 0 ? 50 : 100)( + T.fromIO(() => { + log.push(n) + return n + }) + ) + ) + const as = RA.makeBy(4, append) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe(as, _.sequenceArray)(undefined)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 2, 1, 3]) + }) + + it('sequenceSeqArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const append = (n: number): _.ReaderTask => + _.fromTask( + T.delay(n % 2 === 0 ? 50 : 100)( + T.fromIO(() => { + log.push(n) + return n + }) + ) + ) + const as = RA.makeBy(4, append) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe(as, _.sequenceSeqArray)(undefined)(), [0, 1, 2, 3]) + U.deepStrictEqual(log, [0, 1, 2, 3]) + }) }) }) From 5706ea22faf0ff78bb6fb181c2c57c78949fd220 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 19:25:17 +0200 Subject: [PATCH 138/162] ReaderTaskEither - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` TaskEither - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` --- CHANGELOG.md | 22 ++++ docs/modules/ReaderTaskEither.ts.md | 131 +++++++++++++++++------ docs/modules/TaskEither.ts.md | 153 +++++++++++++++++++-------- src/ReaderTaskEither.ts | 84 ++++++++++++--- src/TaskEither.ts | 99 +++++++++++++++--- test/ReaderTaskEither.ts | 157 +++++++++++++++++++++------- test/TaskEither.ts | 134 +++++++++++++++++------- 7 files changed, 611 insertions(+), 169 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca8fcce2b..3435aa26a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - `ReaderTaskEither` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -94,6 +101,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - `TaskEither` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - **New Feature** - add `Endomorphism` module - add `Predicate` module @@ -234,6 +248,10 @@ high state of flux, you're at risk of it changing without notice. - add `chainFirstReaderEitherK`, `chainFirstReaderEitherKW` - add `chainFirstTaskEitherK`, `chainFirstTaskEitherKW` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `ReadonlyArray` - add `prependW`, `appendW` (@thewilkybarkid) - add `filterE` @@ -325,6 +343,10 @@ high state of flux, you're at risk of it changing without notice. - add `orElseFirst` / `orElseFirstW` - add `orLeft` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `TaskOption` - add `fromTaskEither` (@thewilkybarkid) - add `Zero` instance diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 377eb8094..26436daaa 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -150,6 +150,7 @@ Added in v2.0.0 - [fromTask](#fromtask) - [fromTaskEither](#fromtaskeither) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) @@ -157,13 +158,17 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - [~~run~~](#run) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -1690,6 +1695,16 @@ Added in v2.0.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: ReaderTaskEither +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1791,51 +1806,103 @@ export declare function bracket( Added in v2.0.4 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: ( - arr: readonly ReaderTaskEither[] -) => ReaderTaskEither +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.9.0 +Added in v2.11.0 -## sequenceSeqArray +## traverseReadonlyArrayWithIndexSeq -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: ( +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> +``` + +Added in v2.11.0 + +## ~~run~~ + +**Signature** + +```ts +export declare function run(ma: ReaderTaskEither, r: R): Promise> +``` + +Added in v2.0.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: ( arr: readonly ReaderTaskEither[] ) => ReaderTaskEither ``` Added in v2.9.0 -## traverseArray +## ~~sequenceSeqArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => ReaderTaskEither -) => (as: readonly A[]) => ReaderTaskEither +export declare const sequenceSeqArray: ( + arr: readonly ReaderTaskEither[] +) => ReaderTaskEither ``` Added in v2.9.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -1847,23 +1914,23 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: ( +export declare const traverseArray: ( f: (a: A) => ReaderTaskEither ) => (as: readonly A[]) => ReaderTaskEither ``` Added in v2.9.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -1875,12 +1942,16 @@ export declare const traverseSeqArrayWithIndex: ( Added in v2.9.0 -## ~~run~~ +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare function run(ma: ReaderTaskEither, r: R): Promise> +export declare const traverseSeqArray: ( + f: (a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.0.0 +Added in v2.9.0 diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 294e8483f..31ffe48a2 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -129,6 +129,7 @@ Added in v2.0.0 - [fromTask](#fromtask) - [fromTaskOption](#fromtaskoption) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [apSW](#apsw) @@ -136,13 +137,17 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - [taskify](#taskify) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -1354,6 +1359,16 @@ Added in v2.11.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: TaskEither +``` + +Added in v2.11.0 + ## Do **Signature** @@ -1445,30 +1460,6 @@ export declare const bracket: ( Added in v2.0.0 -## sequenceArray - -Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. - -**Signature** - -```ts -export declare const sequenceArray: (arr: readonly TaskEither[]) => TaskEither -``` - -Added in v2.9.0 - -## sequenceSeqArray - -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. - -**Signature** - -```ts -export declare const sequenceSeqArray: (arr: readonly TaskEither[]) => TaskEither -``` - -Added in v2.9.0 - ## taskify Convert a node style callback function to one returning a `TaskEither` @@ -1521,23 +1512,89 @@ assert.strictEqual(stat.length, 0) Added in v2.0.0 -## traverseArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => TaskEither +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => TaskEither ) => (as: readonly A[]) => TaskEither ``` +Added in v2.11.0 + +## traverseReadonlyArrayWithIndexSeq + +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskEither +) => (as: readonly A[]) => TaskEither +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => TaskEither +) => (as: ReadonlyNonEmptyArray) => TaskEither> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskEither +) => (as: ReadonlyNonEmptyArray) => TaskEither> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly TaskEither[]) => TaskEither +``` + Added in v2.9.0 -## traverseArrayWithIndex +## ~~sequenceSeqArray~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const sequenceSeqArray: (arr: readonly TaskEither[]) => TaskEither +``` + +Added in v2.9.0 + +## ~~traverseArrayWithIndex~~ + +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -1549,23 +1606,23 @@ export declare const traverseArrayWithIndex: ( Added in v2.9.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: ( +export declare const traverseArray: ( f: (a: A) => TaskEither ) => (as: readonly A[]) => TaskEither ``` Added in v2.9.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -1576,3 +1633,17 @@ export declare const traverseSeqArrayWithIndex: ( ``` Added in v2.9.0 + +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const traverseSeqArray: ( + f: (a: A) => TaskEither +) => (as: readonly A[]) => TaskEither +``` + +Added in v2.9.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 3b7727dc4..52350077b 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -49,7 +49,7 @@ import { FromTask3, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe } from './function' +import { flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor3 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -66,6 +66,7 @@ import { Predicate } from './Predicate' import * as R from './Reader' import { ReaderEither, URI as REURI } from './ReaderEither' import * as RT from './ReaderTask' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import * as T from './Task' @@ -1354,14 +1355,74 @@ export const apSW: ( fa: ReaderTaskEither ) => ReaderTaskEither = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: ReaderTaskEither = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + f: (index: number, a: A) => ReaderTaskEither +): ((as: ReadonlyNonEmptyArray) => ReaderTaskEither>) => + flow(R.traverseReadonlyNonEmptyArrayWithIndex(f), R.map(TE.traverseReadonlyNonEmptyArrayWithIndex(SK))) + /** * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => ReaderTaskEither +): ((as: ReadonlyArray) => ReaderTaskEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = ( + f: (index: number, a: A) => ReaderTaskEither +): ((as: ReadonlyNonEmptyArray) => ReaderTaskEither>) => + flow(R.traverseReadonlyNonEmptyArrayWithIndex(f), R.map(TE.traverseReadonlyNonEmptyArrayWithIndexSeq(SK))) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => ReaderTaskEither +): ((as: ReadonlyArray) => ReaderTaskEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ export const traverseArrayWithIndex = ( f: (index: number, a: A) => ReaderTaskEither @@ -1369,18 +1430,20 @@ export const traverseArrayWithIndex = ( flow(R.traverseArrayWithIndex(f), R.map(TE.sequenceArray)) /** - * Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderTaskEither ): ((as: ReadonlyArray) => ReaderTaskEither>) => traverseArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -1389,9 +1452,10 @@ export const sequenceArray: ( traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArrayWithIndex = ( f: (index: number, a: A) => ReaderTaskEither @@ -1399,18 +1463,20 @@ export const traverseSeqArrayWithIndex = ( flow(R.traverseArrayWithIndex(f), R.map(TE.sequenceSeqArray)) /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArray = ( f: (a: A) => ReaderTaskEither ): ((as: ReadonlyArray) => ReaderTaskEither>) => traverseSeqArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceSeqArray: ( arr: ReadonlyArray> @@ -1418,12 +1484,6 @@ export const sequenceSeqArray: ( /*#__PURE__*/ traverseSeqArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** * Use small, specific instances instead. * diff --git a/src/TaskEither.ts b/src/TaskEither.ts index deea94fed..93897456d 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -48,7 +48,7 @@ import { FromTask2, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe } from './function' +import { flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -59,8 +59,10 @@ import { MonadTask2, MonadTask2C } from './MonadTask' import { MonadThrow2, MonadThrow2C } from './MonadThrow' import { Monoid } from './Monoid' import { NaturalTransformation12C, NaturalTransformation22 } from './NaturalTransformation' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed2 } from './Pointed' import { Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Semigroup } from './Semigroup' import * as T from './Task' @@ -1189,14 +1191,88 @@ export const apSW: ( fa: TaskEither ) => TaskEither = apS as any +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: TaskEither = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + f: (index: number, a: A) => TaskEither +): ((as: ReadonlyNonEmptyArray) => TaskEither>) => + flow(T.traverseReadonlyNonEmptyArrayWithIndex(f), T.map(E.traverseReadonlyNonEmptyArrayWithIndex(SK))) + /** * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => TaskEither +): ((as: ReadonlyArray) => TaskEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = (f: (index: number, a: A) => TaskEither) => ( + as: ReadonlyNonEmptyArray +): TaskEither> => () => + _.tail(as).reduce>>>( + (acc, a, i) => + acc.then((ebs) => + _.isLeft(ebs) + ? acc + : f(i + 1, a)().then((eb) => { + if (_.isLeft(eb)) { + return eb + } + ebs.right.push(eb.right) + return ebs + }) + ), + f(0, _.head(as))().then(E.map(_.singleton)) + ) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => TaskEither +): ((as: ReadonlyArray) => TaskEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.9.0 + * @deprecated */ export const traverseArrayWithIndex = ( f: (index: number, a: A) => TaskEither @@ -1204,27 +1280,30 @@ export const traverseArrayWithIndex = ( flow(T.traverseArrayWithIndex(f), T.map(E.sequenceArray)) /** - * Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => TaskEither ): ((as: ReadonlyArray) => TaskEither>) => traverseArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => TaskEither> = /*#__PURE__*/ traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => TaskEither) => ( as: ReadonlyArray @@ -1246,29 +1325,25 @@ export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => T ) /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const traverseSeqArray = ( f: (a: A) => TaskEither ): ((as: ReadonlyArray) => TaskEither>) => traverseSeqArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => TaskEither> = /*#__PURE__*/ traverseSeqArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** * Use small, specific instances instead. * diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index 49537374e..e9d94ee09 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -1,7 +1,6 @@ -import * as U from './util' import { sequenceT } from '../src/Apply' import * as E from '../src/Either' -import { flow, pipe } from '../src/function' +import { flow, pipe, SK } from '../src/function' import * as I from '../src/IO' import * as IE from '../src/IOEither' import * as N from '../src/number' @@ -10,10 +9,13 @@ import * as R from '../src/Reader' import * as RE from '../src/ReaderEither' import * as RT from '../src/ReaderTask' import * as _ from '../src/ReaderTaskEither' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' +import { left, right } from '../src/Separated' import * as S from '../src/string' import * as T from '../src/Task' import * as TE from '../src/TaskEither' -import { left, right } from '../src/Separated' +import * as U from './util' describe('ReaderTaskEither', () => { describe('pipeables', () => { @@ -480,42 +482,121 @@ describe('ReaderTaskEither', () => { ) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.ReaderTaskEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.ReaderTaskEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceArray)(undefined)(), E.right([1, 2])) - U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(undefined)(), E.left('a')) - U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(undefined)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) - }) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] - it('sequenceSeqArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.ReaderTaskEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.ReaderTaskEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceSeqArray)(undefined)(), E.right([1, 2])) - U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceSeqArray)(undefined)(), E.left('a')) - U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceSeqArray)(undefined)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(await pipe(RA.empty, f)(undefined)(), E.right(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(undefined)(), E.right(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(undefined)(), E.left('e')) + }) + + it('traverseReadonlyArrayWithIndexSeq', async () => { + const f = _.traverseReadonlyArrayWithIndexSeq((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(await pipe(RA.empty, f)(undefined)(), E.right(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(undefined)(), E.right(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(undefined)(), E.left('e')) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const right = (n: number): _.ReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.ReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual( + await pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndex(SK))(undefined)(), + E.right([1, 2]) + ) + U.deepStrictEqual( + await pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndex(SK))(undefined)(), + E.left('a') + ) + U.deepStrictEqual( + await pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndex(SK))(undefined)(), + E.left('b') + ) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceReadonlyArraySeq', async () => { + const log: Array = [] + const right = (n: number): _.ReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.ReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual( + await pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndexSeq(SK))(undefined)(), + E.right([1, 2]) + ) + U.deepStrictEqual( + await pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndexSeq(SK))(undefined)(), + E.left('a') + ) + U.deepStrictEqual( + await pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndexSeq(SK))(undefined)(), + E.left('b') + ) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.ReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.ReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceArray)(undefined)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(undefined)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(undefined)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceSeqArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.ReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.ReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceSeqArray)(undefined)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceSeqArray)(undefined)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceSeqArray)(undefined)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) }) it('getCompactable', async () => { diff --git a/test/TaskEither.ts b/test/TaskEither.ts index f56cd19fb..afb972281 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -2,7 +2,7 @@ import * as U from './util' import { sequenceT } from '../src/Apply' import * as RA from '../src/ReadonlyArray' import * as E from '../src/Either' -import { identity, pipe } from '../src/function' +import { identity, pipe, SK } from '../src/function' import * as I from '../src/IO' import * as IE from '../src/IOEither' import { monoidString } from '../src/Monoid' @@ -14,6 +14,7 @@ import * as TO from '../src/TaskOption' import * as _ from '../src/TaskEither' import * as S from '../src/string' import { left, right } from '../src/Separated' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' describe('TaskEither', () => { // ------------------------------------------------------------------------------------- @@ -563,42 +564,103 @@ describe('TaskEither', () => { ) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.TaskEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.TaskEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceArray)(), E.right([1, 2])) - U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(), E.left('a')) - U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) - }) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] - it('sequenceSeqArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.TaskEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.TaskEither => - _.leftIO(() => { - log.push(s) - return s - }) - U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceSeqArray)(), E.right([1, 2])) - U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceSeqArray)(), E.left('a')) - U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceSeqArray)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(await pipe(RA.empty, f)(), E.right(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(), E.right(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(), E.left('e')) + }) + + it('traverseReadonlyArrayWithIndexSeq', async () => { + const f = _.traverseReadonlyArrayWithIndexSeq((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(await pipe(RA.empty, f)(), E.right(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(), E.right(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(), E.left('e')) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const right = (n: number): _.TaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual(await pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndex(SK))(), E.right([1, 2])) + U.deepStrictEqual(await pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndex(SK))(), E.left('a')) + U.deepStrictEqual(await pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndex(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceReadonlyArraySeq', async () => { + const log: Array = [] + const right = (n: number): _.TaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual(await pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.right([1, 2])) + U.deepStrictEqual(await pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.left('a')) + U.deepStrictEqual(await pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndexSeq(SK))(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.TaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceArray)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceSeqArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.TaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.TaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(1), right(2)], _.sequenceSeqArray)(), E.right([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceSeqArray)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceSeqArray)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) }) it('match', async () => { From 021ce75619053fbed43a08bfeb11e80f1e5ece60 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 19:33:09 +0200 Subject: [PATCH 139/162] State - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 7 ++++ docs/modules/State.ts.md | 78 +++++++++++++++++++++++++++------------- src/ReaderTaskEither.ts | 16 ++++----- src/State.ts | 68 +++++++++++++++++++++++++---------- src/TaskEither.ts | 30 ++++------------ test/State.ts | 28 +++++++++++---- 6 files changed, 146 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3435aa26a..b67747372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,10 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - `Semigroup` - deprecate `semigroupVoid`, use `void` module instead. + - `State` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `Task` - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` @@ -318,6 +322,9 @@ high state of flux, you're at risk of it changing without notice. - `Set` - add `getUnionSemigroup` - add `getDifferenceMagma` + - `State` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `StateReaderTaskEither` - add `fromStateK` - add `chainStateK` diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index 7adcab45d..2ab884781 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -50,11 +50,13 @@ Added in v2.0.0 - [bindTo](#bindto) - [evaluate](#evaluate) - [execute](#execute) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~evalState~~](#evalstate) - [~~execState~~](#execstate) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -412,43 +414,33 @@ export declare const execute: (s: S) => (ma: State) => S Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. - -**Signature** - -```ts -export declare const sequenceArray: (arr: readonly State[]) => State -``` - -Added in v2.9.0 - -## traverseArray - -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => State) => (as: readonly A[]) => State +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: readonly A[]) => State ``` -Added in v2.9.0 +Added in v2.11.0 -## traverseArrayWithIndex +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => State -) => (as: readonly A[]) => State +) => (as: ReadonlyNonEmptyArray) => State> ``` -Added in v2.9.0 +Added in v2.11.0 ## ~~evalState~~ @@ -473,3 +465,41 @@ export declare const execState: (ma: State, s: S) => S ``` Added in v2.0.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly State[]) => State +``` + +Added in v2.9.0 + +## ~~traverseArrayWithIndex~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: readonly A[]) => State +``` + +Added in v2.9.0 + +## ~~traverseArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArray: (f: (a: A) => State) => (as: readonly A[]) => State +``` + +Added in v2.9.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 52350077b..fd1830995 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1424,10 +1424,9 @@ export const traverseReadonlyArrayWithIndexSeq = ( * @since 2.9.0 * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderTaskEither -): ((as: ReadonlyArray) => ReaderTaskEither>) => - flow(R.traverseArrayWithIndex(f), R.map(TE.sequenceArray)) +) => (as: ReadonlyArray) => ReaderTaskEither> = traverseReadonlyArrayWithIndex /** * Use `traverseReadonlyArrayWithIndex` instead. @@ -1437,7 +1436,8 @@ export const traverseArrayWithIndex = ( */ export const traverseArray = ( f: (a: A) => ReaderTaskEither -): ((as: ReadonlyArray) => ReaderTaskEither>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => ReaderTaskEither>) => + traverseReadonlyArrayWithIndex((_, a) => f(a)) /** * Use `traverseReadonlyArrayWithIndex` instead. @@ -1457,10 +1457,9 @@ export const sequenceArray: ( * @since 2.9.0 * @deprecated */ -export const traverseSeqArrayWithIndex = ( +export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => ReaderTaskEither -): ((as: ReadonlyArray) => ReaderTaskEither>) => - flow(R.traverseArrayWithIndex(f), R.map(TE.sequenceSeqArray)) +) => (as: ReadonlyArray) => ReaderTaskEither> = traverseReadonlyArrayWithIndexSeq /** * Use `traverseReadonlyArrayWithIndexSeq` instead. @@ -1470,7 +1469,8 @@ export const traverseSeqArrayWithIndex = ( */ export const traverseSeqArray = ( f: (a: A) => ReaderTaskEither -): ((as: ReadonlyArray) => ReaderTaskEither>) => traverseSeqArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => ReaderTaskEither>) => + traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** * Use `traverseReadonlyArrayWithIndexSeq` instead. diff --git a/src/State.ts b/src/State.ts index 2edbd328a..31423e596 100644 --- a/src/State.ts +++ b/src/State.ts @@ -8,7 +8,10 @@ import { FromState2 } from './FromState' import { identity, pipe } from './function' import { bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' import { Monad2 } from './Monad' +import { NonEmptyArray } from './NonEmptyArray' import { Pointed2 } from './Pointed' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' +import * as _ from './internal' // ------------------------------------------------------------------------------------- // model @@ -315,45 +318,72 @@ export const apS = // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. * - * @since 2.9.0 + * @since 2.11.0 */ -export const traverseArrayWithIndex = (f: (index: number, a: A) => State) => ( - as: ReadonlyArray -): State> => (s) => { - let lastState = s - const values = [] - for (let i = 0; i < as.length; i++) { - const [newValue, newState] = f(i, as[i])(lastState) - values.push(newValue) - lastState = newState +export const traverseReadonlyNonEmptyArrayWithIndex = (f: (index: number, a: A) => State) => ( + as: ReadonlyNonEmptyArray +): State> => (s) => { + const [b, s2] = f(0, _.head(as))(s) + const bs: NonEmptyArray = [b] + let out = s2 + for (let i = 1; i < as.length; i++) { + const [b, s2] = f(i, as[i])(out) + bs.push(b) + out = s2 } - return [values, lastState] + return [bs, out] } /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => State +): ((as: ReadonlyArray) => State>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : of(_.emptyReadonlyArray)) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated + */ +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: ReadonlyArray) => State> = traverseReadonlyArrayWithIndex + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * + * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => State -): ((as: ReadonlyArray) => State>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => State>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => State> = /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - /** * Use [`evaluate`](#evaluate) instead * diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 93897456d..4fcbcd1ab 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -1274,10 +1274,9 @@ export const traverseReadonlyArrayWithIndexSeq = ( * @since 2.9.0 * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => TaskEither -): ((as: ReadonlyArray) => TaskEither>) => - flow(T.traverseArrayWithIndex(f), T.map(E.sequenceArray)) +) => (as: ReadonlyArray) => TaskEither> = traverseReadonlyArrayWithIndex /** * Use `traverseReadonlyArrayWithIndex` instead. @@ -1287,7 +1286,7 @@ export const traverseArrayWithIndex = ( */ export const traverseArray = ( f: (a: A) => TaskEither -): ((as: ReadonlyArray) => TaskEither>) => traverseArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => TaskEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** * Use `traverseReadonlyArrayWithIndex` instead. @@ -1305,24 +1304,9 @@ export const sequenceArray: (arr: ReadonlyArray>) => Task * @since 2.9.0 * @deprecated */ -export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => TaskEither) => ( - as: ReadonlyArray -): TaskEither> => () => - as.reduce>>>( - (acc, a, i) => - acc.then((ebs) => - _.isLeft(ebs) - ? acc - : f(i, a)().then((eb) => { - if (_.isLeft(eb)) { - return eb - } - ebs.right.push(eb.right) - return ebs - }) - ), - Promise.resolve(_.right([])) - ) +export const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => TaskEither +) => (as: ReadonlyArray) => TaskEither> = traverseReadonlyArrayWithIndexSeq /** * Use `traverseReadonlyArrayWithIndexSeq` instead. @@ -1332,7 +1316,7 @@ export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => T */ export const traverseSeqArray = ( f: (a: A) => TaskEither -): ((as: ReadonlyArray) => TaskEither>) => traverseSeqArrayWithIndex((_, a) => f(a)) +): ((as: ReadonlyArray) => TaskEither>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** * Use `traverseReadonlyArrayWithIndexSeq` instead. diff --git a/test/State.ts b/test/State.ts index 4d10543f2..0cb3153b7 100644 --- a/test/State.ts +++ b/test/State.ts @@ -1,6 +1,8 @@ -import * as U from './util' import { pipe, tuple } from '../src/function' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' import * as _ from '../src/State' +import * as U from './util' describe('State', () => { describe('pipeables', () => { @@ -87,11 +89,23 @@ describe('State', () => { U.deepStrictEqual(pipe(_.of(1), _.bindTo('a'), _.apS('b', _.of('b')))(undefined), [{ a: 1, b: 'b' }, undefined]) }) - it('sequenceArray', () => { - const append = (n: number): _.State, number> => (s) => [n, [...s, n]] - U.deepStrictEqual(pipe([append(1), append(2)], _.sequenceArray)([]), [ - [1, 2], - [1, 2] - ]) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => _.of(a + i)) + U.deepStrictEqual(pipe(RA.empty, f)({}), [RA.empty, {}]) + U.deepStrictEqual(pipe(input, f)({}), [['a0', 'b1'], {}]) + }) + + // old + it('sequenceArray', () => { + const append = (n: number): _.State, number> => (s) => [n, [...s, n]] + // tslint:disable-next-line: deprecation + U.deepStrictEqual(pipe([append(1), append(2)], _.sequenceArray)([]), [ + [1, 2], + [1, 2] + ]) + }) }) }) From 33e29aee945b7ba807bf1ecf3896e1793902fbb3 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 19:42:56 +0200 Subject: [PATCH 140/162] StateReaderTaskEither - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` --- CHANGELOG.md | 6 ++ docs/modules/StateReaderTaskEither.ts.md | 84 +++++++++++----- src/StateReaderTaskEither.ts | 58 ++++++++--- test/StateReaderTaskEither.ts | 123 ++++++++++++++++------- 4 files changed, 194 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b67747372..c8ce1400a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,10 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - `StateReaderTaskEither` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `Task` - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` @@ -333,6 +337,8 @@ high state of flux, you're at risk of it changing without notice. - add `chainReaderKW` - add `chainFirstReaderK`, `chainFirstReaderKW` - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` - `string` - add `toUpperCase` - `struct` diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index a47a3b996..6649ed1f8 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -130,12 +130,14 @@ Added in v2.0.0 - [bindW](#bindw) - [evaluate](#evaluate) - [execute](#execute) - - [sequenceArray](#sequencearray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~evalState~~](#evalstate) - [~~execState~~](#execstate) - [~~run~~](#run) + - [~~sequenceArray~~](#sequencearray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) --- @@ -1447,47 +1449,33 @@ export declare const execute: ( Added in v2.8.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(Applicative)`. - -**Signature** - -```ts -export declare const sequenceArray: ( - arr: readonly StateReaderTaskEither[] -) => StateReaderTaskEither -``` - -Added in v2.9.0 - -## traverseArray - -Equivalent to `ReadonlyArray#traverse(Applicative)`. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => StateReaderTaskEither +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => StateReaderTaskEither ) => (as: readonly A[]) => StateReaderTaskEither ``` -Added in v2.9.0 +Added in v2.11.0 -## traverseArrayWithIndex +## traverseReadonlyNonEmptyArrayWithIndex -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => StateReaderTaskEither -) => (as: readonly A[]) => StateReaderTaskEither +) => (as: ReadonlyNonEmptyArray) => StateReaderTaskEither> ``` -Added in v2.9.0 +Added in v2.11.0 ## ~~evalState~~ @@ -1528,3 +1516,45 @@ export declare function run(ma: StateReaderTaskEither, s ``` Added in v2.0.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: ( + arr: readonly StateReaderTaskEither[] +) => StateReaderTaskEither +``` + +Added in v2.9.0 + +## ~~traverseArrayWithIndex~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => StateReaderTaskEither +) => (as: readonly A[]) => StateReaderTaskEither +``` + +Added in v2.9.0 + +## ~~traverseArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => StateReaderTaskEither +) => (as: readonly A[]) => StateReaderTaskEither +``` + +Added in v2.9.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 0be15433f..384e56ce8 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -52,12 +52,14 @@ import { MonadIO4 } from './MonadIO' import { MonadTask4 } from './MonadTask' import { MonadThrow4 } from './MonadThrow' import { NaturalTransformation14C, NaturalTransformation24, NaturalTransformation34 } from './NaturalTransformation' +import { NonEmptyArray } from './NonEmptyArray' import { URI as OURI } from './Option' import { Pointed4 } from './Pointed' import { Predicate } from './Predicate' import * as R from './Reader' import { URI as REURI } from './ReaderEither' import * as RTE from './ReaderTaskEither' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { State } from './State' import * as ST from './StateT' @@ -1135,20 +1137,20 @@ export const apSW: ( // ------------------------------------------------------------------------------------- /** - * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. * - * @since 2.9.0 + * @since 2.11.0 */ -export const traverseArrayWithIndex = ( +export const traverseReadonlyNonEmptyArrayWithIndex = ( f: (index: number, a: A) => StateReaderTaskEither -) => (as: ReadonlyArray): StateReaderTaskEither> => (s) => (r) => () => - as.reduce, S]>>>( +) => (as: ReadonlyNonEmptyArray): StateReaderTaskEither> => (s) => (r) => () => + _.tail(as).reduce, S]>>>( (acc, a, i) => acc.then((ebs) => _.isLeft(ebs) ? acc : f( - i, + i + 1, a )(ebs.right[1])(r)().then((eb) => { if (_.isLeft(eb)) { @@ -1160,23 +1162,53 @@ export const traverseArrayWithIndex = ( return ebs }) ), - Promise.resolve(_.right([[], s])) + f(0, _.head(as))(s)(r)().then(E.map(([b, s]) => [[b], s])) ) /** - * Equivalent to `ReadonlyArray#traverse(Applicative)`. + * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => StateReaderTaskEither +): ((as: ReadonlyArray) => StateReaderTaskEither>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : of(_.emptyReadonlyArray)) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * + * @since 2.9.0 + * @deprecated + */ +export const traverseArrayWithIndex: ( + f: (index: number, a: A) => StateReaderTaskEither +) => (as: ReadonlyArray) => StateReaderTaskEither> = traverseReadonlyArrayWithIndex + +/** + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const traverseArray = ( f: (a: A) => StateReaderTaskEither ): ((as: ReadonlyArray) => StateReaderTaskEither>) => - traverseArrayWithIndex((_, a) => f(a)) + traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(Applicative)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.9.0 + * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -1184,12 +1216,6 @@ export const sequenceArray: ( /*#__PURE__*/ traverseArray(identity) -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** * Use small, specific instances instead. * diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 1b0c179dc..7a5517f1a 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -1,13 +1,15 @@ import * as assert from 'assert' import * as A from '../src/Array' import * as E from '../src/Either' -import { pipe, tuple } from '../src/function' +import { pipe, SK, tuple } from '../src/function' import * as I from '../src/IO' import * as IE from '../src/IOEither' import * as O from '../src/Option' import * as R from '../src/Reader' import * as RE from '../src/ReaderEither' import * as RTE from '../src/ReaderTaskEither' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' import { State } from '../src/State' import * as _ from '../src/StateReaderTaskEither' import * as S from '../src/string' @@ -343,26 +345,92 @@ describe('StateReaderTaskEither', () => { ) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const right = (n: number): _.StateReaderTaskEither => - _.rightIO(() => { - log.push(n) - return n - }) - const left = (s: string): _.StateReaderTaskEither => - _.leftIO(() => { - log.push(s) - return s - }) - assert.deepStrictEqual( - await pipe([right(1), right(2)], _.sequenceArray)(undefined)(undefined)(), - E.right([[1, 2], undefined]) - ) - U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(undefined)(undefined)(), E.left('a')) - U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(undefined)(undefined)(), E.left('b')) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.right(a + i) : _.left('e'))) + U.deepStrictEqual(await pipe(RA.empty, f)(undefined)(undefined)(), E.right(tuple(RA.empty, undefined))) + U.deepStrictEqual(await pipe(input, f)(undefined)(undefined)(), E.right(tuple(['a0', 'b1'], undefined))) + U.deepStrictEqual(await pipe(['a', ''], f)(undefined)(undefined)(), E.left('e')) + const append = (_i: number, n: number): _.StateReaderTaskEither, {}, Error, void> => + _.modify((a) => [...a, n]) + U.deepStrictEqual( + await pipe( + [1, 2, 3], + _.traverseReadonlyArrayWithIndex(append), + _.map(() => undefined) + )([])({})(), + E.right(tuple(undefined, [1, 2, 3])) + ) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const right = (n: number): _.StateReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.StateReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + U.deepStrictEqual( + await pipe([right(1), right(2)], _.traverseReadonlyArrayWithIndex(SK))(undefined)(undefined)(), + E.right(tuple([1, 2], undefined)) + ) + U.deepStrictEqual( + await pipe([right(3), left('a')], _.traverseReadonlyArrayWithIndex(SK))(undefined)(undefined)(), + E.left('a') + ) + U.deepStrictEqual( + await pipe([left('b'), right(4)], _.traverseReadonlyArrayWithIndex(SK))(undefined)(undefined)(), + E.left('b') + ) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const right = (n: number): _.StateReaderTaskEither => + _.rightIO(() => { + log.push(n) + return n + }) + const left = (s: string): _.StateReaderTaskEither => + _.leftIO(() => { + log.push(s) + return s + }) + assert.deepStrictEqual( + // tslint:disable-next-line: deprecation + await pipe([right(1), right(2)], _.sequenceArray)(undefined)(undefined)(), + E.right([[1, 2], undefined]) + ) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([right(3), left('a')], _.sequenceArray)(undefined)(undefined)(), E.left('a')) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([left('b'), right(4)], _.sequenceArray)(undefined)(undefined)(), E.left('b')) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + it('#1486', async () => { + const append = (n: number): _.StateReaderTaskEither, {}, Error, void> => + _.modify((a) => [...a, n]) + U.deepStrictEqual( + await pipe( + [1, 2, 3], + // tslint:disable-next-line: deprecation + _.traverseArray(append), + _.map(() => undefined) + )([])({})(), + E.right(tuple(undefined, [1, 2, 3])) + ) + }) }) it('fromState', async () => { @@ -402,17 +470,4 @@ describe('StateReaderTaskEither', () => { const f = (e: Env) => _.of(e.count + 1) U.deepStrictEqual(await _.asksStateReaderTaskEither(f)({})(e)(), E.right(tuple(1, {}))) }) - - it('#1486', async () => { - const append = (n: number): _.StateReaderTaskEither, {}, Error, void> => - _.modify((a) => [...a, n]) - U.deepStrictEqual( - await pipe( - [1, 2, 3], - _.traverseArray(append), - _.map(() => undefined) - )([])({})(), - E.right(tuple(undefined, [1, 2, 3])) - ) - }) }) From 9d8246c95ed3736f703d0dfd929d531623fab6cc Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 May 2021 19:50:28 +0200 Subject: [PATCH 141/162] TaskOption - add `traverseReadonlyNonEmptyArrayWithIndex` - add `traverseReadonlyArrayWithIndex` - add `traverseReadonlyNonEmptyArrayWithIndexSeq` - add `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseArrayWithIndex` - deprecate `traverseArray` - deprecate `sequenceArray` - deprecate `traverseSeqArrayWithIndex` - deprecate `traverseSeqArray` - deprecate `sequenceSeqArray` --- CHANGELOG.md | 11 +++ docs/modules/TaskOption.ts.md | 121 ++++++++++++++++++++------ src/TaskOption.ts | 122 ++++++++++++++++++++------ test/TaskOption.ts | 157 ++++++++++++++++++++++++---------- 4 files changed, 314 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8ce1400a..4f9f34221 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,13 @@ high state of flux, you're at risk of it changing without notice. - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - `TaskOption` + - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` + - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` + - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `TaskEither` - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` @@ -364,6 +371,10 @@ high state of flux, you're at risk of it changing without notice. - add `fromTaskEither` (@thewilkybarkid) - add `Zero` instance - add `guard` constructor + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` - `TaskThese` - add `ApT` - add `traverseReadonlyNonEmptyArrayWithIndex` diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 4bf96370d..bc2997ee9 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -95,16 +95,21 @@ Added in v2.10.0 - [fromTask](#fromtask) - [fromTaskEither](#fromtaskeither) - [utils](#utils) + - [ApT](#apt) - [Do](#do) - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) - - [sequenceArray](#sequencearray) - - [sequenceSeqArray](#sequenceseqarray) - - [traverseArray](#traversearray) - - [traverseArrayWithIndex](#traversearraywithindex) - - [traverseSeqArray](#traverseseqarray) - - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) + - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) + - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) + - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) + - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [~~sequenceArray~~](#sequencearray) + - [~~sequenceSeqArray~~](#sequenceseqarray) + - [~~traverseArrayWithIndex~~](#traversearraywithindex) + - [~~traverseArray~~](#traversearray) + - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) + - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -881,6 +886,16 @@ Added in v2.11.0 # utils +## ApT + +**Signature** + +```ts +export declare const ApT: TaskOption +``` + +Added in v2.11.0 + ## Do **Signature** @@ -927,45 +942,89 @@ export declare const bindTo: (name: N) => (fa: TaskOption) => TaskOptio Added in v2.10.0 -## sequenceArray +## traverseReadonlyArrayWithIndex -Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (as: readonly TaskOption[]) => TaskOption +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => TaskOption +) => (as: readonly A[]) => TaskOption ``` -Added in v2.10.0 +Added in v2.11.0 -## sequenceSeqArray +## traverseReadonlyArrayWithIndexSeq -Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (as: readonly TaskOption[]) => TaskOption +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskOption +) => (as: readonly A[]) => TaskOption +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndex + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => TaskOption +) => (as: ReadonlyNonEmptyArray) => TaskOption> +``` + +Added in v2.11.0 + +## traverseReadonlyNonEmptyArrayWithIndexSeq + +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + +**Signature** + +```ts +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskOption +) => (as: ReadonlyNonEmptyArray) => TaskOption> +``` + +Added in v2.11.0 + +## ~~sequenceArray~~ + +Use `traverseReadonlyArrayWithIndex` instead. + +**Signature** + +```ts +export declare const sequenceArray: (as: readonly TaskOption[]) => TaskOption ``` Added in v2.10.0 -## traverseArray +## ~~sequenceSeqArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => TaskOption) => (as: readonly A[]) => TaskOption +export declare const sequenceSeqArray: (as: readonly TaskOption[]) => TaskOption ``` Added in v2.10.0 -## traverseArrayWithIndex +## ~~traverseArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** @@ -977,23 +1036,21 @@ export declare const traverseArrayWithIndex: ( Added in v2.10.0 -## traverseSeqArray +## ~~traverseArray~~ -Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndex` instead. **Signature** ```ts -export declare const traverseSeqArray: ( - f: (a: A) => TaskOption -) => (as: readonly A[]) => TaskOption +export declare const traverseArray: (f: (a: A) => TaskOption) => (as: readonly A[]) => TaskOption ``` Added in v2.10.0 -## traverseSeqArrayWithIndex +## ~~traverseSeqArrayWithIndex~~ -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** @@ -1004,3 +1061,17 @@ export declare const traverseSeqArrayWithIndex: ( ``` Added in v2.10.0 + +## ~~traverseSeqArray~~ + +Use `traverseReadonlyArrayWithIndexSeq` instead. + +**Signature** + +```ts +export declare const traverseSeqArray: ( + f: (a: A) => TaskOption +) => (as: readonly A[]) => TaskOption +``` + +Added in v2.10.0 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 76ed670d8..c072af3f6 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -23,17 +23,19 @@ import { FromTask1, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe } from './function' +import { flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1 } from './Functor' import * as _ from './internal' import { Monad1 } from './Monad' import { MonadIO1 } from './MonadIO' import { MonadTask1 } from './MonadTask' import { NaturalTransformation11, NaturalTransformation21 } from './NaturalTransformation' +import { NonEmptyArray } from './NonEmptyArray' import * as O from './Option' import * as OT from './OptionT' import { Pointed1 } from './Pointed' import { Predicate } from './Predicate' +import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Refinement } from './Refinement' import { Separated } from './Separated' import * as T from './Task' @@ -775,74 +777,138 @@ export const apS = /*#__PURE__*/ apS_(ApplyPar) +// ------------------------------------------------------------------------------------- +// sequence T +// ------------------------------------------------------------------------------------- + +/** + * @since 2.11.0 + */ +export const ApT: TaskOption = of(_.emptyReadonlyArray) + // ------------------------------------------------------------------------------------- // array utils // ------------------------------------------------------------------------------------- +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndex = ( + f: (index: number, a: A) => TaskOption +): ((as: ReadonlyNonEmptyArray) => TaskOption>) => + flow(T.traverseReadonlyNonEmptyArrayWithIndex(f), T.map(O.traverseReadonlyNonEmptyArrayWithIndex(SK))) + /** * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndex = ( + f: (index: number, a: A) => TaskOption +): ((as: ReadonlyArray) => TaskOption>) => { + const g = traverseReadonlyNonEmptyArrayWithIndex(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +/** + * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyNonEmptyArrayWithIndexSeq = (f: (index: number, a: A) => TaskOption) => ( + as: ReadonlyNonEmptyArray +): TaskOption> => () => + _.tail(as).reduce>>>( + (acc, a, i) => + acc.then((obs) => + _.isNone(obs) + ? acc + : f(i + 1, a)().then((ob) => { + if (_.isNone(ob)) { + return ob + } + obs.value.push(ob.value) + return obs + }) + ), + f(0, _.head(as))().then(O.map(_.singleton)) + ) + +/** + * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * + * @since 2.11.0 + */ +export const traverseReadonlyArrayWithIndexSeq = ( + f: (index: number, a: A) => TaskOption +): ((as: ReadonlyArray) => TaskOption>) => { + const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f) + return (as) => (_.isNonEmpty(as) ? g(as) : ApT) +} + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + +/** + * Use `traverseReadonlyArrayWithIndex` instead. + * * @since 2.10.0 + * @deprecated */ -export const traverseArrayWithIndex = ( +export const traverseArrayWithIndex: ( f: (index: number, a: A) => TaskOption -): ((as: ReadonlyArray) => TaskOption>) => flow(T.traverseArrayWithIndex(f), T.map(O.sequenceArray)) +) => (as: ReadonlyArray) => TaskOption> = traverseReadonlyArrayWithIndex /** - * Equivalent to `ReadonlyArray#traverse(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.10.0 + * @deprecated */ export const traverseArray: ( f: (a: A) => TaskOption -) => (as: ReadonlyArray) => TaskOption> = (f) => traverseArrayWithIndex((_, a) => f(a)) +) => (as: ReadonlyArray) => TaskOption> = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativePar)`. + * Use `traverseReadonlyArrayWithIndex` instead. * * @since 2.10.0 + * @deprecated */ export const sequenceArray: (as: ReadonlyArray>) => TaskOption> = /*#__PURE__*/ traverseArray(identity) /** - * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ -export const traverseSeqArrayWithIndex = (f: (index: number, a: A) => TaskOption) => ( - as: ReadonlyArray -): TaskOption> => () => - as.reduce>>>( - (acc, a, i) => - acc.then((obs) => - O.isNone(obs) - ? acc - : f(i, a)().then((ob) => { - if (O.isNone(ob)) { - return ob - } - obs.value.push(ob.value) - return obs - }) - ), - Promise.resolve(O.some([])) - ) +export const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => TaskOption +) => (as: ReadonlyArray) => TaskOption> = traverseReadonlyArrayWithIndexSeq /** - * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ export const traverseSeqArray: ( f: (a: A) => TaskOption -) => (as: ReadonlyArray) => TaskOption> = (f) => traverseSeqArrayWithIndex((_, a) => f(a)) +) => (as: ReadonlyArray) => TaskOption> = (f) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`. + * Use `traverseReadonlyArrayWithIndexSeq` instead. * * @since 2.10.0 + * @deprecated */ export const sequenceSeqArray: (as: ReadonlyArray>) => TaskOption> = /*#__PURE__*/ diff --git a/test/TaskOption.ts b/test/TaskOption.ts index d244d4855..1f29c0496 100644 --- a/test/TaskOption.ts +++ b/test/TaskOption.ts @@ -1,9 +1,11 @@ -import * as U from './util' -import { pipe } from '../src/function' +import { pipe, SK } from '../src/function' import * as O from '../src/Option' +import * as RA from '../src/ReadonlyArray' +import { ReadonlyNonEmptyArray } from '../src/ReadonlyNonEmptyArray' import * as T from '../src/Task' import * as TE from '../src/TaskEither' import * as _ from '../src/TaskOption' +import * as U from './util' describe('TaskOption', () => { // ------------------------------------------------------------------------------------- @@ -175,48 +177,115 @@ describe('TaskOption', () => { U.deepStrictEqual(await f(_.none)(), O.none) }) - it('sequenceArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const some = (n: number): _.TaskOption => - _.fromIO(() => { - log.push(n) - return n - }) - const none = (s: string): _.TaskOption => - pipe( - T.fromIO(() => { - log.push(s) - return s - }), - T.map(() => O.none) - ) - U.deepStrictEqual(await pipe([some(1), some(2)], _.sequenceArray)(), O.some([1, 2])) - U.deepStrictEqual(await pipe([some(3), none('a')], _.sequenceArray)(), O.none) - U.deepStrictEqual(await pipe([none('b'), some(4)], _.sequenceArray)(), O.none) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) - }) - - it('sequenceSeqArray', async () => { - // tslint:disable-next-line: readonly-array - const log: Array = [] - const some = (n: number): _.TaskOption => - _.fromIO(() => { - log.push(n) - return n - }) - const none = (s: string): _.TaskOption => - pipe( - T.fromIO(() => { - log.push(s) - return s - }), - T.map(() => O.none) - ) - U.deepStrictEqual(await pipe([some(1), some(2)], _.sequenceSeqArray)(), O.some([1, 2])) - U.deepStrictEqual(await pipe([some(3), none('a')], _.sequenceSeqArray)(), O.none) - U.deepStrictEqual(await pipe([none('b'), some(4)], _.sequenceSeqArray)(), O.none) - U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + describe('array utils', () => { + const input: ReadonlyNonEmptyArray = ['a', 'b'] + + it('traverseReadonlyArrayWithIndex', async () => { + const f = _.traverseReadonlyArrayWithIndex((i, a: string) => (a.length > 0 ? _.some(a + i) : _.none)) + U.deepStrictEqual(await pipe(RA.empty, f)(), O.some(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(), O.some(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(), O.none) + }) + + it('traverseReadonlyArrayWithIndexSeq', async () => { + const f = _.traverseReadonlyArrayWithIndexSeq((i, a: string) => (a.length > 0 ? _.some(a + i) : _.none)) + U.deepStrictEqual(await pipe(RA.empty, f)(), O.some(RA.empty)) + U.deepStrictEqual(await pipe(input, f)(), O.some(['a0', 'b1'])) + U.deepStrictEqual(await pipe(['a', ''], f)(), O.none) + }) + + it('sequenceReadonlyArray', async () => { + const log: Array = [] + const some = (n: number): _.TaskOption => + _.fromIO(() => { + log.push(n) + return n + }) + const none = (s: string): _.TaskOption => + pipe( + T.fromIO(() => { + log.push(s) + return s + }), + T.map(() => O.none) + ) + U.deepStrictEqual(await pipe([some(1), some(2)], _.traverseReadonlyArrayWithIndex(SK))(), O.some([1, 2])) + U.deepStrictEqual(await pipe([some(3), none('a')], _.traverseReadonlyArrayWithIndex(SK))(), O.none) + U.deepStrictEqual(await pipe([none('b'), some(4)], _.traverseReadonlyArrayWithIndex(SK))(), O.none) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceReadonlyArraySeq', async () => { + const log: Array = [] + const some = (n: number): _.TaskOption => + _.fromIO(() => { + log.push(n) + return n + }) + const none = (s: string): _.TaskOption => + pipe( + T.fromIO(() => { + log.push(s) + return s + }), + T.map(() => O.none) + ) + U.deepStrictEqual(await pipe([some(1), some(2)], _.traverseReadonlyArrayWithIndexSeq(SK))(), O.some([1, 2])) + U.deepStrictEqual(await pipe([some(3), none('a')], _.traverseReadonlyArrayWithIndexSeq(SK))(), O.none) + U.deepStrictEqual(await pipe([none('b'), some(4)], _.traverseReadonlyArrayWithIndexSeq(SK))(), O.none) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) + + // old + it('sequenceArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const some = (n: number): _.TaskOption => + _.fromIO(() => { + log.push(n) + return n + }) + const none = (s: string): _.TaskOption => + pipe( + T.fromIO(() => { + log.push(s) + return s + }), + T.map(() => O.none) + ) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([some(1), some(2)], _.sequenceArray)(), O.some([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([some(3), none('a')], _.sequenceArray)(), O.none) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([none('b'), some(4)], _.sequenceArray)(), O.none) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b', 4]) + }) + + it('sequenceSeqArray', async () => { + // tslint:disable-next-line: readonly-array + const log: Array = [] + const some = (n: number): _.TaskOption => + _.fromIO(() => { + log.push(n) + return n + }) + const none = (s: string): _.TaskOption => + pipe( + T.fromIO(() => { + log.push(s) + return s + }), + T.map(() => O.none) + ) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([some(1), some(2)], _.sequenceSeqArray)(), O.some([1, 2])) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([some(3), none('a')], _.sequenceSeqArray)(), O.none) + // tslint:disable-next-line: deprecation + U.deepStrictEqual(await pipe([none('b'), some(4)], _.sequenceSeqArray)(), O.none) + U.deepStrictEqual(log, [1, 2, 3, 'a', 'b']) + }) }) it('tryCatchK', async () => { From 59f91666d5127d99ae6c7e7efa6df28cf2c825e1 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 6 May 2021 15:55:43 +0200 Subject: [PATCH 142/162] remove deprecation comments when possible --- perf/Either/sequenceArray.ts | 1 + perf/IO/sequenceArray.ts | 1 + perf/Option/sequenceArray.ts | 1 + ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- perf/StateReaderTaskEither/sequenceArray.ts | 1 + perf/Task/sequenceArray.ts | 1 + perf/Task/stack.ts | 1 + ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- ...s => traverseReadonlyArrayWithIndexSeq.ts} | 4 +- scripts/build.ts | 4 +- src/Array.ts | 4 +- src/Bounded.ts | 4 +- src/Date.ts | 16 +++- src/Json.ts | 15 ++-- src/Monoid.ts | 8 +- src/NonEmptyArray.ts | 3 +- src/Ord.ts | 3 +- src/ReadonlyArray.ts | 9 +-- src/ReadonlyNonEmptyArray.ts | 3 +- src/ReadonlyRecord.ts | 4 +- src/Record.ts | 5 +- src/boolean.ts | 54 ++++++++----- src/function.ts | 4 +- src/number.ts | 78 ++++++++++++------- src/string.ts | 27 ++++--- src/struct.ts | 7 +- test/BooleanAlgebra.ts | 23 ++++++ test/Eq.ts | 8 ++ test/Field.ts | 19 ++++- test/Ord.ts | 8 ++ test/Semigroup.ts | 45 +++++++++++ test/Show.ts | 17 ++++ 35 files changed, 287 insertions(+), 111 deletions(-) rename perf/ReaderTask/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (89%) rename perf/ReaderTaskEither/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (89%) rename perf/Task/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (88%) rename perf/TaskEither/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (88%) rename perf/TaskOption.ts/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (88%) rename perf/TaskOption/{traverseSeqArrayWithIndex.ts => traverseReadonlyArrayWithIndexSeq.ts} (88%) diff --git a/perf/Either/sequenceArray.ts b/perf/Either/sequenceArray.ts index 710825ec0..7c071ce43 100644 --- a/perf/Either/sequenceArray.ts +++ b/perf/Either/sequenceArray.ts @@ -18,6 +18,7 @@ suite pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { + // tslint:disable-next-line: deprecation pipe(as, _.sequenceArray) }) .on('cycle', function (event: any) { diff --git a/perf/IO/sequenceArray.ts b/perf/IO/sequenceArray.ts index 7cdaf6915..44c88e5cd 100644 --- a/perf/IO/sequenceArray.ts +++ b/perf/IO/sequenceArray.ts @@ -18,6 +18,7 @@ suite pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { + // tslint:disable-next-line: deprecation pipe(as, _.sequenceArray) }) .on('cycle', function (event: any) { diff --git a/perf/Option/sequenceArray.ts b/perf/Option/sequenceArray.ts index 7e15baa0d..bb3b2a672 100644 --- a/perf/Option/sequenceArray.ts +++ b/perf/Option/sequenceArray.ts @@ -18,6 +18,7 @@ suite return pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { + // tslint:disable-next-line: deprecation return pipe(as, _.sequenceArray) }) .on('cycle', function (event: any) { diff --git a/perf/ReaderTask/traverseSeqArrayWithIndex.ts b/perf/ReaderTask/traverseReadonlyArrayWithIndexSeq.ts similarity index 89% rename from perf/ReaderTask/traverseSeqArrayWithIndex.ts rename to perf/ReaderTask/traverseReadonlyArrayWithIndexSeq.ts index 99c1798d0..73a9b37cb 100644 --- a/perf/ReaderTask/traverseSeqArrayWithIndex.ts +++ b/perf/ReaderTask/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )(undefined)() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )(undefined)() }) .on('cycle', function (event: any) { diff --git a/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts b/perf/ReaderTaskEither/traverseReadonlyArrayWithIndexSeq.ts similarity index 89% rename from perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts rename to perf/ReaderTaskEither/traverseReadonlyArrayWithIndexSeq.ts index 3e03076d3..f348fc6f1 100644 --- a/perf/ReaderTaskEither/traverseSeqArrayWithIndex.ts +++ b/perf/ReaderTaskEither/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )(undefined)() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )(undefined)() }) .on('cycle', function (event: any) { diff --git a/perf/StateReaderTaskEither/sequenceArray.ts b/perf/StateReaderTaskEither/sequenceArray.ts index 6b5b976d6..7a45b8736 100644 --- a/perf/StateReaderTaskEither/sequenceArray.ts +++ b/perf/StateReaderTaskEither/sequenceArray.ts @@ -18,6 +18,7 @@ suite pipe(as, RNEA.sequence(_.Applicative)) }) .add('_.sequenceArray', function () { + // tslint:disable-next-line: deprecation pipe(as, _.sequenceArray) }) .on('cycle', function (event: any) { diff --git a/perf/Task/sequenceArray.ts b/perf/Task/sequenceArray.ts index c97d0f68a..34a8f528f 100644 --- a/perf/Task/sequenceArray.ts +++ b/perf/Task/sequenceArray.ts @@ -18,6 +18,7 @@ suite return pipe(as, RNEA.sequence(_.ApplicativePar))() }) .add('_.sequenceArray', function () { + // tslint:disable-next-line: deprecation return pipe(as, _.sequenceArray)() }) .on('cycle', function (event: any) { diff --git a/perf/Task/stack.ts b/perf/Task/stack.ts index 86be2da38..f9e11010b 100644 --- a/perf/Task/stack.ts +++ b/perf/Task/stack.ts @@ -7,6 +7,7 @@ const as = RNEA.range(0, 100000) // tslint:disable-next-line: no-floating-promises pipe( as, + // tslint:disable-next-line: deprecation _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) // tslint:disable-next-line: no-console )().then((as) => console.log(as.length)) diff --git a/perf/Task/traverseSeqArrayWithIndex.ts b/perf/Task/traverseReadonlyArrayWithIndexSeq.ts similarity index 88% rename from perf/Task/traverseSeqArrayWithIndex.ts rename to perf/Task/traverseReadonlyArrayWithIndexSeq.ts index 3795930d6..976e117d1 100644 --- a/perf/Task/traverseSeqArrayWithIndex.ts +++ b/perf/Task/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )() }) .on('cycle', function (event: any) { diff --git a/perf/TaskEither/traverseSeqArrayWithIndex.ts b/perf/TaskEither/traverseReadonlyArrayWithIndexSeq.ts similarity index 88% rename from perf/TaskEither/traverseSeqArrayWithIndex.ts rename to perf/TaskEither/traverseReadonlyArrayWithIndexSeq.ts index 7e1832509..f380e4596 100644 --- a/perf/TaskEither/traverseSeqArrayWithIndex.ts +++ b/perf/TaskEither/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )() }) .on('cycle', function (event: any) { diff --git a/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts b/perf/TaskOption.ts/traverseReadonlyArrayWithIndexSeq.ts similarity index 88% rename from perf/TaskOption.ts/traverseSeqArrayWithIndex.ts rename to perf/TaskOption.ts/traverseReadonlyArrayWithIndexSeq.ts index e997da572..0fd892612 100644 --- a/perf/TaskOption.ts/traverseSeqArrayWithIndex.ts +++ b/perf/TaskOption.ts/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )() }) .on('cycle', function (event: any) { diff --git a/perf/TaskOption/traverseSeqArrayWithIndex.ts b/perf/TaskOption/traverseReadonlyArrayWithIndexSeq.ts similarity index 88% rename from perf/TaskOption/traverseSeqArrayWithIndex.ts rename to perf/TaskOption/traverseReadonlyArrayWithIndexSeq.ts index e997da572..0fd892612 100644 --- a/perf/TaskOption/traverseSeqArrayWithIndex.ts +++ b/perf/TaskOption/traverseReadonlyArrayWithIndexSeq.ts @@ -20,10 +20,10 @@ suite RNEA.traverseWithIndex(_.ApplicativeSeq)((_i, a) => _.of(a)) )() }) - .add('_.traverseSeqArrayWithIndex', function () { + .add('_.traverseReadonlyArrayWithIndexSeq', function () { return pipe( as, - _.traverseSeqArrayWithIndex((_i, a) => _.of(a)) + _.traverseReadonlyArrayWithIndexSeq((_i, a) => _.of(a)) )() }) .on('cycle', function (event: any) { diff --git a/scripts/build.ts b/scripts/build.ts index 5e311cf25..67d1d2fe3 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -33,7 +33,7 @@ export const FILES: ReadonlyArray = ['CHANGELOG.md', 'LICENSE', 'README. export const copyFiles: Build> = (C) => pipe( FILES, - TE.traverseArrayWithIndex((_, from) => C.copyFile(from, path.resolve(OUTPUT_FOLDER, from))) + TE.traverseReadonlyArrayWithIndex((_, from) => C.copyFile(from, path.resolve(OUTPUT_FOLDER, from))) ) export const makeModules: Build = (C) => { @@ -41,7 +41,7 @@ export const makeModules: Build = (C) => { return pipe( C.glob(`${OUTPUT_FOLDER}/lib/*.js`), TE.map(getModules), - TE.chain(TE.traverseArrayWithIndex((_, a) => makeSingleModuleC(a))), + TE.chain(TE.traverseReadonlyArrayWithIndex((_, a) => makeSingleModuleC(a))), TE.map(() => undefined) ) } diff --git a/src/Array.ts b/src/Array.ts index aa25c0b5f..963d18ba1 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -2277,6 +2277,8 @@ export const apS = // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use `NonEmptyArray` module instead. * @@ -2301,7 +2303,6 @@ export const empty: Array = [] * @since 2.0.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const cons = NEA.cons /** @@ -2311,7 +2312,6 @@ export const cons = NEA.cons * @since 2.0.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const snoc = NEA.snoc /** diff --git a/src/Bounded.ts b/src/Bounded.ts index 79d97ca85..2b286a2a7 100644 --- a/src/Bounded.ts +++ b/src/Bounded.ts @@ -26,6 +26,8 @@ export interface Bounded extends Ord { // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use [`Bounded`](./number.ts.html#Bounded) instead. * @@ -34,9 +36,7 @@ export interface Bounded extends Ord { * @deprecated */ export const boundedNumber: Bounded = { - // tslint:disable-next-line: deprecation equals: ordNumber.equals, - // tslint:disable-next-line: deprecation compare: ordNumber.compare, top: Infinity, bottom: -Infinity diff --git a/src/Date.ts b/src/Date.ts index dc03bb3d1..95dfb449e 100644 --- a/src/Date.ts +++ b/src/Date.ts @@ -2,8 +2,10 @@ * @since 2.0.0 */ import * as E from './Eq' +import { pipe } from './function' import { IO } from './IO' import * as O from './Ord' +import * as N from './number' // ------------------------------------------------------------------------------------- // instances @@ -13,8 +15,9 @@ import * as O from './Ord' * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Eq: E.Eq = E.eqDate +export const Eq: E.Eq = { + equals: (first, second) => first.valueOf() === second.valueOf() +} /** * @category instances @@ -49,8 +52,13 @@ export const eqYear: E.Eq = { * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Ord: O.Ord = O.ordDate +export const Ord: O.Ord = + /*#__PURE__*/ + pipe( + N.Ord, + /*#__PURE__*/ + O.contramap((date) => date.valueOf()) + ) // ------------------------------------------------------------------------------------- // utils diff --git a/src/Json.ts b/src/Json.ts index fd2acbbb8..4a188af53 100644 --- a/src/Json.ts +++ b/src/Json.ts @@ -1,7 +1,7 @@ /** * @since 2.10.0 */ -import { Either, parseJSON, stringifyJSON } from './Either' +import { Either, tryCatch } from './Either' import { identity } from './function' /** @@ -34,8 +34,7 @@ export interface JsonArray extends ReadonlyArray {} * * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const parse = (s: string): Either => parseJSON(s, identity) +export const parse = (s: string): Either => tryCatch(() => JSON.parse(s), identity) /** * Converts a JavaScript value to a JavaScript Object Notation (JSON) string. @@ -58,5 +57,11 @@ export const parse = (s: string): Either => parseJSON(s, identity * * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const stringify = (a: A): Either => stringifyJSON(a, identity) +export const stringify = (a: A): Either => + tryCatch(() => { + const s = JSON.stringify(a) + if (typeof s !== 'string') { + throw new Error('Converting unsupported structure to JSON') + } + return s + }, identity) diff --git a/src/Monoid.ts b/src/Monoid.ts index 301605277..2e35e80cf 100644 --- a/src/Monoid.ts +++ b/src/Monoid.ts @@ -202,6 +202,8 @@ export const concatAll = (M: Monoid): ((as: ReadonlyArray) => A) => Se. // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use [`Monoid`](./void.ts.html#monoid) instead. * @@ -210,7 +212,6 @@ export const concatAll = (M: Monoid): ((as: ReadonlyArray) => A) => Se. * @deprecated */ export const monoidVoid: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupVoid.concat, empty: undefined } @@ -280,7 +281,6 @@ export const fold = concatAll * @deprecated */ export const monoidAll: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupAll.concat, empty: true } @@ -293,7 +293,6 @@ export const monoidAll: Monoid = { * @deprecated */ export const monoidAny: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupAny.concat, empty: false } @@ -326,7 +325,6 @@ export const getEndomorphismMonoid = (): Monoid> => r * @deprecated */ export const monoidString: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupString.concat, empty: '' } @@ -339,7 +337,6 @@ export const monoidString: Monoid = { * @deprecated */ export const monoidSum: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupSum.concat, empty: 0 } @@ -352,7 +349,6 @@ export const monoidSum: Monoid = { * @deprecated */ export const monoidProduct: Monoid = { - // tslint:disable-next-line: deprecation concat: Se.semigroupProduct.concat, empty: 1 } diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 93cc79c5a..3276a1edd 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -1220,6 +1220,8 @@ export const updateLast = (a: A): ((as: NonEmptyArray) => NonEmptyArray // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * This is just `sort` followed by `group`. * @@ -1249,7 +1251,6 @@ export function groupSort(O: Ord): (as: Array) => Array(refinement: Refinement): (as: NonEmptyArray) => Option> export function filter(predicate: Predicate): (as: NonEmptyArray) => Option> export function filter(predicate: Predicate): (as: NonEmptyArray) => Option> { - // tslint:disable-next-line: deprecation return filterWithIndex((_, a) => predicate(a)) } diff --git a/src/Ord.ts b/src/Ord.ts index 9c5d4547a..1695bb3f3 100644 --- a/src/Ord.ts +++ b/src/Ord.ts @@ -318,6 +318,8 @@ export const between = (O: Ord): ((low: A, hi: A) => (a: A) => boolean) => // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use [`tuple`](#tuple) instead. * @@ -394,7 +396,6 @@ export const ordNumber: Ord = strictOrd export const ordDate: Ord = /*#__PURE__*/ pipe( - // tslint:disable-next-line: deprecation ordNumber, /*#__PURE__*/ contramap((date) => date.valueOf()) diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 4e1c9a3ce..af98b9e9a 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -776,9 +776,8 @@ export const findLastIndex = (predicate: Predicate) => (as: ReadonlyArray< * * @since 2.5.0 */ -export const insertAt: (i: number, a: A) => (as: ReadonlyArray) => Option> = - // tslint:disable-next-line: deprecation - RNEA.insertAt +export const insertAt = (i: number, a: A) => (as: ReadonlyArray): Option> => + i < 0 || i > as.length ? _.none : _.some(RNEA.unsafeInsertAt(i, a, as)) /** * Change the element at the specified index, creating a new array, or returning `None` if the index is out of bounds @@ -2507,6 +2506,8 @@ export const apS = // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use `ReadonlyNonEmptyArray` module instead. * @@ -2523,7 +2524,6 @@ export const range = RNEA.range * @since 2.5.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const cons = RNEA.cons /** @@ -2533,7 +2533,6 @@ export const cons = RNEA.cons * @since 2.5.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const snoc = RNEA.snoc /** diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index de89f672e..1c716beab 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -1282,6 +1282,8 @@ export const updateLast = (a: A): ((as: ReadonlyNonEmptyArray) => Readonly // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * This is just `sort` followed by `group`. * @@ -1313,7 +1315,6 @@ export function filter( ): (as: ReadonlyNonEmptyArray) => Option> export function filter(predicate: Predicate): (as: ReadonlyNonEmptyArray) => Option> export function filter(predicate: Predicate): (as: ReadonlyNonEmptyArray) => Option> { - // tslint:disable-next-line: deprecation return filterWithIndex((_, a) => predicate(a)) } diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 5d6565213..9ac409da0 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1496,6 +1496,8 @@ export const getDifferenceMagma = (): Magma> => ({ // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use `getFoldable` instead. * @@ -1566,9 +1568,7 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex(S.Ord) } -// tslint:disable-next-line: deprecation const _wither = witherDefault(Traversable, Compactable) -// tslint:disable-next-line: deprecation const _wilt = wiltDefault(Traversable, Compactable) /** diff --git a/src/Record.ts b/src/Record.ts index 0a3236aac..85c9759d2 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -1005,6 +1005,8 @@ export const getDifferenceMagma = (): Magma> => ({ // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use `getFoldable` instead. * @@ -1075,9 +1077,7 @@ export const TraversableWithIndex: TraversableWithIndex1 = { traverseWithIndex: _traverseWithIndex(S.Ord) } -// tslint:disable-next-line: deprecation const _wither = witherDefault(Traversable, Compactable) -// tslint:disable-next-line: deprecation const _wilt = wiltDefault(Traversable, Compactable) /** @@ -1127,7 +1127,6 @@ export const insertAt: (k: string, a: A) => (r: Record) => Record< * @since 2.0.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const hasOwnProperty: (k: string, r: Record) => k is K = RR.hasOwnProperty /** diff --git a/src/boolean.ts b/src/boolean.ts index c189be2d4..8411912b8 100644 --- a/src/boolean.ts +++ b/src/boolean.ts @@ -3,12 +3,12 @@ */ import * as BA from './BooleanAlgebra' import * as E from './Eq' -import { Semigroup, semigroupAll, semigroupAny } from './Semigroup' import { Lazy } from './function' -import { Monoid, monoidAll, monoidAny } from './Monoid' +import { Monoid } from './Monoid' import * as O from './Ord' -import * as S from './Show' import { Refinement } from './Refinement' +import { Semigroup } from './Semigroup' +import * as S from './Show' // ------------------------------------------------------------------------------------- // refinements @@ -80,15 +80,22 @@ export const fold = match * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Eq: E.Eq = E.eqBoolean +export const Eq: E.Eq = { + equals: (first, second) => first === second +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const BooleanAlgebra: BA.BooleanAlgebra = BA.booleanAlgebraBoolean +export const BooleanAlgebra: BA.BooleanAlgebra = { + meet: (first, second) => first && second, + join: (first, second) => first || second, + zero: false, + one: true, + implies: (first, second) => !first || second, + not: (b) => !b +} /** * `boolean` semigroup under conjunction. @@ -102,8 +109,9 @@ export const BooleanAlgebra: BA.BooleanAlgebra = BA.booleanAlgebraBoole * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const SemigroupAll: Semigroup = semigroupAll +export const SemigroupAll: Semigroup = { + concat: (first, second) => first && second +} /** * `boolean` semigroup under disjunction. @@ -118,8 +126,9 @@ export const SemigroupAll: Semigroup = semigroupAll * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const SemigroupAny: Semigroup = semigroupAny +export const SemigroupAny: Semigroup = { + concat: (first, second) => first || second +} /** * `boolean` monoid under conjunction. @@ -135,8 +144,10 @@ export const SemigroupAny: Semigroup = semigroupAny * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const MonoidAll: Monoid = monoidAll +export const MonoidAll: Monoid = { + concat: SemigroupAll.concat, + empty: true +} /** * `boolean` monoid under disjunction. @@ -153,19 +164,24 @@ export const MonoidAll: Monoid = monoidAll * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const MonoidAny: Monoid = monoidAny +export const MonoidAny: Monoid = { + concat: SemigroupAny.concat, + empty: false +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Ord: O.Ord = O.ordBoolean +export const Ord: O.Ord = { + equals: Eq.equals, + compare: (first, second) => (first < second ? -1 : first > second ? 1 : 0) +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Show: S.Show = S.showBoolean +export const Show: S.Show = { + show: (b) => JSON.stringify(b) +} diff --git a/src/function.ts b/src/function.ts index 7a29140ae..db756f123 100644 --- a/src/function.ts +++ b/src/function.ts @@ -725,6 +725,8 @@ export const SK = (_: A, b: B): B => b // deprecated // ------------------------------------------------------------------------------------- +// tslint:disable: deprecation + /** * Use `Refinement` module instead. * @@ -751,7 +753,6 @@ export interface Predicate { * @since 2.0.0 * @deprecated */ -// tslint:disable-next-line: deprecation export function not(predicate: Predicate): Predicate { return (a) => !predicate(a) } @@ -773,7 +774,6 @@ export interface Endomorphism { * @since 2.10.0 * @deprecated */ -// tslint:disable-next-line: deprecation export const getEndomorphismMonoid = (): Monoid> => ({ concat: (first, second) => flow(first, second), empty: identity diff --git a/src/number.ts b/src/number.ts index f70405bde..5ed8aef9c 100644 --- a/src/number.ts +++ b/src/number.ts @@ -4,12 +4,12 @@ import * as B from './Bounded' import * as E from './Eq' import * as F from './Field' -import * as O from './Ord' -import * as S from './Show' -import { Semigroup, semigroupProduct, semigroupSum } from './Semigroup' -import { Monoid, monoidProduct, monoidSum } from './Monoid' import { Magma } from './Magma' +import { Monoid } from './Monoid' +import * as O from './Ord' import { Refinement } from './Refinement' +import { Semigroup } from './Semigroup' +import * as S from './Show' // ------------------------------------------------------------------------------------- // refinements @@ -29,43 +29,44 @@ export const isNumber: Refinement = (u: unknown): u is number = * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Eq: E.Eq = E.eqNumber - -/** - * @category instances - * @since 2.10.0 - */ -// tslint:disable-next-line: deprecation -export const Ord: O.Ord = O.ordNumber +export const Eq: E.Eq = { + equals: (first, second) => first === second +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Bounded: B.Bounded = B.boundedNumber +export const Ord: O.Ord = { + equals: Eq.equals, + compare: (first, second) => (first < second ? -1 : first > second ? 1 : 0) +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Field: F.Field = F.fieldNumber +export const Bounded: B.Bounded = { + equals: Eq.equals, + compare: Ord.compare, + top: Infinity, + bottom: -Infinity +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Show: S.Show = S.showNumber +export const Show: S.Show = { + show: (n) => JSON.stringify(n) +} /** * @category instances * @since 2.11.0 */ export const MagmaSub: Magma = { - concat: Field.sub + concat: (first, second) => first - second } /** @@ -79,8 +80,9 @@ export const MagmaSub: Magma = { * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const SemigroupSum: Semigroup = semigroupSum +export const SemigroupSum: Semigroup = { + concat: (first, second) => first + second +} /** * `number` semigroup under multiplication. @@ -93,8 +95,9 @@ export const SemigroupSum: Semigroup = semigroupSum * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const SemigroupProduct: Semigroup = semigroupProduct +export const SemigroupProduct: Semigroup = { + concat: (first, second) => first * second +} /** * `number` monoid under addition. @@ -109,8 +112,10 @@ export const SemigroupProduct: Semigroup = semigroupProduct * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const MonoidSum: Monoid = monoidSum +export const MonoidSum: Monoid = { + concat: SemigroupSum.concat, + empty: 0 +} /** * `number` monoid under multiplication. @@ -125,5 +130,22 @@ export const MonoidSum: Monoid = monoidSum * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const MonoidProduct: Monoid = monoidProduct +export const MonoidProduct: Monoid = { + concat: SemigroupProduct.concat, + empty: 1 +} + +/** + * @category instances + * @since 2.10.0 + */ +export const Field: F.Field = { + add: SemigroupSum.concat, + zero: 0, + mul: SemigroupProduct.concat, + one: 1, + sub: MagmaSub.concat, + degree: (_) => 1, + div: (first, second) => first / second, + mod: (first, second) => first % second +} diff --git a/src/string.ts b/src/string.ts index b626bd98f..3df363bc5 100644 --- a/src/string.ts +++ b/src/string.ts @@ -26,8 +26,9 @@ export const isString: Refinement = (u: unknown): u is string = * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Eq: E.Eq = E.eqString +export const Eq: E.Eq = { + equals: (first, second) => first === second +} /** * `string` semigroup under concatenation. @@ -40,8 +41,9 @@ export const Eq: E.Eq = E.eqString * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Semigroup: S.Semigroup = S.semigroupString +export const Semigroup: S.Semigroup = { + concat: (first, second) => first + second +} /** * `string` monoid under concatenation. @@ -56,22 +58,27 @@ export const Semigroup: S.Semigroup = S.semigroupString * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Monoid: M.Monoid = M.monoidString +export const Monoid: M.Monoid = { + concat: Semigroup.concat, + empty: '' +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Ord: O.Ord = O.ordString +export const Ord: O.Ord = { + equals: Eq.equals, + compare: (first, second) => (first < second ? -1 : first > second ? 1 : 0) +} /** * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const Show: Sh.Show = Sh.showString +export const Show: Sh.Show = { + show: (s) => JSON.stringify(s) +} // ------------------------------------------------------------------------------------- // utils diff --git a/src/struct.ts b/src/struct.ts index 9528ad6b1..c9c5050a7 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -2,7 +2,7 @@ * @since 2.10.0 */ import * as _ from './internal' -import { getObjectSemigroup, Semigroup } from './Semigroup' +import { Semigroup } from './Semigroup' // ------------------------------------------------------------------------------------- // instances @@ -25,8 +25,9 @@ import { getObjectSemigroup, Semigroup } from './Semigroup' * @category instances * @since 2.10.0 */ -// tslint:disable-next-line: deprecation -export const getAssignSemigroup: () => Semigroup = getObjectSemigroup +export const getAssignSemigroup = (): Semigroup => ({ + concat: (first, second) => Object.assign({}, first, second) +}) // ------------------------------------------------------------------------------------- // utils diff --git a/test/BooleanAlgebra.ts b/test/BooleanAlgebra.ts index 91c3d160c..d0cba401b 100644 --- a/test/BooleanAlgebra.ts +++ b/test/BooleanAlgebra.ts @@ -3,6 +3,29 @@ import * as _ from '../src/BooleanAlgebra' import * as B from '../src/boolean' describe('BooleanAlgebra', () => { + it('booleanAlgebraBoolean', () => { + // tslint:disable-next-line: deprecation + const BA = _.booleanAlgebraBoolean + U.deepStrictEqual(BA.implies(true, true), true) + U.deepStrictEqual(BA.implies(true, false), false) + U.deepStrictEqual(BA.implies(false, true), true) + U.deepStrictEqual(BA.implies(false, false), true) + + U.deepStrictEqual(BA.join(true, true), true) + U.deepStrictEqual(BA.join(true, false), true) + U.deepStrictEqual(BA.join(false, true), true) + U.deepStrictEqual(BA.join(false, false), false) + + U.deepStrictEqual(BA.meet(true, true), true) + U.deepStrictEqual(BA.meet(true, false), false) + + U.deepStrictEqual(BA.not(true), false) + U.deepStrictEqual(BA.not(false), true) + + U.deepStrictEqual(BA.one, true) + U.deepStrictEqual(BA.zero, false) + }) + it('booleanAlgebraVoid', () => { const BA = _.booleanAlgebraVoid U.deepStrictEqual(BA.implies(undefined, undefined), undefined) diff --git a/test/Eq.ts b/test/Eq.ts index 4fef10987..7c43c1426 100644 --- a/test/Eq.ts +++ b/test/Eq.ts @@ -77,4 +77,12 @@ describe('Eq', () => { U.deepStrictEqual(eq.equals(['a', 1, true], ['a', 2, true]), false) U.deepStrictEqual(eq.equals(['a', 1, true], ['a', 1, false]), false) }) + + it('eqDate', () => { + // tslint:disable-next-line: deprecation + const E = _.eqDate + U.deepStrictEqual(E.equals(new Date(0), new Date(0)), true) + U.deepStrictEqual(E.equals(new Date(0), new Date(1)), false) + U.deepStrictEqual(E.equals(new Date(1), new Date(0)), false) + }) }) diff --git a/test/Field.ts b/test/Field.ts index 10b4c6e60..c05fcfa39 100644 --- a/test/Field.ts +++ b/test/Field.ts @@ -1,18 +1,31 @@ import * as U from './util' -import { gcd, lcm } from '../src/Field' +import * as _ from '../src/Field' import * as N from '../src/number' describe('Field', () => { it('gcd', () => { - const gcdNumber = gcd(N.Eq, N.Field) + const gcdNumber = _.gcd(N.Eq, N.Field) U.deepStrictEqual(gcdNumber(10, 5), 5) U.deepStrictEqual(gcdNumber(10, 2), 2) U.deepStrictEqual(gcdNumber(10, 3), 1) }) it('lcm', () => { - const lcmNumber = lcm(N.Eq, N.Field) + const lcmNumber = _.lcm(N.Eq, N.Field) U.deepStrictEqual(lcmNumber(4, 6), 12) U.deepStrictEqual(lcmNumber(4, 0), 0) }) + + it('fieldNumber', () => { + // tslint:disable-next-line: deprecation + const F = _.fieldNumber + U.deepStrictEqual(F.add(1, 2), 3) + U.deepStrictEqual(F.div(4, 2), 2) + U.deepStrictEqual(F.mod(4, 2), 0) + U.deepStrictEqual(F.mul(4, 2), 8) + U.deepStrictEqual(F.sub(3, 2), 1) + U.deepStrictEqual(F.degree(0), 1) + U.deepStrictEqual(F.degree(1), 1) + U.deepStrictEqual(F.degree(2), 1) + }) }) diff --git a/test/Ord.ts b/test/Ord.ts index 71b9402e2..5b2460281 100644 --- a/test/Ord.ts +++ b/test/Ord.ts @@ -159,4 +159,12 @@ describe('Ord', () => { ['a', 1] ]) }) + + it('ordDate', () => { + // tslint:disable-next-line: deprecation + const O = _.ordDate + U.deepStrictEqual(O.compare(new Date(0), new Date(0)), 0) + U.deepStrictEqual(O.compare(new Date(0), new Date(1)), -1) + U.deepStrictEqual(O.compare(new Date(1), new Date(0)), 1) + }) }) diff --git a/test/Semigroup.ts b/test/Semigroup.ts index e1a2abccb..22cf88e8f 100644 --- a/test/Semigroup.ts +++ b/test/Semigroup.ts @@ -56,4 +56,49 @@ describe('Semigroup', () => { const S = _.struct(Object.create({ a: 1 })) U.deepStrictEqual(S.concat({}, {}), {}) }) + + it('semigroupAll', () => { + // tslint:disable-next-line: deprecation + const S = _.semigroupAll + U.deepStrictEqual(S.concat(true, true), true) + U.deepStrictEqual(S.concat(false, true), false) + U.deepStrictEqual(S.concat(true, false), false) + U.deepStrictEqual(S.concat(false, false), false) + }) + + it('semigroupAny', () => { + // tslint:disable-next-line: deprecation + const S = _.semigroupAny + U.deepStrictEqual(S.concat(true, true), true) + U.deepStrictEqual(S.concat(false, true), true) + U.deepStrictEqual(S.concat(true, false), true) + U.deepStrictEqual(S.concat(false, false), false) + }) + + it('semigroupSum', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.semigroupSum.concat(2, 3), 5) + }) + + it('semigroupProduct', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.semigroupProduct.concat(2, 3), 6) + }) + + it('getObjectSemigroup', () => { + type T = { + readonly foo?: number + readonly bar: string + } + const foo: T = { + foo: 123, + bar: '456' + } + const bar: T = { + bar: '123' + } + // tslint:disable-next-line: deprecation + const S = _.getObjectSemigroup() + U.deepStrictEqual(S.concat(foo, bar), Object.assign({}, foo, bar)) + }) }) diff --git a/test/Show.ts b/test/Show.ts index b3a799647..ea9c86b50 100644 --- a/test/Show.ts +++ b/test/Show.ts @@ -17,4 +17,21 @@ describe('Show', () => { const Sh = _.tuple(S.Show, N.Show) U.deepStrictEqual(Sh.show(['a', 1]), '["a", 1]') }) + + it('showBoolean', () => { + // tslint:disable-next-line: deprecation + const Sh = _.showBoolean + U.deepStrictEqual(Sh.show(true), 'true') + U.deepStrictEqual(Sh.show(false), 'false') + }) + + it('showNumber', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.showNumber.show(1), '1') + }) + + it('showString', () => { + // tslint:disable-next-line: deprecation + U.deepStrictEqual(_.showString.show('a'), '"a"') + }) }) From fd1836504dbb6a2353e2ff215cdf332c3ae2c85a Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 7 May 2021 09:01:52 +0200 Subject: [PATCH 143/162] Docs: refactor Option --- docs/modules/Option.ts.md | 635 +++++++++--------- src/Option.ts | 1280 ++++++++++++++++++------------------- 2 files changed, 925 insertions(+), 990 deletions(-) diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 3cc22a372..c9a611c81 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -23,41 +23,6 @@ Added in v2.0.0

Table of contents

-- [Alt](#alt) - - [alt](#alt) - - [altW](#altw) -- [Apply](#apply) - - [ap](#ap) -- [Compactable](#compactable) - - [compact](#compact) - - [separate](#separate) -- [Extend](#extend) - - [extend](#extend) -- [Filterable](#filterable) - - [filter](#filter) - - [filterMap](#filtermap) - - [partition](#partition) - - [partitionMap](#partitionmap) -- [Foldable](#foldable) - - [foldMap](#foldmap) - - [reduce](#reduce) - - [reduceRight](#reduceright) -- [Functor](#functor) - - [map](#map) -- [Monad](#monad) - - [chain](#chain) -- [MonadThrow](#monadthrow) - - [throwError](#throwerror) -- [Pointed](#pointed) - - [of](#of) -- [Traversable](#traversable) - - [sequence](#sequence) - - [traverse](#traverse) -- [Witherable](#witherable) - - [wilt](#wilt) - - [wither](#wither) -- [Zero](#zero) - - [zero](#zero) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -82,26 +47,49 @@ Added in v2.0.0 - [getOrElseW](#getorelsew) - [match](#match) - [matchW](#matchw) +- [instance operations](#instance-operations) + - [alt](#alt) + - [altW](#altw) + - [ap](#ap) + - [chain](#chain) + - [compact](#compact) + - [extend](#extend) + - [filter](#filter) + - [filterMap](#filtermap) + - [foldMap](#foldmap) + - [map](#map) + - [of](#of) + - [partition](#partition) + - [partitionMap](#partitionmap) + - [reduce](#reduce) + - [reduceRight](#reduceright) + - [separate](#separate) + - [sequence](#sequence) + - [throwError](#throwerror) + - [traverse](#traverse) + - [wilt](#wilt) + - [wither](#wither) + - [zero](#zero) - [instances](#instances) - - [Alt](#alt-1) + - [Alt](#alt) - [Alternative](#alternative) - [Applicative](#applicative) - - [Apply](#apply-1) + - [Apply](#apply) - [Chain](#chain) - - [Compactable](#compactable-1) - - [Extend](#extend-1) - - [Filterable](#filterable-1) - - [Foldable](#foldable-1) + - [Compactable](#compactable) + - [Extend](#extend) + - [Filterable](#filterable) + - [Foldable](#foldable) - [FromEither](#fromeither) - - [Functor](#functor-1) - - [Monad](#monad-1) - - [MonadThrow](#monadthrow-1) - - [Pointed](#pointed-1) - - [Traversable](#traversable-1) + - [Functor](#functor) + - [Monad](#monad) + - [MonadThrow](#monadthrow) + - [Pointed](#pointed) + - [Traversable](#traversable) - [URI](#uri) - [URI (type alias)](#uri-type-alias) - - [Witherable](#witherable-1) - - [Zero](#zero-1) + - [Witherable](#witherable) + - [Zero](#zero) - [getEq](#geteq) - [getMonoid](#getmonoid) - [getOrd](#getord) @@ -145,638 +133,609 @@ Added in v2.0.0 --- -# Alt +# combinators -## alt +## apFirst -Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to -types of kind `* -> *`. +Combine two effectful actions, keeping only the result of the first. -In case of `Option` returns the left-most non-`None` value. +Derivable from `Apply`. **Signature** ```ts -export declare const alt:
(that: Lazy>) => (fa: Option) => Option +export declare const apFirst: (second: Option) => (first: Option) => Option ``` -**Example** +Added in v2.0.0 -```ts -import * as O from 'fp-ts/Option' -import { pipe } from 'fp-ts/function' +## apSecond -assert.deepStrictEqual( - pipe( - O.some('a'), - O.alt(() => O.some('b')) - ), - O.some('a') -) -assert.deepStrictEqual( - pipe( - O.none, - O.alt(() => O.some('b')) - ), - O.some('b') -) +Combine two effectful actions, keeping only the result of the second. + +Derivable from `Apply`. + +**Signature** + +```ts +export declare const apSecond: (second: Option) => (first: Option) => Option ``` Added in v2.0.0 -## altW - -Less strict version of [`alt`](#alt). +## chainEitherK **Signature** ```ts -export declare const altW: (that: Lazy>) => (fa: Option) => Option +export declare const chainEitherK: (f: (a: A) => Either) => (ma: Option) => Option ``` -Added in v2.9.0 +Added in v2.11.0 -# Apply +## chainFirst -## ap +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. -Apply a function to an argument under a type constructor. +Derivable from `Chain`. **Signature** ```ts -export declare const ap: (fa: Option) => (fab: Option<(a: A) => B>) => Option +export declare const chainFirst: (f: (a: A) => Option) => (first: Option) => Option ``` Added in v2.0.0 -# Compactable +## duplicate -## compact +Derivable from `Extend`. **Signature** ```ts -export declare const compact: (fa: Option>) => Option +export declare const duplicate: (ma: Option) => Option> ``` Added in v2.0.0 -## separate +## flap + +Derivable from `Functor`. **Signature** ```ts -export declare const separate: (ma: Option>) => Separated, Option> +export declare const flap: (a: A) => (fab: Option<(a: A) => B>) => Option ``` -Added in v2.0.0 +Added in v2.10.0 -# Extend +## flatten -## extend +Derivable from `Chain`. **Signature** ```ts -export declare const extend: (f: (wa: Option) => B) => (wa: Option) => Option +export declare const flatten: (mma: Option>) => Option ``` Added in v2.0.0 -# Filterable - -## filter +## fromEitherK **Signature** ```ts -export declare const filter: { - (refinement: Refinement): (fa: Option) => Option - (predicate: Predicate): (fb: Option) => Option -} +export declare const fromEitherK: (f: (...a: A) => Either) => (...a: A) => Option ``` -Added in v2.0.0 +Added in v2.11.0 -## filterMap +## ~~mapNullable~~ + +Use [`chainNullableK`](#chainnullablek) instead. **Signature** ```ts -export declare const filterMap: (f: (a: A) => Option) => (fa: Option) => Option +export declare const mapNullable: (f: (a: A) => B | null | undefined) => (ma: Option) => Option ``` Added in v2.0.0 -## partition +# constructors + +## fromPredicate + +Returns a _smart constructor_ based on the given predicate. **Signature** ```ts -export declare const partition: { - (refinement: Refinement): (fa: Option) => Separated, Option> - (predicate: Predicate): (fb: Option) => Separated, Option> -} +export declare function fromPredicate(refinement: Refinement): (a: A) => Option +export declare function fromPredicate(predicate: Predicate): (b: B) => Option ``` -Added in v2.0.0 +**Example** -## partitionMap +```ts +import { none, some, fromPredicate } from 'fp-ts/Option' -**Signature** +const getOption = fromPredicate((n: number) => n >= 0) -```ts -export declare const partitionMap: ( - f: (a: A) => Either -) => (fa: Option) => Separated, Option> +assert.deepStrictEqual(getOption(-1), none) +assert.deepStrictEqual(getOption(1), some(1)) ``` Added in v2.0.0 -# Foldable +## getLeft -## foldMap +Returns the `Left` value of an `Either` if possible. **Signature** ```ts -export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Option) => M +export declare const getLeft: (ma: Either) => Option +``` + +**Example** + +```ts +import { getLeft, none, some } from 'fp-ts/Option' +import { right, left } from 'fp-ts/Either' + +assert.deepStrictEqual(getLeft(right(1)), none) +assert.deepStrictEqual(getLeft(left('a')), some('a')) ``` Added in v2.0.0 -## reduce +## getRight + +Returns the `Right` value of an `Either` if possible. **Signature** ```ts -export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Option) => B +export declare const getRight: (ma: Either) => Option +``` + +**Example** + +```ts +import { getRight, none, some } from 'fp-ts/Option' +import { right, left } from 'fp-ts/Either' + +assert.deepStrictEqual(getRight(right(1)), some(1)) +assert.deepStrictEqual(getRight(left('a')), none) ``` Added in v2.0.0 -## reduceRight +## guard **Signature** ```ts -export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Option) => B +export declare const guard: (b: boolean) => Option ``` -Added in v2.0.0 - -# Functor +Added in v2.11.0 -## map +## none -`map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types -use the type constructor `F` to represent some computational context. +`None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value. **Signature** ```ts -export declare const map: (f: (a: A) => B) => (fa: Option) => Option +export declare const none: Option ``` Added in v2.0.0 -# Monad - -## chain +## some -Composes computations in sequence, using the return value of one computation to determine the next computation. +Constructs a `Some`. Represents an optional value that exists. **Signature** ```ts -export declare const chain: (f: (a: A) => Option) => (ma: Option) => Option +export declare const some: (a: A) => Option ``` Added in v2.0.0 -# MonadThrow +# destructors -## throwError +## fold + +Alias of [`match`](#match). **Signature** ```ts -export declare const throwError: (e: E) => Option +export declare const fold: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B ``` -Added in v2.7.0 +Added in v2.0.0 -# Pointed +## foldW -## of +Alias of [`matchW`](#matchw). **Signature** ```ts -export declare const of: (a: A) => Option +export declare const foldW: (onNone: Lazy, onSome: (a: A) => C) => (ma: Option) => B | C ``` -Added in v2.7.0 +Added in v2.10.0 -# Traversable +## getOrElse -## sequence +Extracts the value out of the structure, if it exists. Otherwise returns the given default value **Signature** ```ts -export declare const sequence: Sequence1<'Option'> +export declare const getOrElse: (onNone: Lazy) => (ma: Option) => A ``` -Added in v2.6.3 - -## traverse - -**Signature** +**Example** ```ts -export declare const traverse: PipeableTraverse1<'Option'> +import { some, none, getOrElse } from 'fp-ts/Option' +import { pipe } from 'fp-ts/function' + +assert.strictEqual( + pipe( + some(1), + getOrElse(() => 0) + ), + 1 +) +assert.strictEqual( + pipe( + none, + getOrElse(() => 0) + ), + 0 +) ``` -Added in v2.6.3 +Added in v2.0.0 -# Witherable +## getOrElseW -## wilt +Less strict version of [`getOrElse`](#getorelse). **Signature** ```ts -export declare const wilt: PipeableWilt1<'Option'> +export declare const getOrElseW: (onNone: Lazy) => (ma: Option) => B | A ``` -Added in v2.6.5 +Added in v2.6.0 -## wither +## match + +Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is +returned, otherwise the function is applied to the value inside the `Some` and the result is returned. **Signature** ```ts -export declare const wither: PipeableWither1<'Option'> +export declare const match: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B ``` -Added in v2.6.5 - -# Zero - -## zero - -**Signature** +**Example** ```ts -export declare const zero: () => Option -``` +import { some, none, match } from 'fp-ts/Option' +import { pipe } from 'fp-ts/function' -Added in v2.7.0 +assert.strictEqual( + pipe( + some(1), + match( + () => 'a none', + (a) => `a some containing ${a}` + ) + ), + 'a some containing 1' +) -# combinators +assert.strictEqual( + pipe( + none, + match( + () => 'a none', + (a) => `a some containing ${a}` + ) + ), + 'a none' +) +``` -## apFirst +Added in v2.10.0 -Combine two effectful actions, keeping only the result of the first. +## matchW -Derivable from `Apply`. +Less strict version of [`match`](#match). **Signature** ```ts -export declare const apFirst: (second: Option) => (first: Option) => Option +export declare const matchW: (onNone: Lazy, onSome: (a: A) => C) => (ma: Option) => B | C ``` -Added in v2.0.0 +Added in v2.10.0 -## apSecond +# instance operations -Combine two effectful actions, keeping only the result of the second. +## alt -Derivable from `Apply`. +Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to +types of kind `* -> *`. + +In case of `Option` returns the left-most non-`None` value. **Signature** ```ts -export declare const apSecond: (second: Option) => (first: Option) => Option +export declare const alt: (that: Lazy>) => (fa: Option) => Option ``` -Added in v2.0.0 - -## chainEitherK - -**Signature** +**Example** ```ts -export declare const chainEitherK: (f: (a: A) => Either) => (ma: Option) => Option -``` +import * as O from 'fp-ts/Option' +import { pipe } from 'fp-ts/function' -Added in v2.11.0 +assert.deepStrictEqual( + pipe( + O.some('a'), + O.alt(() => O.some('b')) + ), + O.some('a') +) +assert.deepStrictEqual( + pipe( + O.none, + O.alt(() => O.some('b')) + ), + O.some('b') +) +``` -## chainFirst +Added in v2.0.0 -Composes computations in sequence, using the return value of one computation to determine the next computation and -keeping only the result of the first. +## altW -Derivable from `Chain`. +Less strict version of [`alt`](#alt). **Signature** ```ts -export declare const chainFirst: (f: (a: A) => Option) => (first: Option) => Option +export declare const altW: (that: Lazy>) => (fa: Option) => Option ``` -Added in v2.0.0 - -## duplicate +Added in v2.9.0 -Derivable from `Extend`. +## ap **Signature** ```ts -export declare const duplicate: (ma: Option) => Option> +export declare const ap: (fa: Option) => (fab: Option<(a: A) => B>) => Option ``` Added in v2.0.0 -## flap +## chain -Derivable from `Functor`. +Composes computations in sequence, using the return value of one computation to determine the next computation. **Signature** ```ts -export declare const flap: (a: A) => (fab: Option<(a: A) => B>) => Option +export declare const chain: (f: (a: A) => Option) => (ma: Option) => Option ``` -Added in v2.10.0 - -## flatten +Added in v2.0.0 -Derivable from `Chain`. +## compact **Signature** ```ts -export declare const flatten: (mma: Option>) => Option +export declare const compact: (fa: Option>) => Option ``` Added in v2.0.0 -## fromEitherK +## extend **Signature** ```ts -export declare const fromEitherK: (f: (...a: A) => Either) => (...a: A) => Option +export declare const extend: (f: (wa: Option) => B) => (wa: Option) => Option ``` -Added in v2.11.0 - -## ~~mapNullable~~ +Added in v2.0.0 -Use [`chainNullableK`](#chainnullablek) instead. +## filter **Signature** ```ts -export declare const mapNullable: (f: (a: A) => B | null | undefined) => (ma: Option) => Option +export declare const filter: { + (refinement: Refinement): (fa: Option) => Option + (predicate: Predicate): (fb: Option) => Option +} ``` Added in v2.0.0 -# constructors - -## fromPredicate - -Returns a _smart constructor_ based on the given predicate. +## filterMap **Signature** ```ts -export declare function fromPredicate(refinement: Refinement): (a: A) => Option -export declare function fromPredicate(predicate: Predicate): (b: B) => Option +export declare const filterMap: (f: (a: A) => Option) => (fa: Option) => Option ``` -**Example** +Added in v2.0.0 -```ts -import { none, some, fromPredicate } from 'fp-ts/Option' +## foldMap -const getOption = fromPredicate((n: number) => n >= 0) +**Signature** -assert.deepStrictEqual(getOption(-1), none) -assert.deepStrictEqual(getOption(1), some(1)) +```ts +export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Option) => M ``` Added in v2.0.0 -## getLeft - -Returns the `Left` value of an `Either` if possible. +## map **Signature** ```ts -export declare function getLeft(ma: Either): Option -``` - -**Example** - -```ts -import { getLeft, none, some } from 'fp-ts/Option' -import { right, left } from 'fp-ts/Either' - -assert.deepStrictEqual(getLeft(right(1)), none) -assert.deepStrictEqual(getLeft(left('a')), some('a')) +export declare const map: (f: (a: A) => B) => (fa: Option) => Option ``` Added in v2.0.0 -## getRight - -Returns the `Right` value of an `Either` if possible. +## of **Signature** ```ts -export declare function getRight(ma: Either): Option +export declare const of: (a: A) => Option ``` -**Example** +Added in v2.7.0 -```ts -import { getRight, none, some } from 'fp-ts/Option' -import { right, left } from 'fp-ts/Either' +## partition -assert.deepStrictEqual(getRight(right(1)), some(1)) -assert.deepStrictEqual(getRight(left('a')), none) +**Signature** + +```ts +export declare const partition: { + (refinement: Refinement): (fa: Option) => Separated, Option> + (predicate: Predicate): (fb: Option) => Separated, Option> +} ``` Added in v2.0.0 -## guard +## partitionMap **Signature** ```ts -export declare const guard: (b: boolean) => Option +export declare const partitionMap: ( + f: (a: A) => Either +) => (fa: Option) => Separated, Option> ``` -Added in v2.11.0 - -## none +Added in v2.0.0 -`None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value. +## reduce **Signature** ```ts -export declare const none: Option +export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Option) => B ``` Added in v2.0.0 -## some - -Constructs a `Some`. Represents an optional value that exists. +## reduceRight **Signature** ```ts -export declare const some: (a: A) => Option +export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Option) => B ``` Added in v2.0.0 -# destructors - -## fold - -Alias of [`match`](#match). +## separate **Signature** ```ts -export declare const fold: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B +export declare const separate: (ma: Option>) => Separated, Option> ``` Added in v2.0.0 -## foldW - -Alias of [`matchW`](#matchw). +## sequence **Signature** ```ts -export declare const foldW: (onNone: Lazy, onSome: (a: A) => C) => (ma: Option) => B | C +export declare const sequence: Sequence1<'Option'> ``` -Added in v2.10.0 - -## getOrElse +Added in v2.6.3 -Extracts the value out of the structure, if it exists. Otherwise returns the given default value +## throwError **Signature** ```ts -export declare const getOrElse: (onNone: Lazy) => (ma: Option) => A -``` - -**Example** - -```ts -import { some, none, getOrElse } from 'fp-ts/Option' -import { pipe } from 'fp-ts/function' - -assert.strictEqual( - pipe( - some(1), - getOrElse(() => 0) - ), - 1 -) -assert.strictEqual( - pipe( - none, - getOrElse(() => 0) - ), - 0 -) +export declare const throwError: (e: E) => Option ``` -Added in v2.0.0 - -## getOrElseW +Added in v2.7.0 -Less strict version of [`getOrElse`](#getorelse). +## traverse **Signature** ```ts -export declare const getOrElseW: (onNone: Lazy) => (ma: Option) => B | A +export declare const traverse: PipeableTraverse1<'Option'> ``` -Added in v2.6.0 - -## match +Added in v2.6.3 -Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is -returned, otherwise the function is applied to the value inside the `Some` and the result is returned. +## wilt **Signature** ```ts -export declare const match: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B +export declare const wilt: PipeableWilt1<'Option'> ``` -**Example** +Added in v2.6.5 -```ts -import { some, none, match } from 'fp-ts/Option' -import { pipe } from 'fp-ts/function' +## wither -assert.strictEqual( - pipe( - some(1), - match( - () => 'a none', - (a) => `a some containing ${a}` - ) - ), - 'a some containing 1' -) +**Signature** -assert.strictEqual( - pipe( - none, - match( - () => 'a none', - (a) => `a some containing ${a}` - ) - ), - 'a none' -) +```ts +export declare const wither: PipeableWither1<'Option'> ``` -Added in v2.10.0 - -## matchW +Added in v2.6.5 -Less strict version of [`match`](#match). +## zero **Signature** ```ts -export declare const matchW: (onNone: Lazy, onSome: (a: A) => C) => (ma: Option) => B | C +export declare const zero: () => Option ``` -Added in v2.10.0 +Added in v2.7.0 # instances @@ -975,7 +934,7 @@ Added in v2.11.0 **Signature** ```ts -export declare function getEq(E: Eq): Eq> +export declare const getEq: (E: Eq) => Eq> ``` **Example** @@ -1009,7 +968,7 @@ concatenated using the provided `Semigroup` **Signature** ```ts -export declare function getMonoid(S: Semigroup): Monoid> +export declare const getMonoid: (S: Semigroup) => Monoid> ``` **Example** @@ -1038,7 +997,7 @@ the type the `Option` contains. **Signature** ```ts -export declare function getOrd(O: Ord): Ord> +export declare const getOrd: (O: Ord) => Ord> ``` **Example** @@ -1062,7 +1021,7 @@ Added in v2.0.0 **Signature** ```ts -export declare function getShow(S: Show): Show> +export declare const getShow: (S: Show) => Show> ``` Added in v2.0.0 diff --git a/src/Option.ts b/src/Option.ts index e51b57cff..b14f93d0c 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -77,38 +77,6 @@ export interface Some { */ export type Option = None | Some -// ------------------------------------------------------------------------------------- -// refinements -// ------------------------------------------------------------------------------------- - -/** - * Returns `true` if the option is an instance of `Some`, `false` otherwise. - * - * @example - * import { some, none, isSome } from 'fp-ts/Option' - * - * assert.strictEqual(isSome(some(1)), true) - * assert.strictEqual(isSome(none), false) - * - * @category refinements - * @since 2.0.0 - */ -export const isSome: (fa: Option) => fa is Some = _.isSome - -/** - * Returns `true` if the option is `None`, `false` otherwise. - * - * @example - * import { some, none, isNone } from 'fp-ts/Option' - * - * assert.strictEqual(isNone(some(1)), false) - * assert.strictEqual(isNone(none), true) - * - * @category refinements - * @since 2.0.0 - */ -export const isNone = (fa: Option): fa is None => fa._tag === 'None' - // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- @@ -162,9 +130,7 @@ export function fromPredicate(predicate: Predicate): (b: B) = * @category constructors * @since 2.0.0 */ -export function getLeft(ma: Either): Option { - return ma._tag === 'Right' ? none : some(ma.left) -} +export const getLeft = (ma: Either): Option => (ma._tag === 'Right' ? none : some(ma.left)) /** * Returns the `Right` value of an `Either` if possible. @@ -179,372 +145,258 @@ export function getLeft(ma: Either): Option { * @category constructors * @since 2.0.0 */ -export function getRight(ma: Either): Option { - return ma._tag === 'Left' ? none : some(ma.right) -} +export const getRight = (ma: Either): Option => (ma._tag === 'Left' ? none : some(ma.right)) // ------------------------------------------------------------------------------------- -// natural transformations +// non-pipeables // ------------------------------------------------------------------------------------- -/** - * Transforms an `Either` to an `Option` discarding the error. - * - * Alias of [getRight](#getright) - * - * @category natural transformations - * @since 2.0.0 - */ -export const fromEither: FromEither1['fromEither'] = getRight +const _map: Monad1['map'] = (fa, f) => pipe(fa, map(f)) +const _ap: Monad1['ap'] = (fab, fa) => pipe(fab, ap(fa)) +const _chain: Monad1['chain'] = (ma, f) => pipe(ma, chain(f)) +const _reduce: Foldable1['reduce'] = (fa, b, f) => pipe(fa, reduce(b, f)) +const _foldMap: Foldable1['foldMap'] = (M) => { + const foldMapM = foldMap(M) + return (fa, f) => pipe(fa, foldMapM(f)) +} +const _reduceRight: Foldable1['reduceRight'] = (fa, b, f) => pipe(fa, reduceRight(b, f)) +const _traverse: Traversable1['traverse'] = ( + F: ApplicativeHKT +): ((ta: Option, f: (a: A) => HKT) => HKT>) => { + const traverseF = traverse(F) + return (ta, f) => pipe(ta, traverseF(f)) +} +/* istanbul ignore next */ +const _alt: Alt1['alt'] = (fa, that) => pipe(fa, alt(that)) +const _filter: Filterable1['filter'] = (fa: Option, predicate: Predicate) => pipe(fa, filter(predicate)) +/* istanbul ignore next */ +const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) +/* istanbul ignore next */ +const _extend: Extend1['extend'] = (wa, f) => pipe(wa, extend(f)) +/* istanbul ignore next */ +const _partition: Filterable1['partition'] = (fa: Option, predicate: Predicate) => + pipe(fa, partition(predicate)) +/* istanbul ignore next */ +const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) // ------------------------------------------------------------------------------------- -// destructors +// instances // ------------------------------------------------------------------------------------- /** - * Less strict version of [`match`](#match). - * - * @category destructors - * @since 2.10.0 + * @category instances + * @since 2.0.0 */ -export const matchW = (onNone: Lazy, onSome: (a: A) => C) => (ma: Option): B | C => - isNone(ma) ? onNone() : onSome(ma.value) +export const URI = 'Option' /** - * Alias of [`matchW`](#matchw). - * - * @category destructors - * @since 2.10.0 + * @category instances + * @since 2.0.0 */ -export const foldW = matchW +export type URI = typeof URI -/** - * Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is - * returned, otherwise the function is applied to the value inside the `Some` and the result is returned. - * - * @example - * import { some, none, match } from 'fp-ts/Option' - * import { pipe } from 'fp-ts/function' - * - * assert.strictEqual( - * pipe( - * some(1), - * match(() => 'a none', a => `a some containing ${a}`) - * ), - * 'a some containing 1' - * ) - * - * assert.strictEqual( - * pipe( - * none, - * match(() => 'a none', a => `a some containing ${a}`) - * ), - * 'a none' - * ) - * - * @category destructors - * @since 2.10.0 - */ -export const match: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B = matchW +declare module './HKT' { + interface URItoKind { + readonly [URI]: Option + } +} /** - * Alias of [`match`](#match). - * - * @category destructors + * @category instances * @since 2.0.0 */ -export const fold = match - -/** - * Less strict version of [`getOrElse`](#getorelse). - * - * @category destructors - * @since 2.6.0 - */ -export const getOrElseW = (onNone: Lazy) => (ma: Option): A | B => (isNone(ma) ? onNone() : ma.value) +export const getShow = (S: Show): Show> => ({ + show: (ma) => (isNone(ma) ? 'none' : `some(${S.show(ma.value)})`) +}) /** - * Extracts the value out of the structure, if it exists. Otherwise returns the given default value - * * @example - * import { some, none, getOrElse } from 'fp-ts/Option' - * import { pipe } from 'fp-ts/function' + * import { none, some, getEq } from 'fp-ts/Option' + * import * as N from 'fp-ts/number' * - * assert.strictEqual( - * pipe( - * some(1), - * getOrElse(() => 0) - * ), - * 1 - * ) - * assert.strictEqual( - * pipe( - * none, - * getOrElse(() => 0) - * ), - * 0 - * ) + * const E = getEq(N.Eq) + * assert.strictEqual(E.equals(none, none), true) + * assert.strictEqual(E.equals(none, some(1)), false) + * assert.strictEqual(E.equals(some(1), none), false) + * assert.strictEqual(E.equals(some(1), some(2)), false) + * assert.strictEqual(E.equals(some(1), some(1)), true) * - * @category destructors + * @category instances * @since 2.0.0 */ -export const getOrElse: (onNone: Lazy) => (ma: Option) => A = getOrElseW - -// ------------------------------------------------------------------------------------- -// interop -// ------------------------------------------------------------------------------------- +export const getEq = (E: Eq): Eq> => ({ + equals: (x, y) => x === y || (isNone(x) ? isNone(y) : isNone(y) ? false : E.equals(x.value, y.value)) +}) /** - * Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise - * returns the value wrapped in a `Some`. + * The `Ord` instance allows `Option` values to be compared with + * `compare`, whenever there is an `Ord` instance for + * the type the `Option` contains. + * + * `None` is considered to be less than any `Some` value. + * * * @example - * import { none, some, fromNullable } from 'fp-ts/Option' + * import { none, some, getOrd } from 'fp-ts/Option' + * import * as N from 'fp-ts/number' * - * assert.deepStrictEqual(fromNullable(undefined), none) - * assert.deepStrictEqual(fromNullable(null), none) - * assert.deepStrictEqual(fromNullable(1), some(1)) + * const O = getOrd(N.Ord) + * assert.strictEqual(O.compare(none, none), 0) + * assert.strictEqual(O.compare(none, some(1)), -1) + * assert.strictEqual(O.compare(some(1), none), 1) + * assert.strictEqual(O.compare(some(1), some(2)), -1) + * assert.strictEqual(O.compare(some(1), some(1)), 0) * - * @category interop + * @category instances * @since 2.0.0 */ -export const fromNullable = (a: A): Option> => (a == null ? none : some(a as NonNullable)) +export const getOrd = (O: Ord): Ord> => ({ + equals: getEq(O).equals, + compare: (x, y) => (x === y ? 0 : isSome(x) ? (isSome(y) ? O.compare(x.value, y.value) : 1) : -1) +}) /** - * Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in a - * `Some`. + * Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are + * concatenated using the provided `Semigroup` * - * See also [`tryCatchK`](#trycatchk). + * | x | y | concat(x, y) | + * | ------- | ------- | ------------------ | + * | none | none | none | + * | some(a) | none | some(a) | + * | none | some(b) | some(b) | + * | some(a) | some(b) | some(concat(a, b)) | * * @example - * import { none, some, tryCatch } from 'fp-ts/Option' + * import { getMonoid, some, none } from 'fp-ts/Option' + * import { SemigroupSum } from 'fp-ts/number' * - * assert.deepStrictEqual( - * tryCatch(() => { - * throw new Error() - * }), - * none - * ) - * assert.deepStrictEqual(tryCatch(() => 1), some(1)) + * const M = getMonoid(SemigroupSum) + * assert.deepStrictEqual(M.concat(none, none), none) + * assert.deepStrictEqual(M.concat(some(1), none), some(1)) + * assert.deepStrictEqual(M.concat(none, some(1)), some(1)) + * assert.deepStrictEqual(M.concat(some(1), some(2)), some(3)) * - * @category interop + * @category instances * @since 2.0.0 */ -export const tryCatch = (f: Lazy): Option => { - try { - return some(f()) - } catch (e) { - return none - } +export const getMonoid = (S: Semigroup): Monoid> => ({ + concat: (x, y) => (isNone(x) ? y : isNone(y) ? x : some(S.concat(x.value, y.value))), + empty: none +}) + +/** + * @category instance operations + * @since 2.0.0 + */ +export const map: (f: (a: A) => B) => (fa: Option) => Option = (f) => (fa) => + isNone(fa) ? none : some(f(fa.value)) + +/** + * @category instances + * @since 2.7.0 + */ +export const Functor: Functor1 = { + URI, + map: _map } /** - * Converts a function that may throw to one returning a `Option`. + * Derivable from `Functor`. * - * @category interop + * @category combinators * @since 2.10.0 */ -export const tryCatchK = , B>(f: (...a: A) => B): ((...a: A) => Option) => (...a) => - tryCatch(() => f(...a)) +export const flap = + /*#__PURE__*/ + flap_(Functor) /** - * Returns a *smart constructor* from a function that returns a nullable value. - * - * @example - * import { fromNullableK, none, some } from 'fp-ts/Option' - * - * const f = (s: string): number | undefined => { - * const n = parseFloat(s) - * return isNaN(n) ? undefined : n - * } - * - * const g = fromNullableK(f) - * - * assert.deepStrictEqual(g('1'), some(1)) - * assert.deepStrictEqual(g('a'), none) - * - * @category interop - * @since 2.9.0 + * @category instance operations + * @since 2.7.0 */ -export const fromNullableK: , B>( - f: (...a: A) => B | null | undefined -) => (...a: A) => Option> = (f) => flow(f, fromNullable) +export const of: Pointed1['of'] = some /** - * This is `chain` + `fromNullable`, useful when working with optional values. - * - * @example - * import { some, none, fromNullable, chainNullableK } from 'fp-ts/Option' - * import { pipe } from 'fp-ts/function' - * - * interface Employee { - * readonly company?: { - * readonly address?: { - * readonly street?: { - * readonly name?: string - * } - * } - * } - * } - * - * const employee1: Employee = { company: { address: { street: { name: 'high street' } } } } - * - * assert.deepStrictEqual( - * pipe( - * fromNullable(employee1.company), - * chainNullableK(company => company.address), - * chainNullableK(address => address.street), - * chainNullableK(street => street.name) - * ), - * some('high street') - * ) - * - * const employee2: Employee = { company: { address: { street: {} } } } - * - * assert.deepStrictEqual( - * pipe( - * fromNullable(employee2.company), - * chainNullableK(company => company.address), - * chainNullableK(address => address.street), - * chainNullableK(street => street.name) - * ), - * none - * ) - * - * @category interop - * @since 2.9.0 + * @category instances + * @since 2.10.0 */ -export const chainNullableK = (f: (a: A) => B | null | undefined) => (ma: Option): Option => - isNone(ma) ? none : fromNullable(f(ma.value)) +export const Pointed: Pointed1 = { + URI, + of +} /** - * Extracts the value out of the structure, if it exists. Otherwise returns `null`. - * - * @example - * import { some, none, toNullable } from 'fp-ts/Option' - * import { pipe } from 'fp-ts/function' - * - * assert.strictEqual( - * pipe( - * some(1), - * toNullable - * ), - * 1 - * ) - * assert.strictEqual( - * pipe( - * none, - * toNullable - * ), - * null - * ) - * - * @category interop + * @category instance operations * @since 2.0.0 */ -export const toNullable: (ma: Option) => A | null = - /*#__PURE__*/ - match(constNull, identity) +export const ap: (fa: Option) => (fab: Option<(a: A) => B>) => Option = (fa) => (fab) => + isNone(fab) ? none : isNone(fa) ? none : some(fab.value(fa.value)) /** - * Extracts the value out of the structure, if it exists. Otherwise returns `undefined`. - * - * @example - * import { some, none, toUndefined } from 'fp-ts/Option' - * import { pipe } from 'fp-ts/function' - * - * assert.strictEqual( - * pipe( - * some(1), - * toUndefined - * ), - * 1 - * ) - * assert.strictEqual( - * pipe( - * none, - * toUndefined - * ), - * undefined - * ) - * - * @category interop - * @since 2.0.0 + * @category instances + * @since 2.10.0 */ -export const toUndefined: (ma: Option) => A | undefined = - /*#__PURE__*/ - match(constUndefined, identity) - -// ------------------------------------------------------------------------------------- -// non-pipeables -// ------------------------------------------------------------------------------------- - -const _map: Monad1['map'] = (fa, f) => pipe(fa, map(f)) -const _ap: Monad1['ap'] = (fab, fa) => pipe(fab, ap(fa)) -const _chain: Monad1['chain'] = (ma, f) => pipe(ma, chain(f)) -const _reduce: Foldable1['reduce'] = (fa, b, f) => pipe(fa, reduce(b, f)) -const _foldMap: Foldable1['foldMap'] = (M) => { - const foldMapM = foldMap(M) - return (fa, f) => pipe(fa, foldMapM(f)) -} -const _reduceRight: Foldable1['reduceRight'] = (fa, b, f) => pipe(fa, reduceRight(b, f)) -const _traverse: Traversable1['traverse'] = ( - F: ApplicativeHKT -): ((ta: Option, f: (a: A) => HKT) => HKT>) => { - const traverseF = traverse(F) - return (ta, f) => pipe(ta, traverseF(f)) +export const Apply: Apply1 = { + URI, + map: _map, + ap: _ap } -/* istanbul ignore next */ -const _alt: Alt1['alt'] = (fa, that) => pipe(fa, alt(that)) -const _filter: Filterable1['filter'] = (fa: Option, predicate: Predicate) => pipe(fa, filter(predicate)) -/* istanbul ignore next */ -const _filterMap: Filterable1['filterMap'] = (fa, f) => pipe(fa, filterMap(f)) -/* istanbul ignore next */ -const _extend: Extend1['extend'] = (wa, f) => pipe(wa, extend(f)) -/* istanbul ignore next */ -const _partition: Filterable1['partition'] = (fa: Option, predicate: Predicate) => - pipe(fa, partition(predicate)) -/* istanbul ignore next */ -const _partitionMap: Filterable1['partitionMap'] = (fa, f) => pipe(fa, partitionMap(f)) - -// ------------------------------------------------------------------------------------- -// type class members -// ------------------------------------------------------------------------------------- /** - * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types - * use the type constructor `F` to represent some computational context. + * Combine two effectful actions, keeping only the result of the first. + * + * Derivable from `Apply`. * - * @category Functor + * @category combinators * @since 2.0.0 */ -export const map: (f: (a: A) => B) => (fa: Option) => Option = (f) => (fa) => - isNone(fa) ? none : some(f(fa.value)) +export const apFirst = + /*#__PURE__*/ + apFirst_(Apply) /** - * Apply a function to an argument under a type constructor. + * Combine two effectful actions, keeping only the result of the second. * - * @category Apply + * Derivable from `Apply`. + * + * @category combinators * @since 2.0.0 */ -export const ap: (fa: Option) => (fab: Option<(a: A) => B>) => Option = (fa) => (fab) => - isNone(fab) ? none : isNone(fa) ? none : some(fab.value(fa.value)) +export const apSecond = + /*#__PURE__*/ + apSecond_(Apply) /** - * @category Pointed + * @category instances * @since 2.7.0 */ -export const of: Pointed1['of'] = some +export const Applicative: Applicative1 = { + URI, + map: _map, + ap: _ap, + of +} /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * - * @category Monad + * @category instance operations * @since 2.0.0 */ export const chain: (f: (a: A) => Option) => (ma: Option) => Option = (f) => (ma) => isNone(ma) ? none : f(ma.value) +/** + * @category instances + * @since 2.10.0 + */ +export const Chain: Chain1 = { + URI, + map: _map, + ap: _ap, + chain: _chain +} + /** * Derivable from `Chain`. * @@ -555,10 +407,67 @@ export const flatten: (mma: Option>) => Option = /*#__PURE__*/ chain(identity) +/** + * @category instances + * @since 2.7.0 + */ +export const Monad: Monad1 = { + URI, + map: _map, + ap: _ap, + of, + chain: _chain +} + +/** + * Composes computations in sequence, using the return value of one computation to determine the next computation and + * keeping only the result of the first. + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const chainFirst = + /*#__PURE__*/ + chainFirst_(Chain) + +/** + * @category instance operations + * @since 2.0.0 + */ +export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Option) => B = (b, f) => (fa) => + isNone(fa) ? b : f(b, fa.value) + +/** + * @category instance operations + * @since 2.0.0 + */ +export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Option) => M = (M) => (f) => (fa) => + isNone(fa) ? M.empty : f(fa.value) + +/** + * @category instance operations + * @since 2.0.0 + */ +export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Option) => B = (b, f) => (fa) => + isNone(fa) ? b : f(fa.value, b) + +/** + * @category instances + * @since 2.7.0 + */ +export const Foldable: Foldable1 = { + URI, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight +} + /** * Less strict version of [`alt`](#alt). * - * @category Alt + * @category instance operations * @since 2.9.0 */ export const altW: (that: Lazy>) => (fa: Option) => Option = (that) => (fa) => @@ -589,25 +498,59 @@ export const altW: (that: Lazy>) => (fa: Option) => Option(that: Lazy>) => (fa: Option) => Option = altW /** - * @category Zero + * @category instances * @since 2.7.0 */ -export const zero: Zero1['zero'] = () => none +export const Alt: Alt1 = { + URI, + map: _map, + alt: _alt +} /** - * @category MonadThrow + * @category instance operations * @since 2.7.0 */ -export const throwError: MonadThrow1['throwError'] = () => none +export const zero: Zero1['zero'] = () => none + +/** + * @category instances + * @since 2.11.0 + */ +export const Zero: Zero1 = { + URI, + zero +} + +/** + * @category constructors + * @since 2.11.0 + */ +export const guard = + /*#__PURE__*/ + guard_(Zero, Pointed) + +/** + * @category instances + * @since 2.7.0 + */ +export const Alternative: Alternative1 = { + URI, + map: _map, + ap: _ap, + of, + alt: _alt, + zero +} /** - * @category Extend + * @category instance operations * @since 2.0.0 */ export const extend: (f: (wa: Option) => B) => (wa: Option) => Option = (f) => (wa) => @@ -624,28 +567,17 @@ export const duplicate: (ma: Option) => Option> = extend(identity) /** - * @category Foldable - * @since 2.0.0 - */ -export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Option) => B = (b, f) => (fa) => - isNone(fa) ? b : f(b, fa.value) - -/** - * @category Foldable - * @since 2.0.0 - */ -export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Option) => M = (M) => (f) => (fa) => - isNone(fa) ? M.empty : f(fa.value) - -/** - * @category Foldable - * @since 2.0.0 + * @category instances + * @since 2.7.0 */ -export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Option) => B = (b, f) => (fa) => - isNone(fa) ? b : f(fa.value, b) +export const Extend: Extend1 = { + URI, + map: _map, + extend: _extend +} /** - * @category Compactable + * @category instance operations * @since 2.0.0 */ export const compact: (fa: Option>) => Option = flatten @@ -655,14 +587,24 @@ const defaultSeparated = separated(none, none) /** - * @category Compactable + * @category instance operations * @since 2.0.0 */ export const separate: (ma: Option>) => Separated, Option> = (ma) => isNone(ma) ? defaultSeparated : separated(getLeft(ma.value), getRight(ma.value)) /** - * @category Filterable + * @category instances + * @since 2.7.0 + */ +export const Compactable: Compactable1 = { + URI, + compact, + separate +} + +/** + * @category instance operations * @since 2.0.0 */ export const filter: { @@ -672,14 +614,14 @@ export const filter: { isNone(fb) ? none : predicate(fb.value) ? fb : none /** - * @category Filterable + * @category instance operations * @since 2.0.0 */ export const filterMap: (f: (a: A) => Option) => (fa: Option) => Option = (f) => (fa) => isNone(fa) ? none : f(fa.value) /** - * @category Filterable + * @category instance operations * @since 2.0.0 */ export const partition: { @@ -689,7 +631,7 @@ export const partition: { separated(_filter(fb, not(predicate)), _filter(fb, predicate)) /** - * @category Filterable + * @category instance operations * @since 2.0.0 */ export const partitionMap: ( @@ -697,7 +639,22 @@ export const partitionMap: ( ) => (fa: Option) => Separated, Option> = (f) => flow(map(f), separate) /** - * @category Traversable + * @category instances + * @since 2.7.0 + */ +export const Filterable: Filterable1 = { + URI, + map: _map, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap +} + +/** + * @category instance operations * @since 2.6.3 */ export const traverse: PipeableTraverse1 = (F: ApplicativeHKT) => (f: (a: A) => HKT) => ( @@ -705,7 +662,7 @@ export const traverse: PipeableTraverse1 = (F: ApplicativeHKT) => > => (isNone(ta) ? F.of(none) : F.map(f(ta.value), some)) /** - * @category Traversable + * @category instance operations * @since 2.6.3 */ export const sequence: Traversable1['sequence'] = (F: ApplicativeHKT) => ( @@ -713,7 +670,29 @@ export const sequence: Traversable1['sequence'] = (F: ApplicativeHKT) ): HKT> => (isNone(ta) ? F.of(none) : F.map(ta.value, some)) /** - * @category Witherable + * @category instances + * @since 2.7.0 + */ +export const Traversable: Traversable1 = { + URI, + map: _map, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight, + traverse: _traverse, + sequence +} + +const _wither: Witherable1['wither'] = + /*#__PURE__*/ + witherDefault(Traversable, Compactable) + +const _wilt: Witherable1['wilt'] = + /*#__PURE__*/ + wiltDefault(Traversable, Compactable) + +/** + * @category instance operations * @since 2.6.5 */ export const wither: PipeableWither1 = ( @@ -724,7 +703,7 @@ export const wither: PipeableWither1 = ( } /** - * @category Witherable + * @category instance operations * @since 2.6.5 */ export const wilt: PipeableWilt1 = ( @@ -734,394 +713,391 @@ export const wilt: PipeableWilt1 = ( return (f) => (fa) => _wiltF(fa, f) } -// ------------------------------------------------------------------------------------- -// instances -// ------------------------------------------------------------------------------------- - /** * @category instances - * @since 2.0.0 + * @since 2.7.0 */ -export const URI = 'Option' +export const Witherable: Witherable1 = { + URI, + map: _map, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight, + traverse: _traverse, + sequence, + compact, + separate, + filter: _filter, + filterMap: _filterMap, + partition: _partition, + partitionMap: _partitionMap, + wither: _wither, + wilt: _wilt +} /** - * @category instances - * @since 2.0.0 + * @category instance operations + * @since 2.7.0 */ -export type URI = typeof URI - -declare module './HKT' { - interface URItoKind { - readonly [URI]: Option - } -} +export const throwError: MonadThrow1['throwError'] = () => none /** * @category instances - * @since 2.0.0 + * @since 2.7.0 */ -export function getShow(S: Show): Show> { - return { - show: (ma) => (isNone(ma) ? 'none' : `some(${S.show(ma.value)})`) - } +export const MonadThrow: MonadThrow1 = { + URI, + map: _map, + ap: _ap, + of, + chain: _chain, + throwError } /** - * @example - * import { none, some, getEq } from 'fp-ts/Option' - * import * as N from 'fp-ts/number' + * Transforms an `Either` to an `Option` discarding the error. * - * const E = getEq(N.Eq) - * assert.strictEqual(E.equals(none, none), true) - * assert.strictEqual(E.equals(none, some(1)), false) - * assert.strictEqual(E.equals(some(1), none), false) - * assert.strictEqual(E.equals(some(1), some(2)), false) - * assert.strictEqual(E.equals(some(1), some(1)), true) + * Alias of [getRight](#getright) * - * @category instances + * @category natural transformations * @since 2.0.0 */ -export function getEq(E: Eq): Eq> { - return { - equals: (x, y) => x === y || (isNone(x) ? isNone(y) : isNone(y) ? false : E.equals(x.value, y.value)) - } +export const fromEither: FromEither1['fromEither'] = getRight + +/** + * @category instances + * @since 2.11.0 + */ +export const FromEither: FromEither1 = { + URI, + fromEither } + /** - * The `Ord` instance allows `Option` values to be compared with - * `compare`, whenever there is an `Ord` instance for - * the type the `Option` contains. - * - * `None` is considered to be less than any `Some` value. - * + * @category combinators + * @since 2.11.0 + */ +export const fromEitherK = + /*#__PURE__*/ + fromEitherK_(FromEither) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainEitherK = + /*#__PURE__*/ + chainEitherK_(FromEither, Chain) + +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * Returns `true` if the option is an instance of `Some`, `false` otherwise. * * @example - * import { none, some, getOrd } from 'fp-ts/Option' - * import * as N from 'fp-ts/number' + * import { some, none, isSome } from 'fp-ts/Option' * - * const O = getOrd(N.Ord) - * assert.strictEqual(O.compare(none, none), 0) - * assert.strictEqual(O.compare(none, some(1)), -1) - * assert.strictEqual(O.compare(some(1), none), 1) - * assert.strictEqual(O.compare(some(1), some(2)), -1) - * assert.strictEqual(O.compare(some(1), some(1)), 0) + * assert.strictEqual(isSome(some(1)), true) + * assert.strictEqual(isSome(none), false) * - * @category instances + * @category refinements * @since 2.0.0 */ -export function getOrd(O: Ord): Ord> { - return { - equals: getEq(O).equals, - compare: (x, y) => (x === y ? 0 : isSome(x) ? (isSome(y) ? O.compare(x.value, y.value) : 1) : -1) - } -} +export const isSome: (fa: Option) => fa is Some = _.isSome /** - * Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are - * concatenated using the provided `Semigroup` - * - * | x | y | concat(x, y) | - * | ------- | ------- | ------------------ | - * | none | none | none | - * | some(a) | none | some(a) | - * | none | some(b) | some(b) | - * | some(a) | some(b) | some(concat(a, b)) | + * Returns `true` if the option is `None`, `false` otherwise. * * @example - * import { getMonoid, some, none } from 'fp-ts/Option' - * import { SemigroupSum } from 'fp-ts/number' + * import { some, none, isNone } from 'fp-ts/Option' * - * const M = getMonoid(SemigroupSum) - * assert.deepStrictEqual(M.concat(none, none), none) - * assert.deepStrictEqual(M.concat(some(1), none), some(1)) - * assert.deepStrictEqual(M.concat(none, some(1)), some(1)) - * assert.deepStrictEqual(M.concat(some(1), some(2)), some(3)) + * assert.strictEqual(isNone(some(1)), false) + * assert.strictEqual(isNone(none), true) * - * @category instances + * @category refinements * @since 2.0.0 */ -export function getMonoid(S: Semigroup): Monoid> { - return { - concat: (x, y) => (isNone(x) ? y : isNone(y) ? x : some(S.concat(x.value, y.value))), - empty: none - } -} +export const isNone = (fa: Option): fa is None => fa._tag === 'None' -/** - * @category instances - * @since 2.7.0 - */ -export const Functor: Functor1 = { - URI, - map: _map -} +// ------------------------------------------------------------------------------------- +// destructors +// ------------------------------------------------------------------------------------- /** - * Derivable from `Functor`. + * Less strict version of [`match`](#match). * - * @category combinators + * @category destructors * @since 2.10.0 */ -export const flap = - /*#__PURE__*/ - flap_(Functor) - -/** - * @category instances - * @since 2.10.0 - */ -export const Pointed: Pointed1 = { - URI, - of -} +export const matchW = (onNone: Lazy, onSome: (a: A) => C) => (ma: Option): B | C => + isNone(ma) ? onNone() : onSome(ma.value) /** - * @category instances + * Alias of [`matchW`](#matchw). + * + * @category destructors * @since 2.10.0 */ -export const Apply: Apply1 = { - URI, - map: _map, - ap: _ap -} +export const foldW = matchW /** - * Combine two effectful actions, keeping only the result of the first. + * Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is + * returned, otherwise the function is applied to the value inside the `Some` and the result is returned. * - * Derivable from `Apply`. + * @example + * import { some, none, match } from 'fp-ts/Option' + * import { pipe } from 'fp-ts/function' * - * @category combinators - * @since 2.0.0 - */ -export const apFirst = - /*#__PURE__*/ - apFirst_(Apply) - -/** - * Combine two effectful actions, keeping only the result of the second. + * assert.strictEqual( + * pipe( + * some(1), + * match(() => 'a none', a => `a some containing ${a}`) + * ), + * 'a some containing 1' + * ) * - * Derivable from `Apply`. + * assert.strictEqual( + * pipe( + * none, + * match(() => 'a none', a => `a some containing ${a}`) + * ), + * 'a none' + * ) * - * @category combinators - * @since 2.0.0 - */ -export const apSecond = - /*#__PURE__*/ - apSecond_(Apply) - -/** - * @category instances - * @since 2.7.0 - */ -export const Applicative: Applicative1 = { - URI, - map: _map, - ap: _ap, - of -} - -/** - * @category instances + * @category destructors * @since 2.10.0 */ -export const Chain: Chain1 = { - URI, - map: _map, - ap: _ap, - chain: _chain -} - -/** - * @category instances - * @since 2.7.0 - */ -export const Monad: Monad1 = { - URI, - map: _map, - ap: _ap, - of, - chain: _chain -} +export const match: (onNone: Lazy, onSome: (a: A) => B) => (ma: Option) => B = matchW /** - * Composes computations in sequence, using the return value of one computation to determine the next computation and - * keeping only the result of the first. - * - * Derivable from `Chain`. + * Alias of [`match`](#match). * - * @category combinators + * @category destructors * @since 2.0.0 */ -export const chainFirst = - /*#__PURE__*/ - chainFirst_(Chain) - -/** - * @category instances - * @since 2.7.0 - */ -export const Foldable: Foldable1 = { - URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight -} - -/** - * @category instances - * @since 2.7.0 - */ -export const Alt: Alt1 = { - URI, - map: _map, - alt: _alt -} - -/** - * @category instances - * @since 2.11.0 - */ -export const Zero: Zero1 = { - URI, - zero -} - -/** - * @category constructors - * @since 2.11.0 - */ -export const guard = - /*#__PURE__*/ - guard_(Zero, Pointed) +export const fold = match /** - * @category instances - * @since 2.7.0 + * Less strict version of [`getOrElse`](#getorelse). + * + * @category destructors + * @since 2.6.0 */ -export const Alternative: Alternative1 = { - URI, - map: _map, - ap: _ap, - of, - alt: _alt, - zero -} +export const getOrElseW = (onNone: Lazy) => (ma: Option): A | B => (isNone(ma) ? onNone() : ma.value) /** - * @category instances - * @since 2.7.0 + * Extracts the value out of the structure, if it exists. Otherwise returns the given default value + * + * @example + * import { some, none, getOrElse } from 'fp-ts/Option' + * import { pipe } from 'fp-ts/function' + * + * assert.strictEqual( + * pipe( + * some(1), + * getOrElse(() => 0) + * ), + * 1 + * ) + * assert.strictEqual( + * pipe( + * none, + * getOrElse(() => 0) + * ), + * 0 + * ) + * + * @category destructors + * @since 2.0.0 */ -export const Extend: Extend1 = { - URI, - map: _map, - extend: _extend -} +export const getOrElse: (onNone: Lazy) => (ma: Option) => A = getOrElseW -/** - * @category instances - * @since 2.7.0 - */ -export const Compactable: Compactable1 = { - URI, - compact, - separate -} +// ------------------------------------------------------------------------------------- +// interop +// ------------------------------------------------------------------------------------- /** - * @category instances - * @since 2.7.0 + * Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise + * returns the value wrapped in a `Some`. + * + * @example + * import { none, some, fromNullable } from 'fp-ts/Option' + * + * assert.deepStrictEqual(fromNullable(undefined), none) + * assert.deepStrictEqual(fromNullable(null), none) + * assert.deepStrictEqual(fromNullable(1), some(1)) + * + * @category interop + * @since 2.0.0 */ -export const Filterable: Filterable1 = { - URI, - map: _map, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap -} +export const fromNullable = (a: A): Option> => (a == null ? none : some(a as NonNullable)) /** - * @category instances - * @since 2.7.0 + * Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in a + * `Some`. + * + * See also [`tryCatchK`](#trycatchk). + * + * @example + * import { none, some, tryCatch } from 'fp-ts/Option' + * + * assert.deepStrictEqual( + * tryCatch(() => { + * throw new Error() + * }), + * none + * ) + * assert.deepStrictEqual(tryCatch(() => 1), some(1)) + * + * @category interop + * @since 2.0.0 */ -export const Traversable: Traversable1 = { - URI, - map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - traverse: _traverse, - sequence +export const tryCatch = (f: Lazy): Option => { + try { + return some(f()) + } catch (e) { + return none + } } -const _wither: Witherable1['wither'] = - /*#__PURE__*/ - witherDefault(Traversable, Compactable) - -const _wilt: Witherable1['wilt'] = - /*#__PURE__*/ - wiltDefault(Traversable, Compactable) - /** - * @category instances - * @since 2.7.0 + * Converts a function that may throw to one returning a `Option`. + * + * @category interop + * @since 2.10.0 */ -export const Witherable: Witherable1 = { - URI, - map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - traverse: _traverse, - sequence, - compact, - separate, - filter: _filter, - filterMap: _filterMap, - partition: _partition, - partitionMap: _partitionMap, - wither: _wither, - wilt: _wilt -} +export const tryCatchK = , B>(f: (...a: A) => B): ((...a: A) => Option) => (...a) => + tryCatch(() => f(...a)) /** - * @category instances - * @since 2.7.0 + * Returns a *smart constructor* from a function that returns a nullable value. + * + * @example + * import { fromNullableK, none, some } from 'fp-ts/Option' + * + * const f = (s: string): number | undefined => { + * const n = parseFloat(s) + * return isNaN(n) ? undefined : n + * } + * + * const g = fromNullableK(f) + * + * assert.deepStrictEqual(g('1'), some(1)) + * assert.deepStrictEqual(g('a'), none) + * + * @category interop + * @since 2.9.0 */ -export const MonadThrow: MonadThrow1 = { - URI, - map: _map, - ap: _ap, - of, - chain: _chain, - throwError -} +export const fromNullableK: , B>( + f: (...a: A) => B | null | undefined +) => (...a: A) => Option> = (f) => flow(f, fromNullable) /** - * @category instances - * @since 2.11.0 + * This is `chain` + `fromNullable`, useful when working with optional values. + * + * @example + * import { some, none, fromNullable, chainNullableK } from 'fp-ts/Option' + * import { pipe } from 'fp-ts/function' + * + * interface Employee { + * readonly company?: { + * readonly address?: { + * readonly street?: { + * readonly name?: string + * } + * } + * } + * } + * + * const employee1: Employee = { company: { address: { street: { name: 'high street' } } } } + * + * assert.deepStrictEqual( + * pipe( + * fromNullable(employee1.company), + * chainNullableK(company => company.address), + * chainNullableK(address => address.street), + * chainNullableK(street => street.name) + * ), + * some('high street') + * ) + * + * const employee2: Employee = { company: { address: { street: {} } } } + * + * assert.deepStrictEqual( + * pipe( + * fromNullable(employee2.company), + * chainNullableK(company => company.address), + * chainNullableK(address => address.street), + * chainNullableK(street => street.name) + * ), + * none + * ) + * + * @category interop + * @since 2.9.0 */ -export const FromEither: FromEither1 = { - URI, - fromEither -} +export const chainNullableK = (f: (a: A) => B | null | undefined) => (ma: Option): Option => + isNone(ma) ? none : fromNullable(f(ma.value)) /** - * @category combinators - * @since 2.11.0 + * Extracts the value out of the structure, if it exists. Otherwise returns `null`. + * + * @example + * import { some, none, toNullable } from 'fp-ts/Option' + * import { pipe } from 'fp-ts/function' + * + * assert.strictEqual( + * pipe( + * some(1), + * toNullable + * ), + * 1 + * ) + * assert.strictEqual( + * pipe( + * none, + * toNullable + * ), + * null + * ) + * + * @category interop + * @since 2.0.0 */ -export const fromEitherK = +export const toNullable: (ma: Option) => A | null = /*#__PURE__*/ - fromEitherK_(FromEither) + match(constNull, identity) /** - * @category combinators - * @since 2.11.0 + * Extracts the value out of the structure, if it exists. Otherwise returns `undefined`. + * + * @example + * import { some, none, toUndefined } from 'fp-ts/Option' + * import { pipe } from 'fp-ts/function' + * + * assert.strictEqual( + * pipe( + * some(1), + * toUndefined + * ), + * 1 + * ) + * assert.strictEqual( + * pipe( + * none, + * toUndefined + * ), + * undefined + * ) + * + * @category interop + * @since 2.0.0 */ -export const chainEitherK = +export const toUndefined: (ma: Option) => A | undefined = /*#__PURE__*/ - chainEitherK_(FromEither, Chain) + match(constUndefined, identity) // ------------------------------------------------------------------------------------- // utils From de74cd2816e0f40b5b79ff09c25c18c88256e748 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 7 May 2021 10:24:56 +0200 Subject: [PATCH 144/162] Docs: refactor Either --- docs/modules/Either.ts.md | 746 ++++++++++---------- src/Either.ts | 1394 ++++++++++++++++++------------------- src/Option.ts | 172 ++--- 3 files changed, 1135 insertions(+), 1177 deletions(-) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index b9903dea0..32e8810ec 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -24,33 +24,6 @@ Added in v2.0.0

Table of contents

-- [Alt](#alt) - - [alt](#alt) - - [altW](#altw) -- [Apply](#apply) - - [ap](#ap) - - [apW](#apw) -- [Bifunctor](#bifunctor) - - [bimap](#bimap) - - [mapLeft](#mapleft) -- [Extend](#extend) - - [extend](#extend) -- [Foldable](#foldable) - - [foldMap](#foldmap) - - [reduce](#reduce) - - [reduceRight](#reduceright) -- [Functor](#functor) - - [map](#map) -- [Monad](#monad) - - [chain](#chain) - - [chainW](#chainw) -- [MonadThrow](#monadthrow) - - [throwError](#throwerror) -- [Pointed](#pointed) - - [of](#of) -- [Traversable](#traversable) - - [sequence](#sequence) - - [traverse](#traverse) - [combinators](#combinators) - [apFirst](#apfirst) - [apSecond](#apsecond) @@ -80,21 +53,39 @@ Added in v2.0.0 - [getOrElseW](#getorelsew) - [match](#match) - [matchW](#matchw) +- [instance operations](#instance-operations) + - [alt](#alt) + - [altW](#altw) + - [ap](#ap) + - [apW](#apw) + - [bimap](#bimap) + - [chain](#chain) + - [chainW](#chainw) + - [extend](#extend) + - [foldMap](#foldmap) + - [map](#map) + - [mapLeft](#mapleft) + - [of](#of) + - [reduce](#reduce) + - [reduceRight](#reduceright) + - [sequence](#sequence) + - [throwError](#throwerror) + - [traverse](#traverse) - [instances](#instances) - - [Alt](#alt-1) + - [Alt](#alt) - [Applicative](#applicative) - - [Apply](#apply-1) - - [Bifunctor](#bifunctor-1) + - [Apply](#apply) + - [Bifunctor](#bifunctor) - [Chain](#chain) - [ChainRec](#chainrec) - - [Extend](#extend-1) - - [Foldable](#foldable-1) + - [Extend](#extend) + - [Foldable](#foldable) - [FromEither](#fromeither) - - [Functor](#functor-1) - - [Monad](#monad-1) - - [MonadThrow](#monadthrow-1) - - [Pointed](#pointed-1) - - [Traversable](#traversable-1) + - [Functor](#functor) + - [Monad](#monad) + - [MonadThrow](#monadthrow) + - [Pointed](#pointed) + - [Traversable](#traversable) - [URI](#uri) - [URI (type alias)](#uri-type-alias) - [getAltValidation](#getaltvalidation) @@ -149,749 +140,728 @@ Added in v2.0.0 --- -# Alt - -## alt - -Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to -types of kind `* -> *`. - -**Signature** - -```ts -export declare const alt: (that: Lazy>) => (fa: Either) => Either -``` +# combinators -Added in v2.0.0 +## apFirst -## altW +Combine two effectful actions, keeping only the result of the first. -Less strict version of [`alt`](#alt). +Derivable from `Apply`. **Signature** ```ts -export declare const altW: (that: Lazy>) => (fa: Either) => Either +export declare const apFirst: (second: Either) =>
(first: Either) => Either ``` -Added in v2.9.0 +Added in v2.0.0 -# Apply +## apSecond -## ap +Combine two effectful actions, keeping only the result of the second. -Apply a function to an argument under a type constructor. +Derivable from `Apply`. **Signature** ```ts -export declare const ap: (fa: Either) => (fab: Either B>) => Either +export declare const apSecond: (second: Either) => (first: Either) => Either ``` Added in v2.0.0 -## apW +## chainFirst -Less strict version of [`ap`](#ap). +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +Derivable from `Chain`. **Signature** ```ts -export declare const apW: (fa: Either) => (fab: Either B>) => Either +export declare const chainFirst: (f: (a: A) => Either) => (ma: Either) => Either ``` -Added in v2.8.0 +Added in v2.0.0 -# Bifunctor +## chainFirstW -## bimap +Less strict version of [`chainFirst`](#chainfirst) -Map a pair of functions over the two type arguments of the bifunctor. +Derivable from `Chain`. **Signature** ```ts -export declare const bimap: (f: (e: E) => G, g: (a: A) => B) => (fa: Either) => Either +export declare const chainFirstW: ( + f: (a: A) => Either +) => (ma: Either) => Either ``` -Added in v2.0.0 - -## mapLeft +Added in v2.8.0 -Map a function over the first type argument of a bifunctor. +## chainOptionK **Signature** ```ts -export declare const mapLeft: (f: (e: E) => G) => (fa: Either) => Either +export declare const chainOptionK: ( + onNone: Lazy +) => (f: (a: A) => Option) => (ma: Either) => Either ``` -Added in v2.0.0 +Added in v2.11.0 -# Extend +## duplicate -## extend +Derivable from `Extend`. **Signature** ```ts -export declare const extend: (f: (wa: Either) => B) => (wa: Either) => Either +export declare const duplicate: (ma: Either) => Either> ``` Added in v2.0.0 -# Foldable - -## foldMap - -Map each element of the structure to a monoid, and combine the results. +## filterOrElse **Signature** ```ts -export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Either) => M +export declare const filterOrElse: { + (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E): (mb: Either) => Either +} ``` **Example** ```ts -import { pipe } from 'fp-ts/function' import * as E from 'fp-ts/Either' -import * as S from 'fp-ts/string' - -const yell = (a: string) => `${a}!` - -assert.deepStrictEqual(pipe(E.right('a'), E.foldMap(S.Monoid)(yell)), 'a!') +import { pipe } from 'fp-ts/function' -assert.deepStrictEqual(pipe(E.left('e'), E.foldMap(S.Monoid)(yell)), S.Monoid.empty) +assert.deepStrictEqual( + pipe( + E.right(1), + E.filterOrElse( + (n) => n > 0, + () => 'error' + ) + ), + E.right(1) +) +assert.deepStrictEqual( + pipe( + E.right(-1), + E.filterOrElse( + (n) => n > 0, + () => 'error' + ) + ), + E.left('error') +) +assert.deepStrictEqual( + pipe( + E.left('a'), + E.filterOrElse( + (n) => n > 0, + () => 'error' + ) + ), + E.left('a') +) ``` Added in v2.0.0 -## reduce +## filterOrElseW -Left-associative fold of a structure. +Less strict version of [`filterOrElse`](#filterorelse). **Signature** ```ts -export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) => B +export declare const filterOrElseW: { + (refinement: Refinement, onFalse: (a: A) => E2): ( + ma: Either + ) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either +} ``` -**Example** +Added in v2.9.0 -```ts -import { pipe } from 'fp-ts/function' -import * as E from 'fp-ts/Either' +## flap -const startWith = 'prefix' -const concat = (a: string, b: string) => `${a}:${b}` +Derivable from `Functor`. -assert.deepStrictEqual(pipe(E.right('a'), E.reduce(startWith, concat)), 'prefix:a') +**Signature** -assert.deepStrictEqual(pipe(E.left('e'), E.reduce(startWith, concat)), 'prefix') +```ts +export declare const flap: (a: A) => (fab: Either B>) => Either ``` -Added in v2.0.0 +Added in v2.10.0 -## reduceRight +## flatten -Right-associative fold of a structure. +The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level. + +Derivable from `Chain`. **Signature** ```ts -export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Either) => B +export declare const flatten: (mma: Either>) => Either ``` **Example** ```ts -import { pipe } from 'fp-ts/function' import * as E from 'fp-ts/Either' -const startWith = 'postfix' -const concat = (a: string, b: string) => `${a}:${b}` - -assert.deepStrictEqual(pipe(E.right('a'), E.reduceRight(startWith, concat)), 'a:postfix') - -assert.deepStrictEqual(pipe(E.left('e'), E.reduceRight(startWith, concat)), 'postfix') +assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a')) +assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e')) +assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e')) ``` Added in v2.0.0 -# Functor - -## map +## flattenW -`map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types -use the type constructor `F` to represent some computational context. +Less strict version of [`flatten`](#flatten). **Signature** ```ts -export declare const map: (f: (a: A) => B) => (fa: Either) => Either +export declare const flattenW: (mma: Either>) => Either ``` -Added in v2.0.0 - -# Monad - -## chain +Added in v2.11.0 -Composes computations in sequence, using the return value of one computation to determine the next computation. +## fromOptionK **Signature** ```ts -export declare const chain: (f: (a: A) => Either) => (ma: Either) => Either +export declare const fromOptionK: (onNone: Lazy) => (f: (...a: A) => Option) => (...a: A) => Either ``` -Added in v2.0.0 +Added in v2.10.0 -## chainW +## orElse -Less strict version of [`chain`](#chain). +Useful for recovering from errors. **Signature** ```ts -export declare const chainW: (f: (a: A) => Either) => (ma: Either) => Either +export declare const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either ``` -Added in v2.6.0 +Added in v2.0.0 -# MonadThrow +## orElseW -## throwError +Less strict version of [`orElse`](#orelse). **Signature** ```ts -export declare const throwError: (e: E) => Either +export declare const orElseW: ( + onLeft: (e: E1) => Either +) => (ma: Either) => Either ``` -Added in v2.6.3 +Added in v2.10.0 -# Pointed +## swap -## of +Returns a `Right` if is a `Left` (and vice versa). **Signature** ```ts -export declare const of: (a: A) => Either +export declare const swap: (ma: Either) => Either ``` -Added in v2.7.0 - -# Traversable +Added in v2.0.0 -## sequence +# constructors -Evaluate each monadic action in the structure from left to right, and collect the results. +## fromPredicate **Signature** ```ts -export declare const sequence: Sequence2<'Either'> +export declare const fromPredicate: { + (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Either +} ``` **Example** ```ts +import { fromPredicate, left, right } from 'fp-ts/Either' import { pipe } from 'fp-ts/function' -import * as E from 'fp-ts/Either' -import * as O from 'fp-ts/Option' - -assert.deepStrictEqual(pipe(E.right(O.some('a')), E.sequence(O.Applicative)), O.some(E.right('a'))) -assert.deepStrictEqual(pipe(E.right(O.none), E.sequence(O.Applicative)), O.none) -``` - -Added in v2.6.3 - -## traverse - -Map each element of a structure to an action, evaluate these actions from left to right, and collect the results. - -**Signature** - -```ts -export declare const traverse: PipeableTraverse2<'Either'> -``` - -**Example** - -```ts -import { pipe } from 'fp-ts/function' -import * as RA from 'fp-ts/ReadonlyArray' -import * as E from 'fp-ts/Either' -import * as O from 'fp-ts/Option' - -assert.deepStrictEqual(pipe(E.right(['a']), E.traverse(O.Applicative)(RA.head)), O.some(E.right('a'))) - -assert.deepStrictEqual(pipe(E.right([]), E.traverse(O.Applicative)(RA.head)), O.none) +assert.deepStrictEqual( + pipe( + 1, + fromPredicate( + (n) => n > 0, + () => 'error' + ) + ), + right(1) +) +assert.deepStrictEqual( + pipe( + -1, + fromPredicate( + (n) => n > 0, + () => 'error' + ) + ), + left('error') +) ``` -Added in v2.6.3 - -# combinators - -## apFirst +Added in v2.0.0 -Combine two effectful actions, keeping only the result of the first. +## left -Derivable from `Apply`. +Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this +structure. **Signature** ```ts -export declare const apFirst: (second: Either) => (first: Either) => Either +export declare const left: (e: E) => Either ``` Added in v2.0.0 -## apSecond - -Combine two effectful actions, keeping only the result of the second. +## right -Derivable from `Apply`. +Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias +of this structure. **Signature** ```ts -export declare const apSecond: (second: Either) => (first: Either) => Either +export declare const right: (a: A) => Either ``` Added in v2.0.0 -## chainFirst - -Composes computations in sequence, using the return value of one computation to determine the next computation and -keeping only the result of the first. +## ~~parseJSON~~ -Derivable from `Chain`. +Use [`parse`](./Json.ts.html#parse) instead. **Signature** ```ts -export declare const chainFirst: (f: (a: A) => Either) => (ma: Either) => Either +export declare function parseJSON(s: string, onError: (reason: unknown) => E): Either ``` Added in v2.0.0 -## chainFirstW - -Less strict version of [`chainFirst`](#chainfirst) +## ~~stringifyJSON~~ -Derivable from `Chain`. +Use [`stringify`](./Json.ts.html#stringify) instead. **Signature** ```ts -export declare const chainFirstW: ( - f: (a: A) => Either -) => (ma: Either) => Either +export declare const stringifyJSON: (u: unknown, onError: (reason: unknown) => E) => Either ``` -Added in v2.8.0 +Added in v2.0.0 -## chainOptionK +# destructors + +## fold + +Alias of [`match`](#match). **Signature** ```ts -export declare const chainOptionK: ( - onNone: Lazy -) => (f: (a: A) => Option) => (ma: Either) => Either +export declare const fold: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B ``` -Added in v2.11.0 +Added in v2.0.0 -## duplicate +## foldW -Derivable from `Extend`. +Alias of [`matchW`](#matchw). **Signature** ```ts -export declare const duplicate: (ma: Either) => Either> +export declare const foldW: (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either) => B | C ``` -Added in v2.0.0 +Added in v2.10.0 -## filterOrElse +## getOrElse + +Returns the wrapped value if it's a `Right` or a default value if is a `Left`. **Signature** ```ts -export declare const filterOrElse: { - (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either - (predicate: Predicate, onFalse: (a: A) => E): (mb: Either) => Either -} +export declare const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A ``` **Example** ```ts -import * as E from 'fp-ts/Either' +import { getOrElse, left, right } from 'fp-ts/Either' import { pipe } from 'fp-ts/function' assert.deepStrictEqual( pipe( - E.right(1), - E.filterOrElse( - (n) => n > 0, - () => 'error' - ) - ), - E.right(1) -) -assert.deepStrictEqual( - pipe( - E.right(-1), - E.filterOrElse( - (n) => n > 0, - () => 'error' - ) + right(1), + getOrElse(() => 0) ), - E.left('error') + 1 ) assert.deepStrictEqual( pipe( - E.left('a'), - E.filterOrElse( - (n) => n > 0, - () => 'error' - ) + left('error'), + getOrElse(() => 0) ), - E.left('a') + 0 ) ``` Added in v2.0.0 -## filterOrElseW +## getOrElseW -Less strict version of [`filterOrElse`](#filterorelse). +Less strict version of [`getOrElse`](#getorelse). **Signature** ```ts -export declare const filterOrElseW: { - (refinement: Refinement, onFalse: (a: A) => E2): ( - ma: Either - ) => Either - (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either -} +export declare const getOrElseW: (onLeft: (e: E) => B) => (ma: Either) => B | A ``` -Added in v2.9.0 +Added in v2.6.0 -## flap +## match -Derivable from `Functor`. +Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function, +if the value is a `Right` the inner value is applied to the second function. **Signature** ```ts -export declare const flap: (a: A) => (fab: Either B>) => Either +export declare const match: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B ``` -Added in v2.10.0 - -## flatten +**Example** -The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level. +```ts +import { match, left, right } from 'fp-ts/Either' +import { pipe } from 'fp-ts/function' -Derivable from `Chain`. +function onLeft(errors: Array): string { + return `Errors: ${errors.join(', ')}` +} -**Signature** +function onRight(value: number): string { + return `Ok: ${value}` +} -```ts -export declare const flatten: (mma: Either>) => Either +assert.strictEqual(pipe(right(1), match(onLeft, onRight)), 'Ok: 1') +assert.strictEqual(pipe(left(['error 1', 'error 2']), match(onLeft, onRight)), 'Errors: error 1, error 2') ``` -**Example** +Added in v2.10.0 -```ts -import * as E from 'fp-ts/Either' +## matchW -assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a')) -assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e')) -assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e')) +Less strict version of [`match`](#match). + +**Signature** + +```ts +export declare const matchW: (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either) => B | C ``` -Added in v2.0.0 +Added in v2.10.0 -## flattenW +# instance operations -Less strict version of [`flatten`](#flatten). +## alt + +Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to +types of kind `* -> *`. **Signature** ```ts -export declare const flattenW: (mma: Either>) => Either +export declare const alt: (that: Lazy>) => (fa: Either) => Either ``` -Added in v2.11.0 +Added in v2.0.0 -## fromOptionK +## altW + +Less strict version of [`alt`](#alt). **Signature** ```ts -export declare const fromOptionK: (onNone: Lazy) => (f: (...a: A) => Option) => (...a: A) => Either +export declare const altW: (that: Lazy>) => (fa: Either) => Either ``` -Added in v2.10.0 +Added in v2.9.0 -## orElse +## ap -Useful for recovering from errors. +Apply a function to an argument under a type constructor. **Signature** ```ts -export declare const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either +export declare const ap: (fa: Either) => (fab: Either B>) => Either ``` Added in v2.0.0 -## orElseW +## apW -Less strict version of [`orElse`](#orelse). +Less strict version of [`ap`](#ap). **Signature** ```ts -export declare const orElseW: ( - onLeft: (e: E1) => Either -) => (ma: Either) => Either +export declare const apW: (fa: Either) => (fab: Either B>) => Either ``` -Added in v2.10.0 +Added in v2.8.0 -## swap +## bimap -Returns a `Right` if is a `Left` (and vice versa). +Map a pair of functions over the two type arguments of the bifunctor. **Signature** ```ts -export declare function swap(ma: Either): Either +export declare const bimap: (f: (e: E) => G, g: (a: A) => B) => (fa: Either) => Either ``` Added in v2.0.0 -# constructors +## chain -## fromPredicate +Composes computations in sequence, using the return value of one computation to determine the next computation. **Signature** ```ts -export declare const fromPredicate: { - (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either - (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Either -} +export declare const chain: (f: (a: A) => Either) => (ma: Either) => Either ``` -**Example** +Added in v2.0.0 -```ts -import { fromPredicate, left, right } from 'fp-ts/Either' -import { pipe } from 'fp-ts/function' +## chainW -assert.deepStrictEqual( - pipe( - 1, - fromPredicate( - (n) => n > 0, - () => 'error' - ) - ), - right(1) -) -assert.deepStrictEqual( - pipe( - -1, - fromPredicate( - (n) => n > 0, - () => 'error' - ) - ), - left('error') -) -``` +Less strict version of [`chain`](#chain). -Added in v2.0.0 +**Signature** -## left +```ts +export declare const chainW: (f: (a: A) => Either) => (ma: Either) => Either +``` -Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this -structure. +Added in v2.6.0 + +## extend **Signature** ```ts -export declare const left: (e: E) => Either +export declare const extend: (f: (wa: Either) => B) => (wa: Either) => Either ``` Added in v2.0.0 -## right +## foldMap -Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias -of this structure. +Map each element of the structure to a monoid, and combine the results. **Signature** ```ts -export declare const right: (a: A) => Either +export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Either) => M ``` -Added in v2.0.0 +**Example** -## ~~parseJSON~~ +```ts +import { pipe } from 'fp-ts/function' +import * as E from 'fp-ts/Either' +import * as S from 'fp-ts/string' -Use [`parse`](./Json.ts.html#parse) instead. +const yell = (a: string) => `${a}!` -**Signature** +assert.deepStrictEqual(pipe(E.right('a'), E.foldMap(S.Monoid)(yell)), 'a!') -```ts -export declare function parseJSON(s: string, onError: (reason: unknown) => E): Either +assert.deepStrictEqual(pipe(E.left('e'), E.foldMap(S.Monoid)(yell)), S.Monoid.empty) ``` Added in v2.0.0 -## ~~stringifyJSON~~ - -Use [`stringify`](./Json.ts.html#stringify) instead. +## map **Signature** ```ts -export declare const stringifyJSON: (u: unknown, onError: (reason: unknown) => E) => Either +export declare const map: (f: (a: A) => B) => (fa: Either) => Either ``` Added in v2.0.0 -# destructors - -## fold +## mapLeft -Alias of [`match`](#match). +Map a function over the first type argument of a bifunctor. **Signature** ```ts -export declare const fold: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B +export declare const mapLeft: (f: (e: E) => G) => (fa: Either) => Either ``` Added in v2.0.0 -## foldW - -Alias of [`matchW`](#matchw). +## of **Signature** ```ts -export declare const foldW: (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either) => B | C +export declare const of: (a: A) => Either ``` -Added in v2.10.0 +Added in v2.7.0 -## getOrElse +## reduce -Returns the wrapped value if it's a `Right` or a default value if is a `Left`. +Left-associative fold of a structure. **Signature** ```ts -export declare const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A +export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) => B ``` **Example** ```ts -import { getOrElse, left, right } from 'fp-ts/Either' import { pipe } from 'fp-ts/function' +import * as E from 'fp-ts/Either' -assert.deepStrictEqual( - pipe( - right(1), - getOrElse(() => 0) - ), - 1 -) -assert.deepStrictEqual( - pipe( - left('error'), - getOrElse(() => 0) - ), - 0 -) +const startWith = 'prefix' +const concat = (a: string, b: string) => `${a}:${b}` + +assert.deepStrictEqual(pipe(E.right('a'), E.reduce(startWith, concat)), 'prefix:a') + +assert.deepStrictEqual(pipe(E.left('e'), E.reduce(startWith, concat)), 'prefix') ``` Added in v2.0.0 -## getOrElseW +## reduceRight -Less strict version of [`getOrElse`](#getorelse). +Right-associative fold of a structure. **Signature** ```ts -export declare const getOrElseW: (onLeft: (e: E) => B) => (ma: Either) => B | A +export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Either) => B ``` -Added in v2.6.0 +**Example** -## match +```ts +import { pipe } from 'fp-ts/function' +import * as E from 'fp-ts/Either' -Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function, -if the value is a `Right` the inner value is applied to the second function. +const startWith = 'postfix' +const concat = (a: string, b: string) => `${a}:${b}` + +assert.deepStrictEqual(pipe(E.right('a'), E.reduceRight(startWith, concat)), 'a:postfix') + +assert.deepStrictEqual(pipe(E.left('e'), E.reduceRight(startWith, concat)), 'postfix') +``` + +Added in v2.0.0 + +## sequence + +Evaluate each monadic action in the structure from left to right, and collect the results. **Signature** ```ts -export declare const match: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B +export declare const sequence: Sequence2<'Either'> ``` **Example** ```ts -import { match, left, right } from 'fp-ts/Either' import { pipe } from 'fp-ts/function' +import * as E from 'fp-ts/Either' +import * as O from 'fp-ts/Option' -function onLeft(errors: Array): string { - return `Errors: ${errors.join(', ')}` -} +assert.deepStrictEqual(pipe(E.right(O.some('a')), E.sequence(O.Applicative)), O.some(E.right('a'))) -function onRight(value: number): string { - return `Ok: ${value}` -} +assert.deepStrictEqual(pipe(E.right(O.none), E.sequence(O.Applicative)), O.none) +``` -assert.strictEqual(pipe(right(1), match(onLeft, onRight)), 'Ok: 1') -assert.strictEqual(pipe(left(['error 1', 'error 2']), match(onLeft, onRight)), 'Errors: error 1, error 2') +Added in v2.6.3 + +## throwError + +**Signature** + +```ts +export declare const throwError: (e: E) => Either ``` -Added in v2.10.0 +Added in v2.6.3 -## matchW +## traverse -Less strict version of [`match`](#match). +Map each element of a structure to an action, evaluate these actions from left to right, and collect the results. **Signature** ```ts -export declare const matchW: (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either) => B | C +export declare const traverse: PipeableTraverse2<'Either'> ``` -Added in v2.10.0 +**Example** + +```ts +import { pipe } from 'fp-ts/function' +import * as RA from 'fp-ts/ReadonlyArray' +import * as E from 'fp-ts/Either' +import * as O from 'fp-ts/Option' + +assert.deepStrictEqual(pipe(E.right(['a']), E.traverse(O.Applicative)(RA.head)), O.some(E.right('a'))) + +assert.deepStrictEqual(pipe(E.right([]), E.traverse(O.Applicative)(RA.head)), O.none) +``` + +Added in v2.6.3 # instances @@ -1060,7 +1030,7 @@ Added in v2.0.0 **Signature** ```ts -export declare function getAltValidation(SE: Semigroup): Alt2C +export declare const getAltValidation: (SE: Semigroup) => Alt2C<'Either', E> ``` Added in v2.7.0 @@ -1070,7 +1040,7 @@ Added in v2.7.0 **Signature** ```ts -export declare function getApplicativeValidation(SE: Semigroup): Applicative2C +export declare const getApplicativeValidation: (SE: Semigroup) => Applicative2C<'Either', E> ``` Added in v2.7.0 @@ -1092,7 +1062,7 @@ Added in v2.10.0 **Signature** ```ts -export declare function getEq(EL: Eq, EA: Eq): Eq> +export declare const getEq: (EL: Eq, EA: Eq) => Eq> ``` Added in v2.0.0 @@ -1104,7 +1074,7 @@ Builds a `Filterable` instance for `Either` given `Monoid` for the left side **Signature** ```ts -export declare function getFilterable(M: Monoid): Filterable2C +export declare const getFilterable: (M: Monoid) => Filterable2C<'Either', E> ``` Added in v2.10.0 @@ -1117,7 +1087,7 @@ concatenated using the provided `Semigroup` **Signature** ```ts -export declare function getSemigroup(S: Semigroup): Semigroup> +export declare const getSemigroup: (S: Semigroup) => Semigroup> ``` **Example** @@ -1140,7 +1110,7 @@ Added in v2.0.0 **Signature** ```ts -export declare function getShow(SE: Show, SA: Show): Show> +export declare const getShow: (SE: Show, SA: Show) => Show> ``` Added in v2.0.0 @@ -1152,7 +1122,7 @@ Builds `Witherable` instance for `Either` given `Monoid` for the left side **Signature** ```ts -export declare function getWitherable(M: Monoid): Witherable2C +export declare const getWitherable: (M: Monoid) => Witherable2C<'Either', E> ``` Added in v2.0.0 diff --git a/src/Either.ts b/src/Either.ts index 751c1a66f..720934f47 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -84,26 +84,6 @@ export interface Right { */ export type Either = Left | Right -// ------------------------------------------------------------------------------------- -// refinements -// ------------------------------------------------------------------------------------- - -/** - * Returns `true` if the either is an instance of `Left`, `false` otherwise. - * - * @category refinements - * @since 2.0.0 - */ -export const isLeft: (ma: Either) => ma is Left = _.isLeft - -/** - * Returns `true` if the either is an instance of `Right`, `false` otherwise. - * - * @category refinements - * @since 2.0.0 - */ -export const isRight: (ma: Either) => ma is Right = _.isRight - // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- @@ -126,237 +106,6 @@ export const left: (e: E) => Either = _.left */ export const right: (a: A) => Either = _.right -// ------------------------------------------------------------------------------------- -// destructors -// ------------------------------------------------------------------------------------- - -/** - * Less strict version of [`match`](#match). - * - * @category destructors - * @since 2.10.0 - */ -export const matchW = (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either): B | C => - isLeft(ma) ? onLeft(ma.left) : onRight(ma.right) - -/** - * Alias of [`matchW`](#matchw). - * - * @category destructors - * @since 2.10.0 - */ -export const foldW = matchW - -/** - * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function, - * if the value is a `Right` the inner value is applied to the second function. - * - * @example - * import { match, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * - * function onLeft(errors: Array): string { - * return `Errors: ${errors.join(', ')}` - * } - * - * function onRight(value: number): string { - * return `Ok: ${value}` - * } - * - * assert.strictEqual( - * pipe( - * right(1), - * match(onLeft, onRight) - * ), - * 'Ok: 1' - * ) - * assert.strictEqual( - * pipe( - * left(['error 1', 'error 2']), - * match(onLeft, onRight) - * ), - * 'Errors: error 1, error 2' - * ) - * - * @category destructors - * @since 2.10.0 - */ -export const match: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B = matchW - -/** - * Alias of [`match`](#match). - * - * @category destructors - * @since 2.0.0 - */ -export const fold: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B = match - -/** - * Less strict version of [`getOrElse`](#getorelse). - * - * @category destructors - * @since 2.6.0 - */ -export const getOrElseW = (onLeft: (e: E) => B) => (ma: Either): A | B => - isLeft(ma) ? onLeft(ma.left) : ma.right - -/** - * Returns the wrapped value if it's a `Right` or a default value if is a `Left`. - * - * @example - * import { getOrElse, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * - * assert.deepStrictEqual( - * pipe( - * right(1), - * getOrElse(() => 0) - * ), - * 1 - * ) - * assert.deepStrictEqual( - * pipe( - * left('error'), - * getOrElse(() => 0) - * ), - * 0 - * ) - * - * @category destructors - * @since 2.0.0 - */ -export const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A = getOrElseW - -// ------------------------------------------------------------------------------------- -// interop -// ------------------------------------------------------------------------------------- - -// TODO: make lazy in v3 -/** - * Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use - * the provided default as a `Left`. - * - * @example - * import { fromNullable, left, right } from 'fp-ts/Either' - * - * const parse = fromNullable('nully') - * - * assert.deepStrictEqual(parse(1), right(1)) - * assert.deepStrictEqual(parse(null), left('nully')) - * - * @category interop - * @since 2.0.0 - */ -export const fromNullable = (e: E) => (a: A): Either> => - a == null ? left(e) : right(a as NonNullable) - -/** - * Constructs a new `Either` from a function that might throw. - * - * See also [`tryCatchK`](#trycatchk). - * - * @example - * import * as E from 'fp-ts/Either' - * - * const unsafeHead = (as: ReadonlyArray): A => { - * if (as.length > 0) { - * return as[0] - * } else { - * throw new Error('empty array') - * } - * } - * - * const head = (as: ReadonlyArray): E.Either => - * E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error'))) - * - * assert.deepStrictEqual(head([]), E.left(new Error('empty array'))) - * assert.deepStrictEqual(head([1, 2, 3]), E.right(1)) - * - * @category interop - * @since 2.0.0 - */ -export const tryCatch = (f: Lazy, onThrow: (e: unknown) => E): Either => { - try { - return right(f()) - } catch (e) { - return left(onThrow(e)) - } -} - -/** - * Converts a function that may throw to one returning a `Either`. - * - * @category interop - * @since 2.10.0 - */ -export const tryCatchK = , B, E>( - f: (...a: A) => B, - onThrow: (error: unknown) => E -): ((...a: A) => Either) => (...a) => tryCatch(() => f(...a), onThrow) - -/** - * @category interop - * @since 2.9.0 - */ -export const fromNullableK = ( - e: E -): (, B>( - f: (...a: A) => B | null | undefined -) => (...a: A) => Either>) => { - const from = fromNullable(e) - return (f) => flow(f, from) -} - -/** - * @category interop - * @since 2.9.0 - */ -export const chainNullableK = ( - e: E -): ((f: (a: A) => B | null | undefined) => (ma: Either) => Either>) => { - const from = fromNullableK(e) - return (f) => chain(from(f)) -} - -/** - * @category interop - * @since 2.10.0 - */ -export const toUnion: (fa: Either) => E | A = - /*#__PURE__*/ - foldW(identity, identity) - -// ------------------------------------------------------------------------------------- -// combinators -// ------------------------------------------------------------------------------------- - -/** - * Returns a `Right` if is a `Left` (and vice versa). - * - * @category combinators - * @since 2.0.0 - */ -export function swap(ma: Either): Either { - return isLeft(ma) ? right(ma.left) : left(ma.right) -} - -/** - * Less strict version of [`orElse`](#orelse). - * - * @category combinators - * @since 2.10.0 - */ -export const orElseW = (onLeft: (e: E1) => Either) => (ma: Either): Either => - isLeft(ma) ? onLeft(ma.left) : ma - -/** - * Useful for recovering from errors. - * - * @category combinators - * @since 2.0.0 - */ -export const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either = orElseW - // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- @@ -392,42 +141,233 @@ const _chainRec: ChainRec2['chainRec'] = (a, f) => ) // ------------------------------------------------------------------------------------- -// type class members +// instances // ------------------------------------------------------------------------------------- /** - * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types - * use the type constructor `F` to represent some computational context. - * - * @category Functor + * @category instances * @since 2.0.0 */ -export const map: (f: (a: A) => B) => (fa: Either) => Either = (f) => (fa) => - isLeft(fa) ? fa : right(f(fa.right)) +export const URI = 'Either' /** - * Map a pair of functions over the two type arguments of the bifunctor. - * - * @category Bifunctor + * @category instances * @since 2.0.0 */ -export const bimap: (f: (e: E) => G, g: (a: A) => B) => (fa: Either) => Either = (f, g) => ( - fa -) => (isLeft(fa) ? left(f(fa.left)) : right(g(fa.right))) +export type URI = typeof URI + +declare module './HKT' { + interface URItoKind2 { + readonly [URI]: Either + } +} /** - * Map a function over the first type argument of a bifunctor. + * @category instances + * @since 2.0.0 + */ +export const getShow = (SE: Show, SA: Show): Show> => ({ + show: (ma) => (isLeft(ma) ? `left(${SE.show(ma.left)})` : `right(${SA.show(ma.right)})`) +}) + +/** + * @category instances + * @since 2.0.0 + */ +export const getEq = (EL: Eq, EA: Eq): Eq> => ({ + equals: (x, y) => + x === y || (isLeft(x) ? isLeft(y) && EL.equals(x.left, y.left) : isRight(y) && EA.equals(x.right, y.right)) +}) + +/** + * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are + * concatenated using the provided `Semigroup` + * + * @example + * import { getSemigroup, left, right } from 'fp-ts/Either' + * import { SemigroupSum } from 'fp-ts/number' + * + * const S = getSemigroup(SemigroupSum) + * assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a')) + * assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2)) + * assert.deepStrictEqual(S.concat(right(1), left('b')), right(1)) + * assert.deepStrictEqual(S.concat(right(1), right(2)), right(3)) * - * @category Bifunctor + * @category instances * @since 2.0.0 */ -export const mapLeft: (f: (e: E) => G) => (fa: Either) => Either = (f) => (fa) => - isLeft(fa) ? left(f(fa.left)) : fa +export const getSemigroup = (S: Semigroup): Semigroup> => ({ + concat: (x, y) => (isLeft(y) ? x : isLeft(x) ? y : right(S.concat(x.right, y.right))) +}) + +/** + * Builds a `Compactable` instance for `Either` given `Monoid` for the left side. + * + * @category instances + * @since 2.10.0 + */ +export const getCompactable = (M: Monoid): Compactable2C => { + const empty = left(M.empty) + return { + URI, + _E: undefined as any, + compact: (ma) => (isLeft(ma) ? ma : ma.right._tag === 'None' ? empty : right(ma.right.value)), + separate: (ma) => + isLeft(ma) + ? separated(ma, ma) + : isLeft(ma.right) + ? separated(right(ma.right.left), empty) + : separated(empty, right(ma.right.right)) + } +} + +/** + * Builds a `Filterable` instance for `Either` given `Monoid` for the left side + * + * @category instances + * @since 2.10.0 + */ +export const getFilterable = (M: Monoid): Filterable2C => { + const empty = left(M.empty) + + const { compact, separate } = getCompactable(M) + + const filter = (ma: Either, predicate: Predicate): Either => + isLeft(ma) ? ma : predicate(ma.right) ? ma : empty + + const partition = (ma: Either, p: Predicate): Separated, Either> => { + return isLeft(ma) + ? separated(ma, ma) + : p(ma.right) + ? separated(empty, right(ma.right)) + : separated(right(ma.right), empty) + } + + return { + URI, + _E: undefined as any, + map: _map, + compact, + separate, + filter, + filterMap: (ma, f) => { + if (isLeft(ma)) { + return ma + } + const ob = f(ma.right) + return ob._tag === 'None' ? empty : right(ob.value) + }, + partition, + partitionMap: (ma, f) => { + if (isLeft(ma)) { + return separated(ma, ma) + } + const e = f(ma.right) + return isLeft(e) ? separated(right(e.left), empty) : separated(empty, right(e.right)) + } + } +} + +/** + * Builds `Witherable` instance for `Either` given `Monoid` for the left side + * + * @category instances + * @since 2.0.0 + */ +export const getWitherable = (M: Monoid): Witherable2C => { + const F_ = getFilterable(M) + const C = getCompactable(M) + return { + URI, + _E: undefined as any, + map: _map, + compact: F_.compact, + separate: F_.separate, + filter: F_.filter, + filterMap: F_.filterMap, + partition: F_.partition, + partitionMap: F_.partitionMap, + traverse: _traverse, + sequence, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight, + wither: witherDefault(Traversable, C), + wilt: wiltDefault(Traversable, C) + } +} + +/** + * @category instances + * @since 2.7.0 + */ +export const getApplicativeValidation = (SE: Semigroup): Applicative2C => ({ + URI, + _E: undefined as any, + map: _map, + ap: (fab, fa) => + isLeft(fab) + ? isLeft(fa) + ? left(SE.concat(fab.left, fa.left)) + : fab + : isLeft(fa) + ? fa + : right(fab.right(fa.right)), + of +}) + +/** + * @category instances + * @since 2.7.0 + */ +export const getAltValidation = (SE: Semigroup): Alt2C => ({ + URI, + _E: undefined as any, + map: _map, + alt: (me, that) => { + if (isRight(me)) { + return me + } + const ea = that() + return isLeft(ea) ? left(SE.concat(me.left, ea.left)) : ea + } +}) + +/** + * @category instance operations + * @since 2.0.0 + */ +export const map: (f: (a: A) => B) => (fa: Either) => Either = (f) => (fa) => + isLeft(fa) ? fa : right(f(fa.right)) + +/** + * @category instances + * @since 2.7.0 + */ +export const Functor: Functor2 = { + URI, + map: _map +} + +/** + * @category instance operations + * @since 2.7.0 + */ +export const of: (a: A) => Either = right + +/** + * @category instances + * @since 2.10.0 + */ +export const Pointed: Pointed2 = { + URI, + of +} /** * Less strict version of [`ap`](#ap). * - * @category Apply + * @category instance operations * @since 2.8.0 */ export const apW: (fa: Either) => (fab: Either B>) => Either = (fa) => ( @@ -437,21 +377,36 @@ export const apW: (fa: Either) => (fab: Either(fa: Either) => (fab: Either B>) => Either = apW /** - * @category Pointed + * @category instances + * @since 2.10.0 + */ +export const Apply: Apply2 = { + URI, + map: _map, + ap: _ap +} + +/** + * @category instances * @since 2.7.0 */ -export const of: (a: A) => Either = right +export const Applicative: Applicative2 = { + URI, + map: _map, + ap: _ap, + of +} /** * Less strict version of [`chain`](#chain). * - * @category Monad + * @category instance operations * @since 2.6.0 */ export const chainW = (f: (a: A) => Either) => (ma: Either): Either => @@ -460,102 +415,62 @@ export const chainW = (f: (a: A) => Either) => (ma: Either< /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * - * @category Monad + * @category instance operations * @since 2.0.0 */ export const chain: (f: (a: A) => Either) => (ma: Either) => Either = chainW /** - * Less strict version of [`flatten`](#flatten). - * - * @category combinators - * @since 2.11.0 + * @category instances + * @since 2.10.0 */ -export const flattenW: (mma: Either>) => Either = - /*#__PURE__*/ - chainW(identity) +export const Chain: Chain2 = { + URI, + map: _map, + ap: _ap, + chain: _chain +} /** - * The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level. - * - * Derivable from `Chain`. + * @category instances + * @since 2.7.0 + */ +export const Monad: Monad2 = { + URI, + map: _map, + ap: _ap, + of, + chain: _chain +} + +/** + * Left-associative fold of a structure. * * @example + * import { pipe } from 'fp-ts/function' * import * as E from 'fp-ts/Either' * - * assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a')) - * assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e')) - * assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e')) + * const startWith = 'prefix' + * const concat = (a: string, b: string) => `${a}:${b}` * - * @category combinators + * assert.deepStrictEqual( + * pipe(E.right('a'), E.reduce(startWith, concat)), + * 'prefix:a' + * ) + * + * assert.deepStrictEqual( + * pipe(E.left('e'), E.reduce(startWith, concat)), + * 'prefix' + * ) + * + * @category instance operations * @since 2.0.0 */ -export const flatten: (mma: Either>) => Either = flattenW +export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) => B = (b, f) => (fa) => + isLeft(fa) ? b : f(b, fa.right) /** - * Less strict version of [`alt`](#alt). - * - * @category Alt - * @since 2.9.0 - */ -export const altW: (that: Lazy>) => (fa: Either) => Either = (that) => ( - fa -) => (isLeft(fa) ? that() : fa) - -/** - * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to - * types of kind `* -> *`. - * - * @category Alt - * @since 2.0.0 - */ -export const alt: (that: Lazy>) => (fa: Either) => Either = altW - -/** - * @category Extend - * @since 2.0.0 - */ -export const extend: (f: (wa: Either) => B) => (wa: Either) => Either = (f) => (wa) => - isLeft(wa) ? wa : right(f(wa)) - -/** - * Derivable from `Extend`. - * - * @category combinators - * @since 2.0.0 - */ -export const duplicate: (ma: Either) => Either> = - /*#__PURE__*/ - extend(identity) - -/** - * Left-associative fold of a structure. - * - * @example - * import { pipe } from 'fp-ts/function' - * import * as E from 'fp-ts/Either' - * - * const startWith = 'prefix' - * const concat = (a: string, b: string) => `${a}:${b}` - * - * assert.deepStrictEqual( - * pipe(E.right('a'), E.reduce(startWith, concat)), - * 'prefix:a' - * ) - * - * assert.deepStrictEqual( - * pipe(E.left('e'), E.reduce(startWith, concat)), - * 'prefix' - * ) - * - * @category Foldable - * @since 2.0.0 - */ -export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) => B = (b, f) => (fa) => - isLeft(fa) ? b : f(b, fa.right) - -/** - * Map each element of the structure to a monoid, and combine the results. + * Map each element of the structure to a monoid, and combine the results. * * @example * import { pipe } from 'fp-ts/function' @@ -574,7 +489,7 @@ export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) * S.Monoid.empty * ) * - * @category Foldable + * @category instance operations * @since 2.0.0 */ export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Either) => M = (M) => (f) => (fa) => @@ -600,12 +515,23 @@ export const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Either * 'postfix' * ) * - * @category Foldable + * @category instance operations * @since 2.0.0 */ export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Either) => B = (b, f) => (fa) => isLeft(fa) ? b : f(fa.right, b) +/** + * @category instances + * @since 2.7.0 + */ +export const Foldable: Foldable2 = { + URI, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight +} + /** * Map each element of a structure to an action, evaluate these actions from left to right, and collect the results. * @@ -625,7 +551,7 @@ export const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Either = (F: ApplicativeHKT) => (f: (a: A) => HKT) => ( @@ -650,7 +576,7 @@ export const traverse: PipeableTraverse2 = (F: ApplicativeHKT) => ['sequence'] = (F: ApplicativeHKT) => ( @@ -659,485 +585,420 @@ export const sequence: Traversable2['sequence'] = (F: ApplicativeHKT) return isLeft(ma) ? F.of(left(ma.left)) : F.map>(ma.right, right) } -/** - * @category MonadThrow - * @since 2.6.3 - */ -export const throwError: MonadThrow2['throwError'] = left - -// ------------------------------------------------------------------------------------- -// instances -// ------------------------------------------------------------------------------------- - /** * @category instances - * @since 2.0.0 + * @since 2.7.0 */ -export const URI = 'Either' +export const Traversable: Traversable2 = { + URI, + map: _map, + reduce: _reduce, + foldMap: _foldMap, + reduceRight: _reduceRight, + traverse: _traverse, + sequence +} /** - * @category instances + * Map a pair of functions over the two type arguments of the bifunctor. + * + * @category instance operations * @since 2.0.0 */ -export type URI = typeof URI - -declare module './HKT' { - interface URItoKind2 { - readonly [URI]: Either - } -} +export const bimap: (f: (e: E) => G, g: (a: A) => B) => (fa: Either) => Either = (f, g) => ( + fa +) => (isLeft(fa) ? left(f(fa.left)) : right(g(fa.right))) /** - * @category instances + * Map a function over the first type argument of a bifunctor. + * + * @category instance operations * @since 2.0.0 */ -export function getShow(SE: Show, SA: Show): Show> { - return { - show: (ma) => (isLeft(ma) ? `left(${SE.show(ma.left)})` : `right(${SA.show(ma.right)})`) - } -} +export const mapLeft: (f: (e: E) => G) => (fa: Either) => Either = (f) => (fa) => + isLeft(fa) ? left(f(fa.left)) : fa /** * @category instances - * @since 2.0.0 + * @since 2.7.0 */ -export function getEq(EL: Eq, EA: Eq): Eq> { - return { - equals: (x, y) => - x === y || (isLeft(x) ? isLeft(y) && EL.equals(x.left, y.left) : isRight(y) && EA.equals(x.right, y.right)) - } +export const Bifunctor: Bifunctor2 = { + URI, + bimap: _bimap, + mapLeft: _mapLeft } /** - * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are - * concatenated using the provided `Semigroup` - * - * @example - * import { getSemigroup, left, right } from 'fp-ts/Either' - * import { SemigroupSum } from 'fp-ts/number' - * - * const S = getSemigroup(SemigroupSum) - * assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a')) - * assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2)) - * assert.deepStrictEqual(S.concat(right(1), left('b')), right(1)) - * assert.deepStrictEqual(S.concat(right(1), right(2)), right(3)) + * Less strict version of [`alt`](#alt). * - * @category instances - * @since 2.0.0 + * @category instance operations + * @since 2.9.0 */ -export function getSemigroup(S: Semigroup): Semigroup> { - return { - concat: (x, y) => (isLeft(y) ? x : isLeft(x) ? y : right(S.concat(x.right, y.right))) - } -} +export const altW: (that: Lazy>) => (fa: Either) => Either = (that) => ( + fa +) => (isLeft(fa) ? that() : fa) /** - * Builds a `Compactable` instance for `Either` given `Monoid` for the left side. + * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to + * types of kind `* -> *`. * - * @category instances - * @since 2.10.0 + * @category instance operations + * @since 2.0.0 */ -export const getCompactable = (M: Monoid): Compactable2C => { - const empty = left(M.empty) - return { - URI, - _E: undefined as any, - compact: (ma) => (isLeft(ma) ? ma : ma.right._tag === 'None' ? empty : right(ma.right.value)), - separate: (ma) => - isLeft(ma) - ? separated(ma, ma) - : isLeft(ma.right) - ? separated(right(ma.right.left), empty) - : separated(empty, right(ma.right.right)) - } -} +export const alt: (that: Lazy>) => (fa: Either) => Either = altW /** - * Builds a `Filterable` instance for `Either` given `Monoid` for the left side - * * @category instances - * @since 2.10.0 + * @since 2.7.0 */ -export function getFilterable(M: Monoid): Filterable2C { - const empty = left(M.empty) - - const { compact, separate } = getCompactable(M) - - const filter = (ma: Either, predicate: Predicate): Either => - isLeft(ma) ? ma : predicate(ma.right) ? ma : empty - - const partition = (ma: Either, p: Predicate): Separated, Either> => { - return isLeft(ma) - ? separated(ma, ma) - : p(ma.right) - ? separated(empty, right(ma.right)) - : separated(right(ma.right), empty) - } - - return { - URI, - _E: undefined as any, - map: _map, - compact, - separate, - filter, - filterMap: (ma, f) => { - if (isLeft(ma)) { - return ma - } - const ob = f(ma.right) - return ob._tag === 'None' ? empty : right(ob.value) - }, - partition, - partitionMap: (ma, f) => { - if (isLeft(ma)) { - return separated(ma, ma) - } - const e = f(ma.right) - return isLeft(e) ? separated(right(e.left), empty) : separated(empty, right(e.right)) - } - } +export const Alt: Alt2 = { + URI, + map: _map, + alt: _alt } /** - * Builds `Witherable` instance for `Either` given `Monoid` for the left side - * - * @category instances + * @category instance operations * @since 2.0.0 */ -export function getWitherable(M: Monoid): Witherable2C { - const F_ = getFilterable(M) - const C = getCompactable(M) - return { - URI, - _E: undefined as any, - map: _map, - compact: F_.compact, - separate: F_.separate, - filter: F_.filter, - filterMap: F_.filterMap, - partition: F_.partition, - partitionMap: F_.partitionMap, - traverse: _traverse, - sequence, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - wither: witherDefault(Traversable, C), - wilt: wiltDefault(Traversable, C) - } -} +export const extend: (f: (wa: Either) => B) => (wa: Either) => Either = (f) => (wa) => + isLeft(wa) ? wa : right(f(wa)) /** * @category instances * @since 2.7.0 */ -export function getApplicativeValidation(SE: Semigroup): Applicative2C { - return { - URI, - _E: undefined as any, - map: _map, - ap: (fab, fa) => - isLeft(fab) - ? isLeft(fa) - ? left(SE.concat(fab.left, fa.left)) - : fab - : isLeft(fa) - ? fa - : right(fab.right(fa.right)), - of - } +export const Extend: Extend2 = { + URI, + map: _map, + extend: _extend } /** * @category instances * @since 2.7.0 */ -export function getAltValidation(SE: Semigroup): Alt2C { - return { - URI, - _E: undefined as any, - map: _map, - alt: (me, that) => { - if (isRight(me)) { - return me - } - const ea = that() - return isLeft(ea) ? left(SE.concat(me.left, ea.left)) : ea - } - } +export const ChainRec: ChainRec2 = { + URI, + map: _map, + ap: _ap, + chain: _chain, + chainRec: _chainRec } +/** + * @category instance operations + * @since 2.6.3 + */ +export const throwError: MonadThrow2['throwError'] = left + /** * @category instances * @since 2.7.0 */ -export const Functor: Functor2 = { +export const MonadThrow: MonadThrow2 = { URI, - map: _map + map: _map, + ap: _ap, + of, + chain: _chain, + throwError } -/** - * Derivable from `Functor`. - * - * @category combinators - * @since 2.10.0 - */ -export const flap = - /*#_PURE_*/ - flap_(Functor) - /** * @category instances * @since 2.10.0 */ -export const Pointed: Pointed2 = { +export const FromEither: FromEither2 = { URI, - of + fromEither: identity } /** - * @category instances - * @since 2.10.0 + * @example + * import { fromPredicate, left, right } from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual( + * pipe( + * 1, + * fromPredicate( + * (n) => n > 0, + * () => 'error' + * ) + * ), + * right(1) + * ) + * assert.deepStrictEqual( + * pipe( + * -1, + * fromPredicate( + * (n) => n > 0, + * () => 'error' + * ) + * ), + * left('error') + * ) + * + * @category constructors + * @since 2.0.0 */ -export const Apply: Apply2 = { - URI, - map: _map, - ap: _ap -} +export const fromPredicate = + /*#__PURE__*/ + fromPredicate_(FromEither) + +// ------------------------------------------------------------------------------------- +// natural transformations +// ------------------------------------------------------------------------------------- /** - * Combine two effectful actions, keeping only the result of the first. + * @example + * import * as E from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * import * as O from 'fp-ts/Option' * - * Derivable from `Apply`. + * assert.deepStrictEqual( + * pipe( + * O.some(1), + * E.fromOption(() => 'error') + * ), + * E.right(1) + * ) + * assert.deepStrictEqual( + * pipe( + * O.none, + * E.fromOption(() => 'error') + * ), + * E.left('error') + * ) * - * @category combinators + * @category natural transformations * @since 2.0.0 */ -export const apFirst = +export const fromOption = /*#__PURE__*/ - apFirst_(Apply) + fromOption_(FromEither) + +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- /** - * Combine two effectful actions, keeping only the result of the second. - * - * Derivable from `Apply`. + * Returns `true` if the either is an instance of `Left`, `false` otherwise. * - * @category combinators + * @category refinements * @since 2.0.0 */ -export const apSecond = - /*#__PURE__*/ - apSecond_(Apply) +export const isLeft: (ma: Either) => ma is Left = _.isLeft /** - * @category instances - * @since 2.7.0 + * Returns `true` if the either is an instance of `Right`, `false` otherwise. + * + * @category refinements + * @since 2.0.0 */ -export const Applicative: Applicative2 = { - URI, - map: _map, - ap: _ap, - of -} +export const isRight: (ma: Either) => ma is Right = _.isRight + +// ------------------------------------------------------------------------------------- +// destructors +// ------------------------------------------------------------------------------------- /** - * @category instances + * Less strict version of [`match`](#match). + * + * @category destructors * @since 2.10.0 */ -export const Chain: Chain2 = { - URI, - map: _map, - ap: _ap, - chain: _chain -} +export const matchW = (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either): B | C => + isLeft(ma) ? onLeft(ma.left) : onRight(ma.right) /** - * @category instances - * @since 2.7.0 + * Alias of [`matchW`](#matchw). + * + * @category destructors + * @since 2.10.0 */ -export const Monad: Monad2 = { - URI, - map: _map, - ap: _ap, - of, - chain: _chain -} +export const foldW = matchW /** - * Composes computations in sequence, using the return value of one computation to determine the next computation and - * keeping only the result of the first. + * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function, + * if the value is a `Right` the inner value is applied to the second function. * - * Derivable from `Chain`. + * @example + * import { match, left, right } from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' * - * @category combinators - * @since 2.0.0 + * function onLeft(errors: Array): string { + * return `Errors: ${errors.join(', ')}` + * } + * + * function onRight(value: number): string { + * return `Ok: ${value}` + * } + * + * assert.strictEqual( + * pipe( + * right(1), + * match(onLeft, onRight) + * ), + * 'Ok: 1' + * ) + * assert.strictEqual( + * pipe( + * left(['error 1', 'error 2']), + * match(onLeft, onRight) + * ), + * 'Errors: error 1, error 2' + * ) + * + * @category destructors + * @since 2.10.0 */ -export const chainFirst: (f: (a: A) => Either) => (ma: Either) => Either = - /*#__PURE__*/ - chainFirst_(Chain) +export const match: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B = matchW /** - * Less strict version of [`chainFirst`](#chainfirst) - * - * Derivable from `Chain`. + * Alias of [`match`](#match). * - * @category combinators - * @since 2.8.0 + * @category destructors + * @since 2.0.0 */ -export const chainFirstW: ( - f: (a: A) => Either -) => (ma: Either) => Either = chainFirst as any +export const fold: (onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either) => B = match /** - * @category instances - * @since 2.7.0 + * Less strict version of [`getOrElse`](#getorelse). + * + * @category destructors + * @since 2.6.0 */ -export const Foldable: Foldable2 = { - URI, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight -} +export const getOrElseW = (onLeft: (e: E) => B) => (ma: Either): A | B => + isLeft(ma) ? onLeft(ma.left) : ma.right /** - * @category instances - * @since 2.7.0 + * Returns the wrapped value if it's a `Right` or a default value if is a `Left`. + * + * @example + * import { getOrElse, left, right } from 'fp-ts/Either' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual( + * pipe( + * right(1), + * getOrElse(() => 0) + * ), + * 1 + * ) + * assert.deepStrictEqual( + * pipe( + * left('error'), + * getOrElse(() => 0) + * ), + * 0 + * ) + * + * @category destructors + * @since 2.0.0 */ -export const Traversable: Traversable2 = { - URI, - map: _map, - reduce: _reduce, - foldMap: _foldMap, - reduceRight: _reduceRight, - traverse: _traverse, - sequence -} +export const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A = getOrElseW + +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- /** - * @category instances - * @since 2.7.0 + * Derivable from `Functor`. + * + * @category combinators + * @since 2.10.0 */ -export const Bifunctor: Bifunctor2 = { - URI, - bimap: _bimap, - mapLeft: _mapLeft -} +export const flap = + /*#_PURE_*/ + flap_(Functor) /** - * @category instances - * @since 2.7.0 - */ -export const Alt: Alt2 = { - URI, - map: _map, - alt: _alt -} + * Combine two effectful actions, keeping only the result of the first. + * + * Derivable from `Apply`. + * + * @category combinators + * @since 2.0.0 + */ +export const apFirst = + /*#__PURE__*/ + apFirst_(Apply) /** - * @category instances - * @since 2.7.0 + * Combine two effectful actions, keeping only the result of the second. + * + * Derivable from `Apply`. + * + * @category combinators + * @since 2.0.0 */ -export const Extend: Extend2 = { - URI, - map: _map, - extend: _extend -} +export const apSecond = + /*#__PURE__*/ + apSecond_(Apply) /** - * @category instances - * @since 2.7.0 + * Composes computations in sequence, using the return value of one computation to determine the next computation and + * keeping only the result of the first. + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 */ -export const ChainRec: ChainRec2 = { - URI, - map: _map, - ap: _ap, - chain: _chain, - chainRec: _chainRec -} +export const chainFirst: (f: (a: A) => Either) => (ma: Either) => Either = + /*#__PURE__*/ + chainFirst_(Chain) /** - * @category instances - * @since 2.7.0 + * Less strict version of [`chainFirst`](#chainfirst) + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.8.0 */ -export const MonadThrow: MonadThrow2 = { - URI, - map: _map, - ap: _ap, - of, - chain: _chain, - throwError -} +export const chainFirstW: ( + f: (a: A) => Either +) => (ma: Either) => Either = chainFirst as any /** - * @category instances - * @since 2.10.0 + * Less strict version of [`flatten`](#flatten). + * + * @category combinators + * @since 2.11.0 */ -export const FromEither: FromEither2 = { - URI, - fromEither: identity -} +export const flattenW: (mma: Either>) => Either = + /*#__PURE__*/ + chainW(identity) /** + * The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level. + * + * Derivable from `Chain`. + * * @example * import * as E from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * import * as O from 'fp-ts/Option' * - * assert.deepStrictEqual( - * pipe( - * O.some(1), - * E.fromOption(() => 'error') - * ), - * E.right(1) - * ) - * assert.deepStrictEqual( - * pipe( - * O.none, - * E.fromOption(() => 'error') - * ), - * E.left('error') - * ) + * assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a')) + * assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e')) + * assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e')) * - * @category natural transformations + * @category combinators * @since 2.0.0 */ -export const fromOption = - /*#__PURE__*/ - fromOption_(FromEither) +export const flatten: (mma: Either>) => Either = flattenW /** - * @example - * import { fromPredicate, left, right } from 'fp-ts/Either' - * import { pipe } from 'fp-ts/function' - * - * assert.deepStrictEqual( - * pipe( - * 1, - * fromPredicate( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * right(1) - * ) - * assert.deepStrictEqual( - * pipe( - * -1, - * fromPredicate( - * (n) => n > 0, - * () => 'error' - * ) - * ), - * left('error') - * ) + * Derivable from `Extend`. * - * @category constructors + * @category combinators * @since 2.0.0 */ -export const fromPredicate = +export const duplicate: (ma: Either) => Either> = /*#__PURE__*/ - fromPredicate_(FromEither) + extend(identity) /** * @category combinators @@ -1211,6 +1072,129 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either } = filterOrElse +/** + * Returns a `Right` if is a `Left` (and vice versa). + * + * @category combinators + * @since 2.0.0 + */ +export const swap = (ma: Either): Either => (isLeft(ma) ? right(ma.left) : left(ma.right)) + +/** + * Less strict version of [`orElse`](#orelse). + * + * @category combinators + * @since 2.10.0 + */ +export const orElseW = (onLeft: (e: E1) => Either) => (ma: Either): Either => + isLeft(ma) ? onLeft(ma.left) : ma + +/** + * Useful for recovering from errors. + * + * @category combinators + * @since 2.0.0 + */ +export const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either = orElseW + +// ------------------------------------------------------------------------------------- +// interop +// ------------------------------------------------------------------------------------- + +/** + * Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use + * the provided default as a `Left`. + * + * @example + * import { fromNullable, left, right } from 'fp-ts/Either' + * + * const parse = fromNullable('nully') + * + * assert.deepStrictEqual(parse(1), right(1)) + * assert.deepStrictEqual(parse(null), left('nully')) + * + * @category interop + * @since 2.0.0 + */ +export const fromNullable = (e: E) => (a: A): Either> => + a == null ? left(e) : right(a as NonNullable) + +/** + * Constructs a new `Either` from a function that might throw. + * + * See also [`tryCatchK`](#trycatchk). + * + * @example + * import * as E from 'fp-ts/Either' + * + * const unsafeHead = (as: ReadonlyArray): A => { + * if (as.length > 0) { + * return as[0] + * } else { + * throw new Error('empty array') + * } + * } + * + * const head = (as: ReadonlyArray): E.Either => + * E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error'))) + * + * assert.deepStrictEqual(head([]), E.left(new Error('empty array'))) + * assert.deepStrictEqual(head([1, 2, 3]), E.right(1)) + * + * @category interop + * @since 2.0.0 + */ +export const tryCatch = (f: Lazy, onThrow: (e: unknown) => E): Either => { + try { + return right(f()) + } catch (e) { + return left(onThrow(e)) + } +} + +/** + * Converts a function that may throw to one returning a `Either`. + * + * @category interop + * @since 2.10.0 + */ +export const tryCatchK = , B, E>( + f: (...a: A) => B, + onThrow: (error: unknown) => E +): ((...a: A) => Either) => (...a) => tryCatch(() => f(...a), onThrow) + +/** + * @category interop + * @since 2.9.0 + */ +export const fromNullableK = ( + e: E +): (, B>( + f: (...a: A) => B | null | undefined +) => (...a: A) => Either>) => { + const from = fromNullable(e) + return (f) => flow(f, from) +} + +/** + * @category interop + * @since 2.9.0 + */ +export const chainNullableK = ( + e: E +): ((f: (a: A) => B | null | undefined) => (ma: Either) => Either>) => { + const from = fromNullableK(e) + return (f) => chain(from(f)) +} + +/** + * @category interop + * @since 2.10.0 + */ +export const toUnion: (fa: Either) => E | A = + /*#__PURE__*/ + foldW(identity, identity) + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- diff --git a/src/Option.ts b/src/Option.ts index b14f93d0c..80d9e8fce 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -300,16 +300,6 @@ export const Functor: Functor1 = { map: _map } -/** - * Derivable from `Functor`. - * - * @category combinators - * @since 2.10.0 - */ -export const flap = - /*#__PURE__*/ - flap_(Functor) - /** * @category instance operations * @since 2.7.0 @@ -342,30 +332,6 @@ export const Apply: Apply1 = { ap: _ap } -/** - * Combine two effectful actions, keeping only the result of the first. - * - * Derivable from `Apply`. - * - * @category combinators - * @since 2.0.0 - */ -export const apFirst = - /*#__PURE__*/ - apFirst_(Apply) - -/** - * Combine two effectful actions, keeping only the result of the second. - * - * Derivable from `Apply`. - * - * @category combinators - * @since 2.0.0 - */ -export const apSecond = - /*#__PURE__*/ - apSecond_(Apply) - /** * @category instances * @since 2.7.0 @@ -397,16 +363,6 @@ export const Chain: Chain1 = { chain: _chain } -/** - * Derivable from `Chain`. - * - * @category combinators - * @since 2.0.0 - */ -export const flatten: (mma: Option>) => Option = - /*#__PURE__*/ - chain(identity) - /** * @category instances * @since 2.7.0 @@ -419,19 +375,6 @@ export const Monad: Monad1 = { chain: _chain } -/** - * Composes computations in sequence, using the return value of one computation to determine the next computation and - * keeping only the result of the first. - * - * Derivable from `Chain`. - * - * @category combinators - * @since 2.0.0 - */ -export const chainFirst = - /*#__PURE__*/ - chainFirst_(Chain) - /** * @category instance operations * @since 2.0.0 @@ -556,16 +499,6 @@ export const Alternative: Alternative1 = { export const extend: (f: (wa: Option) => B) => (wa: Option) => Option = (f) => (wa) => isNone(wa) ? none : some(f(wa)) -/** - * Derivable from `Extend`. - * - * @category combinators - * @since 2.0.0 - */ -export const duplicate: (ma: Option) => Option> = - /*#__PURE__*/ - extend(identity) - /** * @category instances * @since 2.7.0 @@ -580,7 +513,9 @@ export const Extend: Extend1 = { * @category instance operations * @since 2.0.0 */ -export const compact: (fa: Option>) => Option = flatten +export const compact: (fa: Option>) => Option = + /*#__PURE__*/ + chain(identity) const defaultSeparated = /*#__PURE__*/ @@ -773,22 +708,6 @@ export const FromEither: FromEither1 = { fromEither } -/** - * @category combinators - * @since 2.11.0 - */ -export const fromEitherK = - /*#__PURE__*/ - fromEitherK_(FromEither) - -/** - * @category combinators - * @since 2.11.0 - */ -export const chainEitherK = - /*#__PURE__*/ - chainEitherK_(FromEither, Chain) - // ------------------------------------------------------------------------------------- // refinements // ------------------------------------------------------------------------------------- @@ -914,6 +833,91 @@ export const getOrElseW = (onNone: Lazy) => (ma: Option): A | B => ( */ export const getOrElse: (onNone: Lazy) => (ma: Option) => A = getOrElseW +// ------------------------------------------------------------------------------------- +// combinators +// ------------------------------------------------------------------------------------- + +/** + * Derivable from `Functor`. + * + * @category combinators + * @since 2.10.0 + */ +export const flap = + /*#__PURE__*/ + flap_(Functor) + +/** + * Combine two effectful actions, keeping only the result of the first. + * + * Derivable from `Apply`. + * + * @category combinators + * @since 2.0.0 + */ +export const apFirst = + /*#__PURE__*/ + apFirst_(Apply) + +/** + * Combine two effectful actions, keeping only the result of the second. + * + * Derivable from `Apply`. + * + * @category combinators + * @since 2.0.0 + */ +export const apSecond = + /*#__PURE__*/ + apSecond_(Apply) + +/** + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const flatten: (mma: Option>) => Option = compact + +/** + * Composes computations in sequence, using the return value of one computation to determine the next computation and + * keeping only the result of the first. + * + * Derivable from `Chain`. + * + * @category combinators + * @since 2.0.0 + */ +export const chainFirst = + /*#__PURE__*/ + chainFirst_(Chain) + +/** + * Derivable from `Extend`. + * + * @category combinators + * @since 2.0.0 + */ +export const duplicate: (ma: Option) => Option> = + /*#__PURE__*/ + extend(identity) + +/** + * @category combinators + * @since 2.11.0 + */ +export const fromEitherK = + /*#__PURE__*/ + fromEitherK_(FromEither) + +/** + * @category combinators + * @since 2.11.0 + */ +export const chainEitherK = + /*#__PURE__*/ + chainEitherK_(FromEither, Chain) + // ------------------------------------------------------------------------------------- // interop // ------------------------------------------------------------------------------------- From 89c90f2cc9b378a058c7641799ed226ce89ed855 Mon Sep 17 00:00:00 2001 From: gcanti Date: Mon, 10 May 2021 16:01:27 +0200 Subject: [PATCH 145/162] - `string` - `toLowerCase` - `replace` - `split` - `trim` - `trimLeft` - `trimRight` - `includes` - `startsWith` - `endsWith` - `slice` --- CHANGELOG.md | 10 ++++ docs/modules/string.ts.md | 110 ++++++++++++++++++++++++++++++++++++++ src/string.ts | 74 +++++++++++++++++++++---- test/string.ts | 42 +++++++++++++++ 4 files changed, 226 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f9f34221..a66959ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -348,6 +348,16 @@ high state of flux, you're at risk of it changing without notice. - add `traverseReadonlyArrayWithIndex` - `string` - add `toUpperCase` + - add `toLowerCase` + - add `replace` + - add `split` + - add `trim` + - add `trimLeft` + - add `trimRight` + - add `includes` + - add `startsWith` + - add `endsWith` + - add `slice` - `struct` - add `evolve` - `Task` diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index 7eb9ffc6a..4acd73300 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -22,9 +22,19 @@ Added in v2.10.0 - [isString](#isstring) - [utils](#utils) - [empty](#empty) + - [endsWith](#endswith) + - [includes](#includes) - [isEmpty](#isempty) + - [replace](#replace) - [size](#size) + - [slice](#slice) + - [split](#split) + - [startsWith](#startswith) + - [toLowerCase](#tolowercase) - [toUpperCase](#touppercase) + - [trim](#trim) + - [trimLeft](#trimleft) + - [trimRight](#trimright) --- @@ -128,6 +138,26 @@ export declare const empty: string Added in v2.10.0 +## endsWith + +**Signature** + +```ts +export declare const endsWith: (searchString: string, position?: number | undefined) => (s: string) => boolean +``` + +Added in v2.11.0 + +## includes + +**Signature** + +```ts +export declare const includes: (searchString: string, position?: number | undefined) => (s: string) => boolean +``` + +Added in v2.11.0 + ## isEmpty Test whether a `string` is empty. @@ -140,6 +170,16 @@ export declare const isEmpty: (s: string) => boolean Added in v2.10.0 +## replace + +**Signature** + +```ts +export declare const replace: (searchValue: string | RegExp, replaceValue: string) => (s: string) => string +``` + +Added in v2.11.0 + ## size Calculate the number of characters in a `string`. @@ -152,6 +192,46 @@ export declare const size: (s: string) => number Added in v2.10.0 +## slice + +**Signature** + +```ts +export declare const slice: (start: number, end: number) => (s: string) => string +``` + +Added in v2.11.0 + +## split + +**Signature** + +```ts +export declare const split: (separator: string | RegExp) => (s: string) => ReadonlyArray +``` + +Added in v2.11.0 + +## startsWith + +**Signature** + +```ts +export declare const startsWith: (searchString: string, position?: number | undefined) => (s: string) => boolean +``` + +Added in v2.11.0 + +## toLowerCase + +**Signature** + +```ts +export declare const toLowerCase: (s: string) => string +``` + +Added in v2.11.0 + ## toUpperCase **Signature** @@ -161,3 +241,33 @@ export declare const toUpperCase: (s: string) => string ``` Added in v2.11.0 + +## trim + +**Signature** + +```ts +export declare const trim: (s: string) => string +``` + +Added in v2.11.0 + +## trimLeft + +**Signature** + +```ts +export declare const trimLeft: (s: string) => string +``` + +Added in v2.11.0 + +## trimRight + +**Signature** + +```ts +export declare const trimRight: (s: string) => string +``` + +Added in v2.11.0 diff --git a/src/string.ts b/src/string.ts index 3df363bc5..361cebade 100644 --- a/src/string.ts +++ b/src/string.ts @@ -8,16 +8,6 @@ import * as O from './Ord' import * as Sh from './Show' import { Refinement } from './Refinement' -// ------------------------------------------------------------------------------------- -// refinements -// ------------------------------------------------------------------------------------- - -/** - * @category refinements - * @since 2.11.0 - */ -export const isString: Refinement = (u: unknown): u is string => typeof u === 'string' - // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- @@ -80,6 +70,16 @@ export const Show: Sh.Show = { show: (s) => JSON.stringify(s) } +// ------------------------------------------------------------------------------------- +// refinements +// ------------------------------------------------------------------------------------- + +/** + * @category refinements + * @since 2.11.0 + */ +export const isString: Refinement = (u: unknown): u is string => typeof u === 'string' + // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- @@ -109,3 +109,57 @@ export const size = (s: string): number => s.length * @since 2.11.0 */ export const toUpperCase = (s: string): string => s.toUpperCase() + +/** + * @since 2.11.0 + */ +export const toLowerCase = (s: string): string => s.toLowerCase() + +/** + * @since 2.11.0 + */ +export const replace = (searchValue: string | RegExp, replaceValue: string) => (s: string): string => + s.replace(searchValue, replaceValue) + +/** + * @since 2.11.0 + */ +export const split = (separator: string | RegExp) => (s: string): ReadonlyArray => s.split(separator) + +/** + * @since 2.11.0 + */ +export const trim = (s: string): string => s.trim() + +/** + * @since 2.11.0 + */ +export const trimLeft = (s: string): string => s.trimLeft() + +/** + * @since 2.11.0 + */ +export const trimRight = (s: string): string => s.trimRight() + +/** + * @since 2.11.0 + */ +export const includes = (searchString: string, position?: number) => (s: string): boolean => + s.includes(searchString, position) + +/** + * @since 2.11.0 + */ +export const startsWith = (searchString: string, position?: number) => (s: string): boolean => + s.startsWith(searchString, position) + +/** + * @since 2.11.0 + */ +export const endsWith = (searchString: string, position?: number) => (s: string): boolean => + s.endsWith(searchString, position) + +/** + * @since 2.11.0 + */ +export const slice = (start: number, end: number) => (s: string): string => s.slice(start, end) diff --git a/test/string.ts b/test/string.ts index 464f0ee32..dd2e3d1e8 100644 --- a/test/string.ts +++ b/test/string.ts @@ -1,5 +1,6 @@ import * as U from './util' import * as _ from '../src/string' +import { pipe } from '../src/function' describe('string', () => { // ------------------------------------------------------------------------------------- @@ -33,4 +34,45 @@ describe('string', () => { it('toUpperCase', () => { U.deepStrictEqual(_.toUpperCase('a'), 'A') }) + + it('toLowerCase', () => { + U.deepStrictEqual(_.toLowerCase('A'), 'a') + }) + + it('replace', () => { + U.deepStrictEqual(pipe('abc', _.replace('b', 'd')), 'adc') + }) + + it('split', () => { + U.deepStrictEqual(pipe('abc', _.split('')), ['a', 'b', 'c']) + }) + + it('trim', () => { + U.deepStrictEqual(pipe(' a ', _.trim), 'a') + }) + + it('trimLeft', () => { + U.deepStrictEqual(pipe(' a ', _.trimLeft), 'a ') + }) + + it('trimRight', () => { + U.deepStrictEqual(pipe(' a ', _.trimRight), ' a') + }) + + it('includes', () => { + U.deepStrictEqual(pipe('abc', _.includes('b')), true) + U.deepStrictEqual(pipe('abc', _.includes('b', 2)), false) + }) + + it('startsWith', () => { + U.deepStrictEqual(pipe('abc', _.startsWith('a')), true) + }) + + it('endsWith', () => { + U.deepStrictEqual(pipe('abc', _.endsWith('c')), true) + }) + + it('slice', () => { + U.deepStrictEqual(pipe('abcd', _.slice(1, 3)), 'bc') + }) }) From 24ca6dd58039c903dc652d9375d950acd5dee697 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 11 May 2021 15:34:27 +0200 Subject: [PATCH 146/162] Docs: fix links --- docs/modules/BooleanAlgebra.ts.md | 4 ++-- docs/modules/Bounded.ts.md | 2 +- docs/modules/Compactable.ts.md | 2 +- docs/modules/Either.ts.md | 8 ++++---- docs/modules/Eq.ts.md | 8 ++++---- docs/modules/Field.ts.md | 2 +- docs/modules/IO.ts.md | 4 ++-- docs/modules/IOEither.ts.md | 6 +++--- docs/modules/Monoid.ts.md | 14 +++++++------- docs/modules/NonEmptyArray.ts.md | 2 +- docs/modules/Option.ts.md | 4 ++-- docs/modules/Ord.ts.md | 8 ++++---- docs/modules/Reader.ts.md | 4 ++-- docs/modules/ReaderEither.ts.md | 6 +++--- docs/modules/ReaderTask.ts.md | 6 +++--- docs/modules/ReaderTaskEither.ts.md | 6 +++--- docs/modules/ReadonlyNonEmptyArray.ts.md | 4 ++-- docs/modules/Ring.ts.md | 2 +- docs/modules/Semiring.ts.md | 2 +- docs/modules/Show.ts.md | 6 +++--- docs/modules/Task.ts.md | 4 ++-- docs/modules/TaskEither.ts.md | 6 +++--- docs/modules/TaskThese.ts.md | 2 +- src/BooleanAlgebra.ts | 4 ++-- src/Bounded.ts | 2 +- src/Compactable.ts | 2 +- src/Either.ts | 8 ++++---- src/Eq.ts | 8 ++++---- src/Field.ts | 2 +- src/IO.ts | 4 ++-- src/IOEither.ts | 6 +++--- src/Monoid.ts | 14 +++++++------- src/NonEmptyArray.ts | 2 +- src/Option.ts | 4 ++-- src/Ord.ts | 8 ++++---- src/Reader.ts | 4 ++-- src/ReaderEither.ts | 6 +++--- src/ReaderTask.ts | 6 +++--- src/ReaderTaskEither.ts | 6 +++--- src/ReadonlyNonEmptyArray.ts | 4 ++-- src/Ring.ts | 2 +- src/Semiring.ts | 2 +- src/Show.ts | 6 +++--- src/Task.ts | 4 ++-- src/TaskEither.ts | 6 +++--- src/TaskThese.ts | 2 +- 46 files changed, 112 insertions(+), 112 deletions(-) diff --git a/docs/modules/BooleanAlgebra.ts.md b/docs/modules/BooleanAlgebra.ts.md index 7517ab489..c2efa2053 100644 --- a/docs/modules/BooleanAlgebra.ts.md +++ b/docs/modules/BooleanAlgebra.ts.md @@ -73,7 +73,7 @@ Added in v2.0.0 ## ~~booleanAlgebraBoolean~~ -Use [`BooleanAlgebra`](./boolean.ts.html#BooleanAlgebra) instead. +Use [`BooleanAlgebra`](./boolean.ts.html#booleanalgebra) instead. **Signature** @@ -85,7 +85,7 @@ Added in v2.0.0 ## ~~getFunctionBooleanAlgebra~~ -Use [`getBooleanAlgebra`](./function.ts.html#getBooleanAlgebra) instead. +Use [`getBooleanAlgebra`](./function.ts.html#getbooleanalgebra) instead. **Signature** diff --git a/docs/modules/Bounded.ts.md b/docs/modules/Bounded.ts.md index 56b6758b9..2ddcd46cc 100644 --- a/docs/modules/Bounded.ts.md +++ b/docs/modules/Bounded.ts.md @@ -29,7 +29,7 @@ Added in v2.0.0 ## ~~boundedNumber~~ -Use [`Bounded`](./number.ts.html#Bounded) instead. +Use [`Bounded`](./number.ts.html#bounded) instead. **Signature** diff --git a/docs/modules/Compactable.ts.md b/docs/modules/Compactable.ts.md index abcd4b579..3f21e2053 100644 --- a/docs/modules/Compactable.ts.md +++ b/docs/modules/Compactable.ts.md @@ -369,7 +369,7 @@ Added in v2.0.0 ## ~~Separated~~ (interface) -Use [`Separated`](./Separated.ts.html#Separated) instead. +Use [`Separated`](./Separated.ts.html#separated) instead. **Signature** diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 32e8810ec..b4c5903f8 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1148,7 +1148,7 @@ Added in v2.0.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1160,7 +1160,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values are concatenated using the provided `Semigroup` @@ -1175,7 +1175,7 @@ Added in v2.0.0 ## ~~getValidationMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1187,7 +1187,7 @@ Added in v2.0.0 ## ~~getValidationSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/Eq.ts.md b/docs/modules/Eq.ts.md index 7a5590c2e..4971123fd 100644 --- a/docs/modules/Eq.ts.md +++ b/docs/modules/Eq.ts.md @@ -201,7 +201,7 @@ Added in v2.10.0 ## ~~eqBoolean~~ -Use [`Eq`](./boolean.ts.html#Eq) instead. +Use [`Eq`](./boolean.ts.html#eq) instead. **Signature** @@ -213,7 +213,7 @@ Added in v2.0.0 ## ~~eqDate~~ -Use [`Eq`](./Date.ts.html#Eq) instead. +Use [`Eq`](./Date.ts.html#eq) instead. **Signature** @@ -225,7 +225,7 @@ Added in v2.0.0 ## ~~eqNumber~~ -Use [`Eq`](./number.ts.html#Eq) instead. +Use [`Eq`](./number.ts.html#eq) instead. **Signature** @@ -237,7 +237,7 @@ Added in v2.0.0 ## ~~eqString~~ -Use [`Eq`](./string.ts.html#Eq) instead. +Use [`Eq`](./string.ts.html#eq) instead. **Signature** diff --git a/docs/modules/Field.ts.md b/docs/modules/Field.ts.md index 516fe8928..42acf1abf 100644 --- a/docs/modules/Field.ts.md +++ b/docs/modules/Field.ts.md @@ -28,7 +28,7 @@ Added in v2.0.0 ## ~~fieldNumber~~ -Use [`Field`](./number.ts.html#Field) instead. +Use [`Field`](./number.ts.html#field) instead. **Signature** diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index f6074d405..9e831a632 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -319,7 +319,7 @@ Added in v2.0.0 ## ~~getMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -331,7 +331,7 @@ Added in v2.0.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 87dee80d5..8445b9ed0 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -931,7 +931,7 @@ Added in v2.7.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -943,7 +943,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** @@ -969,7 +969,7 @@ Added in v2.0.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/Monoid.ts.md b/docs/modules/Monoid.ts.md index 55a3458e3..d73c07e41 100644 --- a/docs/modules/Monoid.ts.md +++ b/docs/modules/Monoid.ts.md @@ -275,9 +275,9 @@ Added in v2.0.0 ## ~~getEndomorphismMonoid~~ -Use [`getEndomorphismMonoid`](./function.ts.html#getEndomorphismMonoid) instead. +Use [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) instead. -**Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getEndomorphismMonoid) is reversed. +**Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) is reversed. **Signature** @@ -289,7 +289,7 @@ Added in v2.0.0 ## ~~getFunctionMonoid~~ -Use [`getMonoid`](./function.ts.html#getMonoid) instead. +Use [`getMonoid`](./function.ts.html#getmonoid) instead. **Signature** @@ -313,7 +313,7 @@ Added in v2.0.0 ## ~~monoidAny~~ -Use [`MonoidAny`](./boolean.ts.html#MonoidAny) instead. +Use [`MonoidAny`](./boolean.ts.html#monoidany) instead. **Signature** @@ -325,7 +325,7 @@ Added in v2.0.0 ## ~~monoidProduct~~ -Use [`MonoidProduct`](./number.ts.html#MonoidProduct) instead. +Use [`MonoidProduct`](./number.ts.html#monoidproduct) instead. **Signature** @@ -337,7 +337,7 @@ Added in v2.0.0 ## ~~monoidString~~ -Use [`Monoid`](./string.ts.html#Monoid) instead. +Use [`Monoid`](./string.ts.html#monoid) instead. **Signature** @@ -349,7 +349,7 @@ Added in v2.0.0 ## ~~monoidSum~~ -Use [`MonoidSum`](./number.ts.html#MonoidSum) instead. +Use [`MonoidSum`](./number.ts.html#monoidsum) instead. **Signature** diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 35987906b..865fc64f7 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -790,7 +790,7 @@ Added in v2.5.1 ## ~~filterWithIndex~~ -Use [`filterWithIndex`](./Array.ts.html#filterWithIndex) instead. +Use [`filterWithIndex`](./Array.ts.html#filterwithindex) instead. **Signature** diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index c9a611c81..ee13ba5c2 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1028,7 +1028,7 @@ Added in v2.0.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1040,7 +1040,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/Ord.ts.md b/docs/modules/Ord.ts.md index c820d4ac7..148a4d525 100644 --- a/docs/modules/Ord.ts.md +++ b/docs/modules/Ord.ts.md @@ -278,7 +278,7 @@ Added in v2.0.0 ## ~~ordBoolean~~ -Use [`Ord`](./boolean.ts.html#Ord) instead. +Use [`Ord`](./boolean.ts.html#ord) instead. **Signature** @@ -290,7 +290,7 @@ Added in v2.0.0 ## ~~ordDate~~ -Use [`Ord`](./Date.ts.html#Ord) instead. +Use [`Ord`](./Date.ts.html#ord) instead. **Signature** @@ -302,7 +302,7 @@ Added in v2.0.0 ## ~~ordNumber~~ -Use [`Ord`](./number.ts.html#Ord) instead. +Use [`Ord`](./number.ts.html#ord) instead. **Signature** @@ -314,7 +314,7 @@ Added in v2.0.0 ## ~~ordString~~ -Use [`Ord`](./string.ts.html#Ord) instead. +Use [`Ord`](./string.ts.html#ord) instead. **Signature** diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 2aff33fa0..b69807de4 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -525,7 +525,7 @@ Added in v2.0.0 ## ~~getMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -537,7 +537,7 @@ Added in v2.0.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 327ee3765..0d360fe4e 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -1027,7 +1027,7 @@ Added in v2.10.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1039,7 +1039,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** @@ -1065,7 +1065,7 @@ Added in v2.3.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index d9271426a..36b5d00e6 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -273,7 +273,7 @@ Added in v2.11.0 ## chainFirstReaderKW -Less strict version of [`chainFirstReaderK`](#chainfirstReaderk). +Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). **Signature** @@ -614,7 +614,7 @@ Added in v2.3.0 ## ~~getMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -626,7 +626,7 @@ Added in v2.3.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 26436daaa..19ed85745 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -1512,7 +1512,7 @@ Added in v2.10.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1524,7 +1524,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values are concatenated using the provided `Semigroup` @@ -1553,7 +1553,7 @@ Added in v2.3.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index 098f70266..dba59ecfb 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -885,7 +885,7 @@ Added in v2.5.1 ## ~~filterWithIndex~~ -Use [`filterWithIndex`](./ReadonlyArray.ts.html#filterWithIndex) instead. +Use [`filterWithIndex`](./ReadonlyArray.ts.html#filterwithindex) instead. **Signature** @@ -933,7 +933,7 @@ Added in v2.5.0 ## ~~insertAt~~ -Use [`insertAt`](./ReadonlyArray.ts.html#insertAt) instead. +Use [`insertAt`](./ReadonlyArray.ts.html#insertat) instead. **Signature** diff --git a/docs/modules/Ring.ts.md b/docs/modules/Ring.ts.md index 00be1e3e6..375f25e2b 100644 --- a/docs/modules/Ring.ts.md +++ b/docs/modules/Ring.ts.md @@ -80,7 +80,7 @@ Added in v2.0.0 ## ~~getFunctionRing~~ -Use [`getRing`](./function.ts.html#getRing) instead. +Use [`getRing`](./function.ts.html#getring) instead. **Signature** diff --git a/docs/modules/Semiring.ts.md b/docs/modules/Semiring.ts.md index a08adeccf..0e0eaf7c3 100644 --- a/docs/modules/Semiring.ts.md +++ b/docs/modules/Semiring.ts.md @@ -43,7 +43,7 @@ Added in v2.0.0 ## ~~getFunctionSemiring~~ -Use [`getSemiring`](./function.ts.html#getSemiring) instead. +Use [`getSemiring`](./function.ts.html#getsemiring) instead. **Signature** diff --git a/docs/modules/Show.ts.md b/docs/modules/Show.ts.md index 7d825f609..f3474685d 100644 --- a/docs/modules/Show.ts.md +++ b/docs/modules/Show.ts.md @@ -89,7 +89,7 @@ Added in v2.0.0 ## ~~showBoolean~~ -Use [`Show`](./boolean.ts.html#Show) instead. +Use [`Show`](./boolean.ts.html#show) instead. **Signature** @@ -101,7 +101,7 @@ Added in v2.0.0 ## ~~showNumber~~ -Use [`Show`](./number.ts.html#Show) instead. +Use [`Show`](./number.ts.html#show) instead. **Signature** @@ -113,7 +113,7 @@ Added in v2.0.0 ## ~~showString~~ -Use [`Show`](./string.ts.html#Show) instead. +Use [`Show`](./string.ts.html#show) instead. **Signature** diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index bc727d3a8..571a3a978 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -459,7 +459,7 @@ Added in v2.0.0 ## ~~getMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`. @@ -473,7 +473,7 @@ Added in v2.0.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 31ffe48a2..b60bd5002 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -1146,7 +1146,7 @@ Added in v2.1.0 ## ~~getApplyMonoid~~ -Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. +Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. **Signature** @@ -1158,7 +1158,7 @@ Added in v2.0.0 ## ~~getApplySemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** @@ -1170,7 +1170,7 @@ Added in v2.0.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index 99835100f..a8adf0974 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -536,7 +536,7 @@ Added in v2.7.0 ## ~~getSemigroup~~ -Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. +Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. **Signature** diff --git a/src/BooleanAlgebra.ts b/src/BooleanAlgebra.ts index ff30e017b..7c3026e68 100644 --- a/src/BooleanAlgebra.ts +++ b/src/BooleanAlgebra.ts @@ -73,7 +73,7 @@ export const booleanAlgebraVoid: BooleanAlgebra = { export const getDualBooleanAlgebra = reverse /** - * Use [`BooleanAlgebra`](./boolean.ts.html#BooleanAlgebra) instead. + * Use [`BooleanAlgebra`](./boolean.ts.html#booleanalgebra) instead. * * @category instances * @since 2.0.0 @@ -89,7 +89,7 @@ export const booleanAlgebraBoolean: BooleanAlgebra = { } /** - * Use [`getBooleanAlgebra`](./function.ts.html#getBooleanAlgebra) instead. + * Use [`getBooleanAlgebra`](./function.ts.html#getbooleanalgebra) instead. * * @category instances * @since 2.0.0 diff --git a/src/Bounded.ts b/src/Bounded.ts index 2b286a2a7..7eb133c5c 100644 --- a/src/Bounded.ts +++ b/src/Bounded.ts @@ -29,7 +29,7 @@ export interface Bounded extends Ord { // tslint:disable: deprecation /** - * Use [`Bounded`](./number.ts.html#Bounded) instead. + * Use [`Bounded`](./number.ts.html#bounded) instead. * * @category instances * @since 2.0.0 diff --git a/src/Compactable.ts b/src/Compactable.ts index 28fcc0dde..9ca57b666 100644 --- a/src/Compactable.ts +++ b/src/Compactable.ts @@ -372,7 +372,7 @@ export function getCompactableComposition( } /** - * Use [`Separated`](./Separated.ts.html#Separated) instead. + * Use [`Separated`](./Separated.ts.html#separated) instead. * * @since 2.0.0 * @deprecated diff --git a/src/Either.ts b/src/Either.ts index 720934f47..bdf6159ea 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1458,7 +1458,7 @@ export const either: Monad2 & } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values * are concatenated using the provided `Semigroup` @@ -1472,7 +1472,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup(M: Monoid) => Monoid> = getApplicativeMonoid(Applicative) /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -1493,7 +1493,7 @@ export const getValidationSemigroup = (SE: Semigroup, SA: Semigroup) getApplySemigroup_(getApplicativeValidation(SE))(SA) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category instances * @since 2.0.0 diff --git a/src/Eq.ts b/src/Eq.ts index 2e7f7b5e4..a24347c03 100644 --- a/src/Eq.ts +++ b/src/Eq.ts @@ -198,7 +198,7 @@ export const strictEqual: (a: A, b: A) => boolean = eqStrict.equals export const eq: Contravariant1 = Contravariant /** - * Use [`Eq`](./boolean.ts.html#Eq) instead. + * Use [`Eq`](./boolean.ts.html#eq) instead. * * @category instances * @since 2.0.0 @@ -207,7 +207,7 @@ export const eq: Contravariant1 = Contravariant export const eqBoolean: Eq = eqStrict /** - * Use [`Eq`](./string.ts.html#Eq) instead. + * Use [`Eq`](./string.ts.html#eq) instead. * * @category instances * @since 2.0.0 @@ -216,7 +216,7 @@ export const eqBoolean: Eq = eqStrict export const eqString: Eq = eqStrict /** - * Use [`Eq`](./number.ts.html#Eq) instead. + * Use [`Eq`](./number.ts.html#eq) instead. * * @category instances * @since 2.0.0 @@ -225,7 +225,7 @@ export const eqString: Eq = eqStrict export const eqNumber: Eq = eqStrict /** - * Use [`Eq`](./Date.ts.html#Eq) instead. + * Use [`Eq`](./Date.ts.html#eq) instead. * * @category instances * @since 2.0.0 diff --git a/src/Field.ts b/src/Field.ts index a3a339145..61da6d73c 100644 --- a/src/Field.ts +++ b/src/Field.ts @@ -51,7 +51,7 @@ export function lcm(E: Eq, F: Field): (x: A, y: A) => A { // ------------------------------------------------------------------------------------- /** - * Use [`Field`](./number.ts.html#Field) instead. + * Use [`Field`](./number.ts.html#field) instead. * * @category instances * @since 2.0.0 diff --git a/src/IO.ts b/src/IO.ts index 0eb872f7c..8c1ca1de8 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -400,7 +400,7 @@ export const io: Monad1 & MonadIO1 & ChainRec1 = { } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -411,7 +411,7 @@ export const getSemigroup: (S: Semigroup) => Semigroup> = getApplySemigroup(Apply) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category instances * @since 2.0.0 diff --git a/src/IOEither.ts b/src/IOEither.ts index 2942f3d5d..f0458941c 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -1084,7 +1084,7 @@ export const ioEither: Monad2 & Bifunctor2 & Alt2 & MonadIO2 } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -1095,7 +1095,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup(M: Monoid) => Monoid> = getApplicativeMonoid(ApplicativePar) /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 diff --git a/src/Monoid.ts b/src/Monoid.ts index 2e35e80cf..571988ab9 100644 --- a/src/Monoid.ts +++ b/src/Monoid.ts @@ -286,7 +286,7 @@ export const monoidAll: Monoid = { } /** - * Use [`MonoidAny`](./boolean.ts.html#MonoidAny) instead. + * Use [`MonoidAny`](./boolean.ts.html#monoidany) instead. * * @category instances * @since 2.0.0 @@ -298,7 +298,7 @@ export const monoidAny: Monoid = { } /** - * Use [`getMonoid`](./function.ts.html#getMonoid) instead. + * Use [`getMonoid`](./function.ts.html#getmonoid) instead. * * @category instances * @since 2.0.0 @@ -307,9 +307,9 @@ export const monoidAny: Monoid = { export const getFunctionMonoid: (M: Monoid) => () => Monoid<(a: A) => M> = getFM /** - * Use [`getEndomorphismMonoid`](./function.ts.html#getEndomorphismMonoid) instead. + * Use [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) instead. * - * **Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getEndomorphismMonoid) is reversed. + * **Note**. The execution order in [`getEndomorphismMonoid`](./function.ts.html#getendomorphismmonoid) is reversed. * * @category instances * @since 2.0.0 @@ -318,7 +318,7 @@ export const getFunctionMonoid: (M: Monoid) => () => Monoid<(a: export const getEndomorphismMonoid = (): Monoid> => reverse(getEM()) /** - * Use [`Monoid`](./string.ts.html#Monoid) instead. + * Use [`Monoid`](./string.ts.html#monoid) instead. * * @category instances * @since 2.0.0 @@ -330,7 +330,7 @@ export const monoidString: Monoid = { } /** - * Use [`MonoidSum`](./number.ts.html#MonoidSum) instead. + * Use [`MonoidSum`](./number.ts.html#monoidsum) instead. * * @category instances * @since 2.0.0 @@ -342,7 +342,7 @@ export const monoidSum: Monoid = { } /** - * Use [`MonoidProduct`](./number.ts.html#MonoidProduct) instead. + * Use [`MonoidProduct`](./number.ts.html#monoidproduct) instead. * * @category instances * @since 2.0.0 diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index 3276a1edd..b97c670c6 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -1255,7 +1255,7 @@ export function filter(predicate: Predicate): (as: NonEmptyArray) => Op } /** - * Use [`filterWithIndex`](./Array.ts.html#filterWithIndex) instead. + * Use [`filterWithIndex`](./Array.ts.html#filterwithindex) instead. * * @category combinators * @since 2.0.0 diff --git a/src/Option.ts b/src/Option.ts index 80d9e8fce..23eb6ef3a 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -1335,7 +1335,7 @@ export const option: Monad1 & } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -1346,7 +1346,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup> = getApplySemigroup_(Apply) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category instances * @since 2.0.0 diff --git a/src/Ord.ts b/src/Ord.ts index 1695bb3f3..d59f26952 100644 --- a/src/Ord.ts +++ b/src/Ord.ts @@ -360,7 +360,7 @@ const strictOrd = { } /** - * Use [`Ord`](./boolean.ts.html#Ord) instead. + * Use [`Ord`](./boolean.ts.html#ord) instead. * * @category instances * @since 2.0.0 @@ -369,7 +369,7 @@ const strictOrd = { export const ordBoolean: Ord = strictOrd /** - * Use [`Ord`](./string.ts.html#Ord) instead. + * Use [`Ord`](./string.ts.html#ord) instead. * * @category instances * @since 2.0.0 @@ -378,7 +378,7 @@ export const ordBoolean: Ord = strictOrd export const ordString: Ord = strictOrd /** - * Use [`Ord`](./number.ts.html#Ord) instead. + * Use [`Ord`](./number.ts.html#ord) instead. * * @category instances * @since 2.0.0 @@ -387,7 +387,7 @@ export const ordString: Ord = strictOrd export const ordNumber: Ord = strictOrd /** - * Use [`Ord`](./Date.ts.html#Ord) instead. + * Use [`Ord`](./Date.ts.html#ord) instead. * * @category instances * @since 2.0.0 diff --git a/src/Reader.ts b/src/Reader.ts index 6c17f7549..f1ff39ecd 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -553,7 +553,7 @@ export const reader: Monad2 & Profunctor2 & Category2 & Strong2(S: Semigroup) => Semigroup> = getApplySemigroup(Apply) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category instances * @since 2.0.0 diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index cb28a7052..1996893e9 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -1034,7 +1034,7 @@ export const readerEither: Monad3 & Bifunctor3 & Alt3 & MonadThro } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -1045,7 +1045,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup(M: Monoid) => Monoid(S: Semigroup) => Semigroup getApplySemigroup_(ApplySeq) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category instances * @since 2.3.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index fd1830995..6490b3a2c 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1528,7 +1528,7 @@ export const readerTaskEitherSeq: typeof readerTaskEither = { } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values * are concatenated using the provided `Semigroup` @@ -1542,7 +1542,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup(M: Monoid) => Monoid(predicate: Predicate): (as: ReadonlyNonEmptyArray( export const snoc = (init: ReadonlyArray, end: A): ReadonlyNonEmptyArray => pipe(init, concat([end])) /** - * Use [`insertAt`](./ReadonlyArray.ts.html#insertAt) instead. + * Use [`insertAt`](./ReadonlyArray.ts.html#insertat) instead. * * @category combinators * @since 2.5.0 diff --git a/src/Ring.ts b/src/Ring.ts index 77f8d9bbc..84a13981d 100644 --- a/src/Ring.ts +++ b/src/Ring.ts @@ -81,7 +81,7 @@ export const getTupleRing: >>( ) => Ring<{ [K in keyof T]: T[K] extends Ring ? A : never }> = tuple as any /** - * Use [`getRing`](./function.ts.html#getRing) instead. + * Use [`getRing`](./function.ts.html#getring) instead. * * @category instances * @since 2.0.0 diff --git a/src/Semiring.ts b/src/Semiring.ts index cb1b6575b..0e35869c9 100644 --- a/src/Semiring.ts +++ b/src/Semiring.ts @@ -43,7 +43,7 @@ export interface Semiring { // ------------------------------------------------------------------------------------- /** - * Use [`getSemiring`](./function.ts.html#getSemiring) instead. + * Use [`getSemiring`](./function.ts.html#getsemiring) instead. * * @category instances * @since 2.0.0 diff --git a/src/Show.ts b/src/Show.ts index 8c72fb50a..b0538fce8 100644 --- a/src/Show.ts +++ b/src/Show.ts @@ -84,7 +84,7 @@ export const getStructShow: >( ) => Show = struct /** - * Use [`Show`](./boolean.ts.html#Show) instead. + * Use [`Show`](./boolean.ts.html#show) instead. * * @category instances * @since 2.0.0 @@ -95,7 +95,7 @@ export const showBoolean: Show = { } /** - * Use [`Show`](./string.ts.html#Show) instead. + * Use [`Show`](./string.ts.html#show) instead. * * @category instances * @since 2.0.0 @@ -106,7 +106,7 @@ export const showString: Show = { } /** - * Use [`Show`](./number.ts.html#Show) instead. + * Use [`Show`](./number.ts.html#show) instead. * * @category instances * @since 2.0.0 diff --git a/src/Task.ts b/src/Task.ts index bcac75e5c..29535a79d 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -622,7 +622,7 @@ export const taskSeq: typeof task = { } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -633,7 +633,7 @@ export const getSemigroup: (S: Semigroup) => Semigroup> = getApplySemigroup_(ApplySeq) /** - * Use [`getApplicativeMonoid`](./Applicative.ts.html#getApplicativeMonoid) instead. + * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`. * diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 4fcbcd1ab..afbc9c3da 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -1372,7 +1372,7 @@ export const taskEitherSeq: typeof taskEither = { } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 @@ -1383,7 +1383,7 @@ export const getApplySemigroup: (S: Semigroup) => Semigroup(M: Monoid) => Monoid> = getApplicativeMonoid(ApplicativeSeq) /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.0.0 diff --git a/src/TaskThese.ts b/src/TaskThese.ts index 85da381e7..54223ad77 100644 --- a/src/TaskThese.ts +++ b/src/TaskThese.ts @@ -622,7 +622,7 @@ export const taskThese: Functor2 & Bifunctor2 = { } /** - * Use [`getApplySemigroup`](./Apply.ts.html#getApplySemigroup) instead. + * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category instances * @since 2.4.0 From 6df750366f3f6fc607ae48c20b28d6e085072bf8 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 12 May 2021 17:53:59 +0200 Subject: [PATCH 147/162] revert array utils deprecations --- CHANGELOG.md | 69 ------------- docs/modules/Either.ts.md | 80 +++++++-------- docs/modules/IO.ts.md | 52 +++++----- docs/modules/IOEither.ts.md | 112 ++++++++++----------- docs/modules/Option.ts.md | 64 ++++++------ docs/modules/Reader.ts.md | 56 +++++------ docs/modules/ReaderEither.ts.md | 56 +++++------ docs/modules/ReaderTask.ts.md | 112 ++++++++++----------- docs/modules/ReaderTaskEither.ts.md | 122 ++++++++++------------- docs/modules/State.ts.md | 74 +++++++------- docs/modules/StateReaderTaskEither.ts.md | 84 ++++++++-------- docs/modules/Task.ts.md | 108 +++++++++----------- docs/modules/TaskEither.ts.md | 118 ++++++++++------------ docs/modules/TaskOption.ts.md | 108 +++++++++----------- src/Either.ts | 21 ++-- src/IO.ts | 21 ++-- src/IOEither.ts | 30 ++---- src/Option.ts | 21 ++-- src/Reader.ts | 21 ++-- src/ReaderEither.ts | 21 ++-- src/ReaderTask.ts | 27 ++--- src/ReaderTaskEither.ts | 30 ++---- src/State.ts | 21 ++-- src/StateReaderTaskEither.ts | 21 ++-- src/Task.ts | 30 ++---- src/TaskEither.ts | 30 ++---- src/TaskOption.ts | 30 ++---- 27 files changed, 595 insertions(+), 944 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a66959ee5..52ec50271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,27 +20,12 @@ high state of flux, you're at risk of it changing without notice. - **Deprecation** - `Array` - deprecate `range`, use `NonEmptyArray` module instead. - - `Either` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - `function` - deprecate `Endomorphism`, use `Endomorphism` module instead. - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. - deprecate `Predicate`, use `Predicate` module instead. - deprecate `not`, use `Predicate` module instead. - deprecate `Refinement`, use `Refinement` module instead. - - `IO` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `IOEither` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `Monoid` - deprecate `monoidVoid`, use `void` module instead. - `NonEmptyArray` @@ -49,31 +34,6 @@ high state of flux, you're at risk of it changing without notice. - deprecate `getRefinement`, use `Refinement` module instead. - deprecate `getFirstMonoid`, use `getMonoid` module instead. - deprecate `getLastMonoid`, use `getMonoid` module instead. - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `Reader` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `ReaderEither` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `ReaderTask` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - `ReaderTaskEither` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - `ReadonlyArray` - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. - `ReadonlyNonEmptyArray` @@ -94,35 +54,6 @@ high state of flux, you're at risk of it changing without notice. - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) - `Semigroup` - deprecate `semigroupVoid`, use `void` module instead. - - `State` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `StateReaderTaskEither` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - `Task` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - `TaskOption` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - `TaskEither` - - deprecate `traverseArrayWithIndex` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `sequenceArray` in favour of `traverseReadonlyArrayWithIndex` - - deprecate `traverseSeqArrayWithIndex` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `traverseSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - - deprecate `sequenceSeqArray` in favour of `traverseReadonlyArrayWithIndexSeq` - **New Feature** - add `Endomorphism` module - add `Predicate` module diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index b4c5903f8..9a677e36f 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -128,15 +128,15 @@ Added in v2.0.0 - [bindW](#bindw) - [elem](#elem) - [exists](#exists) + - [sequenceArray](#sequencearray) - [toError](#toerror) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~JsonArray~~ (interface)](#jsonarray-interface) - [~~JsonRecord~~ (interface)](#jsonrecord-interface) - [~~Json~~ (type alias)](#json-type-alias) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -1547,6 +1547,16 @@ assert.strictEqual(gt2(right(3)), true) Added in v2.0.0 +## sequenceArray + +**Signature** + +```ts +export declare const sequenceArray: (as: readonly Either[]) => Either +``` + +Added in v2.9.0 + ## toError Default value for the `onError` argument of `tryCatch` @@ -1559,6 +1569,30 @@ export declare function toError(e: unknown): Error Added in v2.0.0 +## traverseArray + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => Either +) => (as: readonly A[]) => Either +``` + +Added in v2.9.0 + +## traverseArrayWithIndex + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => Either +) => (as: readonly A[]) => Either +``` + +Added in v2.9.0 + ## traverseReadonlyArrayWithIndex Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. @@ -1624,43 +1658,3 @@ export type Json = boolean | number | string | null | JsonArray | JsonRecord ``` Added in v2.6.7 - -## ~~sequenceArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const sequenceArray: (as: readonly Either[]) => Either -``` - -Added in v2.9.0 - -## ~~traverseArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => Either -) => (as: readonly A[]) => Either -``` - -Added in v2.9.0 - -## ~~traverseArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArray: ( - f: (a: A) => Either -) => (as: readonly A[]) => Either -``` - -Added in v2.9.0 diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index 9e831a632..0ff85e840 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -61,11 +61,11 @@ Added in v2.0.0 - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -425,68 +425,62 @@ export declare const bindTo: (name: N) => (fa: IO) => IO<{ readonly [K Added in v2.8.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => IO -) => (as: readonly A[]) => IO +export declare const sequenceArray: (arr: readonly IO[]) => IO ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndex +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => IO -) => (as: ReadonlyNonEmptyArray) => IO> +export declare const traverseArray: (f: (a: A) => IO) => (as: readonly A[]) => IO ``` -Added in v2.11.0 - -## ~~sequenceArray~~ +Added in v2.9.0 -Use `traverseReadonlyArrayWithIndex` instead. +## traverseArrayWithIndex **Signature** ```ts -export declare const sequenceArray: (arr: readonly IO[]) => IO +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => IO +) => (as: readonly A[]) => IO ``` Added in v2.9.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyArrayWithIndex: ( f: (index: number, a: A) => IO ) => (as: readonly A[]) => IO ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => IO) => (as: readonly A[]) => IO +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => IO +) => (as: ReadonlyNonEmptyArray) => IO> ``` -Added in v2.9.0 +Added in v2.11.0 diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 8445b9ed0..f0be77bcd 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -117,16 +117,16 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) + - [sequenceArray](#sequencearray) + - [sequenceSeqArray](#sequenceseqarray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - - [~~sequenceArray~~](#sequencearray) - - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) --- @@ -1183,137 +1183,125 @@ export declare const bracket: ( Added in v2.0.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => IOEither -) => (as: readonly A[]) => IOEither +export declare const sequenceArray: (arr: readonly IOEither[]) => IOEither ``` -Added in v2.11.0 +Added in v2.9.0 -## traverseReadonlyArrayWithIndexSeq - -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +## sequenceSeqArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndexSeq: ( - f: (index: number, a: A) => IOEither -) => (as: readonly A[]) => IOEither +export declare const sequenceSeqArray: (arr: readonly IOEither[]) => IOEither ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndex +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => IOEither -) => (as: ReadonlyNonEmptyArray) => IOEither> +export declare const traverseArray: ( + f: (a: A) => IOEither +) => (as: readonly A[]) => IOEither ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndexSeq +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. +## traverseArrayWithIndex **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( +export declare const traverseArrayWithIndex: ( f: (index: number, a: A) => IOEither -) => (as: ReadonlyNonEmptyArray) => IOEither> +) => (as: readonly A[]) => IOEither ``` -Added in v2.11.0 +Added in v2.9.0 -## ~~sequenceArray~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly IOEither[]) => IOEither +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => IOEither +) => (as: readonly A[]) => IOEither ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~sequenceSeqArray~~ +## traverseReadonlyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndexSeq` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (arr: readonly IOEither[]) => IOEither +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => IOEither +) => (as: readonly A[]) => IOEither ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => IOEither -) => (as: readonly A[]) => IOEither +) => (as: ReadonlyNonEmptyArray) => IOEither> ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => IOEither -) => (as: readonly A[]) => IOEither +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => IOEither +) => (as: ReadonlyNonEmptyArray) => IOEither> ``` -Added in v2.9.0 - -## ~~traverseSeqArrayWithIndex~~ +Added in v2.11.0 -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArray **Signature** ```ts -export declare const traverseSeqArrayWithIndex: ( - f: (index: number, a: A) => IOEither +export declare const traverseSeqArray: ( + f: (a: A) => IOEither ) => (as: readonly A[]) => IOEither ``` Added in v2.9.0 -## ~~traverseSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArrayWithIndex **Signature** ```ts -export declare const traverseSeqArray: ( - f: (a: A) => IOEither +export declare const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => IOEither ) => (as: readonly A[]) => IOEither ``` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index ee13ba5c2..f371bebe1 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -124,12 +124,12 @@ Added in v2.0.0 - [bindTo](#bindto) - [elem](#elem) - [exists](#exists) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~getRefinement~~](#getrefinement) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -1566,80 +1566,74 @@ assert.strictEqual( Added in v2.0.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => Option -) => (as: readonly A[]) => Option +export declare const sequenceArray: (arr: readonly Option[]) => Option ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndex +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => Option -) => (as: ReadonlyNonEmptyArray) => Option> +export declare const traverseArray: (f: (a: A) => Option) => (as: readonly A[]) => Option ``` -Added in v2.11.0 - -## ~~getRefinement~~ +Added in v2.9.0 -Use `Refinement` module instead. +## traverseArrayWithIndex **Signature** ```ts -export declare function getRefinement(getOption: (a: A) => Option): Refinement +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => Option +) => (as: readonly A[]) => Option ``` -Added in v2.0.0 +Added in v2.9.0 -## ~~sequenceArray~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly Option[]) => Option +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Option +) => (as: readonly A[]) => Option ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => Option -) => (as: readonly A[]) => Option +) => (as: ReadonlyNonEmptyArray) => Option> ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## ~~getRefinement~~ -Use `traverseReadonlyArrayWithIndex` instead. +Use `Refinement` module instead. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => Option) => (as: readonly A[]) => Option +export declare function getRefinement(getOption: (a: A) => Option): Refinement ``` -Added in v2.9.0 +Added in v2.0.0 diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index b69807de4..81ba06606 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -74,11 +74,11 @@ Added in v2.0.0 - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -661,70 +661,64 @@ export declare const bindW: ( Added in v2.8.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => Reader -) => (as: readonly A[]) => Reader +export declare const sequenceArray: (arr: readonly Reader[]) => Reader ``` -Added in v2.11.0 +Added in v2.9.0 -## traverseReadonlyNonEmptyArrayWithIndex - -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => Reader -) => (as: ReadonlyNonEmptyArray) => Reader> +export declare const traverseArray: ( + f: (a: A) => Reader +) => (as: readonly A[]) => Reader ``` -Added in v2.11.0 - -## ~~sequenceArray~~ +Added in v2.9.0 -Use `traverseReadonlyArrayWithIndex` instead. +## traverseArrayWithIndex **Signature** ```ts -export declare const sequenceArray: (arr: readonly Reader[]) => Reader +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => Reader +) => (as: readonly A[]) => Reader ``` Added in v2.9.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyArrayWithIndex: ( f: (index: number, a: A) => Reader ) => (as: readonly A[]) => Reader ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => Reader -) => (as: readonly A[]) => Reader +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => Reader +) => (as: ReadonlyNonEmptyArray) => Reader> ``` -Added in v2.9.0 +Added in v2.11.0 diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 0d360fe4e..e25abf199 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -115,11 +115,11 @@ Added in v2.0.0 - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -1236,70 +1236,64 @@ export declare const bindW: ( Added in v2.8.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => ReaderEither -) => (as: readonly A[]) => ReaderEither +export declare const sequenceArray: (arr: readonly ReaderEither[]) => ReaderEither ``` -Added in v2.11.0 +Added in v2.9.0 -## traverseReadonlyNonEmptyArrayWithIndex - -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => ReaderEither -) => (as: ReadonlyNonEmptyArray) => ReaderEither> +export declare const traverseArray: ( + f: (a: A) => ReaderEither +) => (as: readonly A[]) => ReaderEither ``` -Added in v2.11.0 - -## ~~sequenceArray~~ +Added in v2.9.0 -Use `traverseReadonlyArrayWithIndex` instead. +## traverseArrayWithIndex **Signature** ```ts -export declare const sequenceArray: (arr: readonly ReaderEither[]) => ReaderEither +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => ReaderEither +) => (as: readonly A[]) => ReaderEither ``` Added in v2.9.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyArrayWithIndex: ( f: (index: number, a: A) => ReaderEither ) => (as: readonly A[]) => ReaderEither ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const traverseArray: ( - f: (a: A) => ReaderEither -) => (as: readonly A[]) => ReaderEither +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => ReaderEither +) => (as: ReadonlyNonEmptyArray) => ReaderEither> ``` -Added in v2.9.0 +Added in v2.11.0 diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 36b5d00e6..005b1d080 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -81,17 +81,17 @@ Added in v2.3.0 - [bind](#bind) - [bindTo](#bindto) - [bindW](#bindw) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) - [~~run~~](#run) - - [~~sequenceArray~~](#sequencearray) - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -790,6 +790,40 @@ export declare const bindW: ( Added in v2.8.0 +## sequenceArray + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly ReaderTask[]) => ReaderTask +``` + +Added in v2.9.0 + +## traverseArray + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask +``` + +Added in v2.9.0 + +## traverseArrayWithIndex + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask +``` + +Added in v2.9.0 + ## traverseReadonlyArrayWithIndex Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. @@ -846,92 +880,48 @@ export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( Added in v2.11.0 -## ~~run~~ - -**Signature** - -```ts -export declare function run(ma: ReaderTask, r: R): Promise -``` - -Added in v2.4.0 - -## ~~sequenceArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. +## traverseSeqArray **Signature** ```ts -export declare const sequenceArray: (arr: readonly ReaderTask[]) => ReaderTask -``` - -Added in v2.9.0 - -## ~~sequenceSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. - -**Signature** - -```ts -export declare const sequenceSeqArray: (arr: readonly ReaderTask[]) => ReaderTask +export declare const traverseSeqArray: ( + f: (a: A) => ReaderTask +) => (as: readonly A[]) => ReaderTask ``` Added in v2.10.0 -## ~~traverseArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndex` instead. +## traverseSeqArrayWithIndex **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => ReaderTask ) => (as: readonly A[]) => ReaderTask ``` -Added in v2.9.0 - -## ~~traverseArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArray: ( - f: (a: A) => ReaderTask -) => (as: readonly A[]) => ReaderTask -``` - -Added in v2.9.0 - -## ~~traverseSeqArrayWithIndex~~ +Added in v2.10.0 -Use `traverseReadonlyArrayWithIndexSeq` instead. +## ~~run~~ **Signature** ```ts -export declare const traverseSeqArrayWithIndex: ( - f: (index: number, a: A) => ReaderTask -) => (as: readonly A[]) => ReaderTask +export declare function run(ma: ReaderTask, r: R): Promise ``` -Added in v2.10.0 +Added in v2.4.0 -## ~~traverseSeqArray~~ +## ~~sequenceSeqArray~~ Use `traverseReadonlyArrayWithIndexSeq` instead. **Signature** ```ts -export declare const traverseSeqArray: ( - f: (a: A) => ReaderTask -) => (as: readonly A[]) => ReaderTask +export declare const sequenceSeqArray: (arr: readonly ReaderTask[]) => ReaderTask ``` Added in v2.10.0 diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 19ed85745..8c865e7fe 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -158,17 +158,17 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) + - [sequenceArray](#sequencearray) + - [sequenceSeqArray](#sequenceseqarray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) - [~~run~~](#run) - - [~~sequenceArray~~](#sequencearray) - - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) --- @@ -1806,131 +1806,123 @@ export declare function bracket( Added in v2.0.4 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => ReaderTaskEither -) => (as: readonly A[]) => ReaderTaskEither +export declare const sequenceArray: ( + arr: readonly ReaderTaskEither[] +) => ReaderTaskEither ``` -Added in v2.11.0 - -## traverseReadonlyArrayWithIndexSeq +Added in v2.9.0 -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +## sequenceSeqArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndexSeq: ( - f: (index: number, a: A) => ReaderTaskEither -) => (as: readonly A[]) => ReaderTaskEither +export declare const sequenceSeqArray: ( + arr: readonly ReaderTaskEither[] +) => ReaderTaskEither ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndex +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => ReaderTaskEither -) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> +export declare const traverseArray: ( + f: (a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.11.0 +Added in v2.9.0 -## traverseReadonlyNonEmptyArrayWithIndexSeq - -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. +## traverseArrayWithIndex **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( +export declare const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderTaskEither -) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.11.0 +Added in v2.9.0 -## ~~run~~ +## traverseReadonlyArrayWithIndex + +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare function run(ma: ReaderTaskEither, r: R): Promise> +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.0.0 +Added in v2.11.0 -## ~~sequenceArray~~ +## traverseReadonlyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceArray: ( - arr: readonly ReaderTaskEither[] -) => ReaderTaskEither +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: readonly A[]) => ReaderTaskEither ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~sequenceSeqArray~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndexSeq` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceSeqArray: ( - arr: readonly ReaderTaskEither[] -) => ReaderTaskEither +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => ReaderTaskEither +) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyNonEmptyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( f: (index: number, a: A) => ReaderTaskEither -) => (as: readonly A[]) => ReaderTaskEither +) => (as: ReadonlyNonEmptyArray) => ReaderTaskEither> ``` -Added in v2.9.0 - -## ~~traverseArray~~ +Added in v2.11.0 -Use `traverseReadonlyArrayWithIndex` instead. +## traverseSeqArray **Signature** ```ts -export declare const traverseArray: ( +export declare const traverseSeqArray: ( f: (a: A) => ReaderTaskEither ) => (as: readonly A[]) => ReaderTaskEither ``` Added in v2.9.0 -## ~~traverseSeqArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArrayWithIndex **Signature** @@ -1942,16 +1934,12 @@ export declare const traverseSeqArrayWithIndex: ( Added in v2.9.0 -## ~~traverseSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## ~~run~~ **Signature** ```ts -export declare const traverseSeqArray: ( - f: (a: A) => ReaderTaskEither -) => (as: readonly A[]) => ReaderTaskEither +export declare function run(ma: ReaderTaskEither, r: R): Promise> ``` -Added in v2.9.0 +Added in v2.0.0 diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index 2ab884781..1d693d443 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -50,13 +50,13 @@ Added in v2.0.0 - [bindTo](#bindto) - [evaluate](#evaluate) - [execute](#execute) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~evalState~~](#evalstate) - [~~execState~~](#execstate) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -414,92 +414,86 @@ export declare const execute: (s: S) => (ma: State) => S Added in v2.8.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => State -) => (as: readonly A[]) => State +export declare const sequenceArray: (arr: readonly State[]) => State ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndex +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => State -) => (as: ReadonlyNonEmptyArray) => State> +export declare const traverseArray: (f: (a: A) => State) => (as: readonly A[]) => State ``` -Added in v2.11.0 - -## ~~evalState~~ +Added in v2.9.0 -Use [`evaluate`](#evaluate) instead +## traverseArrayWithIndex **Signature** ```ts -export declare const evalState: (ma: State, s: S) => A +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: readonly A[]) => State ``` -Added in v2.0.0 +Added in v2.9.0 -## ~~execState~~ +## traverseReadonlyArrayWithIndex -Use [`execute`](#execute) instead +Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const execState: (ma: State, s: S) => S +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: readonly A[]) => State ``` -Added in v2.0.0 +Added in v2.11.0 -## ~~sequenceArray~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly State[]) => State +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( + f: (index: number, a: A) => State +) => (as: ReadonlyNonEmptyArray) => State> ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## ~~evalState~~ -Use `traverseReadonlyArrayWithIndex` instead. +Use [`evaluate`](#evaluate) instead **Signature** ```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => State -) => (as: readonly A[]) => State +export declare const evalState: (ma: State, s: S) => A ``` -Added in v2.9.0 +Added in v2.0.0 -## ~~traverseArray~~ +## ~~execState~~ -Use `traverseReadonlyArrayWithIndex` instead. +Use [`execute`](#execute) instead **Signature** ```ts -export declare const traverseArray: (f: (a: A) => State) => (as: readonly A[]) => State +export declare const execState: (ma: State, s: S) => S ``` -Added in v2.9.0 +Added in v2.0.0 diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 6649ed1f8..e9c0d1263 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -130,14 +130,14 @@ Added in v2.0.0 - [bindW](#bindw) - [evaluate](#evaluate) - [execute](#execute) + - [sequenceArray](#sequencearray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [~~evalState~~](#evalstate) - [~~execState~~](#execstate) - [~~run~~](#run) - - [~~sequenceArray~~](#sequencearray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) --- @@ -1449,6 +1449,42 @@ export declare const execute: ( Added in v2.8.0 +## sequenceArray + +**Signature** + +```ts +export declare const sequenceArray: ( + arr: readonly StateReaderTaskEither[] +) => StateReaderTaskEither +``` + +Added in v2.9.0 + +## traverseArray + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => StateReaderTaskEither +) => (as: readonly A[]) => StateReaderTaskEither +``` + +Added in v2.9.0 + +## traverseArrayWithIndex + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => StateReaderTaskEither +) => (as: readonly A[]) => StateReaderTaskEither +``` + +Added in v2.9.0 + ## traverseReadonlyArrayWithIndex Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. @@ -1516,45 +1552,3 @@ export declare function run(ma: StateReaderTaskEither, s ``` Added in v2.0.0 - -## ~~sequenceArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const sequenceArray: ( - arr: readonly StateReaderTaskEither[] -) => StateReaderTaskEither -``` - -Added in v2.9.0 - -## ~~traverseArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => StateReaderTaskEither -) => (as: readonly A[]) => StateReaderTaskEither -``` - -Added in v2.9.0 - -## ~~traverseArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArray: ( - f: (a: A) => StateReaderTaskEither -) => (as: readonly A[]) => StateReaderTaskEither -``` - -Added in v2.9.0 diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index 571a3a978..c5553ece4 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -72,16 +72,16 @@ Added in v2.0.0 - [bind](#bind) - [bindTo](#bindto) - [never](#never) + - [sequenceArray](#sequencearray) + - [sequenceSeqArray](#sequenceseqarray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - - [~~sequenceArray~~](#sequencearray) - - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) --- @@ -603,134 +603,122 @@ export declare const never: Task Added in v2.0.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => Task -) => (as: readonly A[]) => Task +export declare const sequenceArray: (arr: readonly Task[]) => Task ``` -Added in v2.11.0 - -## traverseReadonlyArrayWithIndexSeq +Added in v2.9.0 -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +## sequenceSeqArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndexSeq: ( - f: (index: number, a: A) => Task -) => (as: readonly A[]) => Task +export declare const sequenceSeqArray: (arr: readonly Task[]) => Task ``` -Added in v2.11.0 +Added in v2.9.0 -## traverseReadonlyNonEmptyArrayWithIndex - -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => Task -) => (as: ReadonlyNonEmptyArray) => Task> +export declare const traverseArray: (f: (a: A) => Task) => (as: readonly A[]) => Task ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndexSeq +Added in v2.9.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. +## traverseArrayWithIndex **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( +export declare const traverseArrayWithIndex: ( f: (index: number, a: A) => Task -) => (as: ReadonlyNonEmptyArray) => Task> +) => (as: readonly A[]) => Task ``` -Added in v2.11.0 +Added in v2.9.0 -## ~~sequenceArray~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (arr: readonly Task[]) => Task +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: readonly A[]) => Task ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~sequenceSeqArray~~ +## traverseReadonlyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndexSeq` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (arr: readonly Task[]) => Task +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => Task +) => (as: readonly A[]) => Task ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => Task -) => (as: readonly A[]) => Task +) => (as: ReadonlyNonEmptyArray) => Task> ``` -Added in v2.9.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => Task) => (as: readonly A[]) => Task +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => Task +) => (as: ReadonlyNonEmptyArray) => Task> ``` -Added in v2.9.0 - -## ~~traverseSeqArrayWithIndex~~ +Added in v2.11.0 -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArray **Signature** ```ts -export declare const traverseSeqArrayWithIndex: ( - f: (index: number, a: A) => Task -) => (as: readonly A[]) => Task +export declare const traverseSeqArray: (f: (a: A) => Task) => (as: readonly A[]) => Task ``` Added in v2.9.0 -## ~~traverseSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArrayWithIndex **Signature** ```ts -export declare const traverseSeqArray: (f: (a: A) => Task) => (as: readonly A[]) => Task +export declare const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => Task +) => (as: readonly A[]) => Task ``` Added in v2.9.0 diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index b60bd5002..01dbacced 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -137,17 +137,17 @@ Added in v2.0.0 - [bindTo](#bindto) - [bindW](#bindw) - [bracket](#bracket) + - [sequenceArray](#sequencearray) + - [sequenceSeqArray](#sequenceseqarray) - [taskify](#taskify) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - - [~~sequenceArray~~](#sequencearray) - - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) --- @@ -1460,6 +1460,26 @@ export declare const bracket: ( Added in v2.0.0 +## sequenceArray + +**Signature** + +```ts +export declare const sequenceArray: (arr: readonly TaskEither[]) => TaskEither +``` + +Added in v2.9.0 + +## sequenceSeqArray + +**Signature** + +```ts +export declare const sequenceSeqArray: (arr: readonly TaskEither[]) => TaskEither +``` + +Added in v2.9.0 + ## taskify Convert a node style callback function to one returning a `TaskEither` @@ -1512,6 +1532,30 @@ assert.strictEqual(stat.length, 0) Added in v2.0.0 +## traverseArray + +**Signature** + +```ts +export declare const traverseArray: ( + f: (a: A) => TaskEither +) => (as: readonly A[]) => TaskEither +``` + +Added in v2.9.0 + +## traverseArrayWithIndex + +**Signature** + +```ts +export declare const traverseArrayWithIndex: ( + f: (index: number, a: A) => TaskEither +) => (as: readonly A[]) => TaskEither +``` + +Added in v2.9.0 + ## traverseReadonlyArrayWithIndex Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. @@ -1568,61 +1612,19 @@ export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( Added in v2.11.0 -## ~~sequenceArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const sequenceArray: (arr: readonly TaskEither[]) => TaskEither -``` - -Added in v2.9.0 - -## ~~sequenceSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. - -**Signature** - -```ts -export declare const sequenceSeqArray: (arr: readonly TaskEither[]) => TaskEither -``` - -Added in v2.9.0 - -## ~~traverseArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndex` instead. - -**Signature** - -```ts -export declare const traverseArrayWithIndex: ( - f: (index: number, a: A) => TaskEither -) => (as: readonly A[]) => TaskEither -``` - -Added in v2.9.0 - -## ~~traverseArray~~ - -Use `traverseReadonlyArrayWithIndex` instead. +## traverseSeqArray **Signature** ```ts -export declare const traverseArray: ( +export declare const traverseSeqArray: ( f: (a: A) => TaskEither ) => (as: readonly A[]) => TaskEither ``` Added in v2.9.0 -## ~~traverseSeqArrayWithIndex~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArrayWithIndex **Signature** @@ -1633,17 +1635,3 @@ export declare const traverseSeqArrayWithIndex: ( ``` Added in v2.9.0 - -## ~~traverseSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. - -**Signature** - -```ts -export declare const traverseSeqArray: ( - f: (a: A) => TaskEither -) => (as: readonly A[]) => TaskEither -``` - -Added in v2.9.0 diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index bc2997ee9..6f6362558 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -100,16 +100,16 @@ Added in v2.10.0 - [apS](#aps) - [bind](#bind) - [bindTo](#bindto) + - [sequenceArray](#sequencearray) + - [sequenceSeqArray](#sequenceseqarray) + - [traverseArray](#traversearray) + - [traverseArrayWithIndex](#traversearraywithindex) - [traverseReadonlyArrayWithIndex](#traversereadonlyarraywithindex) - [traverseReadonlyArrayWithIndexSeq](#traversereadonlyarraywithindexseq) - [traverseReadonlyNonEmptyArrayWithIndex](#traversereadonlynonemptyarraywithindex) - [traverseReadonlyNonEmptyArrayWithIndexSeq](#traversereadonlynonemptyarraywithindexseq) - - [~~sequenceArray~~](#sequencearray) - - [~~sequenceSeqArray~~](#sequenceseqarray) - - [~~traverseArrayWithIndex~~](#traversearraywithindex) - - [~~traverseArray~~](#traversearray) - - [~~traverseSeqArrayWithIndex~~](#traverseseqarraywithindex) - - [~~traverseSeqArray~~](#traverseseqarray) + - [traverseSeqArray](#traverseseqarray) + - [traverseSeqArrayWithIndex](#traverseseqarraywithindex) --- @@ -942,135 +942,123 @@ export declare const bindTo: (name: N) => (fa: TaskOption) => TaskOptio Added in v2.10.0 -## traverseReadonlyArrayWithIndex - -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. +## sequenceArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndex: ( - f: (index: number, a: A) => TaskOption -) => (as: readonly A[]) => TaskOption +export declare const sequenceArray: (as: readonly TaskOption[]) => TaskOption ``` -Added in v2.11.0 - -## traverseReadonlyArrayWithIndexSeq +Added in v2.10.0 -Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. +## sequenceSeqArray **Signature** ```ts -export declare const traverseReadonlyArrayWithIndexSeq: ( - f: (index: number, a: A) => TaskOption -) => (as: readonly A[]) => TaskOption +export declare const sequenceSeqArray: (as: readonly TaskOption[]) => TaskOption ``` -Added in v2.11.0 +Added in v2.10.0 -## traverseReadonlyNonEmptyArrayWithIndex - -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. +## traverseArray **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndex: ( - f: (index: number, a: A) => TaskOption -) => (as: ReadonlyNonEmptyArray) => TaskOption> +export declare const traverseArray: (f: (a: A) => TaskOption) => (as: readonly A[]) => TaskOption ``` -Added in v2.11.0 - -## traverseReadonlyNonEmptyArrayWithIndexSeq +Added in v2.10.0 -Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. +## traverseArrayWithIndex **Signature** ```ts -export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( +export declare const traverseArrayWithIndex: ( f: (index: number, a: A) => TaskOption -) => (as: ReadonlyNonEmptyArray) => TaskOption> +) => (as: readonly A[]) => TaskOption ``` -Added in v2.11.0 +Added in v2.10.0 -## ~~sequenceArray~~ +## traverseReadonlyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const sequenceArray: (as: readonly TaskOption[]) => TaskOption +export declare const traverseReadonlyArrayWithIndex: ( + f: (index: number, a: A) => TaskOption +) => (as: readonly A[]) => TaskOption ``` -Added in v2.10.0 +Added in v2.11.0 -## ~~sequenceSeqArray~~ +## traverseReadonlyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndexSeq` instead. +Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const sequenceSeqArray: (as: readonly TaskOption[]) => TaskOption +export declare const traverseReadonlyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskOption +) => (as: readonly A[]) => TaskOption ``` -Added in v2.10.0 +Added in v2.11.0 -## ~~traverseArrayWithIndex~~ +## traverseReadonlyNonEmptyArrayWithIndex -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`. **Signature** ```ts -export declare const traverseArrayWithIndex: ( +export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => TaskOption -) => (as: readonly A[]) => TaskOption +) => (as: ReadonlyNonEmptyArray) => TaskOption> ``` -Added in v2.10.0 +Added in v2.11.0 -## ~~traverseArray~~ +## traverseReadonlyNonEmptyArrayWithIndexSeq -Use `traverseReadonlyArrayWithIndex` instead. +Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`. **Signature** ```ts -export declare const traverseArray: (f: (a: A) => TaskOption) => (as: readonly A[]) => TaskOption +export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: ( + f: (index: number, a: A) => TaskOption +) => (as: ReadonlyNonEmptyArray) => TaskOption> ``` -Added in v2.10.0 - -## ~~traverseSeqArrayWithIndex~~ +Added in v2.11.0 -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArray **Signature** ```ts -export declare const traverseSeqArrayWithIndex: ( - f: (index: number, a: A) => TaskOption +export declare const traverseSeqArray: ( + f: (a: A) => TaskOption ) => (as: readonly A[]) => TaskOption ``` Added in v2.10.0 -## ~~traverseSeqArray~~ - -Use `traverseReadonlyArrayWithIndexSeq` instead. +## traverseSeqArrayWithIndex **Signature** ```ts -export declare const traverseSeqArray: ( - f: (a: A) => TaskOption +export declare const traverseSeqArrayWithIndex: ( + f: (index: number, a: A) => TaskOption ) => (as: readonly A[]) => TaskOption ``` diff --git a/src/Either.ts b/src/Either.ts index bdf6159ea..411b45b02 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1335,42 +1335,33 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => Either ) => (as: ReadonlyArray) => Either> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => Either ): ((as: ReadonlyArray) => Either>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (as: ReadonlyArray>) => Either> = /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use [`Json`](./Json.ts.html) module instead. * diff --git a/src/IO.ts b/src/IO.ts index 8c1ca1de8..6ca3cebfe 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -347,41 +347,32 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => IO ) => (as: ReadonlyArray) => IO> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = (f: (a: A) => IO): ((as: ReadonlyArray) => IO>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => IO> = /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/IOEither.ts b/src/IOEither.ts index f0458941c..f6a0c0498 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -984,72 +984,54 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => IOEither ) => (as: ReadonlyArray) => IOEither> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => IOEither ): ((as: ReadonlyArray) => IOEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => IOEither> = /*#__PURE__*/ traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => IOEither ) => (as: ReadonlyArray) => IOEither> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArray = ( f: (a: A) => IOEither ): ((as: ReadonlyArray) => IOEither>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => IOEither> = /*#__PURE__*/ traverseSeqArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use [`ApplicativePar`](#applicativepar) instead * diff --git a/src/Option.ts b/src/Option.ts index 23eb6ef3a..b4448596e 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -1243,41 +1243,32 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => Option ) => (as: ReadonlyArray) => Option> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = (f: (a: A) => Option): ((as: ReadonlyArray) => Option>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Option> = /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use `Refinement` module instead. * diff --git a/src/Reader.ts b/src/Reader.ts index f1ff39ecd..b0d6239f0 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -494,42 +494,33 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => Reader ) => (as: ReadonlyArray) => Reader> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => Reader ): ((as: ReadonlyArray) => Reader>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Reader> = /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 1996893e9..7fd236fc4 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -976,37 +976,22 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderEither ) => (as: ReadonlyArray) => ReaderEither> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderEither ): ((as: ReadonlyArray) => ReaderEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -1014,6 +999,12 @@ export const sequenceArray: ( /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 21a54dd88..a0692ecc7 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -644,57 +644,36 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderTask ) => (as: ReadonlyArray) => ReaderTask> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderTask ): ((as: ReadonlyArray) => ReaderTask>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => ReaderTask> = /*#__PURE__*/ traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => ReaderTask ) => (as: ReadonlyArray) => ReaderTask> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseSeqArray = ( f: (a: A) => ReaderTask @@ -710,6 +689,12 @@ export const sequenceSeqArray: (arr: ReadonlyArray>) => R /*#__PURE__*/ traverseSeqArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 6490b3a2c..38435b449 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1412,27 +1412,15 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => ReaderTaskEither ) => (as: ReadonlyArray) => ReaderTaskEither> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => ReaderTaskEither @@ -1440,10 +1428,7 @@ export const traverseArray = ( traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -1452,20 +1437,14 @@ export const sequenceArray: ( traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => ReaderTaskEither ) => (as: ReadonlyArray) => ReaderTaskEither> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArray = ( f: (a: A) => ReaderTaskEither @@ -1473,10 +1452,7 @@ export const traverseSeqArray = ( traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceSeqArray: ( arr: ReadonlyArray> @@ -1484,6 +1460,12 @@ export const sequenceSeqArray: ( /*#__PURE__*/ traverseSeqArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/State.ts b/src/State.ts index 31423e596..f3477a4ae 100644 --- a/src/State.ts +++ b/src/State.ts @@ -348,42 +348,33 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : of(_.emptyReadonlyArray)) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => State ) => (as: ReadonlyArray) => State> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => State ): ((as: ReadonlyArray) => State>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => State> = /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use [`evaluate`](#evaluate) instead * diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 384e56ce8..fdf67115c 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -1177,27 +1177,15 @@ export const traverseReadonlyArrayWithIndex = ( return (as) => (_.isNonEmpty(as) ? g(as) : of(_.emptyReadonlyArray)) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => StateReaderTaskEither ) => (as: ReadonlyArray) => StateReaderTaskEither> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => StateReaderTaskEither @@ -1205,10 +1193,7 @@ export const traverseArray = ( traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: ( arr: ReadonlyArray> @@ -1216,6 +1201,12 @@ export const sequenceArray: ( /*#__PURE__*/ traverseArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/Task.ts b/src/Task.ts index 29535a79d..3efe7244a 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -523,70 +523,52 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => Task ) => (as: ReadonlyArray) => Task> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = (f: (a: A) => Task): ((as: ReadonlyArray) => Task>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => Task> = /*#__PURE__*/ traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => Task ) => (as: ReadonlyArray) => Task> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArray = (f: (a: A) => Task): ((as: ReadonlyArray) => Task>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => Task> = /*#__PURE__*/ traverseSeqArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/TaskEither.ts b/src/TaskEither.ts index afbc9c3da..b59c87426 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -1262,72 +1262,54 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => TaskEither ) => (as: ReadonlyArray) => TaskEither> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseArray = ( f: (a: A) => TaskEither ): ((as: ReadonlyArray) => TaskEither>) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceArray: (arr: ReadonlyArray>) => TaskEither> = /*#__PURE__*/ traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => TaskEither ) => (as: ReadonlyArray) => TaskEither> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const traverseSeqArray = ( f: (a: A) => TaskEither ): ((as: ReadonlyArray) => TaskEither>) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.9.0 - * @deprecated */ export const sequenceSeqArray: (arr: ReadonlyArray>) => TaskEither> = /*#__PURE__*/ traverseSeqArray(identity) +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation + /** * Use small, specific instances instead. * diff --git a/src/TaskOption.ts b/src/TaskOption.ts index c072af3f6..0d5eb0fcb 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -848,68 +848,50 @@ export const traverseReadonlyArrayWithIndexSeq = ( return (as) => (_.isNonEmpty(as) ? g(as) : ApT) } -// ------------------------------------------------------------------------------------- -// deprecated -// ------------------------------------------------------------------------------------- - -// tslint:disable: deprecation - /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseArrayWithIndex: ( f: (index: number, a: A) => TaskOption ) => (as: ReadonlyArray) => TaskOption> = traverseReadonlyArrayWithIndex /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseArray: ( f: (a: A) => TaskOption ) => (as: ReadonlyArray) => TaskOption> = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndex` instead. - * * @since 2.10.0 - * @deprecated */ export const sequenceArray: (as: ReadonlyArray>) => TaskOption> = /*#__PURE__*/ traverseArray(identity) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseSeqArrayWithIndex: ( f: (index: number, a: A) => TaskOption ) => (as: ReadonlyArray) => TaskOption> = traverseReadonlyArrayWithIndexSeq /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.10.0 - * @deprecated */ export const traverseSeqArray: ( f: (a: A) => TaskOption ) => (as: ReadonlyArray) => TaskOption> = (f) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a)) /** - * Use `traverseReadonlyArrayWithIndexSeq` instead. - * * @since 2.10.0 - * @deprecated */ export const sequenceSeqArray: (as: ReadonlyArray>) => TaskOption> = /*#__PURE__*/ traverseSeqArray(identity) + +// ------------------------------------------------------------------------------------- +// deprecated +// ------------------------------------------------------------------------------------- + +// tslint:disable: deprecation From 4567fa685e341afb990aecd95ccd85a08356a27e Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 12 May 2021 17:55:16 +0200 Subject: [PATCH 148/162] release as rc --- CHANGELOG.md | 2 +- scripts/release.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52ec50271..f6094dd29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. -# 2.11 +# 2.11.0-rc.1 - **Deprecation** - `Array` diff --git a/scripts/release.ts b/scripts/release.ts index 0554df72e..c9a177905 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -16,7 +16,7 @@ const exec = (cmd: string, args?: child_process.ExecOptions): TE.TaskEither Date: Wed, 12 May 2021 18:03:06 +0200 Subject: [PATCH 149/162] Option: remove useless type parameter in getLeft, getRight --- docs/modules/Option.ts.md | 4 ++-- src/Option.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index f371bebe1..b660a570b 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -279,7 +279,7 @@ Returns the `Left` value of an `Either` if possible. **Signature** ```ts -export declare const getLeft: (ma: Either) => Option +export declare const getLeft: (ma: Either) => Option ``` **Example** @@ -301,7 +301,7 @@ Returns the `Right` value of an `Either` if possible. **Signature** ```ts -export declare const getRight: (ma: Either) => Option +export declare const getRight: (ma: Either) => Option ``` **Example** diff --git a/src/Option.ts b/src/Option.ts index b4448596e..a18e60441 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -130,7 +130,7 @@ export function fromPredicate(predicate: Predicate): (b: B) = * @category constructors * @since 2.0.0 */ -export const getLeft = (ma: Either): Option => (ma._tag === 'Right' ? none : some(ma.left)) +export const getLeft = (ma: Either): Option => (ma._tag === 'Right' ? none : some(ma.left)) /** * Returns the `Right` value of an `Either` if possible. @@ -145,7 +145,7 @@ export const getLeft = (ma: Either): Option => (ma._tag === 'Righ * @category constructors * @since 2.0.0 */ -export const getRight = (ma: Either): Option => (ma._tag === 'Left' ? none : some(ma.right)) +export const getRight = (ma: Either): Option => (ma._tag === 'Left' ? none : some(ma.right)) // ------------------------------------------------------------------------------------- // non-pipeables From 2554551dd52f9c31abc05163aeb39378b306b398 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 13 May 2021 18:41:07 +0200 Subject: [PATCH 150/162] - Either: revert `exists` signature change - ReadonlyArray: add overload to `filter` (1484) --- docs/modules/Either.ts.md | 2 +- docs/modules/ReadonlyArray.ts.md | 1 + dtslint/ts3.5/ReadonlyArray.ts | 29 +++++++++++++++++++++++++++++ src/Either.ts | 2 +- src/ReadonlyArray.ts | 1 + 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 9a677e36f..9d4151ee7 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1530,7 +1530,7 @@ Returns `false` if `Left` or returns the result of the application of the given **Signature** ```ts -export declare const exists: (predicate: Predicate) => (ma: Either) => boolean +export declare const exists: (predicate: Predicate) => (ma: Either) => boolean ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 469eb864a..085d66977 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -313,6 +313,7 @@ Added in v2.5.0 export declare const filter: { (refinement: Refinement): (fa: readonly A[]) => readonly B[] (predicate: Predicate): (fb: readonly B[]) => readonly B[] + (predicate: Predicate): (fb: readonly A[]) => readonly A[] } ``` diff --git a/dtslint/ts3.5/ReadonlyArray.ts b/dtslint/ts3.5/ReadonlyArray.ts index a70b2ac50..45e609ba0 100644 --- a/dtslint/ts3.5/ReadonlyArray.ts +++ b/dtslint/ts3.5/ReadonlyArray.ts @@ -182,6 +182,35 @@ pipe( ) ) +// #1484 +const isPositive = E.exists((n: number) => n > 0) +declare const eithers: ReadonlyArray> +pipe(eithers, _.filter(E.isRight), _.filter(isPositive)) + +interface Registered { + readonly type: 'Registered' + readonly username: string +} +interface Unregistered { + readonly type: 'Unregistered' +} + +type User = Registered | Unregistered + +declare const users: ReadonlyArray +declare const isRegistered: (u: User) => u is Registered +declare const p: (u: User) => boolean + +const registereds = _.filter(isRegistered)(users) +_.filter(p)(registereds) // $ExpectType readonly Registered[] + +interface Test { + test: string +} +declare const arrayOfTest: Test[] +const isFoo = (t: T) => t.test === 'foo' +pipe(arrayOfTest, _.filter(isFoo)) // $ExpectType readonly Test[] + // // filterWithIndex // diff --git a/src/Either.ts b/src/Either.ts index 411b45b02..368624518 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1228,7 +1228,7 @@ export const elem = (E: Eq) => (a: A, ma: Either): boolean => * * @since 2.0.0 */ -export const exists = (predicate: Predicate) => (ma: Either): boolean => +export const exists = (predicate: Predicate) => (ma: Either): boolean => isLeft(ma) ? false : predicate(ma.right) // ------------------------------------------------------------------------------------- diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index af98b9e9a..cd46f87e5 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -1531,6 +1531,7 @@ export const separate = (fa: ReadonlyArray>): Separated(refinement: Refinement): (fa: ReadonlyArray) => ReadonlyArray (predicate: Predicate): (fb: ReadonlyArray) => ReadonlyArray + (predicate: Predicate): (fb: ReadonlyArray) => ReadonlyArray } = (predicate: Predicate) => (fb: ReadonlyArray) => fb.filter(predicate) /** From 4109a8e68a4ec8b4369d3b91d3ce1dae7288f966 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 07:05:03 +0200 Subject: [PATCH 151/162] - `Array` / `ReadonlyArray` - revert `isOutOfBound` signature change - revert `isEmpty` signature change - revert `size` signature change - `Either` - revert `exists` signature change - revert `elem` signature change - `These` - revert `exists` signature change - revert `elem` signature change - `NonEmptyArray` / `ReadonlyNonEmptyArray` - revert `isOutOfBound` signature change - `Set` / `ReadonlySet` - revert `isEmpty` signature change - revert `size` signature change - `Map` / `ReadonlyMap` - revert `isEmpty` signature change - revert `size` signature change --- CHANGELOG.md | 21 +++++++++++++++++++++ docs/modules/Array.ts.md | 6 +++--- docs/modules/Either.ts.md | 2 +- docs/modules/Map.ts.md | 4 ++-- docs/modules/Option.ts.md | 2 +- docs/modules/ReadonlyArray.ts.md | 6 +++--- docs/modules/ReadonlyMap.ts.md | 4 ++-- docs/modules/ReadonlyRecord.ts.md | 4 ++-- docs/modules/ReadonlySet.ts.md | 4 ++-- docs/modules/Record.ts.md | 4 ++-- docs/modules/Set.ts.md | 4 ++-- docs/modules/These.ts.md | 4 ++-- dtslint/ts3.5/Array.ts | 10 ++++++++++ dtslint/ts3.5/Either.ts | 9 ++++++++- dtslint/ts3.5/Map.ts | 11 +++++++++++ dtslint/ts3.5/ReadonlyArray.ts | 10 ++++++++++ dtslint/ts3.5/ReadonlyMap.ts | 11 +++++++++++ dtslint/ts3.5/ReadonlySet.ts | 11 +++++++++++ dtslint/ts3.5/Set.ts | 11 +++++++++++ package.json | 2 +- src/Array.ts | 6 +++--- src/Either.ts | 2 +- src/Map.ts | 4 ++-- src/NonEmptyArray.ts | 2 +- src/Option.ts | 5 ++--- src/ReadonlyArray.ts | 6 +++--- src/ReadonlyMap.ts | 4 ++-- src/ReadonlyNonEmptyArray.ts | 2 +- src/ReadonlyRecord.ts | 4 ++-- src/ReadonlySet.ts | 4 ++-- src/Record.ts | 4 ++-- src/Set.ts | 4 ++-- src/These.ts | 4 ++-- 33 files changed, 141 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6094dd29..5ac511361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,27 @@ **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. +# 2.11.0-rc.2 + +- `Array` / `ReadonlyArray` + - revert `isOutOfBound` signature change + - revert `isEmpty` signature change + - revert `size` signature change +- `Either` + - revert `exists` signature change + - revert `elem` signature change +- `These` + - revert `exists` signature change + - revert `elem` signature change +- `NonEmptyArray` / `ReadonlyNonEmptyArray` + - revert `isOutOfBound` signature change +- `Set` / `ReadonlySet` + - revert `isEmpty` signature change + - revert `size` signature change +- `Map` / `ReadonlyMap` + - revert `isEmpty` signature change + - revert `size` signature change + # 2.11.0-rc.1 - **Deprecation** diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 7e1412e0c..cf30d0f53 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -2369,7 +2369,7 @@ Test whether an array is empty **Signature** ```ts -export declare const isEmpty: (as: Array) => as is [] +export declare const isEmpty: (as: A[]) => as is [] ``` **Example** @@ -2650,7 +2650,7 @@ Test whether an array contains a particular index **Signature** ```ts -export declare const isOutOfBound: (i: number, as: Array) => boolean +export declare const isOutOfBound: (i: number, as: A[]) => boolean ``` Added in v2.0.0 @@ -2709,7 +2709,7 @@ Calculate the number of elements in a `Array`. **Signature** ```ts -export declare const size: (as: Array) => number +export declare const size: (as: A[]) => number ``` Added in v2.10.0 diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 9d4151ee7..dbe0ce816 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1518,7 +1518,7 @@ Added in v2.8.0 **Signature** ```ts -export declare const elem: (E: Eq) => (a: A, ma: Either) => boolean +export declare const elem: (E: Eq) => (a: A, ma: Either) => boolean ``` Added in v2.0.0 diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index ce1958093..efe711d00 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -573,7 +573,7 @@ Test whether or not a map is empty **Signature** ```ts -export declare const isEmpty: (m: Map) => boolean +export declare const isEmpty: (m: Map) => boolean ``` Added in v2.0.0 @@ -732,7 +732,7 @@ Calculate the number of key/value pairs in a map **Signature** ```ts -export declare const size: (m: Map) => number +export declare const size: (m: Map) => number ``` Added in v2.0.0 diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index b660a570b..522c8e7b7 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1532,7 +1532,7 @@ Returns `true` if the predicate is satisfied by the wrapped value **Signature** ```ts -export declare function exists(predicate: Predicate): (ma: Option) => boolean +export declare const exists: (predicate: Predicate) => (ma: Option) => boolean ``` **Example** diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 085d66977..67118b5dc 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -2210,7 +2210,7 @@ Test whether a `ReadonlyArray` is empty. **Signature** ```ts -export declare const isEmpty: (as: ReadonlyArray) => as is readonly [] +export declare const isEmpty: (as: readonly A[]) => as is readonly [] ``` **Example** @@ -2708,7 +2708,7 @@ Test whether an array contains a particular index **Signature** ```ts -export declare const isOutOfBound: (i: number, as: ReadonlyArray) => boolean +export declare const isOutOfBound: (i: number, as: readonly A[]) => boolean ``` Added in v2.5.0 @@ -2790,7 +2790,7 @@ Calculate the number of elements in a `ReadonlyArray`. **Signature** ```ts -export declare const size: (as: ReadonlyArray) => number +export declare const size: (as: readonly A[]) => number ``` Added in v2.10.0 diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index 84f9a6ee5..7f2166132 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -689,7 +689,7 @@ Test whether or not a map is empty **Signature** ```ts -export declare const isEmpty: (m: ReadonlyMap) => boolean +export declare const isEmpty: (m: ReadonlyMap) => boolean ``` Added in v2.5.0 @@ -851,7 +851,7 @@ Calculate the number of key/value pairs in a map **Signature** ```ts -export declare const size: (m: ReadonlyMap) => number +export declare const size: (m: ReadonlyMap) => number ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index c8e87aab0..6eaa3b2b3 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -932,7 +932,7 @@ Test whether a `ReadonlyRecord` is empty. **Signature** ```ts -export declare const isEmpty: (r: ReadonlyRecord) => boolean +export declare const isEmpty: (r: Readonly>) => boolean ``` Added in v2.5.0 @@ -1099,7 +1099,7 @@ Calculate the number of key/value pairs in a `ReadonlyRecord`, **Signature** ```ts -export declare const size: (r: ReadonlyRecord) => number +export declare const size: (r: Readonly>) => number ``` Added in v2.5.0 diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index cac462735..1b7ca83a7 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -413,7 +413,7 @@ Test whether a `ReadonlySet` is empty. **Signature** ```ts -export declare const isEmpty: (set: ReadonlySet) => boolean +export declare const isEmpty: (set: ReadonlySet) => boolean ``` Added in v2.10.0 @@ -503,7 +503,7 @@ Calculate the number of elements in a `ReadonlySet`. **Signature** ```ts -export declare const size: (set: ReadonlySet) => number +export declare const size: (set: ReadonlySet) => number ``` Added in v2.10.0 diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 79216bf89..38d4248f6 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -793,7 +793,7 @@ Test whether a `Record` is empty. **Signature** ```ts -export declare const isEmpty: (r: Record) => boolean +export declare const isEmpty: (r: Record) => boolean ``` Added in v2.0.0 @@ -996,7 +996,7 @@ Calculate the number of key/value pairs in a `Record`. **Signature** ```ts -export declare const size: (r: Record) => number +export declare const size: (r: Record) => number ``` Added in v2.0.0 diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index 15bae8116..c525e5342 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -349,7 +349,7 @@ Test whether a `Set` is empty. **Signature** ```ts -export declare const isEmpty: (set: Set) => boolean +export declare const isEmpty: (set: Set) => boolean ``` Added in v2.10.0 @@ -427,7 +427,7 @@ Calculate the number of elements in a `Set`. **Signature** ```ts -export declare const size: (set: Set) => number +export declare const size: (set: Set) => number ``` Added in v2.10.0 diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 5ad76e4e6..7c8773eb1 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -739,7 +739,7 @@ Added in v2.11.0 **Signature** ```ts -export declare const elem: (E: Eq) => (a: A) => (ma: These) => boolean +export declare const elem: (E: Eq) => (a: A) => (ma: These) => boolean ``` Added in v2.11.0 @@ -749,7 +749,7 @@ Added in v2.11.0 **Signature** ```ts -export declare const exists: (predicate: Predicate) => (ma: These) => boolean +export declare const exists: (predicate: Predicate) => (ma: These) => boolean ``` Added in v2.11.0 diff --git a/dtslint/ts3.5/Array.ts b/dtslint/ts3.5/Array.ts index 377c083fc..5187b755f 100644 --- a/dtslint/ts3.5/Array.ts +++ b/dtslint/ts3.5/Array.ts @@ -286,3 +286,13 @@ pipe(prns, _.findFirst(predicate)) pipe(prsns, _.findLast(isString)) // $ExpectType Option pipe(prns, _.findLast(predicate)) + +// +// isEmpty +// + +// $ExpectType Either +pipe( + ss, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/dtslint/ts3.5/Either.ts b/dtslint/ts3.5/Either.ts index 809dbf18d..c3030a634 100644 --- a/dtslint/ts3.5/Either.ts +++ b/dtslint/ts3.5/Either.ts @@ -1,6 +1,6 @@ import * as _ from '../../src/Either' import { pipe, flow, identity } from '../../src/function' -import * as B from '../../src/boolean' +import * as RA from '../../src/ReadonlyArray' // // getOrElseW @@ -147,3 +147,10 @@ pipe( () => false ) ) + +// +// exists +// + +declare const es: _.Either[] +const x = pipe(es, RA.filter(_.exists((n) => n > 0))) diff --git a/dtslint/ts3.5/Map.ts b/dtslint/ts3.5/Map.ts index 1730d11c2..6df164788 100644 --- a/dtslint/ts3.5/Map.ts +++ b/dtslint/ts3.5/Map.ts @@ -2,6 +2,7 @@ import * as _ from '../../src/Map' import * as S from '../../src/string' import * as N from '../../src/number' import { pipe } from '../../src/function' +import * as E from '../../src/Either' // // member @@ -128,3 +129,13 @@ pipe( ) => true ) ) + +// +// isEmpty +// + +// $ExpectType Either, Map> +pipe( + prns, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/dtslint/ts3.5/ReadonlyArray.ts b/dtslint/ts3.5/ReadonlyArray.ts index 45e609ba0..84c57d6d2 100644 --- a/dtslint/ts3.5/ReadonlyArray.ts +++ b/dtslint/ts3.5/ReadonlyArray.ts @@ -315,3 +315,13 @@ pipe(prns, _.findFirst(predicate)) pipe(prsns, _.findLast(isString)) // $ExpectType Option pipe(prns, _.findLast(predicate)) + +// +// isEmpty +// + +// $ExpectType Either +pipe( + rss, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/dtslint/ts3.5/ReadonlyMap.ts b/dtslint/ts3.5/ReadonlyMap.ts index e83b41314..44aec6d93 100644 --- a/dtslint/ts3.5/ReadonlyMap.ts +++ b/dtslint/ts3.5/ReadonlyMap.ts @@ -2,6 +2,7 @@ import * as _ from '../../src/ReadonlyMap' import * as N from '../../src/number' import * as S from '../../src/string' import { pipe } from '../../src/function' +import * as E from '../../src/Either' // // member @@ -128,3 +129,13 @@ pipe( ) => true ) ) + +// +// isEmpty +// + +// $ExpectType Either, ReadonlyMap> +pipe( + prns, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/dtslint/ts3.5/ReadonlySet.ts b/dtslint/ts3.5/ReadonlySet.ts index a6c380674..17b3e348b 100644 --- a/dtslint/ts3.5/ReadonlySet.ts +++ b/dtslint/ts3.5/ReadonlySet.ts @@ -1,6 +1,7 @@ import * as _ from '../../src/ReadonlySet' import * as N from '../../src/number' import { pipe } from '../../src/function' +import * as E from '../../src/Either' declare const me: ReadonlySet @@ -86,3 +87,13 @@ pipe( ) => true ) ) + +// +// isEmpty +// + +// $ExpectType Either, ReadonlySet> +pipe( + me, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/dtslint/ts3.5/Set.ts b/dtslint/ts3.5/Set.ts index 9c94bf449..b0a1ca574 100644 --- a/dtslint/ts3.5/Set.ts +++ b/dtslint/ts3.5/Set.ts @@ -1,6 +1,7 @@ import * as _ from '../../src/Set' import * as N from '../../src/number' import { pipe } from '../../src/function' +import * as E from '../../src/Either' declare const me: Set @@ -86,3 +87,13 @@ pipe( ) => true ) ) + +// +// isEmpty +// + +// $ExpectType Either, Set> +pipe( + me, + E.fromPredicate(_.isEmpty, (as) => as) +) diff --git a/package.json b/package.json index 6a4f97c56..9bb10b471 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fp-ts", - "version": "2.11.0-rc.1", + "version": "2.11.0-rc.2", "description": "Functional programming in TypeScript", "main": "lib/index.js", "module": "es6/index.js", diff --git a/src/Array.ts b/src/Array.ts index 963d18ba1..f5f6b9c2a 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -65,7 +65,7 @@ import NonEmptyArray = NEA.NonEmptyArray * @category refinements * @since 2.0.0 */ -export const isEmpty = (as: Array): as is [] => as.length === 0 +export const isEmpty = (as: Array): as is [] => as.length === 0 /** * Test whether an array is non empty narrowing down the type to `NonEmptyArray` @@ -334,14 +334,14 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: Array): N * * @since 2.10.0 */ -export const size = (as: Array): number => as.length +export const size = (as: Array): number => as.length /** * Test whether an array contains a particular index * * @since 2.0.0 */ -export const isOutOfBound: (i: number, as: Array) => boolean = NEA.isOutOfBound +export const isOutOfBound: (i: number, as: Array) => boolean = NEA.isOutOfBound // TODO: remove non-curried overloading in v3 /** diff --git a/src/Either.ts b/src/Either.ts index 368624518..193220ddf 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1211,7 +1211,7 @@ export function toError(e: unknown): Error { /** * @since 2.0.0 */ -export const elem = (E: Eq) => (a: A, ma: Either): boolean => +export const elem = (E: Eq) => (a: A, ma: Either): boolean => isLeft(ma) ? false : E.equals(a, ma.right) /** diff --git a/src/Map.ts b/src/Map.ts index c0cb386f0..f1355191a 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -40,14 +40,14 @@ export const getShow: (SK: Show, SA: Show) => Show> = RM.g * * @since 2.0.0 */ -export const size: (m: Map) => number = RM.size +export const size: (m: Map) => number = RM.size /** * Test whether or not a map is empty * * @since 2.0.0 */ -export const isEmpty: (m: Map) => boolean = RM.isEmpty +export const isEmpty: (m: Map) => boolean = RM.isEmpty // TODO: remove non-curried overloading in v3 /** diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index b97c670c6..f29488c36 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -67,7 +67,7 @@ export const isNonEmpty = (as: Array): as is NonEmptyArray => as.length /** * @internal */ -export const isOutOfBound = (i: number, as: Array): boolean => i < 0 || i >= as.length +export const isOutOfBound = (i: number, as: Array): boolean => i < 0 || i >= as.length /** * @internal diff --git a/src/Option.ts b/src/Option.ts index a18e60441..232a93953 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -1155,9 +1155,8 @@ export function elem(E: Eq): (a: A, ma: Option) => boolean { * * @since 2.0.0 */ -export function exists(predicate: Predicate): (ma: Option) => boolean { - return (ma) => (isNone(ma) ? false : predicate(ma.value)) -} +export const exists = (predicate: Predicate) => (ma: Option): boolean => + isNone(ma) ? false : predicate(ma.value) // ------------------------------------------------------------------------------------- // do notation diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index cd46f87e5..9e7b256d8 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -66,7 +66,7 @@ import ReadonlyNonEmptyArray = RNEA.ReadonlyNonEmptyArray * @category refinements * @since 2.5.0 */ -export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 +export const isEmpty = (as: ReadonlyArray): as is readonly [] => as.length === 0 /** * Test whether a `ReadonlyArray` is non empty. @@ -344,14 +344,14 @@ export const scanRight = (b: B, f: (a: A, b: B) => B) => (as: ReadonlyArra * * @since 2.10.0 */ -export const size = (as: ReadonlyArray): number => as.length +export const size = (as: ReadonlyArray): number => as.length /** * Test whether an array contains a particular index * * @since 2.5.0 */ -export const isOutOfBound: (i: number, as: ReadonlyArray) => boolean = RNEA.isOutOfBound +export const isOutOfBound: (i: number, as: ReadonlyArray) => boolean = RNEA.isOutOfBound // TODO: remove non-curried overloading in v3 /** diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 5735e35c5..13e0b712f 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -69,14 +69,14 @@ export function getShow(SK: Show, SA: Show): Show> * * @since 2.5.0 */ -export const size = (m: ReadonlyMap): number => m.size +export const size = (m: ReadonlyMap): number => m.size /** * Test whether or not a map is empty * * @since 2.5.0 */ -export const isEmpty = (m: ReadonlyMap): boolean => m.size === 0 +export const isEmpty = (m: ReadonlyMap): boolean => m.size === 0 // TODO: remove non-curried overloading in v3 /** diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index be802b637..939a86bfc 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -71,7 +71,7 @@ export const isNonEmpty: (as: ReadonlyArray) => as is ReadonlyNonEmptyArra /** * @internal */ -export const isOutOfBound = (i: number, as: ReadonlyArray): boolean => i < 0 || i >= as.length +export const isOutOfBound = (i: number, as: ReadonlyArray): boolean => i < 0 || i >= as.length /** * @internal diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 9ac409da0..5186938e6 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -60,14 +60,14 @@ export const toRecord = (r: ReadonlyRecord): Record): number => Object.keys(r).length +export const size = (r: ReadonlyRecord): number => Object.keys(r).length /** * Test whether a `ReadonlyRecord` is empty. * * @since 2.5.0 */ -export const isEmpty = (r: ReadonlyRecord): boolean => { +export const isEmpty = (r: ReadonlyRecord): boolean => { for (const k in r) { if (_.has.call(r, k)) { return false diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 9aa3a8af3..9dde8db64 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -438,14 +438,14 @@ export const empty: ReadonlySet = new Set() * * @since 2.10.0 */ -export const isEmpty = (set: ReadonlySet): boolean => set.size === 0 +export const isEmpty = (set: ReadonlySet): boolean => set.size === 0 /** * Calculate the number of elements in a `ReadonlySet`. * * @since 2.10.0 */ -export const size = (set: ReadonlySet): number => set.size +export const size = (set: ReadonlySet): number => set.size /** * @since 2.5.0 diff --git a/src/Record.ts b/src/Record.ts index 85c9759d2..7093f0c04 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -39,14 +39,14 @@ import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault * * @since 2.0.0 */ -export const size: (r: Record) => number = RR.size +export const size: (r: Record) => number = RR.size /** * Test whether a `Record` is empty. * * @since 2.0.0 */ -export const isEmpty: (r: Record) => boolean = RR.isEmpty +export const isEmpty: (r: Record) => boolean = RR.isEmpty const keys_ = (O: Ord) => (r: Record): Array => (Object.keys(r) as any).sort(O.compare) diff --git a/src/Set.ts b/src/Set.ts index e618a6e72..0a8977003 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -436,14 +436,14 @@ export const empty: Set = new Set() * * @since 2.10.0 */ -export const isEmpty = (set: Set): boolean => set.size === 0 +export const isEmpty = (set: Set): boolean => set.size === 0 /** * Calculate the number of elements in a `Set`. * * @since 2.10.0 */ -export const size = (set: Set): number => set.size +export const size = (set: Set): number => set.size /** * @since 2.0.0 diff --git a/src/These.ts b/src/These.ts index a7e62dc1f..57cfe246e 100644 --- a/src/These.ts +++ b/src/These.ts @@ -675,13 +675,13 @@ export const fromOptionK = /** * @since 2.11.0 */ -export const elem = (E: Eq) => (a: A) => (ma: These): boolean => +export const elem = (E: Eq) => (a: A) => (ma: These): boolean => isLeft(ma) ? false : E.equals(a, ma.right) /** * @since 2.11.0 */ -export const exists = (predicate: Predicate) => (ma: These): boolean => +export const exists = (predicate: Predicate) => (ma: These): boolean => isLeft(ma) ? false : predicate(ma.right) /** From cbeb38bfc937221f19217fe200e4d7f68a0c4c66 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 08:00:11 +0200 Subject: [PATCH 152/162] Option: revert getLeft / getRight signature change --- docs/modules/Option.ts.md | 4 ++-- src/Option.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 522c8e7b7..d47ed289c 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -279,7 +279,7 @@ Returns the `Left` value of an `Either` if possible. **Signature** ```ts -export declare const getLeft: (ma: Either) => Option +export declare const getLeft: (ma: Either) => Option ``` **Example** @@ -301,7 +301,7 @@ Returns the `Right` value of an `Either` if possible. **Signature** ```ts -export declare const getRight: (ma: Either) => Option +export declare const getRight: (ma: Either) => Option ``` **Example** diff --git a/src/Option.ts b/src/Option.ts index 232a93953..9a6cf52f3 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -130,7 +130,7 @@ export function fromPredicate(predicate: Predicate): (b: B) = * @category constructors * @since 2.0.0 */ -export const getLeft = (ma: Either): Option => (ma._tag === 'Right' ? none : some(ma.left)) +export const getLeft = (ma: Either): Option => (ma._tag === 'Right' ? none : some(ma.left)) /** * Returns the `Right` value of an `Either` if possible. @@ -145,7 +145,7 @@ export const getLeft = (ma: Either): Option => (ma._tag === 'R * @category constructors * @since 2.0.0 */ -export const getRight = (ma: Either): Option => (ma._tag === 'Left' ? none : some(ma.right)) +export const getRight = (ma: Either): Option => (ma._tag === 'Left' ? none : some(ma.right)) // ------------------------------------------------------------------------------------- // non-pipeables From 5e5fa6690415ff071a6d30f5c3b2b9c917e90ee6 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 10:07:49 +0200 Subject: [PATCH 153/162] fix signatures involving a predicate --- CHANGELOG.md | 2 + docs/modules/Array.ts.md | 28 ++++-- docs/modules/Either.ts.md | 3 + docs/modules/Filterable.ts.md | 12 +++ docs/modules/FromEither.ts.md | 12 +++ docs/modules/IOEither.ts.md | 3 + docs/modules/Map.ts.md | 8 +- docs/modules/NonEmptyArray.ts.md | 3 + docs/modules/Option.ts.md | 3 + docs/modules/OptionT.ts.md | 7 ++ docs/modules/ReaderEither.ts.md | 5 ++ docs/modules/ReaderTaskEither.ts.md | 5 ++ docs/modules/ReadonlyArray.ts.md | 27 ++++-- docs/modules/ReadonlyMap.ts.md | 8 ++ docs/modules/ReadonlyNonEmptyArray.ts.md | 3 + docs/modules/ReadonlyRecord.ts.md | 10 +++ docs/modules/ReadonlySet.ts.md | 4 + docs/modules/Record.ts.md | 8 ++ docs/modules/Set.ts.md | 2 + docs/modules/StateReaderTaskEither.ts.md | 7 ++ docs/modules/TaskEither.ts.md | 3 + docs/modules/TaskOption.ts.md | 3 + docs/modules/TaskThese.ts.md | 1 + src/Array.ts | 74 ++++++++------- src/Either.ts | 1 + src/Filterable.ts | 20 +++-- src/FromEither.ts | 22 ++++- src/IOEither.ts | 3 + src/Map.ts | 44 +++++---- src/NonEmptyArray.ts | 1 + src/Option.ts | 11 +-- src/OptionT.ts | 10 ++- src/ReaderEither.ts | 5 ++ src/ReaderTaskEither.ts | 5 ++ src/ReadonlyArray.ts | 109 ++++++++++++----------- src/ReadonlyMap.ts | 44 +++++---- src/ReadonlyNonEmptyArray.ts | 3 + src/ReadonlyRecord.ts | 18 +++- src/ReadonlySet.ts | 36 ++++---- src/Record.ts | 8 ++ src/Set.ts | 34 +++---- src/StateReaderTaskEither.ts | 7 ++ src/TaskEither.ts | 3 + src/TaskOption.ts | 3 + test/NonEmptyArray.ts | 7 +- 45 files changed, 442 insertions(+), 193 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ac511361..9fc5a91f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ high state of flux, you're at risk of it changing without notice. # 2.11.0-rc.2 +Some signature changes in `2.11.0-rc.1` are causing type inference issues: + - `Array` / `ReadonlyArray` - revert `isOutOfBound` signature change - revert `isEmpty` signature change diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index cf30d0f53..acab64c0d 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -296,7 +296,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const extend: (f: (fa: A[]) => B) => (wa: A[]) => B[] +export declare const extend: (f: (as: A[]) => B) => (as: A[]) => B[] ``` Added in v2.0.0 @@ -309,8 +309,9 @@ Added in v2.0.0 ```ts export declare const filter: { - (refinement: Refinement): (fa: A[]) => B[] - (predicate: Predicate): (fb: B[]) => B[] + (refinement: Refinement): (as: A[]) => B[] + (predicate: Predicate): (bs: B[]) => B[] + (predicate: Predicate): (as: A[]) => A[] } ``` @@ -332,8 +333,9 @@ Added in v2.0.0 ```ts export declare const partition: { - (refinement: Refinement): (fa: A[]) => Separated - (predicate: Predicate): (fb: B[]) => Separated + (refinement: Refinement): (as: A[]) => Separated + (predicate: Predicate): (bs: B[]) => Separated + (predicate: Predicate): (as: A[]) => Separated } ``` @@ -367,8 +369,9 @@ Added in v2.0.0 ```ts export declare const filterWithIndex: { - (refinementWithIndex: RefinementWithIndex): (fa: A[]) => B[] - (predicateWithIndex: PredicateWithIndex): (fb: B[]) => B[] + (refinementWithIndex: RefinementWithIndex): (as: A[]) => B[] + (predicateWithIndex: PredicateWithIndex): (bs: B[]) => B[] + (predicateWithIndex: PredicateWithIndex): (as: A[]) => A[] } ``` @@ -392,8 +395,9 @@ Added in v2.0.0 ```ts export declare const partitionWithIndex: { - (refinementWithIndex: RefinementWithIndex): (fa: A[]) => Separated - (predicateWithIndex: PredicateWithIndex): (fb: B[]) => Separated + (refinementWithIndex: RefinementWithIndex): (as: A[]) => Separated + (predicateWithIndex: PredicateWithIndex): (bs: B[]) => Separated + (predicateWithIndex: PredicateWithIndex): (as: A[]) => Separated } ``` @@ -857,6 +861,7 @@ Remove the longest initial subarray for which all element satisfy the specified ```ts export declare function dropLeftWhile(refinement: Refinement): (as: Array) => Array export declare function dropLeftWhile(predicate: Predicate): (bs: Array) => Array +export declare function dropLeftWhile(predicate: Predicate): (as: Array) => Array ``` **Example** @@ -1263,6 +1268,7 @@ Calculate the longest initial subarray for which all element satisfy the specifi ```ts export declare function takeLeftWhile(refinement: Refinement): (as: Array) => Array export declare function takeLeftWhile(predicate: Predicate): (bs: Array) => Array +export declare function takeLeftWhile(predicate: Predicate): (as: Array) => Array ``` **Example** @@ -1449,6 +1455,7 @@ Added in v2.11.0 ```ts export declare function fromPredicate(refinement: Refinement): (a: A) => Array +export declare function fromPredicate(predicate: Predicate): (b: B) => Array export declare function fromPredicate(predicate: Predicate): (a: A) => Array ``` @@ -1589,6 +1596,7 @@ Find the first element which satisfies a predicate (or a refinement) function ```ts export declare function findFirst(refinement: Refinement): (as: Array) => Option export declare function findFirst(predicate: Predicate): (bs: Array) => Option +export declare function findFirst(predicate: Predicate): (as: Array) => Option ``` **Example** @@ -1651,6 +1659,7 @@ Find the last element which satisfies a predicate function ```ts export declare function findLast(refinement: Refinement): (as: Array) => Option export declare function findLast(predicate: Predicate): (bs: Array) => Option +export declare function findLast(predicate: Predicate): (as: Array) => Option ``` **Example** @@ -1899,6 +1908,7 @@ Split an array into two parts: ```ts export declare function spanLeft(refinement: Refinement): (as: Array) => Spanned export declare function spanLeft(predicate: Predicate): (bs: Array) => Spanned +export declare function spanLeft(predicate: Predicate): (as: Array) => Spanned ``` **Example** diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index dbe0ce816..57be9c548 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -233,6 +233,7 @@ Added in v2.0.0 export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: Either) => Either (predicate: Predicate, onFalse: (a: A) => E): (mb: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E): (ma: Either) => Either } ``` @@ -288,6 +289,7 @@ export declare const filterOrElseW: { ma: Either ) => Either (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either } ``` @@ -399,6 +401,7 @@ Added in v2.0.0 export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Either (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Either + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Either } ``` diff --git a/docs/modules/Filterable.ts.md b/docs/modules/Filterable.ts.md index 299713f4e..b9e272eb3 100644 --- a/docs/modules/Filterable.ts.md +++ b/docs/modules/Filterable.ts.md @@ -72,6 +72,7 @@ export declare function filter( ): { (refinement: Refinement): (fga: Kind2>) => Kind2> (predicate: Predicate): (fgb: Kind2>) => Kind2> + (predicate: Predicate): (fga: Kind2>) => Kind2> } export declare function filter( F: Functor1, @@ -79,6 +80,7 @@ export declare function filter( ): { (refinement: Refinement): (fga: Kind>) => Kind> (predicate: Predicate): (fgb: Kind>) => Kind> + (predicate: Predicate): (fga: Kind>) => Kind> } export declare function filter( F: Functor1, @@ -86,6 +88,7 @@ export declare function filter( ): { (refinement: Refinement): (fga: Kind>) => Kind> (predicate: Predicate): (fgb: Kind>) => Kind> + (predicate: Predicate): (fga: Kind>) => Kind> } export declare function filter( F: Functor, @@ -93,6 +96,7 @@ export declare function filter( ): { (refinement: Refinement): (fga: HKT>) => HKT> (predicate: Predicate): (fgb: HKT>) => HKT> + (predicate: Predicate): (fga: HKT>) => HKT> } ``` @@ -142,6 +146,9 @@ export declare function partition( (predicate: Predicate): ( fgb: Kind2> ) => Separated>, Kind2>> + (predicate: Predicate): ( + fga: Kind2> + ) => Separated>, Kind2>> } export declare function partition( F: Functor1, @@ -153,6 +160,9 @@ export declare function partition( (predicate: Predicate): ( fgb: Kind> ) => Separated>, Kind>> + (predicate: Predicate): ( + fga: Kind> + ) => Separated>, Kind>> } export declare function partition( F: Functor1, @@ -164,6 +174,7 @@ export declare function partition( (predicate: Predicate): ( fgb: Kind> ) => Separated>, Kind>> + (predicate: Predicate): (fga: Kind>) => Separated>, Kind>> } export declare function partition( F: Functor, @@ -173,6 +184,7 @@ export declare function partition( fga: HKT> ) => Separated>, HKT>> (predicate: Predicate): (fgb: HKT>) => Separated>, HKT>> + (predicate: Predicate): (fga: HKT>) => Separated>, HKT>> } ``` diff --git a/docs/modules/FromEither.ts.md b/docs/modules/FromEither.ts.md index 5c5af4780..2186f0671 100644 --- a/docs/modules/FromEither.ts.md +++ b/docs/modules/FromEither.ts.md @@ -121,6 +121,7 @@ export declare function filterOrElse( (predicate: Predicate, onFalse: (a: A) => E): ( mb: Kind4 ) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind4) => Kind4 } export declare function filterOrElse( F: FromEither3, @@ -130,6 +131,7 @@ export declare function filterOrElse( ma: Kind3 ) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 } export declare function filterOrElse( F: FromEither3C, @@ -137,6 +139,7 @@ export declare function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind3) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 } export declare function filterOrElse( F: FromEither2, @@ -144,6 +147,7 @@ export declare function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } export declare function filterOrElse( F: FromEither2C, @@ -151,6 +155,7 @@ export declare function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } export declare function filterOrElse( F: FromEither, @@ -158,6 +163,7 @@ export declare function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: HKT2) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (mb: HKT2) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: HKT2) => HKT2 } ``` @@ -267,36 +273,42 @@ export declare function fromPredicate( ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind4 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind4 } export declare function fromPredicate( F: FromEither3 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 } export declare function fromPredicate( F: FromEither3C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 } export declare function fromPredicate( F: FromEither2 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 } export declare function fromPredicate( F: FromEither2C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 } export declare function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 } ``` diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index f0be77bcd..acac84ef0 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -403,6 +403,7 @@ Added in v2.10.0 export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: IOEither) => IOEither (predicate: Predicate, onFalse: (a: A) => E): (mb: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: IOEither) => IOEither } ``` @@ -422,6 +423,7 @@ export declare const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: IOEither ) => IOEither + (predicate: Predicate, onFalse: (a: A) => E2): (ma: IOEither) => IOEither } ``` @@ -572,6 +574,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => IOEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => IOEither } ``` diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index efe711d00..03af530f9 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -116,6 +116,7 @@ Added in v2.0.0 export declare const filter: { (refinement: Refinement): (fa: Map) => Map (predicate: Predicate): (fb: Map) => Map + (predicate: Predicate): (fa: Map) => Map } ``` @@ -139,6 +140,7 @@ Added in v2.0.0 export declare const partition: { (refinement: Refinement): (fa: Map) => Separated, Map> (predicate: Predicate): (fb: Map) => Separated, Map> + (predicate: Predicate): (fa: Map) => Separated, Map> } ``` @@ -214,6 +216,7 @@ Added in v2.10.0 ```ts export declare function filterWithIndex(p: (k: K, a: A) => a is B): (m: Map) => Map export declare function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map +export declare function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map ``` Added in v2.10.0 @@ -252,7 +255,10 @@ export declare function partitionWithIndex( ): (fa: Map) => Separated, Map> export declare function partitionWithIndex( predicateWithIndex: (k: K, a: A) => boolean -): (fa: Map) => Separated, Map> +): (fb: Map) => Separated, Map> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (fa: Map) => Separated, Map> ``` Added in v2.10.0 diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 865fc64f7..2a9e3116f 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -812,6 +812,9 @@ Use [`filter`](./Array.ts.html#filter) instead. export declare function filter( refinement: Refinement ): (as: NonEmptyArray) => Option> +export declare function filter( + predicate: Predicate +): (bs: NonEmptyArray) => Option> export declare function filter(predicate: Predicate): (as: NonEmptyArray) => Option> ``` diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index d47ed289c..65d51d150 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -257,6 +257,7 @@ Returns a _smart constructor_ based on the given predicate. ```ts export declare function fromPredicate(refinement: Refinement): (a: A) => Option export declare function fromPredicate(predicate: Predicate): (b: B) => Option +export declare function fromPredicate(predicate: Predicate): (a: A) => Option ``` **Example** @@ -577,6 +578,7 @@ Added in v2.0.0 export declare const filter: { (refinement: Refinement): (fa: Option) => Option (predicate: Predicate): (fb: Option) => Option + (predicate: Predicate): (fa: Option) => Option } ``` @@ -630,6 +632,7 @@ Added in v2.7.0 export declare const partition: { (refinement: Refinement): (fa: Option) => Separated, Option> (predicate: Predicate): (fb: Option) => Separated, Option> + (predicate: Predicate): (fa: Option) => Separated, Option> } ``` diff --git a/docs/modules/OptionT.ts.md b/docs/modules/OptionT.ts.md index fa62f23bd..9852126ef 100644 --- a/docs/modules/OptionT.ts.md +++ b/docs/modules/OptionT.ts.md @@ -403,42 +403,49 @@ export declare function fromPredicate( ): { (refinement: Refinement): (a: A) => Kind4> (predicate: Predicate): (b: B) => Kind4> + (predicate: Predicate): (a: A) => Kind4> } export declare function fromPredicate( F: Pointed3 ): { (refinement: Refinement): (a: A) => Kind3> (predicate: Predicate): (b: B) => Kind3> + (predicate: Predicate): (a: A) => Kind3> } export declare function fromPredicate( F: Pointed3C ): { (refinement: Refinement): (a: A) => Kind3> (predicate: Predicate): (b: B) => Kind3> + (predicate: Predicate): (a: A) => Kind3> } export declare function fromPredicate( F: Pointed2 ): { (refinement: Refinement): (a: A) => Kind2> (predicate: Predicate): (b: B) => Kind2> + (predicate: Predicate): (a: A) => Kind2> } export declare function fromPredicate( F: Pointed2C ): { (refinement: Refinement): (a: A) => Kind2> (predicate: Predicate): (b: B) => Kind2> + (predicate: Predicate): (a: A) => Kind2> } export declare function fromPredicate( F: Pointed1 ): { (refinement: Refinement): (a: A) => Kind> (predicate: Predicate): (b: B) => Kind> + (predicate: Predicate): (a: A) => Kind> } export declare function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> (predicate: Predicate): (b: B) => HKT> + (predicate: Predicate): (a: A) => HKT> } ``` diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index e25abf199..72d098f0d 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -477,6 +477,7 @@ export declare const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: ReaderEither ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderEither) => ReaderEither } ``` @@ -496,6 +497,9 @@ export declare const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: ReaderEither ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: ReaderEither + ) => ReaderEither } ``` @@ -693,6 +697,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => ReaderEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => ReaderEither } ``` diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 8c865e7fe..ec10be853 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -760,6 +760,7 @@ export declare const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: ReaderTaskEither ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderTaskEither) => ReaderTaskEither } ``` @@ -779,6 +780,9 @@ export declare const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: ReaderTaskEither ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: ReaderTaskEither + ) => ReaderTaskEither } ``` @@ -1048,6 +1052,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => ReaderTaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => ReaderTaskEither } ``` diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 67118b5dc..2133980de 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -311,9 +311,9 @@ Added in v2.5.0 ```ts export declare const filter: { - (refinement: Refinement): (fa: readonly A[]) => readonly B[] - (predicate: Predicate): (fb: readonly B[]) => readonly B[] - (predicate: Predicate): (fb: readonly A[]) => readonly A[] + (refinement: Refinement): (as: readonly A[]) => readonly B[] + (predicate: Predicate): (bs: readonly B[]) => readonly B[] + (predicate: Predicate): (as: readonly A[]) => readonly A[] } ``` @@ -335,8 +335,9 @@ Added in v2.5.0 ```ts export declare const partition: { - (refinement: Refinement): (fa: readonly A[]) => Separated - (predicate: Predicate): (fa: readonly B[]) => Separated + (refinement: Refinement): (as: readonly A[]) => Separated + (predicate: Predicate): (bs: readonly B[]) => Separated + (predicate: Predicate): (as: readonly A[]) => Separated } ``` @@ -372,8 +373,9 @@ Added in v2.5.0 ```ts export declare const filterWithIndex: { - (refinementWithIndex: RefinementWithIndex): (fa: readonly A[]) => readonly B[] - (predicateWithIndex: PredicateWithIndex): (fb: readonly B[]) => readonly B[] + (refinementWithIndex: RefinementWithIndex): (as: readonly A[]) => readonly B[] + (predicateWithIndex: PredicateWithIndex): (bs: readonly B[]) => readonly B[] + (predicateWithIndex: PredicateWithIndex): (as: readonly A[]) => readonly A[] } ``` @@ -398,11 +400,12 @@ Added in v2.5.0 ```ts export declare const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( - fa: readonly A[] + as: readonly A[] ) => Separated (predicateWithIndex: PredicateWithIndex): ( - fb: readonly B[] + bs: readonly B[] ) => Separated + (predicateWithIndex: PredicateWithIndex): (as: readonly A[]) => Separated } ``` @@ -870,6 +873,7 @@ export declare function dropLeftWhile( export declare function dropLeftWhile( predicate: Predicate ): (bs: ReadonlyArray) => ReadonlyArray +export declare function dropLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray ``` **Example** @@ -1295,6 +1299,7 @@ export declare function takeLeftWhile( export declare function takeLeftWhile( predicate: Predicate ): (bs: ReadonlyArray) => ReadonlyArray +export declare function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray ``` **Example** @@ -1489,6 +1494,7 @@ Added in v2.11.0 ```ts export declare function fromPredicate(refinement: Refinement): (a: A) => ReadonlyArray +export declare function fromPredicate(predicate: Predicate): (b: B) => ReadonlyArray export declare function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray ``` @@ -2469,6 +2475,7 @@ Find the first element which satisfies a predicate (or a refinement) function ```ts export declare function findFirst(refinement: Refinement): (as: ReadonlyArray) => Option export declare function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option +export declare function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option ``` **Example** @@ -2553,6 +2560,7 @@ Find the last element which satisfies a predicate function ```ts export declare function findLast(refinement: Refinement): (as: ReadonlyArray) => Option export declare function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option +export declare function findLast(predicate: Predicate): (as: ReadonlyArray) => Option ``` **Example** @@ -2831,6 +2839,7 @@ Split an array into two parts: ```ts export declare function spanLeft(refinement: Refinement): (as: ReadonlyArray) => Spanned export declare function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned +export declare function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned ``` **Example** diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index 7f2166132..5b025a855 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -124,6 +124,7 @@ Added in v2.5.0 export declare const filter: { (refinement: Refinement): (fa: ReadonlyMap) => ReadonlyMap (predicate: Predicate): (fb: ReadonlyMap) => ReadonlyMap + (predicate: Predicate): (fa: ReadonlyMap) => ReadonlyMap } ``` @@ -151,6 +152,7 @@ export declare const partition: { (predicate: Predicate): ( fb: ReadonlyMap ) => Separated, ReadonlyMap> + (predicate: Predicate): (fa: ReadonlyMap) => Separated, ReadonlyMap> } ``` @@ -232,6 +234,9 @@ export declare function filterWithIndex( export declare function filterWithIndex( predicateWithIndex: (k: K, a: A) => boolean ): (m: ReadonlyMap) => ReadonlyMap +export declare function filterWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => ReadonlyMap ``` Added in v2.10.0 @@ -271,6 +276,9 @@ export declare function partitionWithIndex( export declare function partitionWithIndex( predicateWithIndex: (k: K, a: A) => boolean ): (m: ReadonlyMap) => Separated, ReadonlyMap> +export declare function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => Separated, ReadonlyMap> ``` Added in v2.10.0 diff --git a/docs/modules/ReadonlyNonEmptyArray.ts.md b/docs/modules/ReadonlyNonEmptyArray.ts.md index dba59ecfb..51232b8bb 100644 --- a/docs/modules/ReadonlyNonEmptyArray.ts.md +++ b/docs/modules/ReadonlyNonEmptyArray.ts.md @@ -907,6 +907,9 @@ Use [`filter`](./ReadonlyArray.ts.html#filter) instead. export declare function filter( refinement: Refinement ): (as: ReadonlyNonEmptyArray) => Option> +export declare function filter( + predicate: Predicate +): (bs: ReadonlyNonEmptyArray) => Option> export declare function filter( predicate: Predicate ): (as: ReadonlyNonEmptyArray) => Option> diff --git a/docs/modules/ReadonlyRecord.ts.md b/docs/modules/ReadonlyRecord.ts.md index 6eaa3b2b3..caae504fd 100644 --- a/docs/modules/ReadonlyRecord.ts.md +++ b/docs/modules/ReadonlyRecord.ts.md @@ -138,6 +138,7 @@ Added in v2.5.0 export declare const filter: { (refinement: Refinement): (fa: Readonly>) => Readonly> (predicate: Predicate): (fb: Readonly>) => Readonly> + (predicate: Predicate): (fa: Readonly>) => Readonly> } ``` @@ -167,6 +168,9 @@ export declare const partition: { (predicate: Predicate): ( fb: Readonly> ) => Separated>, Readonly>> + (predicate: Predicate): ( + fa: Readonly> + ) => Separated>, Readonly>> } ``` @@ -796,6 +800,9 @@ Added in v2.5.0 export declare function filterWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: ReadonlyRecord) => ReadonlyRecord +export declare function filterWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: ReadonlyRecord) => ReadonlyRecord export declare function filterWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: ReadonlyRecord) => ReadonlyRecord @@ -1010,6 +1017,9 @@ Added in v2.5.0 export declare function partitionWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: ReadonlyRecord) => Separated, ReadonlyRecord> +export declare function partitionWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: ReadonlyRecord) => Separated, ReadonlyRecord> export declare function partitionWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: ReadonlyRecord) => Separated, ReadonlyRecord> diff --git a/docs/modules/ReadonlySet.ts.md b/docs/modules/ReadonlySet.ts.md index 1b7ca83a7..83c267ff1 100644 --- a/docs/modules/ReadonlySet.ts.md +++ b/docs/modules/ReadonlySet.ts.md @@ -115,6 +115,7 @@ Added in v2.5.0 ```ts export declare function filter(refinement: Refinement): (set: ReadonlySet) => ReadonlySet export declare function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet +export declare function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet ``` Added in v2.5.0 @@ -446,6 +447,9 @@ export declare function partition( export declare function partition( predicate: Predicate ): (set: ReadonlySet) => Separated, ReadonlySet> +export declare function partition( + predicate: Predicate +): (set: ReadonlySet) => Separated, ReadonlySet> ``` Added in v2.5.0 diff --git a/docs/modules/Record.ts.md b/docs/modules/Record.ts.md index 38d4248f6..e492749cf 100644 --- a/docs/modules/Record.ts.md +++ b/docs/modules/Record.ts.md @@ -131,6 +131,7 @@ Added in v2.0.0 export declare const filter: { (refinement: Refinement): (fa: Record) => Record (predicate: Predicate): (fb: Record) => Record + (predicate: Predicate): (fa: Record) => Record } ``` @@ -156,6 +157,7 @@ export declare const partition: { fa: Record ) => Separated, Record> (predicate: Predicate): (fb: Record) => Separated, Record> + (predicate: Predicate): (fa: Record) => Separated, Record> } ``` @@ -659,6 +661,9 @@ Added in v2.0.0 export declare function filterWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: Record) => Record +export declare function filterWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: Record) => Record export declare function filterWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: Record) => Record @@ -897,6 +902,9 @@ Added in v2.0.0 export declare function partitionWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: Record) => Separated, Record> +export declare function partitionWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: Record) => Separated, Record> export declare function partitionWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: Record) => Separated, Record> diff --git a/docs/modules/Set.ts.md b/docs/modules/Set.ts.md index c525e5342..0795d6b77 100644 --- a/docs/modules/Set.ts.md +++ b/docs/modules/Set.ts.md @@ -111,6 +111,7 @@ Added in v2.0.0 ```ts export declare function filter(refinement: Refinement): (set: Set) => Set export declare function filter(predicate: Predicate): (set: Set) => Set +export declare function filter(predicate: Predicate): (set: Set) => Set ``` Added in v2.0.0 @@ -373,6 +374,7 @@ export declare function partition( refinement: Refinement ): (set: Set) => Separated, Set> export declare function partition(predicate: Predicate): (set: Set) => Separated, Set> +export declare function partition(predicate: Predicate): (set: Set) => Separated, Set> ``` Added in v2.0.0 diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index e9c0d1263..c626c7b77 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -641,6 +641,9 @@ export declare const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: StateReaderTaskEither ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + ma: StateReaderTaskEither + ) => StateReaderTaskEither } ``` @@ -660,6 +663,9 @@ export declare const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: StateReaderTaskEither ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: StateReaderTaskEither + ) => StateReaderTaskEither } ``` @@ -863,6 +869,7 @@ export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): ( a: A ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => StateReaderTaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => StateReaderTaskEither } ``` diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index 01dbacced..4482de600 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -522,6 +522,7 @@ Added in v2.11.0 export declare const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither (predicate: Predicate, onFalse: (a: A) => E): (mb: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither } ``` @@ -541,6 +542,7 @@ export declare const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: TaskEither ) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E2): (ma: TaskEither) => TaskEither } ``` @@ -749,6 +751,7 @@ Added in v2.0.0 ```ts export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => TaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => TaskEither } ``` diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 6f6362558..5087e696f 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -181,6 +181,7 @@ Added in v2.10.0 export declare const filter: { (refinement: Refinement): (fb: TaskOption) => TaskOption (predicate: Predicate): (fb: TaskOption) => TaskOption + (predicate: Predicate): (fa: TaskOption) => TaskOption } ``` @@ -204,6 +205,7 @@ Added in v2.10.0 export declare const partition: { (refinement: Refinement): (fb: TaskOption) => Separated, TaskOption> (predicate: Predicate): (fb: TaskOption) => Separated, TaskOption> + (predicate: Predicate): (fa: TaskOption) => Separated, TaskOption> } ``` @@ -433,6 +435,7 @@ Added in v2.10.0 export declare const fromPredicate: { (refinement: Refinement): (a: A) => TaskOption (predicate: Predicate): (b: B) => TaskOption + (predicate: Predicate): (a: A) => TaskOption } ``` diff --git a/docs/modules/TaskThese.ts.md b/docs/modules/TaskThese.ts.md index a8adf0974..44f336bf4 100644 --- a/docs/modules/TaskThese.ts.md +++ b/docs/modules/TaskThese.ts.md @@ -219,6 +219,7 @@ Added in v2.4.0 export declare const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => TaskThese (predicate: Predicate, onFalse: (a: A) => E): (b: B) => TaskThese + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => TaskThese } ``` diff --git a/src/Array.ts b/src/Array.ts index f5f6b9c2a..63bf27ba1 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -159,6 +159,7 @@ export const replicate = (n: number, a: A): Array => makeBy(n, () => a) * @since 2.11.0 */ export function fromPredicate(refinement: Refinement): (a: A) => Array +export function fromPredicate(predicate: Predicate): (b: B) => Array export function fromPredicate(predicate: Predicate): (a: A) => Array export function fromPredicate(predicate: Predicate): (a: A) => Array { return (a) => (predicate(a) ? [a] : []) @@ -466,14 +467,15 @@ export const takeRight = (n: number) => (as: Array): Array => */ export function takeLeftWhile(refinement: Refinement): (as: Array) => Array export function takeLeftWhile(predicate: Predicate): (bs: Array) => Array -export function takeLeftWhile(predicate: Predicate): (bs: Array) => Array { - return (bs: Array) => { - const out: Array = [] - for (const b of bs) { - if (!predicate(b)) { +export function takeLeftWhile(predicate: Predicate): (as: Array) => Array +export function takeLeftWhile(predicate: Predicate): (as: Array) => Array { + return (as: Array) => { + const out: Array = [] + for (const a of as) { + if (!predicate(a)) { break } - out.push(b) + out.push(a) } return out } @@ -515,9 +517,10 @@ export interface Spanned { */ export function spanLeft(refinement: Refinement): (as: Array) => Spanned export function spanLeft(predicate: Predicate): (bs: Array) => Spanned -export function spanLeft(predicate: Predicate): (bs: Array) => Spanned { - return (bs) => { - const [init, rest] = splitAt(spanLeftIndex(bs, predicate))(bs) +export function spanLeft(predicate: Predicate): (as: Array) => Spanned +export function spanLeft(predicate: Predicate): (as: Array) => Spanned { + return (as) => { + const [init, rest] = splitAt(spanLeftIndex(as, predicate))(as) return { init, rest } } } @@ -567,8 +570,9 @@ export const dropRight = (n: number) => (as: Array): Array => */ export function dropLeftWhile(refinement: Refinement): (as: Array) => Array export function dropLeftWhile(predicate: Predicate): (bs: Array) => Array -export function dropLeftWhile(predicate: Predicate): (bs: Array) => Array { - return (bs) => bs.slice(spanLeftIndex(bs, predicate)) +export function dropLeftWhile(predicate: Predicate): (as: Array) => Array +export function dropLeftWhile(predicate: Predicate): (as: Array) => Array { + return (as) => as.slice(spanLeftIndex(as, predicate)) } /** @@ -604,7 +608,8 @@ export const findIndex: (predicate: Predicate) => (as: Array) => Option */ export function findFirst(refinement: Refinement): (as: Array) => Option export function findFirst(predicate: Predicate): (bs: Array) => Option -export function findFirst(predicate: Predicate): (bs: Array) => Option { +export function findFirst(predicate: Predicate): (as: Array) => Option +export function findFirst(predicate: Predicate): (as: Array) => Option { return RA.findFirst(predicate) } @@ -649,7 +654,8 @@ export const findFirstMap: (f: (a: A) => Option) => (as: Array) => O */ export function findLast(refinement: Refinement): (as: Array) => Option export function findLast(predicate: Predicate): (bs: Array) => Option -export function findLast(predicate: Predicate): (bs: Array) => Option { +export function findLast(predicate: Predicate): (as: Array) => Option +export function findLast(predicate: Predicate): (as: Array) => Option { return RA.findLast(predicate) } @@ -1453,18 +1459,20 @@ export const separate = (fa: Array>): Separated, Arr * @since 2.0.0 */ export const filter: { - (refinement: Refinement): (fa: Array) => Array - (predicate: Predicate): (fb: Array) => Array -} = (predicate: Predicate) => (fb: Array) => fb.filter(predicate) + (refinement: Refinement): (as: Array) => Array + (predicate: Predicate): (bs: Array) => Array + (predicate: Predicate): (as: Array) => Array +} = (predicate: Predicate) => (as: Array) => as.filter(predicate) /** * @category Filterable * @since 2.0.0 */ export const partition: { - (refinement: Refinement): (fa: Array) => Separated, Array> - (predicate: Predicate): (fb: Array) => Separated, Array> -} = (predicate: Predicate): ((fb: Array) => Separated, Array>) => + (refinement: Refinement): (as: Array) => Separated, Array> + (predicate: Predicate): (bs: Array) => Separated, Array> + (predicate: Predicate): (as: Array) => Separated, Array> +} = (predicate: Predicate): ((as: Array) => Separated, Array>) => partitionWithIndex((_, a) => predicate(a)) /** @@ -1473,16 +1481,15 @@ export const partition: { */ export const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( - fa: Array + as: Array ) => Separated, Array> - (predicateWithIndex: PredicateWithIndex): (fb: Array) => Separated, Array> -} = (predicateWithIndex: PredicateWithIndex) => ( - fb: Array -): Separated, Array> => { - const left: Array = [] - const right: Array = [] - for (let i = 0; i < fb.length; i++) { - const b = fb[i] + (predicateWithIndex: PredicateWithIndex): (bs: Array) => Separated, Array> + (predicateWithIndex: PredicateWithIndex): (as: Array) => Separated, Array> +} = (predicateWithIndex: PredicateWithIndex) => (as: Array): Separated, Array> => { + const left: Array = [] + const right: Array = [] + for (let i = 0; i < as.length; i++) { + const b = as[i] if (predicateWithIndex(i, b)) { right.push(b) } else { @@ -1542,16 +1549,17 @@ export const alt: (that: Lazy>) => (fa: Array) => Array = altW * @since 2.0.0 */ export const filterWithIndex: { - (refinementWithIndex: RefinementWithIndex): (fa: Array) => Array - (predicateWithIndex: PredicateWithIndex): (fb: Array) => Array -} = (predicateWithIndex: PredicateWithIndex) => (fb: Array): Array => - fb.filter((b, i) => predicateWithIndex(i, b)) + (refinementWithIndex: RefinementWithIndex): (as: Array) => Array + (predicateWithIndex: PredicateWithIndex): (bs: Array) => Array + (predicateWithIndex: PredicateWithIndex): (as: Array) => Array +} = (predicateWithIndex: PredicateWithIndex) => (as: Array): Array => + as.filter((b, i) => predicateWithIndex(i, b)) /** * @category Extend * @since 2.0.0 */ -export const extend: (f: (fa: Array) => B) => (wa: Array) => Array = (f) => (wa) => +export const extend: (f: (as: Array) => B) => (as: Array) => Array = (f) => (wa) => wa.map((_, i) => f(wa.slice(i))) /** diff --git a/src/Either.ts b/src/Either.ts index 193220ddf..a289d872d 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -1070,6 +1070,7 @@ export const filterOrElseW: { ma: Either ) => Either (predicate: Predicate, onFalse: (a: A) => E2): (mb: Either) => Either + (predicate: Predicate, onFalse: (a: A) => E2): (ma: Either) => Either } = filterOrElse /** diff --git a/src/Filterable.ts b/src/Filterable.ts index 0e9be07fd..3ac0b6a0d 100644 --- a/src/Filterable.ts +++ b/src/Filterable.ts @@ -293,6 +293,7 @@ export function filter( ): { (refinement: Refinement): (fga: Kind2>) => Kind2> (predicate: Predicate): (fgb: Kind2>) => Kind2> + (predicate: Predicate): (fga: Kind2>) => Kind2> } export function filter( F: Functor1, @@ -300,6 +301,7 @@ export function filter( ): { (refinement: Refinement): (fga: Kind>) => Kind> (predicate: Predicate): (fgb: Kind>) => Kind> + (predicate: Predicate): (fga: Kind>) => Kind> } export function filter( F: Functor1, @@ -307,6 +309,7 @@ export function filter( ): { (refinement: Refinement): (fga: Kind>) => Kind> (predicate: Predicate): (fgb: Kind>) => Kind> + (predicate: Predicate): (fga: Kind>) => Kind> } export function filter( F: Functor, @@ -314,12 +317,13 @@ export function filter( ): { (refinement: Refinement): (fga: HKT>) => HKT> (predicate: Predicate): (fgb: HKT>) => HKT> + (predicate: Predicate): (fga: HKT>) => HKT> } export function filter( F: Functor, G: Filterable -): (predicate: Predicate) => (fgb: HKT>) => HKT> { - return (predicate) => (fgb) => F.map(fgb, (ga) => G.filter(ga, predicate)) +): (predicate: Predicate) => (fga: HKT>) => HKT> { + return (predicate) => (fga) => F.map(fga, (ga) => G.filter(ga, predicate)) } /** @@ -367,6 +371,9 @@ export function partition( (predicate: Predicate): ( fgb: Kind2> ) => Separated>, Kind2>> + (predicate: Predicate): ( + fga: Kind2> + ) => Separated>, Kind2>> } export function partition( F: Functor1, @@ -378,6 +385,9 @@ export function partition( (predicate: Predicate): ( fgb: Kind> ) => Separated>, Kind>> + (predicate: Predicate): ( + fga: Kind> + ) => Separated>, Kind>> } export function partition( F: Functor1, @@ -389,6 +399,7 @@ export function partition( (predicate: Predicate): ( fgb: Kind> ) => Separated>, Kind>> + (predicate: Predicate): (fga: Kind>) => Separated>, Kind>> } export function partition( F: Functor, @@ -398,13 +409,12 @@ export function partition( fga: HKT> ) => Separated>, HKT>> (predicate: Predicate): (fgb: HKT>) => Separated>, HKT>> + (predicate: Predicate): (fga: HKT>) => Separated>, HKT>> } export function partition( F: Functor, G: Filterable -): ( - predicate: Predicate -) => (fgb: HKT>) => Separated>, HKT>> { +): (predicate: Predicate) => (fga: HKT>) => Separated>, HKT>> { const _filter = filter(F, G) return (predicate) => { const left = _filter(not(predicate)) diff --git a/src/FromEither.ts b/src/FromEither.ts index 0aa22c4c5..4b10fdbf8 100644 --- a/src/FromEither.ts +++ b/src/FromEither.ts @@ -130,45 +130,52 @@ export function fromPredicate( ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind4 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind4 } export function fromPredicate( F: FromEither3 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 } export function fromPredicate( F: FromEither3C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind3 } export function fromPredicate( F: FromEither2 ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 } export function fromPredicate( F: FromEither2C ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => Kind2 } export function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 } export function fromPredicate( F: FromEither ): { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (b: B) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (a: A) => HKT2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => (b: B) => - F.fromEither(predicate(b) ? _.right(b) : _.left(onFalse(b))) + return (predicate: Predicate, onFalse: (a: A) => E) => (a: A) => + F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a))) } // ------------------------------------------------------------------------------------- @@ -343,6 +350,7 @@ export function filterOrElse( (predicate: Predicate, onFalse: (a: A) => E): ( mb: Kind4 ) => Kind4 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind4) => Kind4 } export function filterOrElse( F: FromEither3, @@ -352,6 +360,7 @@ export function filterOrElse( ma: Kind3 ) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 } export function filterOrElse( F: FromEither3C, @@ -359,6 +368,7 @@ export function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind3) => Kind3 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind3) => Kind3 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind3) => Kind3 } export function filterOrElse( F: FromEither2, @@ -366,6 +376,7 @@ export function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } export function filterOrElse( F: FromEither2C, @@ -373,6 +384,7 @@ export function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } export function filterOrElse( F: FromEither, @@ -380,6 +392,7 @@ export function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: HKT2) => HKT2 (predicate: Predicate, onFalse: (a: A) => E): (mb: HKT2) => HKT2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: HKT2) => HKT2 } export function filterOrElse( F: FromEither2, @@ -387,7 +400,8 @@ export function filterOrElse( ): { (refinement: Refinement, onFalse: (a: A) => E): (ma: Kind2) => Kind2 (predicate: Predicate, onFalse: (a: A) => E): (mb: Kind2) => Kind2 + (predicate: Predicate, onFalse: (a: A) => E): (ma: Kind2) => Kind2 } { - return (predicate: Predicate, onFalse: (a: A) => E) => (mb: Kind2): Kind2 => - M.chain(mb, (a) => F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)))) + return (predicate: Predicate, onFalse: (a: A) => E) => (ma: Kind2): Kind2 => + M.chain(ma, (a) => F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)))) } diff --git a/src/IOEither.ts b/src/IOEither.ts index f6a0c0498..f534561e1 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -785,6 +785,7 @@ export const chainEitherKW: ( */ export const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => IOEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => IOEither } = /*#__PURE__*/ @@ -797,6 +798,7 @@ export const fromPredicate: { export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: IOEither) => IOEither (predicate: Predicate, onFalse: (a: A) => E): (mb: IOEither) => IOEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: IOEither) => IOEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -814,6 +816,7 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: IOEither ) => IOEither + (predicate: Predicate, onFalse: (a: A) => E2): (ma: IOEither) => IOEither } = filterOrElse /** diff --git a/src/Map.ts b/src/Map.ts index f1355191a..776e51688 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -413,22 +413,25 @@ export function partitionWithIndex( ): (fa: Map) => Separated, Map> export function partitionWithIndex( predicateWithIndex: (k: K, a: A) => boolean -): (fa: Map) => Separated, Map> +): (fb: Map) => Separated, Map> export function partitionWithIndex( predicateWithIndex: (k: K, a: A) => boolean -): (fa: Map) => Separated, Map> { - return (fa: Map) => { - const left = new Map() - const right = new Map() +): (fa: Map) => Separated, Map> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (fa: Map) => Separated, Map> { + return (fa: Map) => { + const left = new Map() + const right = new Map() const entries = fa.entries() - let e: Next<[K, B]> + let e: Next<[K, A]> // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { - const [k, b] = e.value - if (predicateWithIndex(k, b)) { - right.set(k, b) + const [k, a] = e.value + if (predicateWithIndex(k, a)) { + right.set(k, a) } else { - left.set(k, b) + left.set(k, a) } } return separated(left, right) @@ -460,16 +463,17 @@ export const filterMapWithIndex = (f: (k: K, a: A) => Option) => (fa */ export function filterWithIndex(p: (k: K, a: A) => a is B): (m: Map) => Map export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map -export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map { - return (m: Map) => { - const out = new Map() +export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map +export function filterWithIndex(p: (k: K, a: A) => boolean): (m: Map) => Map { + return (m: Map) => { + const out = new Map() const entries = m.entries() - let e: Next<[K, B]> + let e: Next<[K, A]> // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { - const [k, b] = e.value - if (p(k, b)) { - out.set(k, b) + const [k, a] = e.value + if (p(k, a)) { + out.set(k, a) } } return out @@ -522,7 +526,8 @@ export const compact = (fa: Map>): Map => { export const filter: { (refinement: Refinement): (fa: Map) => Map (predicate: Predicate): (fb: Map) => Map -} = (predicate: Predicate) => (fb: Map) => _filter(fb, predicate) + (predicate: Predicate): (fa: Map) => Map +} = (predicate: Predicate) => (fa: Map) => _filter(fa, predicate) /** * @category Filterable @@ -554,7 +559,8 @@ export const mapWithIndex: (f: (k: K, a: A) => B) => (fa: Map) => export const partition: { (refinement: Refinement): (fa: Map) => Separated, Map> (predicate: Predicate): (fb: Map) => Separated, Map> -} = (predicate: Predicate) => (fb: Map) => _partition(fb, predicate) + (predicate: Predicate): (fa: Map) => Separated, Map> +} = (predicate: Predicate) => (fa: Map) => _partition(fa, predicate) /** * @category Filterable diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index f29488c36..1e29ec95d 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -1249,6 +1249,7 @@ export function groupSort(O: Ord): (as: Array) => Array(refinement: Refinement): (as: NonEmptyArray) => Option> +export function filter(predicate: Predicate): (bs: NonEmptyArray) => Option> export function filter(predicate: Predicate): (as: NonEmptyArray) => Option> export function filter(predicate: Predicate): (as: NonEmptyArray) => Option> { return filterWithIndex((_, a) => predicate(a)) diff --git a/src/Option.ts b/src/Option.ts index 9a6cf52f3..68f731991 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -113,7 +113,8 @@ export const some: (a: A) => Option = _.some */ export function fromPredicate(refinement: Refinement): (a: A) => Option export function fromPredicate(predicate: Predicate): (b: B) => Option -export function fromPredicate(predicate: Predicate): (b: B) => Option { +export function fromPredicate(predicate: Predicate): (a: A) => Option +export function fromPredicate(predicate: Predicate): (a: A) => Option { return (a) => (predicate(a) ? some(a) : none) } @@ -545,8 +546,8 @@ export const Compactable: Compactable1 = { export const filter: { (refinement: Refinement): (fa: Option) => Option (predicate: Predicate): (fb: Option) => Option -} = (predicate: Predicate) => (fb: Option) => - isNone(fb) ? none : predicate(fb.value) ? fb : none + (predicate: Predicate): (fa: Option) => Option +} = (predicate: Predicate) => (fa: Option) => (isNone(fa) ? none : predicate(fa.value) ? fa : none) /** * @category instance operations @@ -562,8 +563,8 @@ export const filterMap: (f: (a: A) => Option) => (fa: Option) => Opt export const partition: { (refinement: Refinement): (fa: Option) => Separated, Option> (predicate: Predicate): (fb: Option) => Separated, Option> -} = (predicate: Predicate) => (fb: Option) => - separated(_filter(fb, not(predicate)), _filter(fb, predicate)) + (predicate: Predicate): (fa: Option) => Separated, Option> +} = (predicate: Predicate) => (fa: Option) => separated(_filter(fa, not(predicate)), _filter(fa, predicate)) /** * @category instance operations diff --git a/src/OptionT.ts b/src/OptionT.ts index 6333bce92..3eb378a26 100644 --- a/src/OptionT.ts +++ b/src/OptionT.ts @@ -246,50 +246,58 @@ export function fromPredicate( ): { (refinement: Refinement): (a: A) => Kind4> (predicate: Predicate): (b: B) => Kind4> + (predicate: Predicate): (a: A) => Kind4> } export function fromPredicate( F: Pointed3 ): { (refinement: Refinement): (a: A) => Kind3> (predicate: Predicate): (b: B) => Kind3> + (predicate: Predicate): (a: A) => Kind3> } export function fromPredicate( F: Pointed3C ): { (refinement: Refinement): (a: A) => Kind3> (predicate: Predicate): (b: B) => Kind3> + (predicate: Predicate): (a: A) => Kind3> } export function fromPredicate( F: Pointed2 ): { (refinement: Refinement): (a: A) => Kind2> (predicate: Predicate): (b: B) => Kind2> + (predicate: Predicate): (a: A) => Kind2> } export function fromPredicate( F: Pointed2C ): { (refinement: Refinement): (a: A) => Kind2> (predicate: Predicate): (b: B) => Kind2> + (predicate: Predicate): (a: A) => Kind2> } export function fromPredicate( F: Pointed1 ): { (refinement: Refinement): (a: A) => Kind> (predicate: Predicate): (b: B) => Kind> + (predicate: Predicate): (a: A) => Kind> } export function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> (predicate: Predicate): (b: B) => HKT> + (predicate: Predicate): (a: A) => HKT> } export function fromPredicate( F: Pointed ): { (refinement: Refinement): (a: A) => HKT> (predicate: Predicate): (b: B) => HKT> + (predicate: Predicate): (a: A) => HKT> } { - return (predicate: Predicate) => (b: B) => F.of(O.fromPredicate(predicate)(b)) + return (predicate: Predicate) => (a: A) => F.of(O.fromPredicate(predicate)(a)) } /** diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 7fd236fc4..d33aa0d32 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -840,6 +840,7 @@ export const chainEitherKW: ( */ export const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => ReaderEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => ReaderEither } = /*#__PURE__*/ @@ -856,6 +857,7 @@ export const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: ReaderEither ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderEither) => ReaderEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -873,6 +875,9 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: ReaderEither ) => ReaderEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: ReaderEither + ) => ReaderEither } = filterOrElse /** diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 38435b449..9e786425f 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1162,6 +1162,7 @@ export const chainEitherKW: ( */ export const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => ReaderTaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => ReaderTaskEither } = /*#__PURE__*/ @@ -1178,6 +1179,7 @@ export const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: ReaderTaskEither ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: ReaderTaskEither) => ReaderTaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -1195,6 +1197,9 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: ReaderTaskEither ) => ReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: ReaderTaskEither + ) => ReaderTaskEither } = filterOrElse /** diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 9e7b256d8..d6e30fbfa 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -160,6 +160,7 @@ export const replicate = (n: number, a: A): ReadonlyArray => makeBy(n, () * @since 2.11.0 */ export function fromPredicate(refinement: Refinement): (a: A) => ReadonlyArray +export function fromPredicate(predicate: Predicate): (b: B) => ReadonlyArray export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray export function fromPredicate(predicate: Predicate): (a: A) => ReadonlyArray { return (a) => (predicate(a) ? [a] : empty) @@ -487,17 +488,18 @@ export const takeRight = (n: number) => (as: ReadonlyArray): ReadonlyArray */ export function takeLeftWhile(refinement: Refinement): (as: ReadonlyArray) => ReadonlyArray export function takeLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray -export function takeLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray { - return (bs: ReadonlyArray) => { - const out: Array = [] - for (const b of bs) { - if (!predicate(b)) { +export function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray +export function takeLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray { + return (as: ReadonlyArray) => { + const out: Array = [] + for (const a of as) { + if (!predicate(a)) { break } - out.push(b) + out.push(a) } const len = out.length - return len === bs.length ? bs : len === 0 ? empty : out + return len === as.length ? as : len === 0 ? empty : out } } @@ -534,9 +536,10 @@ const spanLeftIndex = (as: ReadonlyArray, predicate: Predicate): number */ export function spanLeft(refinement: Refinement): (as: ReadonlyArray) => Spanned export function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned -export function spanLeft(predicate: Predicate): (bs: ReadonlyArray) => Spanned { - return (bs) => { - const [init, rest] = splitAt(spanLeftIndex(bs, predicate))(bs) +export function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned +export function spanLeft(predicate: Predicate): (as: ReadonlyArray) => Spanned { + return (as) => { + const [init, rest] = splitAt(spanLeftIndex(as, predicate))(as) return { init, rest } } } @@ -594,10 +597,11 @@ export const dropRight = (n: number) => (as: ReadonlyArray): ReadonlyArray */ export function dropLeftWhile(refinement: Refinement): (as: ReadonlyArray) => ReadonlyArray export function dropLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray -export function dropLeftWhile(predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray { - return (bs) => { - const i = spanLeftIndex(bs, predicate) - return i === 0 ? bs : i === bs.length ? empty : bs.slice(i) +export function dropLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray +export function dropLeftWhile(predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray { + return (as) => { + const i = spanLeftIndex(as, predicate) + return i === 0 ? as : i === as.length ? empty : as.slice(i) } } @@ -640,11 +644,12 @@ export const findIndex = (predicate: Predicate) => (as: ReadonlyArray): */ export function findFirst(refinement: Refinement): (as: ReadonlyArray) => Option export function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option -export function findFirst(predicate: Predicate): (bs: ReadonlyArray) => Option { - return (bs) => { - for (let i = 0; i < bs.length; i++) { - if (predicate(bs[i])) { - return _.some(bs[i]) +export function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option +export function findFirst(predicate: Predicate): (as: ReadonlyArray) => Option { + return (as) => { + for (let i = 0; i < as.length; i++) { + if (predicate(as[i])) { + return _.some(as[i]) } } return _.none @@ -698,11 +703,12 @@ export const findFirstMap = (f: (a: A) => Option) => (as: ReadonlyArray */ export function findLast(refinement: Refinement): (as: ReadonlyArray) => Option export function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option -export function findLast(predicate: Predicate): (bs: ReadonlyArray) => Option { - return (bs) => { - for (let i = bs.length - 1; i >= 0; i--) { - if (predicate(bs[i])) { - return _.some(bs[i]) +export function findLast(predicate: Predicate): (as: ReadonlyArray) => Option +export function findLast(predicate: Predicate): (as: ReadonlyArray) => Option { + return (as) => { + for (let i = as.length - 1; i >= 0; i--) { + if (predicate(as[i])) { + return _.some(as[i]) } } return _.none @@ -1529,10 +1535,10 @@ export const separate = (fa: ReadonlyArray>): Separated(refinement: Refinement): (fa: ReadonlyArray) => ReadonlyArray - (predicate: Predicate): (fb: ReadonlyArray) => ReadonlyArray - (predicate: Predicate): (fb: ReadonlyArray) => ReadonlyArray -} = (predicate: Predicate) => (fb: ReadonlyArray) => fb.filter(predicate) + (refinement: Refinement): (as: ReadonlyArray) => ReadonlyArray + (predicate: Predicate): (bs: ReadonlyArray) => ReadonlyArray + (predicate: Predicate): (as: ReadonlyArray) => ReadonlyArray +} = (predicate: Predicate) => (as: ReadonlyArray) => as.filter(predicate) /** * @category FilterableWithIndex @@ -1572,13 +1578,12 @@ export const compact: (fa: ReadonlyArray>) => ReadonlyArray = */ export const partition: { (refinement: Refinement): ( - fa: ReadonlyArray + as: ReadonlyArray ) => Separated, ReadonlyArray> - (predicate: Predicate): (fa: ReadonlyArray) => Separated, ReadonlyArray> -} = ( - predicate: Predicate -): ((fa: ReadonlyArray) => Separated, ReadonlyArray>) => - partitionWithIndex((_, b) => predicate(b)) + (predicate: Predicate): (bs: ReadonlyArray) => Separated, ReadonlyArray> + (predicate: Predicate): (as: ReadonlyArray) => Separated, ReadonlyArray> +} = (predicate: Predicate): ((as: ReadonlyArray) => Separated, ReadonlyArray>) => + partitionWithIndex((_, a) => predicate(a)) /** * @category FilterableWithIndex @@ -1586,22 +1591,25 @@ export const partition: { */ export const partitionWithIndex: { (refinementWithIndex: RefinementWithIndex): ( - fa: ReadonlyArray + as: ReadonlyArray ) => Separated, ReadonlyArray> (predicateWithIndex: PredicateWithIndex): ( - fb: ReadonlyArray + bs: ReadonlyArray ) => Separated, ReadonlyArray> -} = (predicateWithIndex: PredicateWithIndex) => ( - fb: ReadonlyArray -): Separated, ReadonlyArray> => { - const left: Array = [] - const right: Array = [] - for (let i = 0; i < fb.length; i++) { - const b = fb[i] - if (predicateWithIndex(i, b)) { - right.push(b) + (predicateWithIndex: PredicateWithIndex): ( + as: ReadonlyArray + ) => Separated, ReadonlyArray> +} = (predicateWithIndex: PredicateWithIndex) => ( + as: ReadonlyArray +): Separated, ReadonlyArray> => { + const left: Array = [] + const right: Array = [] + for (let i = 0; i < as.length; i++) { + const a = as[i] + if (predicateWithIndex(i, a)) { + right.push(a) } else { - left.push(b) + left.push(a) } } return separated(left, right) @@ -1641,10 +1649,11 @@ export const partitionMapWithIndex = (f: (i: number, a: A) => Either(refinementWithIndex: RefinementWithIndex): (fa: ReadonlyArray) => ReadonlyArray - (predicateWithIndex: PredicateWithIndex): (fb: ReadonlyArray) => ReadonlyArray -} = (predicateWithIndex: PredicateWithIndex) => (fb: ReadonlyArray): ReadonlyArray => - fb.filter((b, i) => predicateWithIndex(i, b)) + (refinementWithIndex: RefinementWithIndex): (as: ReadonlyArray) => ReadonlyArray + (predicateWithIndex: PredicateWithIndex): (bs: ReadonlyArray) => ReadonlyArray + (predicateWithIndex: PredicateWithIndex): (as: ReadonlyArray) => ReadonlyArray +} = (predicateWithIndex: PredicateWithIndex) => (as: ReadonlyArray): ReadonlyArray => + as.filter((a, i) => predicateWithIndex(i, a)) /** * @category Extend diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 13e0b712f..d4376010b 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -545,19 +545,22 @@ export function partitionWithIndex( ): (m: ReadonlyMap) => Separated, ReadonlyMap> export function partitionWithIndex( predicateWithIndex: (k: K, a: A) => boolean -): (m: ReadonlyMap) => Separated, ReadonlyMap> { - return (m: ReadonlyMap) => { - const left = new Map() - const right = new Map() +): (m: ReadonlyMap) => Separated, ReadonlyMap> +export function partitionWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => Separated, ReadonlyMap> { + return (m: ReadonlyMap) => { + const left = new Map() + const right = new Map() const entries = m.entries() - let e: Next + let e: Next // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { - const [k, b] = e.value - if (predicateWithIndex(k, b)) { - right.set(k, b) + const [k, a] = e.value + if (predicateWithIndex(k, a)) { + right.set(k, a) } else { - left.set(k, b) + left.set(k, a) } } return separated(left, right) @@ -597,16 +600,19 @@ export function filterWithIndex( ): (m: ReadonlyMap) => ReadonlyMap export function filterWithIndex( predicateWithIndex: (k: K, a: A) => boolean -): (m: ReadonlyMap) => ReadonlyMap { - return (m: ReadonlyMap) => { - const out = new Map() +): (m: ReadonlyMap) => ReadonlyMap +export function filterWithIndex( + predicateWithIndex: (k: K, a: A) => boolean +): (m: ReadonlyMap) => ReadonlyMap { + return (m: ReadonlyMap) => { + const out = new Map() const entries = m.entries() - let e: Next + let e: Next // tslint:disable-next-line: strict-boolean-expressions while (!(e = entries.next()).done) { - const [k, b] = e.value - if (predicateWithIndex(k, b)) { - out.set(k, b) + const [k, a] = e.value + if (predicateWithIndex(k, a)) { + out.set(k, a) } } return out @@ -660,7 +666,8 @@ export const compact = (fa: ReadonlyMap>): ReadonlyMap export const filter: { (refinement: Refinement): (fa: ReadonlyMap) => ReadonlyMap (predicate: Predicate): (fb: ReadonlyMap) => ReadonlyMap -} = (predicate: Predicate) => (fb: ReadonlyMap) => _filter(fb, predicate) + (predicate: Predicate): (fa: ReadonlyMap) => ReadonlyMap +} = (predicate: Predicate) => (fa: ReadonlyMap) => _filter(fa, predicate) /** * @category Filterable @@ -698,7 +705,8 @@ export const partition: { (predicate: Predicate): ( fb: ReadonlyMap ) => Separated, ReadonlyMap> -} = (predicate: Predicate) => (fb: ReadonlyMap) => _partition(fb, predicate) + (predicate: Predicate): (fa: ReadonlyMap) => Separated, ReadonlyMap> +} = (predicate: Predicate) => (fa: ReadonlyMap) => _partition(fa, predicate) /** * @category Filterable diff --git a/src/ReadonlyNonEmptyArray.ts b/src/ReadonlyNonEmptyArray.ts index 939a86bfc..26de9b8b8 100644 --- a/src/ReadonlyNonEmptyArray.ts +++ b/src/ReadonlyNonEmptyArray.ts @@ -1313,6 +1313,9 @@ export function groupSort(O: Ord): (as: ReadonlyArray) => ReadonlyArray export function filter( refinement: Refinement ): (as: ReadonlyNonEmptyArray) => Option> +export function filter( + predicate: Predicate +): (bs: ReadonlyNonEmptyArray) => Option> export function filter(predicate: Predicate): (as: ReadonlyNonEmptyArray) => Option> export function filter(predicate: Predicate): (as: ReadonlyNonEmptyArray) => Option> { return filterWithIndex((_, a) => predicate(a)) diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 5186938e6..698f6a276 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -618,6 +618,9 @@ export function partitionMapWithIndex( export function partitionWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: ReadonlyRecord) => Separated, ReadonlyRecord> +export function partitionWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: ReadonlyRecord) => Separated, ReadonlyRecord> export function partitionWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: ReadonlyRecord) => Separated, ReadonlyRecord> @@ -671,6 +674,9 @@ export function filterMapWithIndex( export function filterWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: ReadonlyRecord) => ReadonlyRecord +export function filterWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: ReadonlyRecord) => ReadonlyRecord export function filterWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: ReadonlyRecord) => ReadonlyRecord @@ -1058,8 +1064,9 @@ const _traverseWithIndex = (O: Ord) => ( export const filter: { (refinement: Refinement): (fa: ReadonlyRecord) => ReadonlyRecord (predicate: Predicate): (fb: ReadonlyRecord) => ReadonlyRecord -} = (predicate: Predicate): ((fb: ReadonlyRecord) => ReadonlyRecord) => - filterWithIndex((_, b) => predicate(b)) + (predicate: Predicate): (fa: ReadonlyRecord) => ReadonlyRecord +} = (predicate: Predicate): ((fa: ReadonlyRecord) => ReadonlyRecord) => + filterWithIndex((_, a) => predicate(a)) /** * @category Filterable @@ -1080,10 +1087,13 @@ export const partition: { (predicate: Predicate): ( fb: ReadonlyRecord ) => Separated, ReadonlyRecord> + (predicate: Predicate): ( + fa: ReadonlyRecord + ) => Separated, ReadonlyRecord> } = ( predicate: Predicate -): ((fb: ReadonlyRecord) => Separated, ReadonlyRecord>) => - partitionWithIndex((_, b) => predicate(b)) +): ((fa: ReadonlyRecord) => Separated, ReadonlyRecord>) => + partitionWithIndex((_, a) => predicate(a)) /** * @category Filterable diff --git a/src/ReadonlySet.ts b/src/ReadonlySet.ts index 9dde8db64..256b4097d 100644 --- a/src/ReadonlySet.ts +++ b/src/ReadonlySet.ts @@ -117,16 +117,17 @@ export function chain(E: Eq): (f: (x: A) => ReadonlySet) => (set: Re */ export function filter(refinement: Refinement): (set: ReadonlySet) => ReadonlySet export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet -export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet { - return (set: ReadonlySet) => { +export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet +export function filter(predicate: Predicate): (set: ReadonlySet) => ReadonlySet { + return (set: ReadonlySet) => { const values = set.values() - let e: Next - const r = new Set() + let e: Next + const r = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { - const value = e.value - if (predicate(value)) { - r.add(value) + const a = e.value + if (predicate(a)) { + r.add(a) } } return r @@ -144,19 +145,22 @@ export function partition( ): (set: ReadonlySet) => Separated, ReadonlySet> export function partition( predicate: Predicate -): (set: ReadonlySet) => Separated, ReadonlySet> { - return (set: ReadonlySet) => { +): (set: ReadonlySet) => Separated, ReadonlySet> +export function partition( + predicate: Predicate +): (set: ReadonlySet) => Separated, ReadonlySet> { + return (set: ReadonlySet) => { const values = set.values() - let e: Next - const right = new Set() - const left = new Set() + let e: Next + const right = new Set() + const left = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { - const value = e.value - if (predicate(value)) { - right.add(value) + const a = e.value + if (predicate(a)) { + right.add(a) } else { - left.add(value) + left.add(a) } } return separated(left, right) diff --git a/src/Record.ts b/src/Record.ts index 7093f0c04..03efa8861 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -416,6 +416,9 @@ export const partitionMapWithIndex: ( export function partitionWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: Record) => Separated, Record> +export function partitionWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: Record) => Separated, Record> export function partitionWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: Record) => Separated, Record> @@ -438,6 +441,9 @@ export const filterMapWithIndex: ( export function filterWithIndex( refinementWithIndex: RefinementWithIndex ): (fa: Record) => Record +export function filterWithIndex( + predicateWithIndex: PredicateWithIndex +): (fb: Record) => Record export function filterWithIndex( predicateWithIndex: PredicateWithIndex ): (fa: Record) => Record @@ -651,6 +657,7 @@ const _traverseWithIndex = (O: Ord) => ( export const filter: { (refinement: Refinement): (fa: Record) => Record (predicate: Predicate): (fb: Record) => Record + (predicate: Predicate): (fa: Record) => Record } = RR.filter /** @@ -668,6 +675,7 @@ export const partition: { fa: Record ) => Separated, Record> (predicate: Predicate): (fb: Record) => Separated, Record> + (predicate: Predicate): (fa: Record) => Separated, Record> } = RR.partition /** diff --git a/src/Set.ts b/src/Set.ts index 0a8977003..9b5179f71 100644 --- a/src/Set.ts +++ b/src/Set.ts @@ -80,16 +80,17 @@ interface Next { */ export function filter(refinement: Refinement): (set: Set) => Set export function filter(predicate: Predicate): (set: Set) => Set -export function filter(predicate: Predicate): (set: Set) => Set { - return (set: Set) => { +export function filter(predicate: Predicate): (set: Set) => Set +export function filter(predicate: Predicate): (set: Set) => Set { + return (set: Set) => { const values = set.values() - let e: Next - const r = new Set() + let e: Next + const r = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { - const value = e.value - if (predicate(value)) { - r.add(value) + const a = e.value + if (predicate(a)) { + r.add(a) } } return r @@ -101,19 +102,20 @@ export function filter(predicate: Predicate): (set: Set) = */ export function partition(refinement: Refinement): (set: Set) => Separated, Set> export function partition(predicate: Predicate): (set: Set) => Separated, Set> -export function partition(predicate: Predicate): (set: Set) => Separated, Set> { - return (set: Set) => { +export function partition(predicate: Predicate): (set: Set) => Separated, Set> +export function partition(predicate: Predicate): (set: Set) => Separated, Set> { + return (set: Set) => { const values = set.values() - let e: Next - const right = new Set() - const left = new Set() + let e: Next + const right = new Set() + const left = new Set() // tslint:disable-next-line: strict-boolean-expressions while (!(e = values.next()).done) { - const value = e.value - if (predicate(value)) { - right.add(value) + const a = e.value + if (predicate(a)) { + right.add(a) } else { - left.add(value) + left.add(a) } } return separated(left, right) diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index fdf67115c..914ab6457 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -939,6 +939,7 @@ export const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): ( a: A ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => StateReaderTaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => StateReaderTaskEither } = /*#__PURE__*/ @@ -955,6 +956,9 @@ export const filterOrElse: { (predicate: Predicate, onFalse: (a: A) => E): ( mb: StateReaderTaskEither ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E): ( + ma: StateReaderTaskEither + ) => StateReaderTaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -972,6 +976,9 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: StateReaderTaskEither ) => StateReaderTaskEither + (predicate: Predicate, onFalse: (a: A) => E2): ( + ma: StateReaderTaskEither + ) => StateReaderTaskEither } = filterOrElse /** diff --git a/src/TaskEither.ts b/src/TaskEither.ts index b59c87426..4a1243d7d 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -941,6 +941,7 @@ export const chainEitherKW: ( */ export const fromPredicate: { (refinement: Refinement, onFalse: (a: A) => E): (a: A) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (b: B) => TaskEither (predicate: Predicate, onFalse: (a: A) => E): (a: A) => TaskEither } = /*#__PURE__*/ @@ -953,6 +954,7 @@ export const fromPredicate: { export const filterOrElse: { (refinement: Refinement, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither (predicate: Predicate, onFalse: (a: A) => E): (mb: TaskEither) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E): (ma: TaskEither) => TaskEither } = /*#__PURE__*/ filterOrElse_(FromEither, Chain) @@ -970,6 +972,7 @@ export const filterOrElseW: { (predicate: Predicate, onFalse: (a: A) => E2): ( mb: TaskEither ) => TaskEither + (predicate: Predicate, onFalse: (a: A) => E2): (ma: TaskEither) => TaskEither } = filterOrElse /** diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 0d5eb0fcb..2e528e584 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -74,6 +74,7 @@ export const some: (a: A) => TaskOption = export const fromPredicate: { (refinement: Refinement): (a: A) => TaskOption (predicate: Predicate): (b: B) => TaskOption + (predicate: Predicate): (a: A) => TaskOption } = /*#__PURE__*/ OT.fromPredicate(T.Pointed) @@ -374,6 +375,7 @@ export const separate: Compactable1['separate'] = export const filter: { (refinement: Refinement): (fb: TaskOption) => TaskOption (predicate: Predicate): (fb: TaskOption) => TaskOption + (predicate: Predicate): (fa: TaskOption) => TaskOption } = /*#__PURE__*/ filter_(T.Functor, O.Filterable) @@ -393,6 +395,7 @@ export const filterMap: (f: (a: A) => Option) => (fga: TaskOption) = export const partition: { (refinement: Refinement): (fb: TaskOption) => Separated, TaskOption> (predicate: Predicate): (fb: TaskOption) => Separated, TaskOption> + (predicate: Predicate): (fa: TaskOption) => Separated, TaskOption> } = /*#__PURE__*/ partition_(T.Functor, O.Filterable) diff --git a/test/NonEmptyArray.ts b/test/NonEmptyArray.ts index 11cf38405..fc165c9ca 100644 --- a/test/NonEmptyArray.ts +++ b/test/NonEmptyArray.ts @@ -275,16 +275,17 @@ describe('NonEmptyArray', () => { const a1 = make(1) const a2 = make(1) const a3 = make(2) + const as: _.NonEmptyArray<{ readonly x: number }> = [a1, a2, a3] U.deepStrictEqual( pipe( - [a1, a2, a3], + as, _.filter(({ x }) => x !== 1) ), O.some([a3]) ) U.deepStrictEqual( pipe( - [a1, a2, a3], + as, _.filter(({ x }) => x !== 2) ), O.some([a1, a2]) @@ -297,7 +298,7 @@ describe('NonEmptyArray', () => { ) U.deepStrictEqual( pipe( - [a1, a2, a3], + as, _.filter(({ x }) => x !== 10) ), O.some([a1, a2, a3]) From 7d1887de8b704b6dc86a29964c33f6ca8decb41a Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 10:19:59 +0200 Subject: [PATCH 154/162] string: split should return a ReadonlyNonEmptyArray --- CHANGELOG.md | 3 +++ docs/modules/string.ts.md | 2 +- src/string.ts | 6 +++++- test/string.ts | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fc5a91f6..01df6a768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ high state of flux, you're at risk of it changing without notice. # 2.11.0-rc.2 +- `string` + - `split` nor returns a `ReadonlyNonEmptyArray` + Some signature changes in `2.11.0-rc.1` are causing type inference issues: - `Array` / `ReadonlyArray` diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index 4acd73300..a2b824510 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -207,7 +207,7 @@ Added in v2.11.0 **Signature** ```ts -export declare const split: (separator: string | RegExp) => (s: string) => ReadonlyArray +export declare const split: (separator: string | RegExp) => (s: string) => ReadonlyNonEmptyArray ``` Added in v2.11.0 diff --git a/src/string.ts b/src/string.ts index 361cebade..d3e550cae 100644 --- a/src/string.ts +++ b/src/string.ts @@ -7,6 +7,7 @@ import * as S from './Semigroup' import * as O from './Ord' import * as Sh from './Show' import { Refinement } from './Refinement' +import { ReadonlyNonEmptyArray, isNonEmpty } from './ReadonlyNonEmptyArray' // ------------------------------------------------------------------------------------- // instances @@ -124,7 +125,10 @@ export const replace = (searchValue: string | RegExp, replaceValue: string) => ( /** * @since 2.11.0 */ -export const split = (separator: string | RegExp) => (s: string): ReadonlyArray => s.split(separator) +export const split = (separator: string | RegExp) => (s: string): ReadonlyNonEmptyArray => { + const out = s.split(separator) + return isNonEmpty(out) ? out : [s] +} /** * @since 2.11.0 diff --git a/test/string.ts b/test/string.ts index dd2e3d1e8..3609bb1a4 100644 --- a/test/string.ts +++ b/test/string.ts @@ -45,6 +45,7 @@ describe('string', () => { it('split', () => { U.deepStrictEqual(pipe('abc', _.split('')), ['a', 'b', 'c']) + U.deepStrictEqual(pipe('', _.split('')), ['']) }) it('trim', () => { From dfe7db525027a301beb8be058d0befadeada5716 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 12:06:47 +0200 Subject: [PATCH 155/162] TaskOption: add missing `FromEither` instance --- CHANGELOG.md | 42 +++++++++++++++++------------------ docs/modules/TaskOption.ts.md | 11 +++++++++ src/TaskOption.ts | 9 ++++++++ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01df6a768..e1db7ccdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,27 +19,27 @@ high state of flux, you're at risk of it changing without notice. - `string` - `split` nor returns a `ReadonlyNonEmptyArray` - -Some signature changes in `2.11.0-rc.1` are causing type inference issues: - -- `Array` / `ReadonlyArray` - - revert `isOutOfBound` signature change - - revert `isEmpty` signature change - - revert `size` signature change -- `Either` - - revert `exists` signature change - - revert `elem` signature change -- `These` - - revert `exists` signature change - - revert `elem` signature change -- `NonEmptyArray` / `ReadonlyNonEmptyArray` - - revert `isOutOfBound` signature change -- `Set` / `ReadonlySet` - - revert `isEmpty` signature change - - revert `size` signature change -- `Map` / `ReadonlyMap` - - revert `isEmpty` signature change - - revert `size` signature change +- `TaskOption` + - add missing `FromEither` instance +- some signature changes in `2.11.0-rc.1` caused type inference issues + - `Array` / `ReadonlyArray` + - revert `isOutOfBound` signature change + - revert `isEmpty` signature change + - revert `size` signature change + - `Either` + - revert `exists` signature change + - revert `elem` signature change + - `These` + - revert `exists` signature change + - revert `elem` signature change + - `NonEmptyArray` / `ReadonlyNonEmptyArray` + - revert `isOutOfBound` signature change + - `Set` / `ReadonlySet` + - revert `isEmpty` signature change + - revert `size` signature change + - `Map` / `ReadonlyMap` + - revert `isEmpty` signature change + - revert `size` signature change # 2.11.0-rc.1 diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 5087e696f..738f0a35b 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -71,6 +71,7 @@ Added in v2.10.0 - [Chain](#chain) - [Compactable](#compactable-1) - [Filterable](#filterable-1) + - [FromEither](#fromeither) - [FromIO](#fromio) - [FromTask](#fromtask) - [Functor](#functor-1) @@ -667,6 +668,16 @@ export declare const Filterable: Filterable1<'TaskOption'> Added in v2.10.0 +## FromEither + +**Signature** + +```ts +export declare const FromEither: FromEither1<'TaskOption'> +``` + +Added in v2.11.0 + ## FromIO **Signature** diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 2e528e584..c528b7017 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -710,6 +710,15 @@ export const chainFirstIOK = /*#__PURE__*/ chainFirstIOK_(FromIO, Chain) +/** + * @category instances + * @since 2.11.0 + */ +export const FromEither: FromEither1 = { + URI, + fromEither +} + /** * @category instances * @since 2.10.0 From 5f0428e7253ade6ec69deb4e882d15a534888206 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 14 May 2021 12:15:25 +0200 Subject: [PATCH 156/162] typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1db7ccdd..df8f14024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ high state of flux, you're at risk of it changing without notice. # 2.11.0-rc.2 - `string` - - `split` nor returns a `ReadonlyNonEmptyArray` + - `split` now returns a `ReadonlyNonEmptyArray` - `TaskOption` - add missing `FromEither` instance - some signature changes in `2.11.0-rc.1` caused type inference issues From c4a237047731a03a3732e0d62dc2844fa1dc24c1 Mon Sep 17 00:00:00 2001 From: gcanti Date: Sat, 15 May 2021 09:07:48 +0200 Subject: [PATCH 157/162] string: add examples --- docs/modules/string.ts.md | 288 +++++++++++++++++++++++++++++--------- src/string.ts | 175 +++++++++++++++++++---- 2 files changed, 372 insertions(+), 91 deletions(-) diff --git a/docs/modules/string.ts.md b/docs/modules/string.ts.md index a2b824510..dab82a8ae 100644 --- a/docs/modules/string.ts.md +++ b/docs/modules/string.ts.md @@ -12,6 +12,14 @@ Added in v2.10.0

Table of contents

+- [combinators](#combinators) + - [replace](#replace) + - [slice](#slice) + - [toLowerCase](#tolowercase) + - [toUpperCase](#touppercase) + - [trim](#trim) + - [trimLeft](#trimleft) + - [trimRight](#trimright) - [instances](#instances) - [Eq](#eq) - [Monoid](#monoid) @@ -25,19 +33,147 @@ Added in v2.10.0 - [endsWith](#endswith) - [includes](#includes) - [isEmpty](#isempty) - - [replace](#replace) - [size](#size) - - [slice](#slice) - [split](#split) - [startsWith](#startswith) - - [toLowerCase](#tolowercase) - - [toUpperCase](#touppercase) - - [trim](#trim) - - [trimLeft](#trimleft) - - [trimRight](#trimright) --- +# combinators + +## replace + +**Signature** + +```ts +export declare const replace: (searchValue: string | RegExp, replaceValue: string) => (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('abc', S.replace('b', 'd')), 'adc') +``` + +Added in v2.11.0 + +## slice + +**Signature** + +```ts +export declare const slice: (start: number, end: number) => (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('abcd', S.slice(1, 3)), 'bc') +``` + +Added in v2.11.0 + +## toLowerCase + +**Signature** + +```ts +export declare const toLowerCase: (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('A', S.toLowerCase), 'a') +``` + +Added in v2.11.0 + +## toUpperCase + +**Signature** + +```ts +export declare const toUpperCase: (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('a', S.toUpperCase), 'A') +``` + +Added in v2.11.0 + +## trim + +**Signature** + +```ts +export declare const trim: (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe(' a ', S.trim), 'a') +``` + +Added in v2.11.0 + +## trimLeft + +**Signature** + +```ts +export declare const trimLeft: (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe(' a ', S.trimLeft), 'a ') +``` + +Added in v2.11.0 + +## trimRight + +**Signature** + +```ts +export declare const trimRight: (s: string) => string +``` + +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe(' a ', S.trimRight), ' a') +``` + +Added in v2.11.0 + # instances ## Eq @@ -48,6 +184,15 @@ Added in v2.10.0 export declare const Eq: E.Eq ``` +**Example** + +```ts +import * as S from 'fp-ts/string' + +assert.deepStrictEqual(S.Eq.equals('a', 'a'), true) +assert.deepStrictEqual(S.Eq.equals('a', 'b'), false) +``` + Added in v2.10.0 ## Monoid @@ -68,6 +213,7 @@ export declare const Monoid: M.Monoid import * as S from 'fp-ts/string' assert.deepStrictEqual(S.Monoid.concat('a', 'b'), 'ab') +assert.deepStrictEqual(S.Monoid.concat('a', S.Monoid.empty), 'a') ``` Added in v2.10.0 @@ -80,6 +226,16 @@ Added in v2.10.0 export declare const Ord: O.Ord ``` +**Example** + +```ts +import * as S from 'fp-ts/string' + +assert.deepStrictEqual(S.Ord.compare('a', 'a'), 0) +assert.deepStrictEqual(S.Ord.compare('a', 'b'), -1) +assert.deepStrictEqual(S.Ord.compare('b', 'a'), 1) +``` + Added in v2.10.0 ## Semigroup @@ -110,6 +266,14 @@ Added in v2.10.0 export declare const Show: Sh.Show ``` +**Example** + +```ts +import * as S from 'fp-ts/string' + +assert.deepStrictEqual(S.Show.show('a'), '"a"') +``` + Added in v2.10.0 # refinements @@ -122,6 +286,15 @@ Added in v2.10.0 export declare const isString: Refinement ``` +**Example** + +```ts +import * as S from 'fp-ts/string' + +assert.deepStrictEqual(S.isString('a'), true) +assert.deepStrictEqual(S.isString(1), false) +``` + Added in v2.11.0 # utils @@ -146,6 +319,16 @@ Added in v2.10.0 export declare const endsWith: (searchString: string, position?: number | undefined) => (s: string) => boolean ``` +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('abc', S.endsWith('c')), true) +assert.deepStrictEqual(pipe('ab', S.endsWith('c')), false) +``` + Added in v2.11.0 ## includes @@ -156,6 +339,16 @@ Added in v2.11.0 export declare const includes: (searchString: string, position?: number | undefined) => (s: string) => boolean ``` +**Example** + +```ts +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('abc', S.includes('b')), true) +assert.deepStrictEqual(pipe('abc', S.includes('d')), false) +``` + Added in v2.11.0 ## isEmpty @@ -168,17 +361,17 @@ Test whether a `string` is empty. export declare const isEmpty: (s: string) => boolean ``` -Added in v2.10.0 - -## replace - -**Signature** +**Example** ```ts -export declare const replace: (searchValue: string | RegExp, replaceValue: string) => (s: string) => string +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('', S.isEmpty), true) +assert.deepStrictEqual(pipe('a', S.isEmpty), false) ``` -Added in v2.11.0 +Added in v2.10.0 ## size @@ -190,17 +383,16 @@ Calculate the number of characters in a `string`. export declare const size: (s: string) => number ``` -Added in v2.10.0 - -## slice - -**Signature** +**Example** ```ts -export declare const slice: (start: number, end: number) => (s: string) => string +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' + +assert.deepStrictEqual(pipe('abc', S.size), 3) ``` -Added in v2.11.0 +Added in v2.10.0 ## split @@ -210,64 +402,34 @@ Added in v2.11.0 export declare const split: (separator: string | RegExp) => (s: string) => ReadonlyNonEmptyArray ``` -Added in v2.11.0 - -## startsWith - -**Signature** - -```ts -export declare const startsWith: (searchString: string, position?: number | undefined) => (s: string) => boolean -``` - -Added in v2.11.0 - -## toLowerCase - -**Signature** +**Example** ```ts -export declare const toLowerCase: (s: string) => string -``` - -Added in v2.11.0 - -## toUpperCase - -**Signature** +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' -```ts -export declare const toUpperCase: (s: string) => string +assert.deepStrictEqual(pipe('abc', S.split('')), ['a', 'b', 'c']) +assert.deepStrictEqual(pipe('', S.split('')), ['']) ``` Added in v2.11.0 -## trim +## startsWith **Signature** ```ts -export declare const trim: (s: string) => string +export declare const startsWith: (searchString: string, position?: number | undefined) => (s: string) => boolean ``` -Added in v2.11.0 - -## trimLeft - -**Signature** +**Example** ```ts -export declare const trimLeft: (s: string) => string -``` - -Added in v2.11.0 - -## trimRight - -**Signature** +import * as S from 'fp-ts/string' +import { pipe } from 'fp-ts/function' -```ts -export declare const trimRight: (s: string) => string +assert.deepStrictEqual(pipe('abc', S.startsWith('a')), true) +assert.deepStrictEqual(pipe('bc', S.startsWith('a')), false) ``` Added in v2.11.0 diff --git a/src/string.ts b/src/string.ts index d3e550cae..14196a414 100644 --- a/src/string.ts +++ b/src/string.ts @@ -14,6 +14,12 @@ import { ReadonlyNonEmptyArray, isNonEmpty } from './ReadonlyNonEmptyArray' // ------------------------------------------------------------------------------------- /** + * @example + * import * as S from 'fp-ts/string' + * + * assert.deepStrictEqual(S.Eq.equals('a', 'a'), true) + * assert.deepStrictEqual(S.Eq.equals('a', 'b'), false) + * * @category instances * @since 2.10.0 */ @@ -45,6 +51,7 @@ export const Semigroup: S.Semigroup = { * import * as S from 'fp-ts/string' * * assert.deepStrictEqual(S.Monoid.concat('a', 'b'), 'ab') + * assert.deepStrictEqual(S.Monoid.concat('a', S.Monoid.empty), 'a') * * @category instances * @since 2.10.0 @@ -55,6 +62,13 @@ export const Monoid: M.Monoid = { } /** + * @example + * import * as S from 'fp-ts/string' + * + * assert.deepStrictEqual(S.Ord.compare('a', 'a'), 0) + * assert.deepStrictEqual(S.Ord.compare('a', 'b'), -1) + * assert.deepStrictEqual(S.Ord.compare('b', 'a'), 1) + * * @category instances * @since 2.10.0 */ @@ -64,6 +78,11 @@ export const Ord: O.Ord = { } /** + * @example + * import * as S from 'fp-ts/string' + * + * assert.deepStrictEqual(S.Show.show('a'), '"a"') + * * @category instances * @since 2.10.0 */ @@ -76,94 +95,194 @@ export const Show: Sh.Show = { // ------------------------------------------------------------------------------------- /** + * @example + * import * as S from 'fp-ts/string' + * + * assert.deepStrictEqual(S.isString('a'), true) + * assert.deepStrictEqual(S.isString(1), false) + * * @category refinements * @since 2.11.0 */ export const isString: Refinement = (u: unknown): u is string => typeof u === 'string' // ------------------------------------------------------------------------------------- -// utils +// combinators // ------------------------------------------------------------------------------------- /** - * An empty `string`. + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' * - * @since 2.10.0 + * assert.deepStrictEqual(pipe('a', S.toUpperCase), 'A') + * + * @category combinators + * @since 2.11.0 */ -export const empty: string = '' +export const toUpperCase = (s: string): string => s.toUpperCase() /** - * Test whether a `string` is empty. + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' * - * @since 2.10.0 + * assert.deepStrictEqual(pipe('A', S.toLowerCase), 'a') + * + * @category combinators + * @since 2.11.0 */ -export const isEmpty = (s: string): boolean => s.length === 0 +export const toLowerCase = (s: string): string => s.toLowerCase() /** - * Calculate the number of characters in a `string`. + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' * - * @since 2.10.0 + * assert.deepStrictEqual(pipe('abc', S.replace('b', 'd')), 'adc') + * + * @category combinators + * @since 2.11.0 */ -export const size = (s: string): number => s.length +export const replace = (searchValue: string | RegExp, replaceValue: string) => (s: string): string => + s.replace(searchValue, replaceValue) /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe(' a ', S.trim), 'a') + * + * @category combinators * @since 2.11.0 */ -export const toUpperCase = (s: string): string => s.toUpperCase() +export const trim = (s: string): string => s.trim() /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe(' a ', S.trimLeft), 'a ') + * + * @category combinators * @since 2.11.0 */ -export const toLowerCase = (s: string): string => s.toLowerCase() +export const trimLeft = (s: string): string => s.trimLeft() /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe(' a ', S.trimRight), ' a') + * + * @category combinators * @since 2.11.0 */ -export const replace = (searchValue: string | RegExp, replaceValue: string) => (s: string): string => - s.replace(searchValue, replaceValue) +export const trimRight = (s: string): string => s.trimRight() /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abcd', S.slice(1, 3)), 'bc') + * + * @category combinators * @since 2.11.0 */ -export const split = (separator: string | RegExp) => (s: string): ReadonlyNonEmptyArray => { - const out = s.split(separator) - return isNonEmpty(out) ? out : [s] -} +export const slice = (start: number, end: number) => (s: string): string => s.slice(start, end) + +// ------------------------------------------------------------------------------------- +// utils +// ------------------------------------------------------------------------------------- /** - * @since 2.11.0 + * An empty `string`. + * + * @since 2.10.0 */ -export const trim = (s: string): string => s.trim() +export const empty: string = '' /** - * @since 2.11.0 + * Test whether a `string` is empty. + * + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('', S.isEmpty), true) + * assert.deepStrictEqual(pipe('a', S.isEmpty), false) + * + * @since 2.10.0 */ -export const trimLeft = (s: string): string => s.trimLeft() +export const isEmpty = (s: string): boolean => s.length === 0 /** + * Calculate the number of characters in a `string`. + * + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abc', S.size), 3) + * + * @since 2.10.0 + */ +export const size = (s: string): number => s.length + +/** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abc', S.split('')), ['a', 'b', 'c']) + * assert.deepStrictEqual(pipe('', S.split('')), ['']) + * * @since 2.11.0 */ -export const trimRight = (s: string): string => s.trimRight() +export const split = (separator: string | RegExp) => (s: string): ReadonlyNonEmptyArray => { + const out = s.split(separator) + return isNonEmpty(out) ? out : [s] +} /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abc', S.includes('b')), true) + * assert.deepStrictEqual(pipe('abc', S.includes('d')), false) + * * @since 2.11.0 */ export const includes = (searchString: string, position?: number) => (s: string): boolean => s.includes(searchString, position) /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abc', S.startsWith('a')), true) + * assert.deepStrictEqual(pipe('bc', S.startsWith('a')), false) + * * @since 2.11.0 */ export const startsWith = (searchString: string, position?: number) => (s: string): boolean => s.startsWith(searchString, position) /** + * @example + * import * as S from 'fp-ts/string' + * import { pipe } from 'fp-ts/function' + * + * assert.deepStrictEqual(pipe('abc', S.endsWith('c')), true) + * assert.deepStrictEqual(pipe('ab', S.endsWith('c')), false) + * * @since 2.11.0 */ export const endsWith = (searchString: string, position?: number) => (s: string): boolean => s.endsWith(searchString, position) - -/** - * @since 2.11.0 - */ -export const slice = (start: number, end: number) => (s: string): string => s.slice(start, end) From dfc276f40f265236f9fdc34451ac001ef0fe73b7 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 11 May 2021 16:51:29 +0200 Subject: [PATCH 158/162] update fpchat link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e8e4b42c9..1ea45fca7 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Make sure to always have a single version of `fp-ts` installed in your project. If you need help with `fp-ts` check out: - this [Discord server](https://discord.gg/HVWmBBXM8A) -- the `#fp-ts` channel on [FP slack](https://fpchat-invite.herokuapp.com/) . +- the `#fp-ts` channel on [FP slack](functionalprogramming.slack.com) . # Development From 00930c7a48b750dbde1f3a6924d31c82095689bd Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Thu, 3 Jun 2021 11:54:11 +0200 Subject: [PATCH 159/162] Enhance ecosystem --- docs/ecosystem.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/ecosystem.md b/docs/ecosystem.md index e2bc6da3c..45ae5b9d1 100644 --- a/docs/ecosystem.md +++ b/docs/ecosystem.md @@ -8,6 +8,11 @@ has_toc: false # Ecosystem +## Tooling + +- [create-fp-ts-lib](https://github.com/no-day/create-fp-ts-lib) - Bootstrap libraries that follow common fp-ts coding, documentation and testing patterns +- [docs-ts](https://github.com/gcanti/docs-ts) - Documentation generator used by fp-ts and many fp-ts libraries + ## Libraries - [fp-ts-contrib](https://github.com/gcanti/fp-ts-contrib) - A community driven utility package for fp-ts @@ -37,6 +42,8 @@ has_toc: false - [fp-ts-graph](https://github.com/no-day/fp-ts-graph) - Immutable, functional graph data structure - [fp-ts-bigint](https://github.com/ericcrosson/fp-ts-bigint) - Opt-in BigInt functions - [fp-ts-generators](https://github.com/no-day/fp-ts-generators) - Seeded pseudorandom generators for structured data +- [fp-ts-sized-vectors](https://github.com/no-day/fp-ts-sized-vectors) - Fixed size generic vector type carrying its length at the typelevel +- [fp-ts-number-instances](https://github.com/no-day/fp-ts-number-instances) - Not fully law abiding instances for the number type ## Bindings From d980779176b1b549830466752222043f0e8e21b6 Mon Sep 17 00:00:00 2001 From: Dawid Szlachta Date: Fri, 4 Jun 2021 18:03:18 +0200 Subject: [PATCH 160/162] Added example for Array.unfold --- docs/modules/Array.ts.md | 15 +++++++++++++++ src/Array.ts | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index acab64c0d..003085f7a 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -558,12 +558,27 @@ Added in v2.6.3 ## unfold +Creates an `Array` from the results of `f(b)`, where `b` is an initial value. +`unfold` stops when `f` returns `Option.none`. + **Signature** ```ts export declare const unfold: (b: B, f: (b: B) => Option) => A[] ``` +**Example** + +```ts +import { unfold } from 'fp-ts/Array' +import { some, none } from 'fp-ts/Option' + +assert.deepStrictEqual( + unfold(5, (n) => (n > 0 ? some([n, n - 1]) : none)), + [5, 4, 3, 2, 1] +) +``` + Added in v2.6.6 # Witherable diff --git a/src/Array.ts b/src/Array.ts index 63bf27ba1..f1269639c 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -1673,6 +1673,17 @@ export const wilt: PipeableWilt1 = ( } /** + * Creates an `Array` from the results of `f(b)`, where `b` is an initial value. + * `unfold` stops when `f` returns `Option.none`. + * @example + * import { unfold } from 'fp-ts/Array' + * import { some, none } from 'fp-ts/Option' + * + * assert.deepStrictEqual( + * unfold(5, (n) => (n > 0 ? some([n, n - 1]) : none)), + * [5, 4, 3, 2, 1] + * ) + * * @category Unfoldable * @since 2.6.6 */ From 781030dcd89459ba64f1194896edb8cc6df25af0 Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 22 Jul 2021 12:11:53 +0200 Subject: [PATCH 161/162] update docs --- docs/modules/Reader.ts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 81ba06606..934796906 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -202,7 +202,7 @@ Added in v2.0.0 **Signature** ```ts -export declare const promap: (f: (d: D) => E, g: (a: A) => B) => (fbc: Reader) => Reader +export declare const promap: (f: (d: D) => E, g: (a: A) => B) => (fea: Reader) => Reader ``` Added in v2.0.0 From c6839aaed97c92493849e322b198638e2261671c Mon Sep 17 00:00:00 2001 From: gcanti Date: Thu, 22 Jul 2021 12:16:21 +0200 Subject: [PATCH 162/162] version 2.11 --- CHANGELOG.md | 331 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- scripts/release.ts | 2 +- 3 files changed, 333 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 134965078..7efe269fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,337 @@ **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. +# 2.11.0 + +- **Deprecation** + - `Array` + - deprecate `range`, use `NonEmptyArray` module instead. + - `function` + - deprecate `Endomorphism`, use `Endomorphism` module instead. + - deprecate `getEndomorphismMonoid`, use `Endomorphism` module instead. + - deprecate `Predicate`, use `Predicate` module instead. + - deprecate `not`, use `Predicate` module instead. + - deprecate `Refinement`, use `Refinement` module instead. + - `Monoid` + - deprecate `monoidVoid`, use `void` module instead. + - `NonEmptyArray` + - deprecate `groupSort` (it's just `sort` followed by `group`) + - `Option` + - deprecate `getRefinement`, use `Refinement` module instead. + - deprecate `getFirstMonoid`, use `getMonoid` module instead. + - deprecate `getLastMonoid`, use `getMonoid` module instead. + - `ReadonlyArray` + - deprecate `range`, use `ReadonlyNonEmptyArray` module instead. + - `ReadonlyNonEmptyArray` + - deprecate `groupSort` (it's just `sort` followed by `group`) + - `Record` / `ReadonlyRecord`: deprecate overloads without `Ord` constraint (@anthonyjoeseph): + - `collect` + - `reduce` + - `foldMap` + - `reduceRight` + - `reduceWithIndex` + - `foldMapWithIndex` + - `reduceRightWithIndex` + - `getShow` + - deprecate `Foldable` in favour of `getFoldable` (@anthonyjoeseph) + - deprecate `FoldableWithIndex` in favour of `getFoldableWithIndex` (@anthonyjoeseph) + - deprecate `Traversable` in favour of `getTraversable` (@anthonyjoeseph) + - deprecate `TraversableWithIndex` in favour of `getTraversableWithIndex` (@anthonyjoeseph) + - deprecate `Witherable` in favour of `getWitherable` (@anthonyjoeseph) + - `Semigroup` + - deprecate `semigroupVoid`, use `void` module instead. +- **New Feature** + - add `Endomorphism` module + - add `Predicate` module + - add `Refinement` module + - add `FromState` module + - add `FromThese` module + - add `void` module + - add `FromReader` module + - add `NaturalTransformation` module + - add `Zero` module + - `Alt` + - add `altAll` + - `Alternative` + - add `altAll` + - `Array` + - add `prependW`, `appendW` (@thewilkybarkid) + - add `fromOption`, `fromPredicate` (@cdimitroulas) + - add `filterE` + - add `ChainRecDepthFirst` instance (@qlonik) + - add `chainRecDepthFirst` + - add `ChainRecBreadthFirst` instance (@qlonik) + - add `chainRecBreadthFirst` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` + - add `fromEither` + - add `FromEither` instance + - add `fromEitherK` + - make `isEmpty` a user defined guard + - add `concat` / `concatW` + - add `match`, `matchW`, `matchLeftW`, `matchRightW` + - add `fromOptionK` + - add `Zero` instance + - add `guard` constructor + - add `exists` alias + - `boolean` + - add `isBoolean` + - `Either` + - add `chainOptionK` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `EitherT` + - add `orElseFirst` + - add `orLeft` + - `function` + - add `SK` (@cdimitroulas) + - add `apply` + - `IO` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `IOEither` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `Magma` + - add `reverse` + - add `filterFirst` + - add `filterSecond` + - add `endo` + - add `concatAll` + - `Map` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` + - add `getFoldable` + - add `foldMap` + - add `reduceRight` + - add `reduceWithIndex` + - add `foldMapWithIndex` + - add `reduceRightWithIndex` + - `NonEmptyArray` + - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - add `union` + - add `getUnionSemigroup` + - add `makeBy` + - add `range` + - make `concat` pipeable + - `number` + - add `MagmaSub` + - add `isNumber` + - `string` + - add `isString` + - `Option` + - add `FromEither` instance + - add `fromEitherK` + - add `chainEitherK` + - add `Zero` instance + - add `guard` constructor + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `Ord` + - add `trivial` instance + - add `equals` + - `Reader` + - add `asksReaderW`, `asksReader` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `ReaderEither` + - add `asksReaderEitherW`, `asksReaderEither` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `ReaderTask` + - add `asksReaderTaskW`, `asksReaderTask` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `ReaderTaskEither` + - add `asksReaderTaskEitherW`, `asksReaderTaskEither` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` + - add `fromReaderTaskK` + - add `fromReaderEitherK` + - add `chainReaderKW` + - add `chainReaderTaskK`, `chainReaderTaskKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `chainFirstReaderTaskK`, `chainFirstReaderTaskKW` + - add `chainReaderEitherK`, `chainReaderEitherKW` + - add `chainFirstReaderEitherK`, `chainFirstReaderEitherKW` + - add `chainFirstTaskEitherK`, `chainFirstTaskEitherKW` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `ReadonlyArray` + - add `prependW`, `appendW` (@thewilkybarkid) + - add `filterE` + - add `ChainRecDepthFirst` instance (@qlonik) + - add `chainRecDepthFirst` + - add `ChainRecBreadthFirst` instance (@qlonik) + - add `chainRecBreadthFirst` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` + - add `fromOption` + - add `fromPredicate` + - add `fromEither` + - add `FromEither` instance + - add `fromEitherK` + - make `isEmpty` a user defined guard + - add `concat` / `concatW` + - add `match`, `matchW`, `matchLeftW`, `matchRightW` + - add `fromOptionK` + - add `Zero` instance + - add `guard` constructor + - add `exists` alias + - `ReadonlyMap` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` + - add `reduce` + - add `foldMap` + - add `reduceRight` + - add `reduceWithIndex` + - add `foldMapWithIndex` + - add `reduceRightWithIndex` + - `ReadonlyNonEmptyArray` + - add `matchLeft`, `matchRight`, `modifyHead`, `modifyLast` (@cdimitroulas) + - add `union` + - add `getUnionSemigroup` + - add `makeBy` + - add `range` + - make `concat` pipeable + - `ReadonlyRecord` + - add `union` (@anthonyjoeseph) + - add `intersection` (@anthonyjoeseph) + - add `difference` (@anthonyjoeseph) + - add `getUnionSemigroup` (@anthonyjoeseph) + - add `getUnionMonoid` (@anthonyjoeseph) + - add `getIntersectionSemigroup` (@anthonyjoeseph) + - add `getDifferenceMagma` (@anthonyjoeseph) + - `ReadonlySet` + - add `getUnionSemigroup` + - add `getDifferenceMagma` + - `Record` + - add `union` + - add `intersection` + - add `difference` + - add `getUnionSemigroup` + - add `getUnionMonoid` + - add `getIntersectionSemigroup` + - add `getDifferenceMagma` + - `Set` + - add `getUnionSemigroup` + - add `getDifferenceMagma` + - `State` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `StateReaderTaskEither` + - add `fromStateK` + - add `chainStateK` + - add `local` + - add `asksStateReaderTaskEitherW`, `asksStateReaderTaskEither` + - add `chainReaderKW` + - add `chainFirstReaderK`, `chainFirstReaderKW` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `string` + - add `toUpperCase` + - add `toLowerCase` + - add `replace` + - add `split` + - add `trim` + - add `trimLeft` + - add `trimRight` + - add `includes` + - add `startsWith` + - add `endsWith` + - add `slice` + - `struct` + - add `evolve` + - `Task` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `TaskEither` + - add `fromTaskOption` (@thewilkybarkid) + - add `fromTaskOptionK` + - add `chainTaskOptionK` + - add `orElseFirst` / `orElseFirstW` + - add `orLeft` + - add `flattenW` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `TaskOption` + - add `fromTaskEither` (@thewilkybarkid) + - add `Zero` instance + - add `guard` constructor + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - add missing `FromEither` instance + - `TaskThese` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - add `traverseReadonlyNonEmptyArrayWithIndexSeq` + - add `traverseReadonlyArrayWithIndexSeq` + - `These` + - add `elem` + - add `exists` + - add `ApT` + - add `traverseReadonlyNonEmptyArrayWithIndex` + - add `traverseReadonlyArrayWithIndex` + - `Tree` + - add `exists` + - `Witherable` + - add `filterE`, #1458 (@vinassefranche) + - add `wiltDefault` + - add `witherDefault` +- **Polish** + - remove unnecessary type parameters + - `Either` + - `isLeft` + - `isRight` + - `Option` + - `isNone` + - `These` + - `isLeft` + - `isRight` + # 2.11.0-rc.2 - `string` diff --git a/package.json b/package.json index 9bb10b471..9ed77aa2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fp-ts", - "version": "2.11.0-rc.2", + "version": "2.11.0", "description": "Functional programming in TypeScript", "main": "lib/index.js", "module": "es6/index.js", diff --git a/scripts/release.ts b/scripts/release.ts index c9a177905..0554df72e 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -16,7 +16,7 @@ const exec = (cmd: string, args?: child_process.ExecOptions): TE.TaskEither