Hyperlinkv0.8.0-beta.28

Result

Result.mapconsteffect/Result.ts:859
<A, A2>(f: (ok: A) => A2): <E>(self: Result<A, E>) => Result<A2, E>
<A, E, A2>(self: Result<A, E>, f: (ok: A) => A2): Result<A2, E>

Transforms the success channel of a Result, leaving the failure channel unchanged.

When to use

Use to apply a transformation to the success value of a Result while preserving any existing failure.

Details

  • If the result is a Success, applies f to the value and returns a new Success
  • If the result is a Failure, returns it as-is
  • Use flatMap if f returns a Result (to avoid nested Results)

Example (Doubling the success value)

import { pipe, Result } from "effect"

const result = pipe(
  Result.succeed(3),
  Result.map((n) => n * 2)
)
console.log(result)
// Output: { _tag: "Success", success: 6, ... }
Source effect/Result.ts:8598 lines
export const map: {
  <A, A2>(f: (ok: A) => A2): <E>(self: Result<A, E>) => Result<A2, E>
  <A, E, A2>(self: Result<A, E>, f: (ok: A) => A2): Result<A2, E>
} = dual(
  2,
  <A, E, A2>(self: Result<A, E>, f: (ok: A) => A2): Result<A2, E> =>
    isSuccess(self) ? succeed(f(self.success)) : fail(self.failure)
)
Referenced by 8 symbols