From 8284160fa986668ab12ecc7b49a6b95200824cbb Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 24 Jan 2018 10:45:29 +0000 Subject: [PATCH] Travis build: 1372 --- README.md | 33 +++++++++++++++++++++++++++++++++ docs/index.html | 12 +++++++++++- snippets/unzipWith.md | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d5dd00baf..9e5bd5721 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ average(1, 2, 3); * [`unionWith`](#unionwith) * [`uniqueElements`](#uniqueelements) * [`unzip`](#unzip) +* [`unzipWith`](#unzipwith-) * [`without`](#without) * [`zip`](#zip) * [`zipObject`](#zipobject) @@ -1997,6 +1998,38 @@ unzip([['a', 1, true], ['b', 2]]); //[['a', 'b'], [1, 2], [true]]
[⬆ Back to top](#table-of-contents) +### unzipWith ![advanced](/advanced.svg) + +Creates an array of elements, ungrouping the elements in an array produced by [zip](#zip) and applying the provided function. + +Use `Math.max.apply()` to get the longest subarray in the array, `Array.map()` to make each element an array. +Use `Array.reduce()` and `Array.forEach()` to map grouped values to individual arrays. +Use `Array.map()` and the spread operator (`...`) to apply `fn` to each individual group of elements. + +```js +const unzipWith = (arr, fn) => + arr + .reduce( + (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc), + Array.from({ + length: Math.max(...arr.map(x => x.length)) + }).map(x => []) + ) + .map(val => fn(...val)); +``` + +
+Examples + +```js +unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)); // [3, 30, 300] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### without Filters out the elements of an array, that have one of the specified values. diff --git a/docs/index.html b/docs/index.html index 5fd0d8992..8180284a3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

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.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

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.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -380,6 +380,16 @@ Object.assig
   );
 
unzip([['a', 1, true], ['b', 2, false]]); //[['a', 'b'], [1, 2], [true, false]]
 unzip([['a', 1, true], ['b', 2]]); //[['a', 'b'], [1, 2], [true]]
+

unzipWithadvanced

Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.

Use Math.max.apply() to get the longest subarray in the array, Array.map() to make each element an array. Use Array.reduce() and Array.forEach() to map grouped values to individual arrays. Use Array.map() and the spread operator (...) to apply fn to each individual group of elements.

const unzipWith = (arr, fn) =>
+  arr
+    .reduce(
+      (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
+      Array.from({
+        length: Math.max(...arr.map(x => x.length))
+      }).map(x => [])
+    )
+    .map(val => fn(...val));
+
unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)); // [3, 30, 300]
 

without

Filters out the elements of an array, that have one of the specified values.

Use Array.filter() to create an array excluding(using !Array.includes()) all given values.

(For a snippet that mutates the original array see pull)

const without = (arr, ...args) => arr.filter(v => !args.includes(v));
 
without([2, 1, 2, 3], 1, 2); // [3]
 

zip

Creates an array of elements, grouped based on the position in the original arrays.

Use Math.max.apply() to get the longest array in the arguments. Creates an array with that length as return value and use Array.from() with a map-function to create an array of grouped elements. If lengths of the argument-arrays vary, undefined is used where no value could be found.

const zip = (...arrays) => {
diff --git a/snippets/unzipWith.md b/snippets/unzipWith.md
index 75314d0b8..c474a2e84 100644
--- a/snippets/unzipWith.md
+++ b/snippets/unzipWith.md
@@ -12,7 +12,7 @@ const unzipWith = (arr, fn) =>
     .reduce(
       (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
       Array.from({
-        length: Math.max(...arr.map(x => x.length)),
+        length: Math.max(...arr.map(x => x.length))
       }).map(x => [])
     )
     .map(val => fn(...val));