Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tapEither #1864

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 64 additions & 26 deletions docs/modules/IOEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Added in v2.0.0

- [combinators](#combinators)
- [tap](#tap)
- [tapEither](#tapeither)
- [constructors](#constructors)
- [left](#left)
- [leftIO](#leftio)
Expand Down Expand Up @@ -79,6 +80,8 @@ Added in v2.0.0
- [chainEitherK](#chaineitherk)
- [chainEitherKW](#chaineitherkw)
- [chainFirst](#chainfirst)
- [chainFirstEitherK](#chainfirsteitherk)
- [chainFirstEitherKW](#chainfirsteitherkw)
- [chainFirstW](#chainfirstw)
- [chainOptionK](#chainoptionk)
- [chainOptionKW](#chainoptionkw)
Expand Down Expand Up @@ -106,8 +109,6 @@ Added in v2.0.0
- [matchEW](#matchew)
- [matchW](#matchw)
- [sequencing](#sequencing)
- [chainFirstEitherK](#chainfirsteitherk)
- [chainFirstEitherKW](#chainfirsteitherkw)
- [chainFirstIOK](#chainfirstiok)
- [chainIOK](#chainiok)
- [flatMap](#flatmap)
Expand Down Expand Up @@ -170,6 +171,39 @@ export declare const tap: {

Added in v2.15.0

## tapEither

Composes computations in sequence, using the return value of one computation to determine the next computation and
keeping only the result of the first.

**Signature**

```ts
export declare const tapEither: {
<E1, A, E2, _>(self: IOEither<E1, A>, f: (a: A) => E.Either<E2, _>): IOEither<E1 | E2, A>
<A, E2, _>(f: (a: A) => E.Either<E2, _>): <E1>(self: IOEither<E1, A>) => IOEither<E2 | E1, A>
}
```

**Example**

```ts
import { pipe } from 'fp-ts/function'
import * as IOE from 'fp-ts/IOEither'
import * as E from 'fp-ts/Either'

const compute = (value: string) =>
pipe(
IOE.of(value),
IOE.tapEither(() => (value.length > 0 ? E.right('ok') : E.left('error')))
)

assert.deepStrictEqual(compute('')(), E.left('error'))
assert.deepStrictEqual(compute('fp-ts')(), E.right('fp-ts'))
```

Added in v2.16.0

# constructors

## left
Expand Down Expand Up @@ -794,6 +828,34 @@ export declare const chainFirst: <E, A, B>(f: (a: A) => IOEither<E, B>) => (ma:

Added in v2.0.0

## chainFirstEitherK

Alias of `tapEither`.

**Signature**

```ts
export declare const chainFirstEitherK: <A, E, B>(f: (a: A) => E.Either<E, B>) => (ma: IOEither<E, A>) => IOEither<E, A>
```

Added in v2.12.0

## chainFirstEitherKW

Alias of `tapEither`.

The `W` suffix (short for **W**idening) means that the error types will be merged.

**Signature**

```ts
export declare const chainFirstEitherKW: <A, E2, B>(
f: (a: A) => E.Either<E2, B>
) => <E1>(ma: IOEither<E1, A>) => IOEither<E2 | E1, A>
```

Added in v2.12.0

## chainFirstW

Alias of `tap`.
Expand Down Expand Up @@ -1098,30 +1160,6 @@ Added in v2.10.0

# sequencing

## chainFirstEitherK

**Signature**

```ts
export declare const chainFirstEitherK: <A, E, B>(f: (a: A) => E.Either<E, B>) => (ma: IOEither<E, A>) => IOEither<E, A>
```

Added in v2.12.0

## chainFirstEitherKW

The `W` suffix (short for **W**idening) means that the error types will be merged.

**Signature**

```ts
export declare const chainFirstEitherKW: <A, E2, B>(
f: (a: A) => E.Either<E2, B>
) => <E1>(ma: IOEither<E1, A>) => IOEither<E2 | E1, A>
```

Added in v2.12.0

## chainFirstIOK

**Signature**
Expand Down
59 changes: 48 additions & 11 deletions docs/modules/IOOption.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Added in v2.12.0

- [combinators](#combinators)
- [tap](#tap)
- [tapEither](#tapeither)
- [constructors](#constructors)
- [none](#none)
- [of](#of)
Expand Down Expand Up @@ -68,6 +69,7 @@ Added in v2.12.0
- [legacy](#legacy)
- [chain](#chain)
- [chainFirst](#chainfirst)
- [chainFirstEitherK](#chainfirsteitherk)
- [lifting](#lifting)
- [fromEitherK](#fromeitherk)
- [fromIOK](#fromiok)
Expand All @@ -87,7 +89,6 @@ Added in v2.12.0
- [matchW](#matchw)
- [sequencing](#sequencing)
- [chainEitherK](#chaineitherk)
- [chainFirstEitherK](#chainfirsteitherk)
- [chainFirstIOK](#chainfirstiok)
- [chainIOK](#chainiok)
- [chainNullableK](#chainnullablek)
Expand Down Expand Up @@ -127,6 +128,40 @@ export declare const tap: {

Added in v2.15.0

## tapEither

Composes computations in sequence, using the return value of one computation to determine the next computation and
keeping only the result of the first.

**Signature**

```ts
export declare const tapEither: {
<A, E, _>(self: IOOption<A>, f: (a: A) => Either<E, _>): IOOption<A>
<A, E, _>(f: (a: A) => Either<E, _>): (self: IOOption<A>) => IOOption<A>
}
```

**Example**

```ts
import { pipe } from 'fp-ts/function'
import * as IOO from 'fp-ts/IOOption'
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'

const compute = (value: number) =>
pipe(
IOO.of(value),
IOO.tapEither((value) => (value > 0 ? E.right('ok') : E.left('error')))
)

assert.deepStrictEqual(compute(1)(), O.of(1))
assert.deepStrictEqual(compute(-1)(), O.none)
```

Added in v2.16.0

# constructors

## none
Expand Down Expand Up @@ -592,6 +627,18 @@ export declare const chainFirst: <A, B>(f: (a: A) => IOOption<B>) => (first: IOO

Added in v2.12.0

## chainFirstEitherK

Alias of `tapEither`.

**Signature**

```ts
export declare const chainFirstEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: IOOption<A>) => IOOption<A>
```

Added in v2.12.0

# lifting

## fromEitherK
Expand Down Expand Up @@ -770,16 +817,6 @@ export declare const chainEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma:

Added in v2.12.0

## chainFirstEitherK

**Signature**

```ts
export declare const chainFirstEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: IOOption<A>) => IOOption<A>
```

Added in v2.12.0

## chainFirstIOK

**Signature**
Expand Down
58 changes: 47 additions & 11 deletions docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Added in v2.0.0

- [combinators](#combinators)
- [tap](#tap)
- [tapEither](#tapeither)
- [constructors](#constructors)
- [getLeft](#getleft)
- [getRight](#getright)
Expand Down Expand Up @@ -137,6 +138,7 @@ Added in v2.0.0
- [legacy](#legacy)
- [chain](#chain)
- [chainFirst](#chainfirst)
- [chainFirstEitherK](#chainfirsteitherk)
- [lifting](#lifting)
- [fromEitherK](#fromeitherk)
- [fromNullableK](#fromnullablek)
Expand All @@ -158,7 +160,6 @@ Added in v2.0.0
- [isSome](#issome)
- [sequencing](#sequencing)
- [chainEitherK](#chaineitherk)
- [chainFirstEitherK](#chainfirsteitherk)
- [chainNullableK](#chainnullablek)
- [flatMap](#flatmap)
- [flatten](#flatten)
Expand Down Expand Up @@ -213,6 +214,39 @@ export declare const tap: {

Added in v2.15.0

## tapEither

Composes computations in sequence, using the return value of one computation to determine the next computation and
keeping only the result of the first.

**Signature**

```ts
export declare const tapEither: {
<A, E, _>(self: Option<A>, f: (a: A) => Either<E, _>): Option<A>
<A, E, _>(f: (a: A) => Either<E, _>): (self: Option<A>) => Option<A>
}
```

**Example**

```ts
import { pipe } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'

const compute = (value: number) =>
pipe(
O.of(value),
O.tapEither((value) => (value > 0 ? E.right('ok') : E.left('error')))
)

assert.deepStrictEqual(compute(1), O.of(1))
assert.deepStrictEqual(compute(-42), O.none)
```

Added in v2.16.0

# constructors

## getLeft
Expand Down Expand Up @@ -1034,6 +1068,18 @@ export declare const chainFirst: <A, B>(f: (a: A) => Option<B>) => (first: Optio

Added in v2.0.0

## chainFirstEitherK

Alias of `tapEither`.

**Signature**

```ts
export declare const chainFirstEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Option<A>) => Option<A>
```

Added in v2.12.0

# lifting

## fromEitherK
Expand Down Expand Up @@ -1300,16 +1346,6 @@ export declare const chainEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma:

Added in v2.11.0

## chainFirstEitherK

**Signature**

```ts
export declare const chainFirstEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Option<A>) => Option<A>
```

Added in v2.12.0

## chainNullableK

This is `chain` + `fromNullable`, useful when working with optional values.
Expand Down
Loading