Skip to content

Commit

Permalink
IOEither: add ApplicativePar, ApplicativeSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Oct 12, 2020
1 parent 7231e84 commit 98eca0d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
**Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.8.4

- **Polish**
- `IOEither`
- add `ApplicativePar` instance (@gcanti)
- add `ApplicativeSeq` instance (@gcanti)
- **Deprecation**
- `IOEither`
- deprecate `Applicative` in favour of `ApplicativePar` (@gcanti)

# 2.8.3

- **Polish**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fp-ts",
"version": "2.8.3",
"version": "2.8.4",
"description": "Functional programming in TypeScript",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
28 changes: 26 additions & 2 deletions src/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ export const chainEitherK: <E, A, B>(
const map_: Monad2<URI>['map'] = (fa, f) => pipe(fa, map(f))
/* istanbul ignore next */
const ap_: Monad2<URI>['ap'] = (fab, fa) => pipe(fab, ap(fa))
const apSeq_: Applicative2<URI>['ap'] = (fab, fa) =>
pipe(
fab,
chain((f) => pipe(fa, map(f)))
)
const of = right
/* istanbul ignore next */
const chain_: Monad2<URI>['chain'] = (ma, f) => pipe(ma, chain(f))
Expand Down Expand Up @@ -508,15 +513,34 @@ export const Bifunctor: Bifunctor2<URI> = {

/**
* @category instances
* @since 2.7.0
* @since 2.8.4
*/
export const Applicative: Applicative2<URI> = {
export const ApplicativePar: Applicative2<URI> = {
URI,
map: map_,
ap: ap_,
of
}

/**
* @category instances
* @since 2.8.4
*/
export const ApplicativeSeq: Applicative2<URI> = {
URI,
map: map_,
ap: apSeq_,
of
}

/**
* Use `ApplicativePar` instead
*
* @since 2.7.0
* @deprecated
*/
export const Applicative: Applicative2<URI> = ApplicativePar

/**
* @category instances
* @since 2.7.0
Expand Down
30 changes: 30 additions & 0 deletions test/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ describe('IOEither', () => {
assert.deepStrictEqual(pipe(_.right(double), _.ap(_.right(1)))(), E.right(2))
})

it('ApplicativePar', () => {
// tslint:disable-next-line: readonly-array
const log: Array<string> = []
const x = sequenceT(_.ApplicativePar)(
_.rightIO<string, number>(() => log.push('a')),
_.leftIO(() => {
log.push('b')
return 'error'
}),
_.rightIO(() => log.push('c'))
)()
assert.deepStrictEqual(x, E.left('error'))
assert.deepStrictEqual(log, ['a', 'b', 'c'])
})

it('ApplicativeSeq', () => {
// tslint:disable-next-line: readonly-array
const log: Array<string> = []
const x = sequenceT(_.ApplicativeSeq)(
_.rightIO<string, number>(() => log.push('a')),
_.leftIO(() => {
log.push('b')
return 'error'
}),
_.rightIO(() => log.push('c'))
)()
assert.deepStrictEqual(x, E.left('error'))
assert.deepStrictEqual(log, ['a', 'b'])
})

it('apFirst', () => {
assert.deepStrictEqual(pipe(_.right('a'), _.apFirst(_.right('b')))(), E.right('a'))
})
Expand Down

0 comments on commit 98eca0d

Please sign in to comment.