Merge remote-tracking branch 'origin/master'

This commit is contained in:
Angelos Chalaris
2018-01-24 16:38:13 +02:00
2 changed files with 39 additions and 1 deletions

View File

@ -227,6 +227,7 @@ average(1, 2, 3);
* [`runPromisesInSeries`](#runpromisesinseries) * [`runPromisesInSeries`](#runpromisesinseries)
* [`sleep`](#sleep) * [`sleep`](#sleep)
* [`times`](#times) * [`times`](#times)
* [`unfold`](#unfold)
</details> </details>
@ -3645,6 +3646,35 @@ console.log(output); // 01234
<br>[⬆ Back to top](#table-of-contents) <br>[⬆ 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;
};
```
<details>
<summary>Examples</summary>
```js
var f = n => (n > 50 ? false : [-n, n + 10]);
unfold(f, 10); // [-10, -20, -30, -40, -50]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
--- ---
## ➗ Math ## ➗ Math

File diff suppressed because one or more lines are too long