Skip to content

Commit

Permalink
replace chain with flatMap when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 21, 2023
1 parent 4b43d6a commit c954d24
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Do.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ const nameDo: T.Task<void> = pipe(
T.chainFirst(() => putStrLn('And your last name? ')),
T.bind('last', () => getLine),
T.bind('full', ({ first, last }) => T.of(first + ' ' + last)),
T.chain(({ full }) => putStrLn('Pleased to meet you, ' + full + '!'))
T.flatMap(({ full }) => putStrLn('Pleased to meet you, ' + full + '!'))
)
```
2 changes: 1 addition & 1 deletion docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const functional = (as: ReadonlyArray<number>): string => {
as,
head,
E.map(double),
E.chain(inverse),
E.flatMap(inverse),
E.match(
(err) => `Error is ${err}`, // onLeft handler
(head) => `Result is ${head}` // onRight handler
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/IORef.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export declare class IORef<A> {
**Example**

```ts
import { io } from 'fp-ts/IO'
import { flatMap } from 'fp-ts/IO'
import { newIORef } from 'fp-ts/IORef'
assert.strictEqual(io.chain(newIORef(1), (ref) => io.chain(ref.write(2), () => ref.read))(), 2)
assert.strictEqual(flatMap(newIORef(1), (ref) => flatMap(ref.write(2), () => ref.read))(), 2)
```

Added in v2.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const functional = (as: ReadonlyArray<number>): string => {
as,
head,
O.map(double),
O.chain(inverse),
O.flatMap(inverse),
O.match(
() => 'no result', // onNone handler
(head) => `Result is ${head}` // onSome handler
Expand Down
2 changes: 1 addition & 1 deletion src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* as,
* head,
* E.map(double),
* E.chain(inverse),
* E.flatMap(inverse),
* E.match(
* (err) => `Error is ${err}`, // onLeft handler
* (head) => `Result is ${head}` // onRight handler
Expand Down
15 changes: 1 addition & 14 deletions src/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,20 +891,7 @@ export const bracketW: <E1, A, E2, B, E3>(
use: (a: A) => IOEither<E2, B>,
release: (a: A, e: E.Either<E2, B>) => IOEither<E3, void>
) => IOEither<E1 | E2 | E3, B> = (acquire, use, release) =>
pipe(
acquire,
chainW((a) =>
pipe(
use(a),
I.chain((e) =>
pipe(
release(a, e),
chainW(() => I.of(e))
)
)
)
)
)
flatMap(acquire, (a) => I.flatMap(use(a), (e) => flatMap(release(a, e), () => I.of(e))))

// -------------------------------------------------------------------------------------
// do notation
Expand Down
4 changes: 2 additions & 2 deletions src/IORef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { IO } from './IO'

/**
* @example
* import { io } from 'fp-ts/IO'
* import { flatMap } from 'fp-ts/IO'
* import { newIORef } from 'fp-ts/IORef'
*
* assert.strictEqual(io.chain(newIORef(1), ref => io.chain(ref.write(2), () => ref.read))(), 2)
* assert.strictEqual(flatMap(newIORef(1), ref => flatMap(ref.write(2), () => ref.read))(), 2)
*
* @category model
* @since 2.0.0
Expand Down
2 changes: 1 addition & 1 deletion src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* as,
* head,
* O.map(double),
* O.chain(inverse),
* O.flatMap(inverse),
* O.match(
* () => 'no result', // onNone handler
* (head) => `Result is ${head}` // onSome handler
Expand Down
15 changes: 1 addition & 14 deletions src/TaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,20 +1259,7 @@ export const bracketW: <E1, A, E2, B, E3>(
use: (a: A) => TaskEither<E2, B>,
release: (a: A, e: E.Either<E2, B>) => TaskEither<E3, void>
) => TaskEither<E1 | E2 | E3, B> = (acquire, use, release) =>
pipe(
acquire,
chainW((a) =>
pipe(
use(a),
T.chain((e) =>
pipe(
release(a, e),
chainW(() => T.of(e))
)
)
)
)
)
flatMap(acquire, (a) => T.flatMap(use(a), (e) => flatMap(release(a, e), () => T.of(e))))

// -------------------------------------------------------------------------------------
// do notation
Expand Down

0 comments on commit c954d24

Please sign in to comment.