Hyperlinkv0.8.0-beta.28

Record

Record.fromIterableWithconsteffect/Record.ts:234
<A, K extends string | symbol, B>(f: (a: A) => readonly [K, B]): (
  self: Iterable<A>
) => Record<ReadonlyRecord.NonLiteralKey<K>, B>
<A, K extends string | symbol, B>(
  self: Iterable<A>,
  f: (a: A) => readonly [K, B]
): Record<ReadonlyRecord.NonLiteralKey<K>, B>

Takes an iterable and a projection function and returns a record. The projection function maps each value of the iterable to a tuple of a key and a value, which is then added to the resulting record.

Example (Building a record from mapped iterable values)

import { Record } from "effect"
import * as assert from "node:assert"

const input = [1, 2, 3, 4]

assert.deepStrictEqual(
  Record.fromIterableWith(input, (a) => [String(a), a * 2]),
  { "1": 2, "2": 4, "3": 6, "4": 8 }
)
constructors
Source effect/Record.ts:23422 lines
export const fromIterableWith: {
  <A, K extends string | symbol, B>(
    f: (a: A) => readonly [K, B]
  ): (self: Iterable<A>) => Record<ReadonlyRecord.NonLiteralKey<K>, B>
  <A, K extends string | symbol, B>(
    self: Iterable<A>,
    f: (a: A) => readonly [K, B]
  ): Record<ReadonlyRecord.NonLiteralKey<K>, B>
} = dual(
  2,
  <A, K extends string, B>(
    self: Iterable<A>,
    f: (a: A) => readonly [K, B]
  ): Record<ReadonlyRecord.NonLiteralKey<K>, B> => {
    const out: Record<string, B> = empty()
    for (const a of self) {
      const [k, b] = f(a)
      out[k] = b
    }
    return out
  }
)
Referenced by 1 symbols