Hyperlinkv0.8.0-beta.28

Stream

Stream.mapBothconsteffect/Stream.ts:1926
<E, E2, A, A2>(options: {
  readonly onFailure: (e: E) => E2
  readonly onSuccess: (a: A) => A2
}): <R>(self: Stream<A, E, R>) => Stream<A2, E2, R>
<A, E, R, E2, A2>(
  self: Stream<A, E, R>,
  options: {
    readonly onFailure: (e: E) => E2
    readonly onSuccess: (a: A) => A2
  }
): Stream<A2, E2, R>

Maps both the failure and success channels of a stream.

Example (Mapping both the failure and success channels of a stream)

import { Console, Effect, Stream } from "effect"

const mapper = {
  onFailure: (error: string) => `error: ${error}`,
  onSuccess: (value: number) => value * 2
}

const program = Effect.gen(function*() {
  const success = yield* Stream.make(1, 2).pipe(
    Stream.mapBoth(mapper),
    Stream.runCollect
  )
  yield* Console.log(success)

  const failure = yield* Stream.fail("boom").pipe(
    Stream.mapBoth(mapper),
    Stream.catch((error: string) => Stream.succeed(error)),
    Stream.runCollect
  )
  yield* Console.log(failure)
})

Effect.runPromise(program)
// Output: [ 2, 4 ]
// Output: [ "error: boom" ]
mapping
Source effect/Stream.ts:192616 lines
export const mapBoth: {
  <E, E2, A, A2>(
    options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
  ): <R>(self: Stream<A, E, R>) => Stream<A2, E2, R>
  <A, E, R, E2, A2>(
    self: Stream<A, E, R>,
    options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
  ): Stream<A2, E2, R>
} = dual(2, <A, E, R, E2, A2>(
  self: Stream<A, E, R>,
  options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2 }
): Stream<A2, E2, R> =>
  self.pipe(
    map(options.onSuccess),
    mapError(options.onFailure)
  ))