Hyperlinkv0.8.0-beta.28

Tuple

Tuple.mapconsteffect/Tuple.ts:389
<L extends Lambda>(lambda: L): <const T extends ReadonlyArray<unknown>>(
  self: T
) => { [K in keyof T]: Apply<L, T[K]> }
<const T extends ReadonlyArray<unknown>, L extends Lambda>(
  self: T,
  lambda: L
): { [K in keyof T]: Apply<L, T[K]> }

Applies a Struct.Lambda transformation to every element in a tuple.

When to use

Use when you want to apply the same transformation to every tuple element.

Details

The lambda lets the compiler track the output type for each element.

Gotchas

The lambda must be created with Struct.lambda; a plain function will not type-check.

Example (Wrapping every element in an array)

import { pipe, Struct, Tuple } from "effect"

interface AsArray extends Struct.Lambda {
  <A>(self: A): Array<A>
  readonly "~lambda.out": Array<this["~lambda.in"]>
}

const asArray = Struct.lambda<AsArray>((a) => [a])
const result = pipe(Tuple.make(1, "hello", true), Tuple.map(asArray))
console.log(result) // [[1], ["hello"], [true]]
Source effect/Tuple.ts:38916 lines
export const map: {
  <L extends Lambda>(
    lambda: L
  ): <const T extends ReadonlyArray<unknown>>(
    self: T
  ) => { [K in keyof T]: Apply<L, T[K]> }
  <const T extends ReadonlyArray<unknown>, L extends Lambda>(
    self: T,
    lambda: L
  ): { [K in keyof T]: Apply<L, T[K]> }
} = dual(
  2,
  <const T extends ReadonlyArray<unknown>, L extends Function>(self: T, lambda: L) => {
    return self.map((e) => lambda(e))
  }
)