Skip to content

Commit

Permalink
feat: flatMapState
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej authored and gcanti committed May 24, 2023
1 parent 3b992ad commit 8c7bbe9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/StateReaderTaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,23 @@ export const flatMapReaderTaskEither: {
): StateReaderTaskEither<S, R1 & R2, E1 | E2, B> => flatMap(self, (a: A) => fromReaderTaskEitherK(f)<S>(a))
)

/**
* @category sequencing
* @since 2.16.0
*/
export const flatMapState: {
<S, A, B>(f: (a: A) => State<S, B>): <R, E>(
self: StateReaderTaskEither<S, R, E, A>
) => StateReaderTaskEither<S, R, E, B>
<S, R, E, A, B>(self: StateReaderTaskEither<S, R, E, A>, f: (a: A) => State<S, B>): StateReaderTaskEither<S, R, E, B>
} = /*#__PURE__*/ dual(
2,
<S, R, E, A, B>(
self: StateReaderTaskEither<S, R, E, A>,
f: (a: A) => State<S, B>
): StateReaderTaskEither<S, R, E, B> => flatMap(self, fromStateK(f))
)

/**
* Less strict version of [`flatten`](#flatten).
*
Expand Down Expand Up @@ -812,7 +829,9 @@ export const fromStateK: <A extends ReadonlyArray<unknown>, S, B>(
) => <R = unknown, E = never>(...a: A) => StateReaderTaskEither<S, R, E, B> = /*#__PURE__*/ fromStateK_(FromState)

/**
* @category sequencing
* Alias of `flatMapState`.
*
* @category legacy
* @since 2.11.0
*/
export const chainStateK: <A, S, B>(
Expand Down
12 changes: 11 additions & 1 deletion test/StateReaderTaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 { of as stateOf, State } from '../src/State'
import * as _ from '../src/StateReaderTaskEither'
import * as S from '../src/string'
import * as T from '../src/Task'
Expand Down Expand Up @@ -638,4 +638,14 @@ describe.concurrent('StateReaderTaskEither', () => {
E.of(2)
)
})

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

0 comments on commit 8c7bbe9

Please sign in to comment.