Skip to content

Commit

Permalink
TaskOption: add flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 20, 2023
1 parent ae33d60 commit b52d3d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ high state of flux, you're at risk of it changing without notice.
- `ReadonlyArray`
- `State`
- `Task`
- `TaskOption`

# 2.13.2

Expand Down
26 changes: 16 additions & 10 deletions src/TaskOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -282,11 +282,19 @@ export const of: <A>(a: A) => TaskOption<A> = some

/**
* @category sequencing
* @since 2.14.0
*/
export const flatMap: {
<A, B>(f: (a: A) => TaskOption<B>): (ma: TaskOption<A>) => TaskOption<B>
<A, B>(ma: TaskOption<A>, f: (a: A) => TaskOption<B>): TaskOption<B>
} = dual(2, OT.flatMap(T.Monad))

/**
* Alias of `flatMap`.
*
* @since 2.10.0
*/
export const chain: <A, B>(f: (a: A) => TaskOption<B>) => (ma: TaskOption<A>) => TaskOption<B> = /*#__PURE__*/ OT.chain(
T.Monad
)
export const chain: <A, B>(f: (a: A) => TaskOption<B>) => (ma: TaskOption<A>) => TaskOption<B> = flatMap

/**
* @category sequencing
Expand Down Expand Up @@ -380,8 +388,6 @@ export const partitionMap: <A, B, C>(
const _map: Functor1<URI>['map'] = (fa, f) => pipe(fa, map(f))
const _ap: Apply1<URI>['ap'] = (fab, fa) => pipe(fab, ap(fa))
/* istanbul ignore next */
const _chain: Monad1<URI>['chain'] = (ma, f) => pipe(ma, chain(f))
/* istanbul ignore next */
const _alt: Alt1<URI>['alt'] = (fa, that) => pipe(fa, alt(that))
/* istanbul ignore next */
const _filter: Filterable1<URI>['filter'] = <A>(fa: TaskOption<A>, predicate: Predicate<A>) =>
Expand Down Expand Up @@ -514,7 +520,7 @@ export const Chain: Chain1<URI> = {
URI,
map: _map,
ap: _ap,
chain: _chain
chain: flatMap
}

/**
Expand Down Expand Up @@ -574,7 +580,7 @@ export const Monad: Monad1<URI> = {
map: _map,
ap: _ap,
of,
chain: _chain
chain: flatMap
}

/**
Expand All @@ -586,7 +592,7 @@ export const MonadIO: MonadIO1<URI> = {
map: _map,
ap: _ap,
of,
chain: _chain,
chain: flatMap,
fromIO
}

Expand All @@ -599,7 +605,7 @@ export const MonadTask: MonadTask1<URI> = {
map: _map,
ap: _ap,
of,
chain: _chain,
chain: flatMap,
fromIO,
fromTask
}
Expand Down
14 changes: 14 additions & 0 deletions test/TaskOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b52d3d2

Please sign in to comment.