From 8489af90e2e0062ade37981eb59483867dd06615 Mon Sep 17 00:00:00 2001 From: rodmoioliveira Date: Wed, 20 May 2020 13:58:39 -0300 Subject: [PATCH 1/3] add juxt --- snippets/juxt.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 snippets/juxt.md diff --git a/snippets/juxt.md b/snippets/juxt.md new file mode 100644 index 000000000..701aa7109 --- /dev/null +++ b/snippets/juxt.md @@ -0,0 +1,28 @@ +--- +title: juxt +tags: array,function,advanced +--- + +Takes several functions as argument and returns a fn that is the juxtaposition of those fns. + +juxt(fn1, fn2, fn3, ...) use `Array.prototype.map()` to return a `func` that can take a variable number of `args`. +When `func` is called, return a array containing the result of applying each fn (fn1, fn2, fn3, ...) to the `args`. + +```js +const juxt = (...fns) => (...x) => [...fns].map(fn => [...x].map(x => fn(x))); +``` + +```js +juxt( + x => x + 1, + x => x - 1, + x => x * 10, + x => x / 10 +)(1, 2, 3) // => [ [ 2, 3, 4 ], [ 0, 1, 2 ], [ 10, 20, 30 ], [ 0.1, 0.2, 0.3 ] ] + +juxt( + s => s.length, + s => s.split(" ").join("-") +)("30 seconds of code") // => [ [ 18 ], [ '30-seconds-of-code' ] ] + +``` From 090b341bc16fd5d6d2d74d90095146a75d05ee51 Mon Sep 17 00:00:00 2001 From: rodmoioliveira Date: Wed, 20 May 2020 14:17:52 -0300 Subject: [PATCH 2/3] make requested changes --- snippets/juxt.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/snippets/juxt.md b/snippets/juxt.md index 701aa7109..427a64bf8 100644 --- a/snippets/juxt.md +++ b/snippets/juxt.md @@ -3,26 +3,24 @@ title: juxt tags: array,function,advanced --- -Takes several functions as argument and returns a fn that is the juxtaposition of those fns. +Takes several functions as argument and returns a function that is the juxtaposition of those functions. -juxt(fn1, fn2, fn3, ...) use `Array.prototype.map()` to return a `func` that can take a variable number of `args`. -When `func` is called, return a array containing the result of applying each fn (fn1, fn2, fn3, ...) to the `args`. +Use `Array.prototype.map()` to return a `fn` that can take a variable number of `args`. +When `fn` is called, return an array containing the result of applying each `fn` to the `args`. ```js -const juxt = (...fns) => (...x) => [...fns].map(fn => [...x].map(x => fn(x))); +const juxt = (...fns) => (...args) => [...fns].map(fn => [...args].map(fn)); ``` ```js juxt( x => x + 1, x => x - 1, - x => x * 10, - x => x / 10 -)(1, 2, 3) // => [ [ 2, 3, 4 ], [ 0, 1, 2 ], [ 10, 20, 30 ], [ 0.1, 0.2, 0.3 ] ] + x => x * 10 +)(1, 2, 3) // [[2,3,4],[0,1,2],[10,20,30]] juxt( s => s.length, s => s.split(" ").join("-") -)("30 seconds of code") // => [ [ 18 ], [ '30-seconds-of-code' ] ] - +)("30 seconds of code") // [[18],['30-seconds-of-code']] ``` From 2b4006747cc441d43e0b1facf90d9a826d32393f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 May 2020 18:53:35 +0300 Subject: [PATCH 3/3] Update juxt.md --- snippets/juxt.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/juxt.md b/snippets/juxt.md index 427a64bf8..4ea85eb95 100644 --- a/snippets/juxt.md +++ b/snippets/juxt.md @@ -17,10 +17,10 @@ juxt( x => x + 1, x => x - 1, x => x * 10 -)(1, 2, 3) // [[2,3,4],[0,1,2],[10,20,30]] +)(1, 2, 3); // [[2,3,4],[0,1,2],[10,20,30]] juxt( s => s.length, s => s.split(" ").join("-") -)("30 seconds of code") // [[18],['30-seconds-of-code']] +)("30 seconds of code"); // [[18],['30-seconds-of-code']] ```