UNPKG

1.34 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Builds a list from a seed value. Accepts an iterator function, which returns
6 * either false to stop iteration or an array of length 2 containing the value
7 * to add to the resulting list and the seed to be used in the next call to the
8 * iterator function.
9 *
10 * The iterator function receives one argument: *(seed)*.
11 *
12 * @func
13 * @memberOf R
14 * @since v0.10.0
15 * @category List
16 * @sig (a -> [b]) -> * -> [b]
17 * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
18 * either false to quit iteration or an array of length two to proceed. The element
19 * at index 0 of this array will be added to the resulting array, and the element
20 * at index 1 will be passed to the next call to `fn`.
21 * @param {*} seed The seed value.
22 * @return {Array} The final list.
23 * @example
24 *
25 * const f = n => n > 50 ? false : [-n, n + 10];
26 * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
27 * @symb R.unfold(f, x) = [f(x)[0], f(f(x)[1])[0], f(f(f(x)[1])[1])[0], ...]
28 */
29
30
31var unfold =
32/*#__PURE__*/
33_curry2(function unfold(fn, seed) {
34 var pair = fn(seed);
35 var result = [];
36
37 while (pair && pair.length) {
38 result[result.length] = pair[0];
39 pair = fn(pair[1]);
40 }
41
42 return result;
43});
44
45module.exports = unfold;
\No newline at end of file