Skip to content

Commit

Permalink
feat: flatMapReaderTaskEither
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej authored and gcanti committed May 24, 2023
1 parent 19b7134 commit 3b992ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/StateReaderTaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ export const fromReaderTaskEitherK =
fromReaderTaskEither(f(...a))

/**
* Alias of `flatMapReaderTaskEither`.
*
* Less strict version of [`chainReaderTaskEitherK`](#chainreadertaskeitherk).
*
* @category sequencing
* @category legacy
* @since 2.6.1
*/
export const chainReaderTaskEitherKW =
Expand All @@ -301,7 +303,9 @@ export const chainReaderTaskEitherKW =
flatMap<S, R, E1, A, R, E1 | E2, B>(ma, fromReaderTaskEitherK(f))

/**
* @category sequencing
* Alias of `flatMapReaderTaskEither`.
*
* @category legacy
* @since 2.4.0
*/
export const chainReaderTaskEitherK: <R, E, A, B>(
Expand Down Expand Up @@ -551,6 +555,26 @@ export const flatMapOption: {
): StateReaderTaskEither<S, R, E1 | E2, B>
} = /*#__PURE__*/ _.flatMapOption(_FromEither, _FlatMap)

/**
* @category sequencing
* @since 2.16.0
*/
export const flatMapReaderTaskEither: {
<A, R2, E2, B>(f: (a: A) => ReaderTaskEither<R2, E2, B>): <S, R1, E1>(
self: StateReaderTaskEither<S, R1, E1, A>
) => StateReaderTaskEither<S, R1 & R2, E1 | E2, B>
<S, R1, E1, A, R2, E2, B>(
self: StateReaderTaskEither<S, R1, E1, A>,
f: (a: A) => ReaderTaskEither<R2, E2, B>
): StateReaderTaskEither<S, R1 & R2, E1 | E2, B>
} = /*#__PURE__*/ dual(
2,
<S, R1, E1, A, R2, E2, B>(
self: StateReaderTaskEither<S, R1, E1, A>,
f: (a: A) => ReaderTaskEither<R2, E2, B>
): StateReaderTaskEither<S, R1 & R2, E1 | E2, B> => flatMap(self, (a: A) => fromReaderTaskEitherK(f)<S>(a))
)

/**
* Less strict version of [`flatten`](#flatten).
*
Expand Down
10 changes: 10 additions & 0 deletions test/StateReaderTaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,14 @@ describe.concurrent('StateReaderTaskEither', () => {
E.left('error')
)
})

it('flatMapReaderTaskEither', async () => {
U.deepStrictEqual(
await pipe(
_.flatMapReaderTaskEither(_.of(1), () => RTE.of(2)),
_.evaluate(state)
)(undefined)(),
E.of(2)
)
})
})

0 comments on commit 3b992ad

Please sign in to comment.