Hyperlinkv0.8.0-beta.28

Array

Array.unfoldconsteffect/Array.ts:4284
<B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>

Builds an array by repeatedly applying a function to a seed value. The function returns Option.some([element, nextSeed]) to continue, or Option.none() to stop.

Example (Generating a sequence)

import { Array, Option } from "effect"

console.log(Array.unfold(1, (n) => n <= 5 ? Option.some([n, n + 1]) : Option.none()))
// [1, 2, 3, 4, 5]
constructorsmakeByrange
Source effect/Array.ts:428414 lines
export const unfold = <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A> => {
  const out: Array<A> = []
  let next: B = b
  while (true) {
    const o = f(next)
    if (Option.isNone(o)) {
      break
    }
    const [a, b] = o.value
    out.push(a)
    next = b
  }
  return out
}