Skip to content

Commit

Permalink
add mapError
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 19, 2023
1 parent e5abd49 commit 7929c17
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 70 deletions.
12 changes: 6 additions & 6 deletions docs/modules/EitherT.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,22 +559,22 @@ Added in v2.10.0
```ts
export declare function mapLeft<F extends URIS3>(
F: Functor3<F>
): <E, G>(f: (e: E) => G) => <R, FE, A>(fea: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <R, FE, A>(self: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
export declare function mapLeft<F extends URIS3, FE>(
F: Functor3C<F, FE>
): <E, G>(f: (e: E) => G) => <R, A>(fea: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <R, A>(self: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
export declare function mapLeft<F extends URIS2>(
F: Functor2<F>
): <E, G>(f: (e: E) => G) => <FE, A>(fea: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <FE, A>(self: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
export declare function mapLeft<F extends URIS2, FE>(
F: Functor2C<F, FE>
): <E, G>(f: (e: E) => G) => <A>(fea: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
export declare function mapLeft<F extends URIS>(
F: Functor1<F>
): <E, G>(f: (e: E) => G) => <A>(fea: Kind<F, Either<E, A>>) => Kind<F, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: Kind<F, Either<E, A>>) => Kind<F, Either<G, A>>
export declare function mapLeft<F>(
F: Functor<F>
): <E, G>(f: (e: E) => G) => <A>(fea: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>>
```

