Hyperlinkv0.8.0-beta.28

Array

Array.makeByconsteffect/Array.ts:203
<A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>
<A>(n: number, f: (i: number) => A): NonEmptyArray<A>

Creates a NonEmptyArray of length n where element i is computed by f(i).

When to use

Use when you need to compute each array element from its index.

Details

n is normalized to an integer greater than or equal to 1, so this function always returns at least one element. Supports both data-first and data-last usage.

Example (Generating values from indices)

import { Array } from "effect"

const result = Array.makeBy(5, (n) => n * 2)
console.log(result) // [0, 2, 4, 6, 8]
constructorsrangereplicate
Source effect/Array.ts:20311 lines
export const makeBy: {
  <A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>
  <A>(n: number, f: (i: number) => A): NonEmptyArray<A>
} = dual(2, <A>(n: number, f: (i: number) => A) => {
  const max = Math.max(1, Math.floor(n))
  const out = new Array(max)
  for (let i = 0; i < max; i++) {
    out[i] = f(i)
  }
  return out as NonEmptyArray<A>
})
Referenced by 6 symbols