diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index 24768f428..e73dd368b 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -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: Monoid) => (f: (a: A) => M) => (fa: Either) => 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 diff --git a/src/Either.ts b/src/Either.ts index 5c92f870d..4d59e9b0c 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -529,6 +529,25 @@ export const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Either) 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 */