← Back

Source

Unfoldable

interface Unfoldable<F> {
  readonly URI: F
  readonly unfoldr: <A, B>(b: B, f: (b: B) => Option<[A, B]>) => HKT<F, A>
}

Added in v1.0.0 (type class)

This class identifies data structures which can be unfolded, generalizing unfoldr on arrays.

empty

empty<F, A>(U: Unfoldable<F>): HKT<F, A>

Added in v1.0.0 (function)

The container with no elements - unfolded with zero iterations.

Example

import { empty } from 'fp-ts/lib/Unfoldable'
import { array } from 'fp-ts/lib/Array'

assert.deepEqual(empty(array), [])

replicate

replicate<F>(U: Unfoldable<F>): <A>(a: A, n: number) => HKT<F, A>

Added in v1.0.0 (function)

Replicate a value some natural number of times.

Example

import { replicate } from 'fp-ts/lib/Unfoldable'
import { array } from 'fp-ts/lib/Array'

assert.deepEqual(replicate(array)('s', 2), ['s', 's'])

replicateA

replicateA<F, T>(
  F: Applicative<F>,
  UT: Unfoldable<T> & Traversable<T>
): <A>(n: number, ma: HKT<F, A>) => HKT<F, HKT<T, A>>

Added in v1.0.0 (function)

Perform an Applicative action n times, and accumulate all the results

Example

import { replicateA } from 'fp-ts/lib/Unfoldable'
import { array } from 'fp-ts/lib/Array'
import { option, some, none } from 'fp-ts/lib/Option'

assert.deepEqual(replicateA(option, array)(2, some(1)), some([1, 1]))
assert.deepEqual(replicateA(option, array)(2, none), none)

singleton

singleton<F>(U: Unfoldable<F>): <A>(a: A) => HKT<F, A>

Added in v1.0.0 (function)

Contain a single value

Example

import { singleton } from 'fp-ts/lib/Unfoldable'
import { array } from 'fp-ts/lib/Array'

assert.deepEqual(singleton(array)(1), [1])