Build README

This commit is contained in:
Angelos Chalaris
2017-12-13 13:35:10 +02:00
parent 14a7110a1e
commit 8c03d44fbe
2 changed files with 14 additions and 10 deletions

View File

@ -171,12 +171,18 @@ const currentUrl = _ => window.location.href;
Use recursion. Use recursion.
If the number of provided arguments (`args`) is sufficient, call the passed function `f`. If the number of provided arguments (`args`) is sufficient, call the passed function `f`.
Otherwise return a curried function `f` that expects the rest of the arguments. Otherwise return a curried function `f` that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. `Math.min()`), you can optionally pass the number of arguments to the second parameter `arity`.
```js ```js
const curry = f => const curry = (f, arity = f.length, next) =>
(...args) => (next = prevArgs =>
args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs); nextArg => {
const args = [ ...prevArgs, nextArg ];
return args.length >= arity ? f(...args) : next(args);
}
)([]);
// curry(Math.pow)(2)(10) -> 1024 // curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
``` ```
### Deep flatten array ### Deep flatten array

View File

@ -3,16 +3,14 @@
Use recursion. Use recursion.
If the number of provided arguments (`args`) is sufficient, call the passed function `f`. If the number of provided arguments (`args`) is sufficient, call the passed function `f`.
Otherwise return a curried function `f` that expects the rest of the arguments. Otherwise return a curried function `f` that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (variadic function) like Math.min for example, you can optionally pass the number of arguments to the second parameter arity. If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. `Math.min()`), you can optionally pass the number of arguments to the second parameter `arity`.
```js ```js
const curry = (f, arity = f.length, next) => const curry = (f, arity = f.length, next) =>
(next = prevArgs => (next = prevArgs =>
nextArg => { nextArg => {
const args = [ ...prevArgs, nextArg ] const args = [ ...prevArgs, nextArg ];
return args.length >= arity return args.length >= arity ? f(...args) : next(args);
? f(...args)
: next(args);
} }
)([]); )([]);
// curry(Math.pow)(2)(10) -> 1024 // curry(Math.pow)(2)(10) -> 1024