Added in v2.10.0
Expand Down
38 changes: 33 additions & 5 deletions docs/modules/IOEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Added in v2.0.0
- [getApplicativeIOValidation](#getapplicativeiovalidation)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [mapLeft](#mapleft)
- [mapError](#maperror)
- [orElse](#orelse)
- [orElseFirstIOK](#orelsefirstiok)
- [orElseW](#orelsew)
Expand Down Expand Up @@ -89,6 +89,7 @@ Added in v2.0.0
- [chainOptionKW](#chainoptionkw)
- [chainW](#chainw)
- [fromOptionK](#fromoptionk)
- [mapLeft](#mapleft)
- [orElseFirst](#orelsefirst)
- [orElseFirstW](#orelsefirstw)
- [lifting](#lifting)
Expand Down Expand Up @@ -515,17 +516,32 @@ export declare const getOrElseW: <E, B>(onLeft: (e: E) => I.IO<B>) => <A>(ma: IO

Added in v2.6.0

## mapLeft
## mapError

Map a function over the first type argument of a bifunctor.
Returns a `IOEither` with its error channel mapped using the specified function.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: IOEither<E, A>) => IOEither<G, A>
export declare const mapError: {
<E, G>(f: (e: E) => G): <A>(self: IOEither<E, A>) => IOEither<G, A>
<E, A, G>(self: IOEither<E, A>, f: (e: E) => G): IOEither<G, A>
}
```

Added in v2.0.0
**Example**

```ts
import * as IOEither from 'fp-ts/IOEither'
import * as Either from 'fp-ts/Either'
const f = (s: string) => new Error(s)
assert.deepStrictEqual(IOEither.mapError(IOEither.right(1), f)(), Either.right(1))
assert.deepStrictEqual(IOEither.mapError(IOEither.left('err'), f)(), Either.left(new Error('err')))
```

Added in v2.16.0

## orElse

Expand Down Expand Up @@ -977,6 +993,18 @@ export declare const fromOptionK: <E>(

Added in v2.10.0

## mapLeft

Alias of `mapError`.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: IOEither<E, A>) => IOEither<G, A>
```

Added in v2.0.0

## orElseFirst

Alias of `tapError`.
Expand Down
38 changes: 33 additions & 5 deletions docs/modules/ReaderEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Added in v2.0.0
- [getApplicativeReaderValidation](#getapplicativereadervalidation)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [mapLeft](#mapleft)
- [mapError](#maperror)
- [orElse](#orelse)
- [orElseW](#orelsew)
- [orLeft](#orleft)
Expand Down Expand Up @@ -79,6 +79,7 @@ Added in v2.0.0
- [chainOptionKW](#chainoptionkw)
- [chainW](#chainw)
- [fromOptionK](#fromoptionk)
- [mapLeft](#mapleft)
- [orElseFirst](#orelsefirst)
- [orElseFirstW](#orelsefirstw)
- [lifting](#lifting)
Expand Down Expand Up @@ -537,17 +538,32 @@ export declare const getOrElseW: <R2, E, B>(

Added in v2.6.0

## mapLeft
## mapError

Map a function over the second type argument of a bifunctor.
Returns a `ReaderEither` with its error channel mapped using the specified function.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <R, A>(fa: ReaderEither<R, E, A>) => ReaderEither<R, G, A>
export declare const mapError: {
<R, E, G>(f: (e: E) => G): <A>(self: ReaderEither<R, E, A>) => ReaderEither<R, G, A>
<R, E, A, G>(self: ReaderEither<R, E, A>, f: (e: E) => G): ReaderEither<R, G, A>
}
```

Added in v2.0.0
**Example**

```ts
import * as ReaderEither from 'fp-ts/ReaderEither'
import * as Either from 'fp-ts/Either'
const f = (s: string) => new Error(s)
assert.deepStrictEqual(ReaderEither.mapError(ReaderEither.right(1), f)({}), Either.right(1))
assert.deepStrictEqual(ReaderEither.mapError(ReaderEither.left('err'), f)({}), Either.left(new Error('err')))
```

Added in v2.16.0

## orElse

Expand Down Expand Up @@ -946,6 +962,18 @@ export declare const fromOptionK: <E>(

Added in v2.10.0

## mapLeft

Alias of `mapError`.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <R, A>(fa: ReaderEither<R, E, A>) => ReaderEither<R, G, A>
```

Added in v2.0.0

## orElseFirst

Alias of `tapError`.
Expand Down
49 changes: 42 additions & 7 deletions docs/modules/ReaderTaskEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Added in v2.0.0
- [getApplicativeReaderTaskValidation](#getapplicativereadertaskvalidation)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [mapLeft](#mapleft)
- [mapError](#maperror)
- [orElse](#orelse)
- [orElseW](#orelsew)
- [orLeft](#orleft)
Expand Down Expand Up @@ -106,6 +106,7 @@ Added in v2.0.0
- [chainW](#chainw)
- [fromNullableK](#fromnullablek)
- [fromOptionK](#fromoptionk)
- [mapLeft](#mapleft)
- [orElseFirst](#orelsefirst)
- [orElseFirstW](#orelsefirstw)
- [lifting](#lifting)
Expand Down Expand Up @@ -816,19 +817,39 @@ export declare const getOrElseW: <R2, E, B>(

Added in v2.6.0

## mapLeft
## mapError

Map a function over the second type argument of a bifunctor.
Returns a `ReaderTaskEither` with its error channel mapped using the specified function.

**Signature**

```ts
export declare const mapLeft: <E, G>(
f: (e: E) => G
) => <R, A>(fa: ReaderTaskEither<R, E, A>) => ReaderTaskEither<R, G, A>
export declare const mapError: {
<R, E, G>(f: (e: E) => G): <A>(self: ReaderTaskEither<R, E, A>) => ReaderTaskEither<R, G, A>
<R, E, A, G>(self: ReaderTaskEither<R, E, A>, f: (e: E) => G): ReaderTaskEither<R, G, A>
}
```

Added in v2.0.0
**Example**

```ts
import * as ReaderTaskEither from 'fp-ts/ReaderTaskEither'
import * as Either from 'fp-ts/Either'
const f = (s: string) => new Error(s)
async function test() {
assert.deepStrictEqual(await ReaderTaskEither.mapError(ReaderTaskEither.right(1), f)({})(), Either.right(1))
assert.deepStrictEqual(
await ReaderTaskEither.mapError(ReaderTaskEither.left('err'), f)({})(),
Either.left(new Error('err'))
)
}
test()
```

Added in v2.16.0

## orElse

Expand Down Expand Up @@ -1368,6 +1389,20 @@ export declare const fromOptionK: <E>(

Added in v2.10.0

## mapLeft

Alias of `mapError`.

**Signature**

```ts
export declare const mapLeft: <E, G>(
f: (e: E) => G
) => <R, A>(fa: ReaderTaskEither<R, E, A>) => ReaderTaskEither<R, G, A>
```

Added in v2.0.0

## orElseFirst

Alias of `tapError`.
Expand Down
42 changes: 37 additions & 5 deletions docs/modules/TaskEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Added in v2.0.0
- [getApplicativeTaskValidation](#getapplicativetaskvalidation)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [mapLeft](#mapleft)
- [mapError](#maperror)
- [orElse](#orelse)
- [orElseFirstIOK](#orelsefirstiok)
- [orElseFirstTaskK](#orelsefirsttaskk)
Expand Down Expand Up @@ -105,6 +105,7 @@ Added in v2.0.0
- [chainW](#chainw)
- [fromNullableK](#fromnullablek)
- [fromOptionK](#fromoptionk)
- [mapLeft](#mapleft)
- [orElseFirst](#orelsefirst)
- [orElseFirstW](#orelsefirstw)
- [lifting](#lifting)
Expand Down Expand Up @@ -722,17 +723,36 @@ export declare const getOrElseW: <E, B>(onLeft: (e: E) => T.Task<B>) => <A>(ma:

Added in v2.6.0

## mapLeft
## mapError

Map a function over the first type argument of a bifunctor.
Returns a `TaskEither` with its error channel mapped using the specified function.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: TaskEither<E, A>) => TaskEither<G, A>
export declare const mapError: {
<E, G>(f: (e: E) => G): <A>(self: TaskEither<E, A>) => TaskEither<G, A>
<E, A, G>(self: TaskEither<E, A>, f: (e: E) => G): TaskEither<G, A>
}
```

Added in v2.0.0
**Example**

```ts
import * as TaskEither from 'fp-ts/TaskEither'
import * as Either from 'fp-ts/Either'
const f = (s: string) => new Error(s)
async function test() {
assert.deepStrictEqual(await TaskEither.mapError(TaskEither.right(1), f)(), Either.right(1))
assert.deepStrictEqual(await TaskEither.mapError(TaskEither.left('err'), f)(), Either.left(new Error('err')))
}
test()
```

Added in v2.16.0

## orElse

Expand Down Expand Up @@ -1362,6 +1382,18 @@ export declare const fromOptionK: <E>(

Added in v2.10.0

## mapLeft

Alias of `mapError`.

**Signature**

```ts
export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: TaskEither<E, A>) => TaskEither<G, A>
```

Added in v2.0.0

## orElseFirst

Alias of `tapError`.
Expand Down
36 changes: 28 additions & 8 deletions src/EitherT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,26 +412,46 @@ export function bimap<F>(
*/
export function mapLeft<F extends URIS3>(
F: Functor3<F>
): <E, G>(f: (e: E) => G) => <R, FE, A>(fea: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <R, FE, A>(self: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
export function mapLeft<F extends URIS3, FE>(
F: Functor3C<F, FE>
): <E, G>(f: (e: E) => G) => <R, A>(fea: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <R, A>(self: Kind3<F, R, FE, Either<E, A>>) => Kind3<F, R, FE, Either<G, A>>
export function mapLeft<F extends URIS2>(
F: Functor2<F>
): <E, G>(f: (e: E) => G) => <FE, A>(fea: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <FE, A>(self: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
export function mapLeft<F extends URIS2, FE>(
F: Functor2C<F, FE>
): <E, G>(f: (e: E) => G) => <A>(fea: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: Kind2<F, FE, Either<E, A>>) => Kind2<F, FE, Either<G, A>>
export function mapLeft<F extends URIS>(
F: Functor1<F>
): <E, G>(f: (e: E) => G) => <A>(fea: Kind<F, Either<E, A>>) => Kind<F, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: Kind<F, Either<E, A>>) => Kind<F, Either<G, A>>
export function mapLeft<F>(
F: Functor<F>
): <E, G>(f: (e: E) => G) => <A>(fea: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>>
): <E, G>(f: (e: E) => G) => <A>(self: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>>
export function mapLeft<F>(
F: Functor<F>
): <E, G>(f: (e: E) => G) => <A>(fea: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>> {
return (f) => (fea) => F.map(fea, E.mapLeft(f))
): <E, G>(f: (e: E) => G) => <A>(self: HKT<F, Either<E, A>>) => HKT<F, Either<G, A>> {
const mapErrorF = mapError(F)
return (f) => (self) => mapErrorF(self, f)
}

/** @internal */
export function mapError<F extends URIS2>(
F: Functor2<F>
): <R, E, A, G>(self: Kind2<F, R, Either<E, A>>, f: (e: E) => G) => Kind2<F, R, Either<G, A>>
/** @internal */
export function mapError<F extends URIS>(
F: Functor1<F>
): <E, A, G>(self: Kind<F, Either<E, A>>, f: (e: E) => G) => Kind<F, Either<G, A>>
/** @internal */
export function mapError<F>(
F: Functor<F>
): <E, A, G>(self: HKT<F, Either<E, A>>, f: (e: E) => G) => HKT<F, Either<G, A>>
/** @internal */
export function mapError<F>(
F: Functor<F>
): <E, A, G>(self: HKT<F, Either<E, A>>, f: (e: E) => G) => HKT<F, Either<G, A>> {
return (self, f) => F.map(self, E.mapLeft(f))
}

/**
Expand Down
Loading

0 comments on commit 7929c17

Please sign in to comment.