Skip to content

unfold

Summary

Produces a list of values from x.

Tip

This function is curried.

Description

First the predicate pred is applied to x. If logical true the result of applying map to x is added to the list. Then the predicate is applied to the next value of x which is obtained by applying next to x and the process repeats until the predicate returns logical false.

Examples

List of ten consecutive days starting from Wednesday.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const day =
  x =>
    ( x === 1 ? 'Mon'
    : x === 2 ? 'Tue'
    : x === 3 ? 'Wed'
    : x === 4 ? 'Thu'
    : x === 5 ? 'Fri'
    : x === 6 ? 'Sat'
              : 'Sun');

unfold( x => x < 13        // (1)
      , x => day(x % 7)    // (2)
      , x => x + 1         // (3)
      , 3);                // (4)

//=> ["Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
  1. Continues unfolding whilst the current value of x satisfies the predicate.

  2. Transforms x. The result is accumulated into the list.

  3. Returns the next value for x.

  4. Starting value for x.

Parameters

Name Type Description
pred function unfold continues whilst the predicate applied to x returns true.
map function unfold applies map to x and adds the result to the list.
next function unfold determines the next value of x by applying next to it.
x *

Return

Array.<*>