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: Either mapError #1882

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 34 additions & 5 deletions docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Added in v2.0.0
- [getApplicativeValidation](#getapplicativevalidation)
- [getOrElse](#getorelse)
- [getOrElseW](#getorelsew)
- [mapLeft](#mapleft)
- [mapError](#maperror)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conceptually Either contains 2 values. Don't think there is a certainty for Left to be an "error".

Copy link
Owner

@gcanti gcanti May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: Yeah, mapError in Either.ts was intentionally omitted (no mapError in @effect/data/Either as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, kinda makes sense. The inconsistency is unfortunate tho as now the TaskEither or ReaderTaskEither have the mapError while the Either doesn't.

True, the Either doesn't need to represent success / fail and can be used for anything where distinction for two outcomes is desired. But, at least from my experience, it is used for failable computations most of the time.

- [orElse](#orelse)
- [orElseW](#orelsew)
- [filtering](#filtering)
Expand Down Expand Up @@ -142,6 +142,7 @@ Added in v2.0.0
- [chainW](#chainw)
- [fromNullableK](#fromnullablek)
- [fromOptionK](#fromoptionk)
- [mapLeft](#mapleft)
- [lifting](#lifting)
- [fromPredicate](#frompredicate)
- [liftNullable](#liftnullable)
Expand Down Expand Up @@ -642,17 +643,31 @@ export declare const getOrElseW: <E, B>(onLeft: (e: E) => B) => <A>(ma: Either<E

Added in v2.6.0

## mapLeft
## mapError

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

**Signature**

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

Added in v2.0.0
**Example**

```ts
import * as Either from 'fp-ts/Either'

const f = (s: string) => new Error(s)

assert.deepStrictEqual(Either.mapError(Either.right(1), f), Either.right(1))
assert.deepStrictEqual(Either.mapError(Either.left('err'), f), Either.left(new Error('err')))
```

Added in v2.17.0

## orElse

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

Added in v2.10.0

## mapLeft

Alias of `mapError`.

Map a function over the first type argument of a bifunctor.

**Signature**

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

Added in v2.0.0

# lifting

## fromPredicate
Expand Down
26 changes: 23 additions & 3 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,33 @@ export const bimap: <E, G, A, B>(f: (e: E) => G, g: (a: A) => B) => (fa: Either<
isLeft(fa) ? left(f(fa.left)) : right(g(fa.right))

/**
* Map a function over the first type argument of a bifunctor.
* Returns an `Either` with its error channel mapped using the specified function.
*
* @example
* import * as Either from 'fp-ts/Either'
*
* const f = (s: string) => new Error(s)
*
* assert.deepStrictEqual(Either.mapError(Either.right(1), f), Either.right(1))
* assert.deepStrictEqual(Either.mapError(Either.left('err'), f), Either.left(new Error('err')))
*
* @category error handling
* @since 2.17.0
*/
export const mapError: {
<E, G>(f: (e: E) => G): <A>(self: Either<E, A>) => Either<G, A>
<E, A, G>(self: Either<E, A>, f: (e: E) => G): Either<G, A>
} = /*#__PURE__*/ dual(2, (self, f) => (isLeft(self) ? left(f(self.left)) : self))

/**
* Alias of `mapError`.
*
* Map a function over the first type argument of a bifunctor.
*
* @category legacy
* @since 2.0.0
*/
export const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: Either<E, A>) => Either<G, A> = (f) => (fa) =>
isLeft(fa) ? left(f(fa.left)) : fa
export const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: Either<E, A>) => Either<G, A> = mapError

/**
* @category instances
Expand Down
5 changes: 5 additions & 0 deletions test/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe.concurrent('Either', () => {
U.deepStrictEqual(pipe(_.left(2), _.mapLeft(U.double)), _.left(4))
})

it('mapError', () => {
U.deepStrictEqual(_.mapError(_.right('bar'), U.double), _.right('bar'))
U.deepStrictEqual(_.mapError(_.left(2), U.double), _.left(4))
})

it('alt', () => {
U.deepStrictEqual(
pipe(
Expand Down