From ca8d6f3c73e90e1131c25c82afa7376e3152f53c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:04:03 +0200 Subject: [PATCH 01/12] Update ary.md --- snippets/ary.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 5342d0c3181ff362536b2f05ae428d3f6d55846b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:04:41 +0200 Subject: [PATCH 02/12] Update collectInto.md --- snippets/collectInto.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) 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) +``` From f2342ff9ed63d31c502b8fdd6f4670213767e319 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:04:58 +0200 Subject: [PATCH 03/12] Update pipeAsyncFunctions.md --- snippets/pipeAsyncFunctions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 47e0d297c81280db7e1375d37cd771c9d2deba0e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:05:35 +0200 Subject: [PATCH 04/12] Update spreadOver.md --- snippets/spreadOver.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) 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 +``` From 1b2a9324ec360e02888246530a7a1a5bd14ff603 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:05:49 +0200 Subject: [PATCH 05/12] Update overArgs.md --- snippets/overArgs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From c8f50e6e44e34d551575c0fefa7469f978389829 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:06:13 +0200 Subject: [PATCH 06/12] Update pipeFunctions.md --- snippets/pipeFunctions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 202ef9d260a778bdad92589f2d2a737f279c87b4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:06:24 +0200 Subject: [PATCH 07/12] Update promisify.md --- snippets/promisify.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 16e9a50eab732a5ad8aa39722a4c986da960ed71 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:06:31 +0200 Subject: [PATCH 08/12] Update rearg.md --- snippets/rearg.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 6e66a65f46059f7483c00f1fde5493960d725fec Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:07:13 +0200 Subject: [PATCH 09/12] Update over.md --- snippets/over.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 94dce697ea58aac7afbeec9172e3b05fe66eb093 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:07:21 +0200 Subject: [PATCH 10/12] Update flip.md --- snippets/flip.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) 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 +``` From 6f3bc038729e9ff0be264f2514e6ce34ce15f4b0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:07:34 +0200 Subject: [PATCH 11/12] Update unary.md --- snippets/unary.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +``` From 0d5c6f63b3ddaa6d68c3059ed0f6944bdb3bfc91 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jan 2020 10:07:54 +0200 Subject: [PATCH 12/12] Update call.md --- snippets/call.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) 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 ] +```