diff --git a/README.md b/README.md index 93bb6b044..60b481fa2 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ average(1, 2, 3); * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep) * [`times`](#times) +* [`unfold`](#unfold) @@ -3645,6 +3646,35 @@ console.log(output); // 01234
[⬆ Back to top](#table-of-contents) + +### unfold + +Builds an array, using an iterator function and an initial seed value. + +Use a `while` loop and `Array.push()` to call the function repeatedly until it returns `false`. +The iterator function accepts one argument (`seed`) and must always return an array with two elements ([`value`, `nextSeed`]) or `false` to terminate. + +```js +const unfold = (fn, seed) => { + let result = [], + val = [null, seed]; + while ((val = fn(val[1]))) result.push(val[0]); + return result; +}; +``` + +
+Examples + +```js +var f = n => (n > 50 ? false : [-n, n + 10]); +unfold(f, 10); // [-10, -20, -30, -40, -50] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + --- ## ➗ Math diff --git a/docs/index.html b/docs/index.html index 55e0a2b47..69426fcb0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -816,6 +816,14 @@ document.bodyShow examples
var output = '';
 times(5, i => (output += i));
 console.log(output); // 01234
+

unfold

Builds an array, using an iterator function and an initial seed value.

Use a while loop and Array.push() to call the function repeatedly until it returns false. The iterator function accepts one argument (seed) and must always return an array with two elements ([value, nextSeed]) or false to terminate.

const unfold = (fn, seed) => {
+  let result = [],
+    val = [null, seed];
+  while ((val = fn(val[1]))) result.push(val[0]);
+  return result;
+};
+
var f = n => (n > 50 ? false : [-n, n + 10]);
+unfold(f, 10); // [-10, -20, -30, -40, -50]
 

Math

average

Returns the average of an of two or more numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
 
average(...[1, 2, 3]); // 2
 average(1, 2, 3); // 2