diff --git a/snippets/ary.md b/snippets/ary.md index 63b1c0cd9..236187603 100644 --- a/snippets/ary.md +++ b/snippets/ary.md @@ -1,6 +1,6 @@ --- title: ary -tags: adapter,function,intermediate +tags: function,intermediate --- Creates a function that accepts up to `n` arguments, ignoring any additional arguments. diff --git a/snippets/call.md b/snippets/call.md index 98d5e0beb..d6976db99 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -1,22 +1,22 @@ ---- -title: call -tags: adapter,function,intermediate ---- - +--- +title: call +tags: function,intermediate +--- + 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. - -```js -const call = (key, ...args) => context => context[key](...args); -``` - -```js -Promise.resolve([1, 2, 3]) - .then(call('map', x => 2 * x)) - .then(console.log); // [ 2, 4, 6 ] -const map = call.bind(null, 'map'); -Promise.resolve([1, 2, 3]) - .then(map(x => 2 * x)) - .then(console.log); // [ 2, 4, 6 ] -``` \ No newline at end of file +Use a closure to call a stored key with stored arguments. + +```js +const call = (key, ...args) => context => context[key](...args); +``` + +```js +Promise.resolve([1, 2, 3]) + .then(call('map', x => 2 * x)) + .then(console.log); // [ 2, 4, 6 ] +const map = call.bind(null, 'map'); +Promise.resolve([1, 2, 3]) + .then(map(x => 2 * x)) + .then(console.log); // [ 2, 4, 6 ] +``` diff --git a/snippets/collectInto.md b/snippets/collectInto.md index 82bee2047..93d00feec 100644 --- a/snippets/collectInto.md +++ b/snippets/collectInto.md @@ -1,20 +1,20 @@ ---- -title: collectInto -tags: adapter,function,array,intermediate ---- - +--- +title: collectInto +tags: function,array,intermediate +--- + Changes a function that accepts an array into a variadic function. -Given a function, return a closure that collects all inputs into an array-accepting function. - -```js -const collectInto = fn => (...args) => fn(args); -``` - -```js -const Pall = collectInto(Promise.all.bind(Promise)); -let p1 = Promise.resolve(1); -let p2 = Promise.resolve(2); -let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3)); -Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds) -``` \ No newline at end of file +Given a function, return a closure that collects all inputs into an array-accepting function. + +```js +const collectInto = fn => (...args) => fn(args); +``` + +```js +const Pall = collectInto(Promise.all.bind(Promise)); +let p1 = Promise.resolve(1); +let p2 = Promise.resolve(2); +let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3)); +Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds) +``` diff --git a/snippets/flip.md b/snippets/flip.md index a6419f811..803dfb2ed 100644 --- a/snippets/flip.md +++ b/snippets/flip.md @@ -1,22 +1,22 @@ ---- -title: flip -tags: adapter,function,intermediate ---- - +--- +title: flip +tags: function,intermediate +--- + Flip takes a function as an argument, then makes the first argument the last. -Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. - -```js -const flip = fn => (first, ...rest) => fn(...rest, first); -``` - -```js -let a = { name: 'John Smith' }; -let b = {}; -const mergeFrom = flip(Object.assign); -let mergePerson = mergeFrom.bind(null, a); -mergePerson(b); // == b -b = {}; -Object.assign(b, a); // == b -``` \ No newline at end of file +Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. + +```js +const flip = fn => (first, ...rest) => fn(...rest, first); +``` + +```js +let a = { name: 'John Smith' }; +let b = {}; +const mergeFrom = flip(Object.assign); +let mergePerson = mergeFrom.bind(null, a); +mergePerson(b); // == b +b = {}; +Object.assign(b, a); // == b +``` diff --git a/snippets/over.md b/snippets/over.md index 859176db8..ce3f133f8 100644 --- a/snippets/over.md +++ b/snippets/over.md @@ -1,6 +1,6 @@ --- title: over -tags: adapter,function,intermediate +tags: function,intermediate --- Creates a function that invokes each provided function with the arguments it receives and returns the results. @@ -14,4 +14,4 @@ const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args)); ```js const minMax = over(Math.min, Math.max); minMax(1, 2, 3, 4, 5); // [1,5] -``` \ No newline at end of file +``` diff --git a/snippets/overArgs.md b/snippets/overArgs.md index f33dab59d..94233b3a6 100644 --- a/snippets/overArgs.md +++ b/snippets/overArgs.md @@ -1,6 +1,6 @@ --- title: overArgs -tags: adapter,function,intermediate +tags: function,intermediate --- Creates a function that invokes the provided function with its arguments transformed. @@ -16,4 +16,4 @@ const square = n => n * n; const double = n => n * 2; const fn = overArgs((x, y) => [x, y], [square, double]); fn(9, 3); // [81, 6] -``` \ No newline at end of file +``` diff --git a/snippets/pipeAsyncFunctions.md b/snippets/pipeAsyncFunctions.md index 4904b600a..3044ba7ea 100644 --- a/snippets/pipeAsyncFunctions.md +++ b/snippets/pipeAsyncFunctions.md @@ -1,6 +1,6 @@ --- title: pipeAsyncFunctions -tags: adapter,function,promise,intermediate +tags: function,promise,intermediate --- Performs left-to-right function composition for asynchronous functions. @@ -23,4 +23,4 @@ const sum = pipeAsyncFunctions( (async () => { console.log(await sum(5)); // 15 (after one second) })(); -``` \ No newline at end of file +``` diff --git a/snippets/pipeFunctions.md b/snippets/pipeFunctions.md index 5de1d9d61..2b14b6a85 100644 --- a/snippets/pipeFunctions.md +++ b/snippets/pipeFunctions.md @@ -1,6 +1,6 @@ --- title: pipeFunctions -tags: adapter,function,intermediate +tags: function,intermediate --- Performs left-to-right function composition. @@ -17,4 +17,4 @@ const add5 = x => x + 5; const multiply = (x, y) => x * y; const multiplyAndAdd5 = pipeFunctions(multiply, add5); multiplyAndAdd5(5, 2); // 15 -``` \ No newline at end of file +``` diff --git a/snippets/promisify.md b/snippets/promisify.md index 134da9a11..93d7d71ce 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -1,6 +1,6 @@ --- title: promisify -tags: adapter,function,promise,intermediate +tags: function,promise,intermediate --- Converts an asynchronous function to return a promise. @@ -20,4 +20,4 @@ const promisify = func => (...args) => ```js const delay = promisify((d, cb) => setTimeout(cb, d)); delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s -``` \ No newline at end of file +``` diff --git a/snippets/rearg.md b/snippets/rearg.md index 61443d363..59bf66926 100644 --- a/snippets/rearg.md +++ b/snippets/rearg.md @@ -1,6 +1,6 @@ --- title: rearg -tags: adapter,function,intermediate +tags: function,intermediate --- Creates a function that invokes the provided function with its arguments arranged according to the specified indexes. @@ -19,4 +19,4 @@ var rearged = rearg( [2, 0, 1] ); rearged('b', 'c', 'a'); // ['a', 'b', 'c'] -``` \ No newline at end of file +``` diff --git a/snippets/spreadOver.md b/snippets/spreadOver.md index 47955e7ab..c967d21e0 100644 --- a/snippets/spreadOver.md +++ b/snippets/spreadOver.md @@ -1,17 +1,17 @@ ---- -title: spreadOver -tags: adapter,intermediate ---- - +--- +title: spreadOver +tags: function,intermediate +--- + Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function. -Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function. - -```js -const spreadOver = fn => argsArr => fn(...argsArr); -``` - -```js -const arrayMax = spreadOver(Math.max); -arrayMax([1, 2, 3]); // 3 -``` \ No newline at end of file +Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function. + +```js +const spreadOver = fn => argsArr => fn(...argsArr); +``` + +```js +const arrayMax = spreadOver(Math.max); +arrayMax([1, 2, 3]); // 3 +``` diff --git a/snippets/unary.md b/snippets/unary.md index ed6a66e0a..0e154aa80 100644 --- a/snippets/unary.md +++ b/snippets/unary.md @@ -1,6 +1,6 @@ --- title: unary -tags: adapter,function,intermediate +tags: function,beginner --- Creates a function that accepts up to one argument, ignoring any additional arguments. @@ -13,4 +13,4 @@ const unary = fn => val => fn(val); ```js ['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10] -``` \ No newline at end of file +```