Hyperlinkv0.8.0-beta.28

Struct

Struct.mapPickconsteffect/Struct.ts:740
<
  S extends object,
  const Keys extends ReadonlyArray<keyof S>,
  L extends Lambda
>(
  keys: Keys,
  lambda: L
): (self: S) => {
  [K in keyof S]: K extends Keys[number] ? Apply<L, S[K]> : S[K]
}
<
  S extends object,
  const Keys extends ReadonlyArray<keyof S>,
  L extends Lambda
>(
  self: S,
  keys: Keys,
  lambda: L
): { [K in keyof S]: K extends Keys[number] ? Apply<L, S[K]> : S[K] }

Applies a Lambda transformation only to the specified keys; all other keys are copied unchanged.

When to use

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

Example (Wrapping only selected values in arrays)

import { pipe, Struct } 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(
  { x: 1, y: 2, z: 3 },
  Struct.mapPick(["x", "z"], asArray)
)
console.log(result) // { x: [1], y: 2, z: [3] }
Source effect/Struct.ts:74022 lines
export const mapPick: {
  <S extends object, const Keys extends ReadonlyArray<keyof S>, L extends Lambda>(
    keys: Keys,
    lambda: L
  ): (
    self: S
  ) => { [K in keyof S]: K extends Keys[number] ? Apply<L, S[K]> : S[K] }
  <S extends object, const Keys extends ReadonlyArray<keyof S>, L extends Lambda>(
    self: S,
    keys: Keys,
    lambda: L
  ): { [K in keyof S]: K extends Keys[number] ? Apply<L, S[K]> : S[K] }
} = dual(
  3,
  <S extends object, const Keys extends ReadonlyArray<keyof S>, L extends Function>(
    self: S,
    keys: Keys,
    lambda: L
  ) => {
    return buildStruct(self, (k, v) => [k, keys.includes(k) ? lambda(v) : v])
  }
)