Hyperlinkv0.8.0-beta.28

Tuple

Tuple.mapPickconsteffect/Tuple.ts:438
<
  const T extends ReadonlyArray<unknown>,
  const I extends ReadonlyArray<Indices<T>>,
  L extends Lambda
>(
  indices: I,
  lambda: L
): (self: T) => {
  [K in keyof T]: K extends `${I[number]}` ? Apply<L, T[K]> : T[K]
}
<
  const T extends ReadonlyArray<unknown>,
  const I extends ReadonlyArray<Indices<T>>,
  L extends Lambda
>(
  self: T,
  indices: I,
  lambda: L
): { [K in keyof T]: K extends `${I[number]}` ? Apply<L, T[K]> : T[K] }

Applies a Struct.Lambda transformation only to the elements at the specified indices; all other elements are copied unchanged.

When to use

Use when you want to apply the same transformation to a subset of positions.

Example (Wrapping only selected elements in arrays)

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.mapPick([0, 2], asArray)
)
console.log(result) // [[1], "hello", [true]]
mappingmapmapOmit
Source effect/Tuple.ts:43823 lines
export const mapPick: {
  <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>, L extends Lambda>(
    indices: I,
    lambda: L
  ): (
    self: T
  ) => { [K in keyof T]: K extends `${I[number]}` ? Apply<L, T[K]> : T[K] }
  <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>, L extends Lambda>(
    self: T,
    indices: I,
    lambda: L
  ): { [K in keyof T]: K extends `${I[number]}` ? Apply<L, T[K]> : T[K] }
} = dual(
  3,
  <const T extends ReadonlyArray<unknown>, L extends Function>(
    self: T,
    indices: ReadonlyArray<number>,
    lambda: L
  ) => {
    const toPick = new Set<number>(indices)
    return self.map((e, i) => (toPick.has(i) ? lambda(e) : e))
  }
)