diff --git a/CHANGELOG.md b/CHANGELOG.md index 377b75a04..d4f6ec47f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ high state of flux, you're at risk of it changing without notice. - `ReadonlyArray` - `State` - `Task` + - `TaskOption` # 2.13.2 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 7952cc209..38ca0d6e3 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -28,7 +28,7 @@ import { FromTask1, fromTaskK as fromTaskK_ } from './FromTask' -import { flow, identity, Lazy, pipe, SK } from './function' +import { dual, flow, identity, Lazy, pipe, SK } from './function' import { bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' import * as _ from './internal' import { IO } from './IO' @@ -282,11 +282,19 @@ export const of: (a: A) => TaskOption = some /** * @category sequencing + * @since 2.14.0 + */ +export const flatMap: { + (f: (a: A) => TaskOption): (ma: TaskOption) => TaskOption + (ma: TaskOption, f: (a: A) => TaskOption): TaskOption +} = dual(2, OT.flatMap(T.Monad)) + +/** + * Alias of `flatMap`. + * * @since 2.10.0 */ -export const chain: (f: (a: A) => TaskOption) => (ma: TaskOption) => TaskOption = /*#__PURE__*/ OT.chain( - T.Monad -) +export const chain: (f: (a: A) => TaskOption) => (ma: TaskOption) => TaskOption = flatMap /** * @category sequencing @@ -380,8 +388,6 @@ export const partitionMap: ( const _map: Functor1['map'] = (fa, f) => pipe(fa, map(f)) const _ap: Apply1['ap'] = (fab, fa) => pipe(fab, ap(fa)) /* istanbul ignore next */ -const _chain: Monad1['chain'] = (ma, f) => pipe(ma, chain(f)) -/* istanbul ignore next */ const _alt: Alt1['alt'] = (fa, that) => pipe(fa, alt(that)) /* istanbul ignore next */ const _filter: Filterable1['filter'] = (fa: TaskOption, predicate: Predicate) => @@ -514,7 +520,7 @@ export const Chain: Chain1 = { URI, map: _map, ap: _ap, - chain: _chain + chain: flatMap } /** @@ -574,7 +580,7 @@ export const Monad: Monad1 = { map: _map, ap: _ap, of, - chain: _chain + chain: flatMap } /** @@ -586,7 +592,7 @@ export const MonadIO: MonadIO1 = { map: _map, ap: _ap, of, - chain: _chain, + chain: flatMap, fromIO } @@ -599,7 +605,7 @@ export const MonadTask: MonadTask1 = { map: _map, ap: _ap, of, - chain: _chain, + chain: flatMap, fromIO, fromTask } diff --git a/test/TaskOption.ts b/test/TaskOption.ts index ddd00029b..61fe60cbe 100644 --- a/test/TaskOption.ts +++ b/test/TaskOption.ts @@ -24,6 +24,20 @@ describe('TaskOption', () => { U.deepStrictEqual(await pipe(_.none, _.ap(_.none))(), O.none) }) + it('flatMap', async () => { + const f = (n: number) => _.some(n * 2) + const g = () => _.none + U.deepStrictEqual(await pipe(_.some(1), _.flatMap(f))(), O.some(2)) + U.deepStrictEqual(await pipe(_.none, _.flatMap(f))(), O.none) + U.deepStrictEqual(await pipe(_.some(1), _.flatMap(g))(), O.none) + U.deepStrictEqual(await pipe(_.none, _.flatMap(g))(), O.none) + + U.deepStrictEqual(await _.flatMap(_.some(1), f)(), O.some(2)) + U.deepStrictEqual(await _.flatMap(_.none, f)(), O.none) + U.deepStrictEqual(await _.flatMap(_.some(1), g)(), O.none) + U.deepStrictEqual(await _.flatMap(_.none, g)(), O.none) + }) + it('chain', async () => { const f = (n: number) => _.some(n * 2) const g = () => _.none