From 4ff011db81c874172b0091a41390db76157b1c30 Mon Sep 17 00:00:00 2001 From: Milan Suk Date: Fri, 19 May 2023 22:35:47 +0200 Subject: [PATCH] feat: tapReader --- docs/modules/ReaderEither.ts.md | 81 ++++++++++++++-------- docs/modules/ReaderIO.ts.md | 77 +++++++++++++-------- docs/modules/ReaderTask.ts.md | 81 ++++++++++++++-------- docs/modules/ReaderTaskEither.ts.md | 81 ++++++++++++++-------- docs/modules/StateReaderTaskEither.ts.md | 88 ++++++++++++++++-------- src/FromReader.ts | 38 +++++++++- src/ReaderEither.ts | 46 +++++++++---- src/ReaderIO.ts | 46 +++++++++---- src/ReaderTask.ts | 46 +++++++++---- src/ReaderTaskEither.ts | 49 ++++++++----- src/StateReaderTaskEither.ts | 54 ++++++++++----- test/ReaderEither.ts | 4 ++ test/ReaderIO.ts | 4 ++ test/ReaderTask.ts | 4 ++ test/ReaderTaskEither.ts | 4 ++ test/StateReaderTaskEither.ts | 10 +++ 16 files changed, 484 insertions(+), 229 deletions(-) diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index a844ae20e..2bc8e0851 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -15,6 +15,7 @@ Added in v2.0.0 - [combinators](#combinators) - [tap](#tap) - [tapEither](#tapeither) + - [tapReader](#tapreader) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -74,6 +75,8 @@ Added in v2.0.0 - [chainFirst](#chainfirst) - [chainFirstEitherK](#chainfirsteitherk) - [chainFirstEitherKW](#chainfirsteitherkw) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstW](#chainfirstw) - [chainOptionK](#chainoptionk) - [chainOptionKW](#chainoptionkw) @@ -104,8 +107,6 @@ Added in v2.0.0 - [matchEW](#matchew) - [matchW](#matchw) - [sequencing](#sequencing) - - [chainFirstReaderK](#chainfirstreaderk) - - [chainFirstReaderKW](#chainfirstreaderkw) - [chainReaderK](#chainreaderk) - [chainReaderKW](#chainreaderkw) - [flatMap](#flatmap) @@ -200,6 +201,22 @@ assert.deepStrictEqual(checkString('fp-ts')(2), E.right(2)) Added in v2.16.0 +## tapReader + +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +**Signature** + +```ts +export declare const tapReader: { + (self: ReaderEither, f: (a: A) => R.Reader): ReaderEither + (f: (a: A) => R.Reader): (self: ReaderEither) => ReaderEither +} +``` + +Added in v2.16.0 + # constructors ## ask @@ -892,6 +909,38 @@ export declare const chainFirstEitherKW: ( Added in v2.12.0 +## chainFirstReaderK + +Alias of `tapReader`. + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Alias of `tapReader`. + +Less strict version of [`chainReaderK`](#chainreaderk). + +The `W` suffix (short for **W**idening) means that the environment types will be merged. + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderEither) => ReaderEither +``` + +Added in v2.11.0 + ## chainFirstW Alias of `tap`. @@ -1243,34 +1292,6 @@ Added in v2.10.0 # sequencing -## 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). - -The `W` suffix (short for **W**idening) means that the environment types will be merged. - -**Signature** - -```ts -export declare const chainFirstReaderKW: ( - f: (a: A) => R.Reader -) => (ma: ReaderEither) => ReaderEither -``` - -Added in v2.11.0 - ## chainReaderK **Signature** diff --git a/docs/modules/ReaderIO.ts.md b/docs/modules/ReaderIO.ts.md index ea7b3e568..48a4df0f8 100644 --- a/docs/modules/ReaderIO.ts.md +++ b/docs/modules/ReaderIO.ts.md @@ -15,6 +15,7 @@ Added in v2.13.0 - [combinators](#combinators) - [tap](#tap) - [tapIO](#tapio) + - [tapReader](#tapreader) - [constructors](#constructors) - [ask](#ask) - [asks](#asks) @@ -45,6 +46,8 @@ Added in v2.13.0 - [chain](#chain) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstW](#chainfirstw) - [chainW](#chainw) - [lifting](#lifting) @@ -58,8 +61,6 @@ Added in v2.13.0 - [model](#model) - [ReaderIO (interface)](#readerio-interface) - [sequencing](#sequencing) - - [chainFirstReaderK](#chainfirstreaderk) - - [chainFirstReaderKW](#chainfirstreaderkw) - [chainIOK](#chainiok) - [chainReaderK](#chainreaderk) - [chainReaderKW](#chainreaderkw) @@ -139,6 +140,22 @@ test() Added in v2.16.0 +## tapReader + +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +**Signature** + +```ts +export declare const tapReader: { + (self: ReaderIO, f: (a: A) => R.Reader): ReaderIO + (f: (a: A) => R.Reader): (self: ReaderIO) => ReaderIO +} +``` + +Added in v2.16.0 + # constructors ## ask @@ -433,6 +450,36 @@ export declare const chainFirstIOK: (f: (a: A) => I.IO) => (first: R Added in v2.13.0 +## chainFirstReaderK + +Alias of `tapReader`. + +**Signature** + +```ts +export declare const chainFirstReaderK: (f: (a: A) => R.Reader) => (ma: ReaderIO) => ReaderIO +``` + +Added in v2.13.0 + +## chainFirstReaderKW + +Alias of `tapReader`. + +Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). + +The `W` suffix (short for **W**idening) means that the environment types will be merged. + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderIO) => ReaderIO +``` + +Added in v2.13.0 + ## chainFirstW Alias of `tap`. @@ -555,32 +602,6 @@ Added in v2.13.0 # sequencing -## chainFirstReaderK - -**Signature** - -```ts -export declare const chainFirstReaderK: (f: (a: A) => R.Reader) => (ma: ReaderIO) => ReaderIO -``` - -Added in v2.13.0 - -## chainFirstReaderKW - -Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). - -The `W` suffix (short for **W**idening) means that the environment types will be merged. - -**Signature** - -```ts -export declare const chainFirstReaderKW: ( - f: (a: A) => R.Reader -) => (ma: ReaderIO) => ReaderIO -``` - -Added in v2.13.0 - ## chainIOK **Signature** diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 8a307394b..d44091f1a 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -15,6 +15,7 @@ Added in v2.3.0 - [combinators](#combinators) - [tap](#tap) - [tapIO](#tapio) + - [tapReader](#tapreader) - [tapTask](#taptask) - [constructors](#constructors) - [ask](#ask) @@ -53,6 +54,8 @@ Added in v2.3.0 - [chain](#chain) - [chainFirst](#chainfirst) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainW](#chainw) @@ -71,8 +74,6 @@ Added in v2.3.0 - [sequencing](#sequencing) - [chainFirstReaderIOK](#chainfirstreaderiok) - [chainFirstReaderIOKW](#chainfirstreaderiokw) - - [chainFirstReaderK](#chainfirstreaderk) - - [chainFirstReaderKW](#chainfirstreaderkw) - [chainIOK](#chainiok) - [chainReaderIOK](#chainreaderiok) - [chainReaderIOKW](#chainreaderiokw) @@ -166,6 +167,22 @@ test() Added in v2.16.0 +## tapReader + +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +**Signature** + +```ts +export declare const tapReader: { + (self: ReaderTask, f: (a: A) => R.Reader): ReaderTask + (f: (a: A) => R.Reader): (self: ReaderTask) => ReaderTask +} +``` + +Added in v2.16.0 + ## tapTask Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -578,6 +595,38 @@ export declare const chainFirstIOK: (f: (a: A) => IO) => (first: Rea Added in v2.10.0 +## chainFirstReaderK + +Alias of `tapReader`. + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Alias of `tapReader`. + +Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). + +The `W` suffix (short for **W**idening) means that the environment types will be merged. + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTask) => ReaderTask +``` + +Added in v2.11.0 + ## chainFirstTaskK Alias of `tapTask`. @@ -762,34 +811,6 @@ export declare const chainFirstReaderIOKW: ( Added in v2.13.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). - -The `W` suffix (short for **W**idening) means that the environment types will be merged. - -**Signature** - -```ts -export declare const chainFirstReaderKW: ( - f: (a: A) => R.Reader -) => (ma: ReaderTask) => ReaderTask -``` - -Added in v2.11.0 - ## chainIOK **Signature** diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 65000d58d..316ecdd1f 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -16,6 +16,7 @@ Added in v2.0.0 - [tap](#tap) - [tapEither](#tapeither) - [tapIO](#tapio) + - [tapReader](#tapreader) - [tapTask](#taptask) - [constructors](#constructors) - [ask](#ask) @@ -98,6 +99,8 @@ Added in v2.0.0 - [chainFirstEitherK](#chainfirsteitherk) - [chainFirstEitherKW](#chainfirsteitherkw) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) + - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) - [chainNullableK](#chainnullablek) @@ -142,8 +145,6 @@ Added in v2.0.0 - [chainFirstReaderEitherKW](#chainfirstreadereitherkw) - [chainFirstReaderIOK](#chainfirstreaderiok) - [chainFirstReaderIOKW](#chainfirstreaderiokw) - - [chainFirstReaderK](#chainfirstreaderk) - - [chainFirstReaderKW](#chainfirstreaderkw) - [chainFirstReaderTaskK](#chainfirstreadertaskk) - [chainFirstReaderTaskKW](#chainfirstreadertaskkw) - [chainFirstTaskEitherK](#chainfirsttaskeitherk) @@ -301,6 +302,22 @@ test() Added in v2.16.0 +## tapReader + +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +**Signature** + +```ts +export declare const tapReader: { + (self: ReaderTaskEither, f: (a: A) => R.Reader): ReaderTaskEither + (f: (a: A) => R.Reader): (self: ReaderTaskEither) => ReaderTaskEither +} +``` + +Added in v2.16.0 + ## tapTask Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -1273,6 +1290,38 @@ export declare const chainFirstIOK: ( Added in v2.10.0 +## chainFirstReaderK + +Alias of `tapReader`. + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + +## chainFirstReaderKW + +Alias of `tapReader`. + +Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). + +The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. + +**Signature** + +```ts +export declare const chainFirstReaderKW: ( + f: (a: A) => R.Reader +) => (ma: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK Alias of `tapTask`. @@ -1816,34 +1865,6 @@ export declare const chainFirstReaderIOKW: ( Added in v2.13.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). - -The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. - -**Signature** - -```ts -export declare const chainFirstReaderKW: ( - f: (a: A) => R.Reader -) => (ma: ReaderTaskEither) => ReaderTaskEither -``` - -Added in v2.11.0 - ## chainFirstReaderTaskK **Signature** diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 92a5e298d..96521b761 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -16,6 +16,7 @@ Added in v2.0.0 - [tap](#tap) - [tapEither](#tapeither) - [tapIO](#tapio) + - [tapReader](#tapreader) - [tapTask](#taptask) - [constructors](#constructors) - [ask](#ask) @@ -80,8 +81,10 @@ Added in v2.0.0 - [chainFirstEitherK](#chainfirsteitherk) - [chainFirstEitherKW](#chainfirsteitherkw) - [chainFirstIOK](#chainfirstiok) + - [chainFirstReaderK](#chainfirstreaderk) - [chainFirstTaskK](#chainfirsttaskk) - [chainFirstW](#chainfirstw) + - [chainReaderKW](#chainreaderkw) - [chainW](#chainw) - [lifting](#lifting) - [fromEitherK](#fromeitherk) @@ -105,7 +108,6 @@ Added in v2.0.0 - [sequencing](#sequencing) - [chainEitherK](#chaineitherk) - [chainEitherKW](#chaineitherkw) - - [chainFirstReaderK](#chainfirstreaderk) - [chainFirstReaderKW](#chainfirstreaderkw) - [chainIOEitherK](#chainioeitherk) - [chainIOEitherKW](#chainioeitherkw) @@ -113,7 +115,6 @@ Added in v2.0.0 - [chainOptionK](#chainoptionk) - [chainOptionKW](#chainoptionkw) - [chainReaderK](#chainreaderk) - - [chainReaderKW](#chainreaderkw) - [chainReaderTaskEitherK](#chainreadertaskeitherk) - [chainReaderTaskEitherKW](#chainreadertaskeitherkw) - [chainStateK](#chainstatek) @@ -219,6 +220,29 @@ export declare const tapIO: { Added in v2.16.0 +## tapReader + +Composes computations in sequence, using the return value of one computation to determine the next computation and +keeping only the result of the first. + +**Signature** + +```ts +export declare const tapReader: { + (self: StateReaderTaskEither, f: (a: A) => R.Reader): StateReaderTaskEither< + S, + R1 & R2, + E, + A + > + (f: (a: A) => R.Reader): ( + self: StateReaderTaskEither + ) => StateReaderTaskEither +} +``` + +Added in v2.16.0 + ## tapTask Composes computations in sequence, using the return value of one computation to determine the next computation and @@ -905,6 +929,20 @@ export declare const chainFirstIOK: ( Added in v2.10.0 +## chainFirstReaderK + +Alias of `tapReader`. + +**Signature** + +```ts +export declare const chainFirstReaderK: ( + f: (a: A) => R.Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainFirstTaskK Alias of `tapTask`. @@ -933,6 +971,24 @@ export declare const chainFirstW: ( Added in v2.8.0 +## chainReaderKW + +Alias of `tapReader`. + +Less strict version of [`chainReaderK`](#chainReaderK). + +The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. + +**Signature** + +```ts +export declare const chainReaderKW: ( + f: (a: A) => R.Reader +) => (ma: StateReaderTaskEither) => StateReaderTaskEither +``` + +Added in v2.11.0 + ## chainW Alias of `flatMap`. @@ -1196,18 +1252,6 @@ export declare const chainEitherKW: ( Added in v2.6.1 -## 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). @@ -1306,22 +1350,6 @@ export declare const chainReaderK: ( Added in v2.11.0 -## chainReaderKW - -Less strict version of [`chainReaderK`](#chainReaderK). - -The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. - -**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/FromReader.ts b/src/FromReader.ts index 4cc83a729..776f564d2 100644 --- a/src/FromReader.ts +++ b/src/FromReader.ts @@ -3,7 +3,7 @@ * * @since 2.11.0 */ -import { Chain, Chain2, Chain3, Chain3C, Chain4, chainFirst } from './Chain' +import { Chain, Chain2, Chain3, Chain3C, Chain4, tap } from './Chain' import { flow } from './function' import { HKT2, Kind2, Kind3, Kind4, URIS2, URIS3, URIS4 } from './HKT' import * as R from './Reader' @@ -176,5 +176,39 @@ export function chainFirstReaderK( F: FromReader2, M: Chain2 ): (f: (a: A) => Reader) => (ma: Kind2) => Kind2 { - return flow(fromReaderK(F), chainFirst(M)) + const tapM = tapReader(F, M) + return (f) => (self) => tapM(self, f) +} + +/** @internal */ +export function tapReader( + F: FromReader4, + M: Chain4 +): (self: Kind4, f: (a: A) => Reader) => Kind4 +/** @internal */ +export function tapReader( + F: FromReader3, + M: Chain3 +): (self: Kind3, f: (a: A) => Reader) => Kind3 +/** @internal */ +export function tapReader( + F: FromReader3C, + M: Chain3C +): (self: Kind3, f: (a: A) => Reader) => Kind3 +/** @internal */ +export function tapReader( + F: FromReader2, + M: Chain2 +): (self: Kind2, f: (a: A) => Reader) => Kind2 +export function tapReader( + F: FromReader, + M: Chain +): (self: HKT2, f: (a: A) => Reader) => HKT2 +/** @internal */ +export function tapReader( + F: FromReader2, + M: Chain2 +): (self: Kind2, f: (a: A) => Reader) => Kind2 { + const tapM = tap(M) + return (self, f) => tapM(self, flow(f, F.fromReader)) } diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 3aab6bad1..0d9c3c44d 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -36,10 +36,10 @@ import { import { ask as ask_, asks as asks_, - chainFirstReaderK as chainFirstReaderK_, chainReaderK as chainReaderK_, FromReader3, - fromReaderK as fromReaderK_ + fromReaderK as fromReaderK_, + tapReader as tapReader_ } from './FromReader' import { dual, flow, identity, LazyArg, pipe, SK } from './function' import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor3, let as let__ } from './Functor' @@ -655,6 +655,15 @@ export const FromEither: FromEither3 = { fromEither } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader3 = { + URI, + fromReader +} + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -702,6 +711,18 @@ export const tapEither: { (f: (a: A) => Either): (self: ReaderEither) => ReaderEither } = /*#__PURE__*/ dual(2, tapEither_(FromEither, 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. + * + * @category combinators + * @since 2.16.0 + */ +export const tapReader: { + (self: ReaderEither, f: (a: A) => Reader): ReaderEither + (f: (a: A) => Reader): (self: ReaderEither) => ReaderEither +} = /*#__PURE__*/ dual(2, tapReader_(FromReader, Chain)) + /** * @category instances * @since 2.7.0 @@ -722,15 +743,6 @@ export const Alt: Alt3 = { alt: _alt } -/** - * @category instances - * @since 2.11.0 - */ -export const FromReader: FromReader3 = { - URI, - fromReader -} - /** * Reads the current context. * @@ -776,24 +788,28 @@ export const chainReaderKW: ( ) => (ma: ReaderEither) => ReaderEither = chainReaderK as any /** - * @category sequencing + * Alias of `tapReader`. + * + * @category legacy * @since 2.11.0 */ export const chainFirstReaderK: ( f: (a: A) => Reader -) => (ma: ReaderEither) => ReaderEither = /*#__PURE__*/ chainFirstReaderK_(FromReader, Chain) +) => (ma: ReaderEither) => ReaderEither = tapReader /** + * Alias of `tapReader`. + * * Less strict version of [`chainReaderK`](#chainreaderk). * * The `W` suffix (short for **W**idening) means that the environment types will be merged. * - * @category sequencing + * @category legacy * @since 2.11.0 */ export const chainFirstReaderKW: ( f: (a: A) => Reader -) => (ma: ReaderEither) => ReaderEither = chainFirstReaderK as any +) => (ma: ReaderEither) => ReaderEither = tapReader /** * @category instances diff --git a/src/ReaderIO.ts b/src/ReaderIO.ts index 096fde2ef..3263108e2 100644 --- a/src/ReaderIO.ts +++ b/src/ReaderIO.ts @@ -8,10 +8,10 @@ import { chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_, tapIO as tapIO_ } import { ask as ask_, asks as asks_, - chainFirstReaderK as chainFirstReaderK_, chainReaderK as chainReaderK_, FromReader2, - fromReaderK as fromReaderK_ + fromReaderK as fromReaderK_, + tapReader as tapReader_ } from './FromReader' import { dual, flow, identity, pipe, SK } from './function' import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor2 } from './Functor' @@ -282,6 +282,15 @@ export const FromIO: FromIO2 = { fromIO } +/** + * @category instances + * @since 2.13.0 + */ +export const FromReader: FromReader2 = { + URI, + fromReader +} + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -323,6 +332,18 @@ export const tapIO: { (f: (a: A) => IO<_>): (self: ReaderIO) => ReaderIO } = /*#__PURE__*/ dual(2, tapIO_(FromIO, 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. + * + * @category combinators + * @since 2.16.0 + */ +export const tapReader: { + (self: ReaderIO, f: (a: A) => Reader): ReaderIO + (f: (a: A) => Reader): (self: ReaderIO) => ReaderIO +} = /*#__PURE__*/ dual(2, tapReader_(FromReader, Chain)) + /** * @category lifting * @since 2.13.0 @@ -346,15 +367,6 @@ export const chainIOK: (f: (a: A) => I.IO) => (first: ReaderIO */ export const chainFirstIOK: (f: (a: A) => I.IO) => (first: ReaderIO) => ReaderIO = tapIO -/** - * @category instances - * @since 2.13.0 - */ -export const FromReader: FromReader2 = { - URI, - fromReader -} - /** * Reads the current context. * @@ -399,23 +411,27 @@ export const chainReaderKW: ( ) => (ma: ReaderIO) => ReaderIO = chainReaderK as any /** - * @category sequencing + * Alias of `tapReader`. + * + * @category legacy * @since 2.13.0 */ export const chainFirstReaderK: (f: (a: A) => R.Reader) => (ma: ReaderIO) => ReaderIO = - /*#__PURE__*/ chainFirstReaderK_(FromReader, Chain) + tapReader /** + * Alias of `tapReader`. + * * Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). * * The `W` suffix (short for **W**idening) means that the environment types will be merged. * - * @category sequencing + * @category legacy * @since 2.13.0 */ export const chainFirstReaderKW: ( f: (a: A) => R.Reader -) => (ma: ReaderIO) => ReaderIO = chainFirstReaderK as any +) => (ma: ReaderIO) => ReaderIO = tapReader // ------------------------------------------------------------------------------------- // do notation diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 9b5108f14..1f5e935f3 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -14,10 +14,10 @@ import { chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_, tapIO as tapIO_ } import { ask as ask_, asks as asks_, - chainFirstReaderK as chainFirstReaderK_, chainReaderK as chainReaderK_, FromReader2, - fromReaderK as fromReaderK_ + fromReaderK as fromReaderK_, + tapReader as tapReader_ } from './FromReader' import { chainTaskK as chainTaskK_, FromTask2, fromTaskK as fromTaskK_, tapTask as tapTask_ } from './FromTask' import { dual, flow, identity, pipe, SK } from './function' @@ -367,6 +367,15 @@ export const FromTask: FromTask2 = { fromTask } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader2 = { + URI, + fromReader +} + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -408,6 +417,18 @@ export const tapIO: { (f: (a: A) => IO<_>): (self: ReaderTask) => ReaderTask } = /*#__PURE__*/ dual(2, tapIO_(FromIO, 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. + * + * @category combinators + * @since 2.16.0 + */ +export const tapReader: { + (self: ReaderTask, f: (a: A) => R.Reader): ReaderTask + (f: (a: A) => R.Reader): (self: ReaderTask) => ReaderTask +} = /*#__PURE__*/ dual(2, tapReader_(FromReader, 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. @@ -459,15 +480,6 @@ export const chainIOK: (f: (a: A) => IO) => (first: ReaderTask */ export const chainFirstIOK: (f: (a: A) => IO) => (first: ReaderTask) => ReaderTask = tapIO -/** - * @category instances - * @since 2.11.0 - */ -export const FromReader: FromReader2 = { - URI, - fromReader -} - /** * Reads the current context. * @@ -512,23 +524,27 @@ export const chainReaderKW: ( ) => (ma: ReaderTask) => ReaderTask = chainReaderK as any /** - * @category sequencing + * Alias of `tapReader`. + * + * @category legacy * @since 2.11.0 */ export const chainFirstReaderK: (f: (a: A) => R.Reader) => (ma: ReaderTask) => ReaderTask = - /*#__PURE__*/ chainFirstReaderK_(FromReader, Chain) + tapReader /** + * Alias of `tapReader`. + * * Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). * * The `W` suffix (short for **W**idening) means that the environment types will be merged. * - * @category sequencing + * @category legacy * @since 2.11.0 */ export const chainFirstReaderKW: ( f: (a: A) => R.Reader -) => (ma: ReaderTask) => ReaderTask = chainFirstReaderK as any +) => (ma: ReaderTask) => ReaderTask = tapReader /** * @category lifting diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index c535af172..49c3d8d42 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -38,10 +38,10 @@ import { chainIOK as chainIOK_, FromIO3, fromIOK as fromIOK_, tapIO as tapIO_ } import { ask as ask_, asks as asks_, - chainFirstReaderK as chainFirstReaderK_, chainReaderK as chainReaderK_, FromReader3, - fromReaderK as fromReaderK_ + fromReaderK as fromReaderK_, + tapReader as tapReader_ } from './FromReader' import { chainTaskK as chainTaskK_, FromTask3, fromTaskK as fromTaskK_, tapTask as tapTask_ } from './FromTask' import { dual, flow, identity, LazyArg, pipe, SK } from './function' @@ -1031,6 +1031,15 @@ export const FromTask: FromTask3 = { fromTask } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader3 = { + URI, + fromReader +} + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -1139,6 +1148,18 @@ export const tapTask: { (f: (a: A) => Task<_>): (self: ReaderTaskEither) => ReaderTaskEither } = /*#__PURE__*/ dual(2, tapTask_(FromTask, 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. + * + * @category combinators + * @since 2.16.0 + */ +export const tapReader: { + (self: ReaderTaskEither, f: (a: A) => Reader): ReaderTaskEither + (f: (a: A) => Reader): (self: ReaderTaskEither) => ReaderTaskEither +} = /*#__PURE__*/ dual(2, tapReader_(FromReader, Chain)) + /** * @category instances * @since 2.7.0 @@ -1159,15 +1180,6 @@ export const Alt: Alt3 = { alt: _alt } -/** - * @category instances - * @since 2.11.0 - */ -export const FromReader: FromReader3 = { - URI, - fromReader -} - /** * Reads the current context. * @@ -1213,27 +1225,28 @@ export const chainReaderKW: ( ) => (ma: ReaderTaskEither) => ReaderTaskEither = chainReaderK as any /** - * @category sequencing + * Alias of `tapReader`. + * + * @category legacy * @since 2.11.0 */ export const chainFirstReaderK: ( f: (a: A) => R.Reader -) => (ma: ReaderTaskEither) => ReaderTaskEither = /*#__PURE__*/ chainFirstReaderK_( - FromReader, - Chain -) +) => (ma: ReaderTaskEither) => ReaderTaskEither = tapReader /** + * Alias of `tapReader`. + * * Less strict version of [`chainFirstReaderK`](#chainfirstreaderk). * * The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. * - * @category sequencing + * @category legacy * @since 2.11.0 */ export const chainFirstReaderKW: ( f: (a: A) => R.Reader -) => (ma: ReaderTaskEither) => ReaderTaskEither = chainFirstReaderK as any +) => (ma: ReaderTaskEither) => ReaderTaskEither = tapReader /** * @category lifting diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 5d0b6bd71..c2c848104 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -23,10 +23,10 @@ import { chainIOK as chainIOK_, FromIO4, fromIOK as fromIOK_, tapIO as tapIO_ } import { ask as ask_, asks as asks_, - chainFirstReaderK as chainFirstReaderK_, chainReaderK as chainReaderK_, FromReader4, - fromReaderK as fromReaderK_ + fromReaderK as fromReaderK_, + tapReader as tapReader_ } from './FromReader' import { chainStateK as chainStateK_, @@ -779,6 +779,15 @@ export const FromTask: FromTask4 = { fromTask } +/** + * @category instances + * @since 2.11.0 + */ +export const FromReader: FromReader4 = { + URI, + fromReader +} + /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. @@ -839,6 +848,25 @@ export const tapTask: { (f: (a: A) => Task<_>): (self: StateReaderTaskEither) => StateReaderTaskEither } = /*#__PURE__*/ dual(2, tapTask_(FromTask, 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. + * + * @category combinators + * @since 2.16.0 + */ +export const tapReader: { + (self: StateReaderTaskEither, f: (a: A) => Reader): StateReaderTaskEither< + S, + R1 & R2, + E, + A + > + (f: (a: A) => Reader): ( + self: StateReaderTaskEither + ) => StateReaderTaskEither +} = /*#__PURE__*/ dual(2, tapReader_(FromReader, Chain)) + /** * @category instances * @since 2.7.0 @@ -859,15 +887,6 @@ export const Alt: Alt4 = { alt: _alt } -/** - * @category instances - * @since 2.11.0 - */ -export const FromReader: FromReader4 = { - URI, - fromReader -} - /** * Reads the current context. * @@ -905,25 +924,28 @@ export const chainReaderK: ( ) /** + * Alias of `tapReader`. + * * Less strict version of [`chainReaderK`](#chainReaderK). * * The `W` suffix (short for **W**idening) means that the environment types and the error types will be merged. * - * @category sequencing + * @category legacy * @since 2.11.0 */ export const chainReaderKW: ( f: (a: A) => Reader -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = chainReaderK as any +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = tapReader /** - * @category sequencing + * Alias of `tapReader`. + * + * @category legacy * @since 2.11.0 */ export const chainFirstReaderK: ( f: (a: A) => Reader -) => (ma: StateReaderTaskEither) => StateReaderTaskEither = - /*#__PURE__*/ chainFirstReaderK_(FromReader, Chain) +) => (ma: StateReaderTaskEither) => StateReaderTaskEither = tapReader /** * Less strict version of [`chainFirstReaderK`](#chainFirstReaderK). diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 7622031f5..0b6a8619a 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -423,4 +423,8 @@ describe.concurrent('ReaderEither', () => { it('asUnit', () => { U.deepStrictEqual(pipe(_.of('a'), _.asUnit)(undefined), E.of(undefined)) }) + + it('tapReader', () => { + U.deepStrictEqual(_.tapReader(_.of(1), () => R.of(2))({}), E.of(1)) + }) }) diff --git a/test/ReaderIO.ts b/test/ReaderIO.ts index b8768df3b..33f4dd419 100644 --- a/test/ReaderIO.ts +++ b/test/ReaderIO.ts @@ -175,4 +175,8 @@ describe.concurrent('ReaderIO', () => { it('asUnit', () => { U.deepStrictEqual(pipe(_.of('a'), _.asUnit)('b')(), undefined) }) + + it('tapReader', () => { + U.deepStrictEqual(_.tapReader(_.of(1), () => R.of(2))({})(), 1) + }) }) diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts index 7251a245f..001fb3aa8 100644 --- a/test/ReaderTask.ts +++ b/test/ReaderTask.ts @@ -311,4 +311,8 @@ describe.concurrent('ReaderTask', () => { it('asUnit', async () => { U.deepStrictEqual(await pipe(_.of('a'), _.asUnit)('b')(), undefined) }) + + it('tapReader', async () => { + U.deepStrictEqual(await _.tapReader(_.of(1), () => R.of(2))({})(), 1) + }) }) diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index f927274b3..757c92d66 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -770,4 +770,8 @@ describe.concurrent('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.left('error'), _.tapTask(add))(undefined)(), E.left('error')) U.deepStrictEqual(ref, [1]) }) + + it('tapReader', async () => { + U.deepStrictEqual(await _.tapReader(_.of(1), () => R.of(2))({})(), E.of(1)) + }) }) diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index 2aaa7fb1d..9f18f1d5f 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -541,4 +541,14 @@ describe.concurrent('StateReaderTaskEither', () => { it('asUnit', async () => { U.deepStrictEqual(await pipe(_.of('a'), _.asUnit, _.evaluate(state))(undefined)(), E.of(undefined)) }) + + it('tapReader', async () => { + U.deepStrictEqual( + await pipe( + _.tapReader(_.of(1), () => R.of(2)), + _.evaluate(state) + )({})(), + E.of(1) + ) + }) })