Skip to content

Commit

Permalink
Add description & example to foldMap in Either
Browse files Browse the repository at this point in the history
  • Loading branch information
SRachamim authored and gcanti committed Oct 26, 2020
1 parent 5e5c0fb commit 082a384
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,28 @@ Added in v2.0.0
## foldMap
Map each element of the structure to a monoid, and combine the results.
**Signature**
```ts
export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => <E>(fa: Either<E, A>) => M
```
**Example**
```ts
import { pipe } from 'fp-ts/function'
import * as E from 'fp-ts/Either'
import { monoidString } from 'fp-ts/Monoid'

const yell = (a: string) => `${a}!`

assert.deepStrictEqual(pipe(E.right('a'), E.foldMap(monoidString)(yell)), 'a!')

assert.deepStrictEqual(pipe(E.left('e'), E.foldMap(monoidString)(yell)), monoidString.empty)
```

Added in v2.0.0

## reduce
Expand Down
19 changes: 19 additions & 0 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,25 @@ export const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => <E>(fa: Either<E, A>)
isLeft(fa) ? b : f(b, fa.right)

/**
* Map each element of the structure to a monoid, and combine the results.
*
* @example
* import { pipe } from 'fp-ts/function';
* import * as E from 'fp-ts/Either'
* import { monoidString } from 'fp-ts/Monoid'
*
* const yell = (a: string) => `${a}!`
*
* assert.deepStrictEqual(
* pipe(E.right('a'), E.foldMap(monoidString)(yell)),
* 'a!',
* )
*
* assert.deepStrictEqual(
* pipe(E.left('e'), E.foldMap(monoidString)(yell)),
* monoidString.empty,
* )
*
* @category Foldable
* @since 2.0.0
*/
Expand Down

0 comments on commit 082a384

Please sign in to comment.