Hyperlinkv0.8.0-beta.28

Struct

Struct.mapconsteffect/Struct.ts:694
<L extends Lambda>(lambda: L): <S extends object>(
  self: S
) => { [K in keyof S]: Apply<L, S[K]> }
<S extends object, L extends Lambda>(self: S, lambda: L): {
  [K in keyof S]: Apply<L, S[K]>
}

Applies a Lambda transformation to every value in a struct.

When to use

Use when you want to apply the same function to every value in a struct.

Details

The lambda must be created with lambda so the compiler can track the output types.

Example (Wrapping every value in an array)

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({ width: 10, height: 20 }, Struct.map(asArray))
console.log(result) // { width: [10], height: [20] }
Source effect/Struct.ts:69414 lines
export const map: {
  <L extends Lambda>(
    lambda: L
  ): <S extends object>(self: S) => { [K in keyof S]: Apply<L, S[K]> }
  <S extends object, L extends Lambda>(
    self: S,
    lambda: L
  ): { [K in keyof S]: Apply<L, S[K]> }
} = dual(
  2,
  <S extends object, L extends Function>(self: S, lambda: L) => {
    return buildStruct(self, (k, v) => [k, lambda(v)])
  }
)