From 7165ee5fd40849557b4ff86e547ca93267d70d48 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 9 Jun 2020 22:54:05 +0200 Subject: [PATCH] remove some `any`s --- docs/modules/Either.ts.md | 6 +- docs/modules/IOEither.ts.md | 14 ++- docs/modules/Option.ts.md | 4 +- docs/modules/Reader.ts.md | 4 +- docs/modules/ReaderEither.ts.md | 50 +++++----- docs/modules/ReaderTaskEither.ts.md | 116 ++++++++++++---------- docs/modules/StateReaderTaskEither.ts.md | 58 ++++++----- docs/modules/TaskEither.ts.md | 32 +++--- src/Either.ts | 32 +++--- src/IOEither.ts | 50 +++++----- src/Option.ts | 18 ++-- src/Reader.ts | 19 ++-- src/ReaderEither.ts | 60 +++++------ src/ReaderTaskEither.ts | 121 ++++++++++++----------- src/StateReaderTaskEither.ts | 112 ++++++++++----------- src/TaskEither.ts | 66 +++++++------ 16 files changed, 416 insertions(+), 346 deletions(-) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 209fc1e05..1e42e8397 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -271,6 +271,8 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts @@ -546,13 +548,15 @@ Added in v2.0.0 **Signature** ```ts -export declare function getOrElse(onLeft: (e: E) => A): (ma: Either) => A +export declare const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A ``` Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index 77b88048d..010caae63 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -190,6 +190,8 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts @@ -236,19 +238,21 @@ Added in v2.0.0 **Signature** ```ts -export declare function chainEitherK(f: (a: A) => Either): (ma: IOEither) => IOEither +export declare const chainEitherK: (f: (a: A) => E.Either) => (ma: IOEither) => IOEither ``` Added in v2.4.0 ## chainEitherKW +Less strict version of [`chainEitherK`](#chainEitherK). + **Signature** ```ts -export declare const chainEitherKW: ( - f: (a: A) => E.Either -) => (ma: IOEither) => IOEither +export declare const chainEitherKW: ( + f: (a: A) => E.Either +) => (ma: IOEither) => IOEither ``` Added in v2.6.1 @@ -412,6 +416,8 @@ Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 0d6a4b9f6..f6595a456 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -634,7 +634,7 @@ Extracts the value out of the structure, if it exists. Otherwise returns the giv **Signature** ```ts -export declare function getOrElse(onNone: () => A): (ma: Option) => A +export declare const getOrElse: (onNone: () => A) => (ma: Option) => A ``` **Example** @@ -663,6 +663,8 @@ Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index d5137c0fe..4bbda597c 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -139,10 +139,12 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts -export declare const chainW: (f: (a: A) => Reader) => (ma: Reader) => Reader +export declare const chainW: (f: (a: A) => Reader) => (ma: Reader) => Reader ``` Added in v2.6.0 diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 9a94a2d86..29df30400 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -25,12 +25,12 @@ Added in v2.0.0 - [map](#map) - [Monad](#monad) - [chain](#chain) - - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) - [chainW](#chainw) - [flatten](#flatten) - [combinators](#combinators) - [chainEitherK](#chaineitherk) + - [chainEitherKW](#chaineitherkw) - [filterOrElse](#filterorelse) - [fromEitherK](#fromeitherk) - [local](#local) @@ -181,18 +181,6 @@ export declare const chain: ( Added in v2.0.0 -## chainEitherKW - -**Signature** - -```ts -export declare const chainEitherKW: ( - f: (a: A) => E.Either -) => (ma: ReaderEither) => ReaderEither -``` - -Added in v2.6.1 - ## chainFirst Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -210,12 +198,14 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts -export declare const chainW: ( - f: (a: A) => ReaderEither -) => (ma: ReaderEither) => ReaderEither +export declare const chainW: ( + f: (a: A) => ReaderEither +) => (ma: ReaderEither) => ReaderEither ``` Added in v2.6.0 @@ -237,13 +227,27 @@ Added in v2.0.0 **Signature** ```ts -export declare function chainEitherK( - f: (a: A) => Either -): (ma: ReaderEither) => ReaderEither +export declare const chainEitherK: ( + f: (a: A) => E.Either +) => (ma: ReaderEither) => ReaderEither ``` Added in v2.4.0 +## chainEitherKW + +Less strict version of [`chainEitherK`](#chainEitherK). + +**Signature** + +```ts +export declare const chainEitherKW: ( + f: (a: A) => E.Either +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.6.1 + ## filterOrElse **Signature** @@ -427,12 +431,14 @@ Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts -export declare const getOrElseW: ( - onLeft: (e: E) => R.Reader -) => (ma: ReaderEither) => R.Reader +export declare const getOrElseW: ( + onLeft: (e: E) => R.Reader +) => (ma: ReaderEither) => R.Reader ``` Added in v2.6.0 diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 19925f7db..069005c02 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -25,16 +25,16 @@ Added in v2.0.0 - [map](#map) - [Monad](#monad) - [chain](#chain) - - [chainEitherKW](#chaineitherkw) - [chainFirst](#chainfirst) - - [chainIOEitherKW](#chainioeitherkw) - - [chainTaskEitherKW](#chaintaskeitherkw) - [chainW](#chainw) - [flatten](#flatten) - [combinators](#combinators) - [chainEitherK](#chaineitherk) + - [chainEitherKW](#chaineitherkw) - [chainIOEitherK](#chainioeitherk) + - [chainIOEitherKW](#chainioeitherkw) - [chainTaskEitherK](#chaintaskeitherk) + - [chainTaskEitherKW](#chaintaskeitherkw) - [filterOrElse](#filterorelse) - [fromEitherK](#fromeitherk) - [fromIOEitherK](#fromioeitherk) @@ -205,18 +205,6 @@ export declare const chain: ( Added in v2.0.0 -## chainEitherKW - -**Signature** - -```ts -export declare const chainEitherKW: ( - f: (a: A) => Either -) => (ma: ReaderTaskEither) => ReaderTaskEither -``` - -Added in v2.6.1 - ## chainFirst Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -232,92 +220,112 @@ export declare const chainFirst: ( Added in v2.0.0 -## chainIOEitherKW +## chainW + +Less strict version of [`chain`](#chain). **Signature** ```ts -export declare const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: ReaderTaskEither) => ReaderTaskEither +export declare const chainW: ( + f: (a: A) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v2.6.1 +Added in v2.6.0 -## chainTaskEitherKW +## flatten **Signature** ```ts -export declare const chainTaskEitherKW: ( - f: (a: A) => TE.TaskEither -) => (ma: ReaderTaskEither) => ReaderTaskEither +export declare const flatten: ( + mma: ReaderTaskEither> +) => ReaderTaskEither ``` -Added in v2.6.1 +Added in v2.0.0 -## chainW +# combinators + +## chainEitherK **Signature** ```ts -export declare const chainW: ( - f: (a: A) => ReaderTaskEither -) => (ma: ReaderTaskEither) => ReaderTaskEither +export declare const chainEitherK: ( + f: (a: A) => Either +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v2.6.0 +Added in v2.4.0 -## flatten +## chainEitherKW + +Less strict version of [`chainEitherK`](#chainEitherK). **Signature** ```ts -export declare const flatten: ( - mma: ReaderTaskEither> -) => ReaderTaskEither +export declare const chainEitherKW: ( + f: (a: A) => Either +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v2.0.0 - -# combinators +Added in v2.6.1 -## chainEitherK +## chainIOEitherK **Signature** ```ts -export declare function chainEitherK( - f: (a: A) => Either -): (ma: ReaderTaskEither) => ReaderTaskEither +export declare const chainIOEitherK: ( + f: (a: A) => IOEither +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` Added in v2.4.0 -## chainIOEitherK +## chainIOEitherKW + +Less strict version of [`chainIOEitherK`](#chainIOEitherK). **Signature** ```ts -export declare function chainIOEitherK( +export declare const chainIOEitherKW: ( f: (a: A) => IOEither -): (ma: ReaderTaskEither) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` -Added in v2.4.0 +Added in v2.6.1 ## chainTaskEitherK **Signature** ```ts -export declare function chainTaskEitherK( - f: (a: A) => TaskEither -): (ma: ReaderTaskEither) => ReaderTaskEither +export declare const chainTaskEitherK: ( + f: (a: A) => TE.TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither ``` Added in v2.4.0 +## chainTaskEitherKW + +Less strict version of [`chainTaskEitherK`](#chainTaskEitherK). + +**Signature** + +```ts +export declare const chainTaskEitherKW: ( + f: (a: A) => TE.TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.6.1 + ## filterOrElse **Signature** @@ -606,21 +614,23 @@ Added in v2.0.0 **Signature** ```ts -export declare function getOrElse( +export declare const getOrElse: ( onLeft: (e: E) => ReaderTask -): (ma: ReaderTaskEither) => ReaderTask +) => (ma: ReaderTaskEither) => ReaderTask ``` Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts -export declare const getOrElseW: ( - onLeft: (e: E) => ReaderTask -) => (ma: ReaderTaskEither) => ReaderTask +export declare const getOrElseW: ( + onLeft: (e: E) => ReaderTask +) => (ma: ReaderTaskEither) => ReaderTask ``` Added in v2.6.0 diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 6bb9648a2..878e7a3f1 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -216,12 +216,14 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts -export declare const chainW: ( - f: (a: A) => StateReaderTaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainW: ( + f: (a: A) => StateReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.6.0 @@ -245,21 +247,23 @@ Added in v2.0.0 **Signature** ```ts -export declare function chainEitherK( +export declare const chainEitherK: ( f: (a: A) => Either -): (ma: StateReaderTaskEither) => StateReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.4.0 ## chainEitherKW +Less strict version of [`chainEitherK`](#chainEitherK). + **Signature** ```ts -export declare const chainEitherKW: ( - f: (a: A) => Either -) => (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainEitherKW: ( + f: (a: A) => Either +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.6.1 @@ -269,21 +273,23 @@ Added in v2.6.1 **Signature** ```ts -export declare function chainIOEitherK( +export declare const chainIOEitherK: ( f: (a: A) => IOEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.4.0 ## chainIOEitherKW +Less strict version of [`chainIOEitherK`](#chainIOEitherK). + **Signature** ```ts -export declare const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainIOEitherKW: ( + f: (a: A) => IOEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.6.1 @@ -293,21 +299,23 @@ Added in v2.6.1 **Signature** ```ts -export declare function chainReaderTaskEitherK( - f: (a: A) => ReaderTaskEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainReaderTaskEitherK: ( + f: (a: A) => RTE.ReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.4.0 ## chainReaderTaskEitherKW +Less strict version of [`chainReaderTaskEitherK`](#chainReaderTaskEitherK). + **Signature** ```ts -export declare const chainReaderTaskEitherKW: ( - f: (a: A) => RTE.ReaderTaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainReaderTaskEitherKW: ( + f: (a: A) => RTE.ReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.6.1 @@ -317,21 +325,23 @@ Added in v2.6.1 **Signature** ```ts -export declare function chainTaskEitherK( +export declare const chainTaskEitherK: ( f: (a: A) => TaskEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.4.0 ## chainTaskEitherKW +Less strict version of [`chainTaskEitherK`](#chainTaskEitherK). + **Signature** ```ts -export declare const chainTaskEitherKW: ( - f: (a: A) => TaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither +export declare const chainTaskEitherKW: ( + f: (a: A) => TaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` Added in v2.6.1 diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index c99b8de54..83102a742 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -200,12 +200,14 @@ Added in v2.0.0 ## chainW +Less strict version of [`chain`](#chain). + **Signature** ```ts -export declare const chainW: ( - f: (a: A) => TaskEither -) => (ma: TaskEither) => TaskEither +export declare const chainW: ( + f: (a: A) => TaskEither +) => (ma: TaskEither) => TaskEither ``` Added in v2.6.0 @@ -248,19 +250,21 @@ Added in v2.0.0 **Signature** ```ts -export declare function chainEitherK(f: (a: A) => Either): (ma: TaskEither) => TaskEither +export declare const chainEitherK: (f: (a: A) => E.Either) => (ma: TaskEither) => TaskEither ``` Added in v2.4.0 ## chainEitherKW +Less strict version of [`chainEitherK`](#chainEitherK). + **Signature** ```ts -export declare const chainEitherKW: ( - f: (a: A) => E.Either -) => (ma: TaskEither) => TaskEither +export declare const chainEitherKW: ( + f: (a: A) => E.Either +) => (ma: TaskEither) => TaskEither ``` Added in v2.6.1 @@ -270,19 +274,23 @@ Added in v2.6.1 **Signature** ```ts -export declare function chainIOEitherK(f: (a: A) => IOEither): (ma: TaskEither) => TaskEither +export declare const chainIOEitherK: ( + f: (a: A) => IOEither +) => (ma: TaskEither) => TaskEither ``` Added in v2.4.0 ## chainIOEitherKW +Less strict version of [`chainIOEitherK`](#chainIOEitherK). + **Signature** ```ts -export declare const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: TaskEither) => TaskEither +export declare const chainIOEitherKW: ( + f: (a: A) => IOEither +) => (ma: TaskEither) => TaskEither ``` Added in v2.6.1 @@ -519,6 +527,8 @@ Added in v2.0.0 ## getOrElseW +Less strict version of [`getOrElse`](#getOrElse). + **Signature** ```ts diff --git a/src/Either.ts b/src/Either.ts index 59b624e54..a77f6f8ff 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -285,18 +285,19 @@ export function fold(onLeft: (e: E) => B, onRight: (a: A) => B): (ma: E } /** + * Less strict version of [`getOrElse`](#getOrElse). + * * @category destructors - * @since 2.0.0 + * @since 2.6.0 */ -export function getOrElse(onLeft: (e: E) => A): (ma: Either) => A { - return (ma) => (isLeft(ma) ? onLeft(ma.left) : ma.right) -} +export const getOrElseW = (onLeft: (e: E) => B) => (ma: Either): A | B => + isLeft(ma) ? onLeft(ma.left) : ma.right /** * @category destructors - * @since 2.6.0 + * @since 2.0.0 */ -export const getOrElseW: (onLeft: (e: E) => B) => (ma: Either) => A | B = getOrElse as any +export const getOrElse: (onLeft: (e: E) => A) => (ma: Either) => A = getOrElseW // ------------------------------------------------------------------------------------- // combinators @@ -392,14 +393,21 @@ export const apSecond = (fb: Either) => (fa: Either): Eithe fb ) +/** + * Less strict version of [`chain`](#chain). + * + * @category Monad + * @since 2.6.0 + */ +export const chainW = (f: (a: A) => Either) => (ma: Either): Either => chain_(ma, f) + /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ -export const chain: (f: (a: A) => Either) => (ma: Either) => Either = (f) => (ma) => - chain_(ma, f) +export const chain: (f: (a: A) => Either) => (ma: Either) => Either = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -411,12 +419,6 @@ export const chain: (f: (a: A) => Either) => (ma: Either) = export const chainFirst: (f: (a: A) => Either) => (ma: Either) => Either = (f) => (ma) => chain_(ma, (a) => map_(f(a), () => a)) -/** - * @category Monad - * @since 2.6.0 - */ -export const chainW: (f: (a: A) => Either) => (ma: Either) => Either = chain as any - /** * @category Monad * @since 2.0.0 @@ -499,7 +501,7 @@ const map_: (fa: Either, f: (a: A) => B) => Either = (ma, f const ap_: (fab: Either B>, fa: Either) => Either = (mab, ma) => isLeft(mab) ? mab : isLeft(ma) ? ma : right(mab.right(ma.right)) const of = right -const chain_: (fa: Either, f: (a: A) => Either) => Either = (ma, f) => +const chain_: (fa: Either, f: (a: A) => Either) => Either = (ma, f) => isLeft(ma) ? ma : f(ma.right) const reduce_: (fa: Either, b: B, f: (b: B, a: A) => B) => B = (fa, b, f) => isLeft(fa) ? b : f(b, fa.right) diff --git a/src/IOEither.ts b/src/IOEither.ts index 115ae7bbc..ee9f721dc 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -109,17 +109,19 @@ export const fold: (onLeft: (e: E) => IO, onRight: (a: A) => IO) flow(E.fold, I.chain) /** + * Less strict version of [`getOrElse`](#getOrElse). + * * @category destructors - * @since 2.0.0 + * @since 2.6.0 */ -export const getOrElse: (onLeft: (e: E) => IO) => (ma: IOEither) => IO = (onLeft) => - I.chain(E.fold(onLeft, I.of)) +export const getOrElseW = (onLeft: (e: E) => IO) => (ma: IOEither): IO => + pipe(ma, I.chain(E.fold>(onLeft, I.of))) /** * @category destructors - * @since 2.6.0 + * @since 2.0.0 */ -export const getOrElseW: (onLeft: (e: E) => IO) => (ma: IOEither) => IO = getOrElse as any +export const getOrElse: (onLeft: (e: E) => IO) => (ma: IOEither) => IO = getOrElseW // ------------------------------------------------------------------------------------- // combinators @@ -164,20 +166,22 @@ export function fromEitherK, B>( } /** + * Less strict version of [`chainEitherK`](#chainEitherK). + * * @category combinators - * @since 2.4.0 + * @since 2.6.1 */ -export function chainEitherK(f: (a: A) => Either): (ma: IOEither) => IOEither { - return chain(fromEitherK(f)) -} +export const chainEitherKW: (f: (a: A) => Either) => (ma: IOEither) => IOEither = ( + f +) => chainW(fromEitherK(f)) /** * @category combinators - * @since 2.6.1 + * @since 2.4.0 */ -export const chainEitherKW: ( - f: (a: A) => Either -) => (ma: IOEither) => IOEither = chainEitherK as any +export const chainEitherK: ( + f: (a: A) => Either +) => (ma: IOEither) => IOEither = chainEitherKW // ------------------------------------------------------------------------------------- // pipeables @@ -247,14 +251,22 @@ export const apSecond = (fb: IOEither) => (fa: IOEither): I ap(fb) ) +/** + * Less strict version of [`chain`](#chain). + * + * @category Monad + * @since 2.6.0 + */ +export const chainW = (f: (a: A) => IOEither) => (ma: IOEither): IOEither => + pipe(ma, I.chain(E.fold>(left, f))) + /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ -export const chain: (f: (a: A) => IOEither) => (ma: IOEither) => IOEither = (f) => - I.chain(E.fold(left, f)) +export const chain: (f: (a: A) => IOEither) => (ma: IOEither) => IOEither = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -271,14 +283,6 @@ export const chainFirst: (f: (a: A) => IOEither) => (ma: IOEither ) ) -/** - * @category Monad - * @since 2.6.0 - */ -export const chainW: ( - f: (a: A) => IOEither -) => (ma: IOEither) => IOEither = chain as any - /** * @category Monad * @since 2.0.0 diff --git a/src/Option.ts b/src/Option.ts index c03a98e58..1aa02a975 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -313,6 +313,14 @@ export function toUndefined(ma: Option): A | undefined { return isNone(ma) ? undefined : ma.value } +/** + * Less strict version of [`getOrElse`](#getOrElse). + * + * @category destructors + * @since 2.6.0 + */ +export const getOrElseW = (onNone: () => B) => (ma: Option): A | B => (isNone(ma) ? onNone() : ma.value) + /** * Extracts the value out of the structure, if it exists. Otherwise returns the given default value * @@ -338,15 +346,7 @@ export function toUndefined(ma: Option): A | undefined { * @category destructors * @since 2.0.0 */ -export function getOrElse(onNone: () => A): (ma: Option) => A { - return (ma) => (isNone(ma) ? onNone() : ma.value) -} - -/** - * @category destructors - * @since 2.6.0 - */ -export const getOrElseW: (onNone: () => B) => (ma: Option) => A | B = getOrElse as any +export const getOrElse: (onNone: () => A) => (ma: Option) => A = getOrElseW // ------------------------------------------------------------------------------------- // combinators diff --git a/src/Reader.ts b/src/Reader.ts index cc2f25bef..26b080a7b 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -129,14 +129,23 @@ export const apSecond = (fb: Reader) => (fa: Reader): Reade ap(fb) ) +/** + * Less strict version of [`chain`](#chain). + * + * @category Monad + * @since 2.6.0 + */ +export const chainW: (f: (a: A) => Reader) => (ma: Reader) => Reader = (f) => ( + fa +) => (r) => f(fa(r))(r) + /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ -export const chain: (f: (a: A) => Reader) => (ma: Reader) => Reader = (f) => (fa) => (r) => - f(fa(r))(r) +export const chain: (f: (a: A) => Reader) => (ma: Reader) => Reader = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -153,12 +162,6 @@ export const chainFirst: (f: (a: A) => Reader) => (ma: Reader(f: (a: A) => Reader) => (ma: Reader) => Reader = chain as any - /** * @category Monad * @since 2.0.0 diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index e020593c9..c13ec984b 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -126,20 +126,22 @@ export const fold: ( ) => (ma: ReaderEither) => Reader = flow(E.fold, R.chain) /** + * Less strict version of [`getOrElse`](#getOrElse). + * * @category destructors - * @since 2.0.0 + * @since 2.6.0 */ -export const getOrElse: (onLeft: (e: E) => Reader) => (ma: ReaderEither) => Reader = ( - onLeft -) => R.chain(E.fold(onLeft, R.of)) +export const getOrElseW = (onLeft: (e: E) => Reader) => ( + ma: ReaderEither +): Reader => pipe(ma, R.chain(E.fold>(onLeft, R.of))) /** * @category destructors - * @since 2.6.0 + * @since 2.0.0 */ -export const getOrElseW: ( - onLeft: (e: E) => Reader -) => (ma: ReaderEither) => Reader = getOrElse as any +export const getOrElse: ( + onLeft: (e: E) => Reader +) => (ma: ReaderEither) => Reader = getOrElseW // ------------------------------------------------------------------------------------- // combinators @@ -179,15 +181,23 @@ export function fromEitherK, B>( return (...a) => fromEither(f(...a)) } +/** + * Less strict version of [`chainEitherK`](#chainEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainEitherKW: ( + f: (a: A) => Either +) => (ma: ReaderEither) => ReaderEither = (f) => chainW(fromEitherK(f)) + /** * @category combinators * @since 2.4.0 */ -export function chainEitherK( +export const chainEitherK: ( f: (a: A) => Either -): (ma: ReaderEither) => ReaderEither { - return chain(fromEitherK(f)) -} +) => (ma: ReaderEither) => ReaderEither = chainEitherKW /** * @category combinators @@ -278,30 +288,24 @@ export const apSecond = (fb: ReaderEither) => (fa: ReaderEi ) /** - * Composes computations in sequence, using the return value of one computation to determine the next computation. + * Less strict version of [`chain`](#chain). * - * @category Monad - * @since 2.0.0 - */ -export const chain: ( - f: (a: A) => ReaderEither -) => (ma: ReaderEither) => ReaderEither = (f) => R.chain(E.fold(left, f)) - -/** * @category Monad * @since 2.6.0 */ -export const chainW: ( - f: (a: A) => ReaderEither -) => (ma: ReaderEither) => ReaderEither = chain as any +export const chainW = (f: (a: A) => ReaderEither) => ( + ma: ReaderEither +): ReaderEither => pipe(ma, R.chain(E.fold>(left, f))) /** + * Composes computations in sequence, using the return value of one computation to determine the next computation. + * * @category Monad - * @since 2.6.1 + * @since 2.0.0 */ -export const chainEitherKW: ( - f: (a: A) => Either -) => (ma: ReaderEither) => ReaderEither = chainEitherK as any +export const chain: ( + f: (a: A) => ReaderEither +) => (ma: ReaderEither) => ReaderEither = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 0918bee74..5c34d5630 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -218,22 +218,23 @@ export function fold( } /** + * Less strict version of [`getOrElse`](#getOrElse). + * * @category destructors - * @since 2.0.0 + * @since 2.6.0 */ -export function getOrElse( - onLeft: (e: E) => ReaderTask -): (ma: ReaderTaskEither) => ReaderTask { - return (ma) => (r) => TE.getOrElse((e) => onLeft(e)(r))(ma(r)) -} +export const getOrElseW = ( + onLeft: (e: E) => ReaderTask +): ((ma: ReaderTaskEither) => ReaderTask) => (ma) => (r) => + TE.getOrElseW((e: E) => onLeft(e)(r))(ma(r)) /** * @category destructors - * @since 2.6.0 + * @since 2.0.0 */ -export const getOrElseW: ( - onLeft: (e: E) => ReaderTask -) => (ma: ReaderTaskEither) => ReaderTask = getOrElse as any +export const getOrElse: ( + onLeft: (e: E) => ReaderTask +) => (ma: ReaderTaskEither) => ReaderTask = getOrElseW // ------------------------------------------------------------------------------------- // combinators @@ -289,15 +290,23 @@ export function fromEitherK, B>( return (...a) => fromEither(f(...a)) } +/** + * Less strict version of [`chainEitherK`](#chainEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainEitherKW: ( + f: (a: A) => Either +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => chainW(fromEitherK(f)) + /** * @category combinators * @since 2.4.0 */ -export function chainEitherK( +export const chainEitherK: ( f: (a: A) => Either -): (ma: ReaderTaskEither) => ReaderTaskEither { - return chain(fromEitherK(f)) -} +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainEitherKW /** * @category combinators @@ -309,15 +318,23 @@ export function fromIOEitherK, B>( return (...a) => fromIOEither(f(...a)) } +/** + * Less strict version of [`chainIOEitherK`](#chainIOEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainIOEitherKW: ( + f: (a: A) => IOEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => chainW(fromIOEitherK(f)) + /** * @category combinators * @since 2.4.0 */ -export function chainIOEitherK( +export const chainIOEitherK: ( f: (a: A) => IOEither -): (ma: ReaderTaskEither) => ReaderTaskEither { - return chain(fromIOEitherK(f)) -} +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainIOEitherKW /** * @category combinators @@ -329,15 +346,23 @@ export function fromTaskEitherK, B>( return (...a) => fromTaskEither(f(...a)) } +/** + * Less strict version of [`chainTaskEitherK`](#chainTaskEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainTaskEitherKW: ( + f: (a: A) => TaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => chainW(fromTaskEitherK(f)) + /** * @category combinators * @since 2.4.0 */ -export function chainTaskEitherK( +export const chainTaskEitherK: ( f: (a: A) => TaskEither -): (ma: ReaderTaskEither) => ReaderTaskEither { - return chain(fromTaskEitherK(f)) -} +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainTaskEitherKW // ------------------------------------------------------------------------------------- // pipeables @@ -417,19 +442,29 @@ export const apSecond = (fb: ReaderTaskEither) => ( ) /** - * Composes computations in sequence, using the return value of one computation to determine the next computation. + * Less strict version of [`chain`](#chain). * * @category Monad - * @since 2.0.0 + * @since 2.6.0 */ -export const chain: ( +export const chainW: ( f: (a: A) => ReaderTaskEither -) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => (fa) => (r) => +) => (ma: ReaderTaskEither) => ReaderTaskEither = (f) => (fa) => (r) => pipe( fa(r), - TE.chain((a) => f(a)(r)) + TE.chainW((a) => f(a)(r)) ) +/** + * Composes computations in sequence, using the return value of one computation to determine the next computation. + * + * @category Monad + * @since 2.0.0 + */ +export const chain: ( + f: (a: A) => ReaderTaskEither +) => (ma: ReaderTaskEither) => ReaderTaskEither = chainW + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -626,38 +661,6 @@ export const readerTaskEitherSeq: typeof readerTaskEither = { throwError: throwError_ } -/** - * @category Monad - * @since 2.6.0 - */ -export const chainW: ( - f: (a: A) => ReaderTaskEither -) => (ma: ReaderTaskEither) => ReaderTaskEither = chain as any - -/** - * @category Monad - * @since 2.6.1 - */ -export const chainEitherKW: ( - f: (a: A) => Either -) => (ma: ReaderTaskEither) => ReaderTaskEither = chainEitherK as any - -/** - * @category Monad - * @since 2.6.1 - */ -export const chainTaskEitherKW: ( - f: (a: A) => TaskEither -) => (ma: ReaderTaskEither) => ReaderTaskEither = chainTaskEitherK as any - -/** - * @category Monad - * @since 2.6.1 - */ -export const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: ReaderTaskEither) => ReaderTaskEither = chainIOEitherK as any - // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index bbf4a6cef..c60fb3241 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -247,15 +247,23 @@ export function fromEitherK, B>( return (...a) => fromEither(f(...a)) } +/** + * Less strict version of [`chainEitherK`](#chainEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainEitherKW = (f: (a: A) => Either) => ( + ma: StateReaderTaskEither +): StateReaderTaskEither => pipe(ma, chainW(fromEitherK(f))) + /** * @category combinators * @since 2.4.0 */ -export function chainEitherK( +export const chainEitherK: ( f: (a: A) => Either -): (ma: StateReaderTaskEither) => StateReaderTaskEither { - return chain(fromEitherK(f)) -} +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainEitherKW /** * @category combinators @@ -267,15 +275,23 @@ export function fromIOEitherK, B>( return (...a) => fromIOEither(f(...a)) } +/** + * Less strict version of [`chainIOEitherK`](#chainIOEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainIOEitherKW = (f: (a: A) => IOEither) => ( + ma: StateReaderTaskEither +): StateReaderTaskEither => pipe(ma, chainW(fromIOEitherK(f))) + /** * @category combinators * @since 2.4.0 */ -export function chainIOEitherK( +export const chainIOEitherK: ( f: (a: A) => IOEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither { - return chain(fromIOEitherK(f)) -} +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainIOEitherKW /** * @category combinators @@ -287,15 +303,23 @@ export function fromTaskEitherK, B>( return (...a) => fromTaskEither(f(...a)) } +/** + * Less strict version of [`chainTaskEitherK`](#chainTaskEitherK). + * + * @category combinators + * @since 2.6.1 + */ +export const chainTaskEitherKW = (f: (a: A) => TaskEither) => ( + ma: StateReaderTaskEither +): StateReaderTaskEither => pipe(ma, chainW(fromTaskEitherK(f))) + /** * @category combinators * @since 2.4.0 */ -export function chainTaskEitherK( +export const chainTaskEitherK: ( f: (a: A) => TaskEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither { - return chain(fromTaskEitherK(f)) -} +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainTaskEitherKW /** * @category combinators @@ -308,48 +332,22 @@ export function fromReaderTaskEitherK, B> } /** - * @category combinators - * @since 2.4.0 - */ -export function chainReaderTaskEitherK( - f: (a: A) => ReaderTaskEither -): (ma: StateReaderTaskEither) => StateReaderTaskEither { - return chain(fromReaderTaskEitherK(f)) -} - -/** - * @category combinators - * @since 2.6.1 - */ -export const chainEitherKW: ( - f: (a: A) => Either -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainEitherK as any - -/** - * @category combinators - * @since 2.6.1 - */ -export const chainTaskEitherKW: ( - f: (a: A) => TaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainTaskEitherK as any - -/** + * Less strict version of [`chainReaderTaskEitherK`](#chainReaderTaskEitherK). + * * @category combinators * @since 2.6.1 */ -export const chainReaderTaskEitherKW: ( - f: (a: A) => ReaderTaskEither -) => ( - ma: StateReaderTaskEither -) => StateReaderTaskEither = chainReaderTaskEitherK as any +export const chainReaderTaskEitherKW = (f: (a: A) => ReaderTaskEither) => ( + ma: StateReaderTaskEither +): StateReaderTaskEither => pipe(ma, chainW(fromReaderTaskEitherK(f))) /** * @category combinators - * @since 2.6.1 + * @since 2.4.0 */ -export const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainIOEitherK as any +export const chainReaderTaskEitherK: ( + f: (a: A) => ReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainReaderTaskEitherKW /** * @category combinators @@ -461,26 +459,28 @@ export const apSecond = (fb: StateReaderTaskEither) => < ) /** - * Composes computations in sequence, using the return value of one computation to determine the next computation. + * Less strict version of [`chain`](#chain). * * @category Monad - * @since 2.0.0 + * @since 2.6.0 */ -export const chain: ( +export const chainW: ( f: (a: A) => StateReaderTaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = (f) => (ma) => (s1) => +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = (f) => (ma) => (s1) => pipe( ma(s1), - RTE.chain(([a, s2]) => f(a)(s2)) + RTE.chainW(([a, s2]) => f(a)(s2)) ) /** + * Composes computations in sequence, using the return value of one computation to determine the next computation. + * * @category Monad - * @since 2.6.0 + * @since 2.0.0 */ -export const chainW: ( - f: (a: A) => StateReaderTaskEither -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chain as any +export const chain: ( + f: (a: A) => StateReaderTaskEither +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 9bd6dd883..13b30ab70 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -168,19 +168,19 @@ export const fold: ( flow(E.fold, T.chain) /** + * Less strict version of [`getOrElse`](#getOrElse). + * * @category destructors - * @since 2.0.0 + * @since 2.6.0 */ -export const getOrElse: (onLeft: (e: E) => Task) => (ma: TaskEither) => Task = (onLeft) => - T.chain(E.fold(onLeft, T.of)) +export const getOrElseW = (onLeft: (e: E) => Task) => (ma: TaskEither): Task => + pipe(ma, T.chain(E.fold>(onLeft, T.of))) /** * @category destructors - * @since 2.6.0 + * @since 2.0.0 */ -export const getOrElseW: ( - onLeft: (e: E) => Task -) => (ma: TaskEither) => Task = getOrElse as any +export const getOrElse: (onLeft: (e: E) => Task) => (ma: TaskEither) => Task = getOrElseW // ------------------------------------------------------------------------------------- // combinators @@ -249,36 +249,40 @@ export function fromIOEitherK, B>( } /** + * Less strict version of [`chainEitherK`](#chainEitherK). + * * @category combinators - * @since 2.4.0 + * @since 2.6.1 */ -export function chainEitherK(f: (a: A) => Either): (ma: TaskEither) => TaskEither { - return chain(fromEitherK(f)) -} +export const chainEitherKW: ( + f: (a: A) => Either +) => (ma: TaskEither) => TaskEither = (f) => chainW(fromEitherK(f)) /** * @category combinators - * @since 2.6.1 + * @since 2.4.0 */ -export const chainEitherKW: ( - f: (a: A) => Either -) => (ma: TaskEither) => TaskEither = chainEitherK as any +export const chainEitherK: ( + f: (a: A) => Either +) => (ma: TaskEither) => TaskEither = chainEitherKW /** + * Less strict version of [`chainIOEitherK`](#chainIOEitherK). + * * @category combinators - * @since 2.4.0 + * @since 2.6.1 */ -export function chainIOEitherK(f: (a: A) => IOEither): (ma: TaskEither) => TaskEither { - return chain(fromIOEitherK(f)) -} +export const chainIOEitherKW: ( + f: (a: A) => IOEither +) => (ma: TaskEither) => TaskEither = (f) => chainW(fromIOEitherK(f)) /** * @category combinators - * @since 2.6.1 + * @since 2.4.0 */ -export const chainIOEitherKW: ( - f: (a: A) => IOEither -) => (ma: TaskEither) => TaskEither = chainIOEitherK as any +export const chainIOEitherK: ( + f: (a: A) => IOEither +) => (ma: TaskEither) => TaskEither = chainIOEitherKW // ------------------------------------------------------------------------------------- // pipeables @@ -349,21 +353,21 @@ export const apSecond = (fb: TaskEither) => (fa: TaskEither ) /** - * Composes computations in sequence, using the return value of one computation to determine the next computation. + * Less strict version of [`chain`](#chain). * * @category Monad - * @since 2.0.0 + * @since 2.6.0 */ -export const chain: (f: (a: A) => TaskEither) => (ma: TaskEither) => TaskEither = (f) => - T.chain(E.fold(left, f)) +export const chainW = (f: (a: A) => TaskEither) => (ma: TaskEither): TaskEither => + pipe(ma, T.chain(E.fold>(left, f))) /** + * Composes computations in sequence, using the return value of one computation to determine the next computation. + * * @category Monad - * @since 2.6.0 + * @since 2.0.0 */ -export const chainW: ( - f: (a: A) => TaskEither -) => (ma: TaskEither) => TaskEither = chain as any +export const chain: (f: (a: A) => TaskEither) => (ma: TaskEither) => TaskEither = chainW /** * Composes computations in sequence, using the return value of one computation to determine the next computation and