diff --git a/README.md b/README.md index 99c5185f..69f8a34b 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,34 @@ Zips given arrays together into pairs. ``` +#### zipN + +Zips given arrays + +##### Type signature + + +```typescript +(...xs: any[][]) => any[][] +``` + + +##### Examples + + +```javascript +zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]] +``` + +```javascript +zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]] +``` + +```javascript +zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]] +``` + + #### zipWith Zips given arrays together with the given function. diff --git a/array/README.md b/array/README.md index 8439e73c..6a225332 100644 --- a/array/README.md +++ b/array/README.md @@ -490,6 +490,34 @@ Zips given arrays together into pairs. ``` +# zipN + +Zips given arrays + +## Type signature + + +```typescript +(...xs: any[][]) => any[][] +``` + + +## Examples + + +```javascript +zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]] +``` + +```javascript +zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]] +``` + +```javascript +zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]] +``` + + # zipWith Zips given arrays together with the given function. diff --git a/array/index.js b/array/index.js index e75718ec..ac0e9e43 100644 --- a/array/index.js +++ b/array/index.js @@ -37,6 +37,7 @@ import sum from "./sum.js"; import take from "./take.js"; import unique from "./unique.js"; import zip from "./zip.js"; +import zipN from "./zipN.js"; import zipWith from "./zipWith.js"; export { @@ -79,6 +80,7 @@ export { take, unique, zip, + zipN, zipWith }; @@ -122,5 +124,6 @@ export default { take, unique, zip, + zipN, zipWith }; diff --git a/array/index.ts b/array/index.ts index d1322252..6b72e29f 100644 --- a/array/index.ts +++ b/array/index.ts @@ -37,6 +37,7 @@ import sum from "./sum"; import take from "./take"; import unique from "./unique"; import zip from "./zip"; +import zipN from "./zipN"; import zipWith from "./zipWith"; export { @@ -79,6 +80,7 @@ export { take, unique, zip, + zipN, zipWith }; @@ -122,5 +124,6 @@ export default { take, unique, zip, + zipN, zipWith }; diff --git a/array/zipN.js b/array/zipN.js new file mode 100644 index 00000000..bd6f462e --- /dev/null +++ b/array/zipN.js @@ -0,0 +1,7 @@ +export default (...xs) => { + const [head = [], ...tail] = xs; + + return head.map((value, index) => + tail.reduce((x, xs) => [...x, xs[index]], [value]) + ); +}; diff --git a/array/zipN.json b/array/zipN.json new file mode 100644 index 00000000..b6394d6a --- /dev/null +++ b/array/zipN.json @@ -0,0 +1,20 @@ +{ + "name": "zipN", + "description": "Zips given arrays", + "signature": "(...xs: any[][]) => any[][]", + "examples": [ + { + "language": "javascript", + "content": "zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]" + }, + { + "language": "javascript", + "content": "zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]" + }, + { + "language": "javascript", + "content": "zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/array/zipN.md b/array/zipN.md new file mode 100644 index 00000000..33cc9d8f --- /dev/null +++ b/array/zipN.md @@ -0,0 +1,27 @@ +# zipN + +Zips given arrays + +## Type signature + + +```typescript +(...xs: any[][]) => any[][] +``` + + +## Examples + + +```javascript +zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]] +``` + +```javascript +zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]] +``` + +```javascript +zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]] +``` + diff --git a/array/zipN.test.ts b/array/zipN.test.ts new file mode 100644 index 00000000..67a7c18e --- /dev/null +++ b/array/zipN.test.ts @@ -0,0 +1,69 @@ +/* eslint-env jest */ +// @ts-ignore ambiguous import +import zipN from "./zipN.ts"; + +describe("zip", () => { + it("should zip with pair constructor", () => { + expect(zipN([1, 2, 3], [4, 5, 6])).toEqual([ + [1, 4], + [2, 5], + [3, 6] + ]); + }); + + it("should zip up to the left arrays length", () => { + expect(zipN([1, 2, 3, 4, 5, 7], [4, 5, 6])).toEqual([ + [1, 4], + [2, 5], + [3, 6], + [4, undefined], + [5, undefined], + [7, undefined] + ]); + + expect(zipN([1, 2, 3], [4, 5, 6, 4, 5, 7])).toEqual([ + [1, 4], + [2, 5], + [3, 6] + ]); + }); + + it("should return the wrapped elements of the source array when only one array is given", () => { + expect(zipN([1, 2])).toEqual([[1], [2]]); + }); + + it("should return an empty array when the given array is empty", () => { + expect(zipN([])).toEqual([]); + }); + + it("should return an empty array when the given nothing", () => { + expect(zipN()).toEqual([]); + }); + + it("should zip three arrays with the same lengths", () => { + expect(zipN([1, 2, 3], [4, 5, 6], [7, 8, 9])).toEqual([ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9] + ]); + }); + + it("should zip three arrays with different lengths", () => { + expect(zipN([1, 2, 3], [4, 5, 6], [7, 8])).toEqual([ + [1, 4, 7], + [2, 5, 8], + [3, 6, undefined] + ]); + + expect(zipN([1, 2, 3], [4, 5], [7, 8, 9])).toEqual([ + [1, 4, 7], + [2, 5, 8], + [3, undefined, 9] + ]); + + expect(zipN([1, 2], [4, 5, 6], [7, 8, 9])).toEqual([ + [1, 4, 7], + [2, 5, 8] + ]); + }); +}); diff --git a/array/zipN.ts b/array/zipN.ts new file mode 100644 index 00000000..2b926720 --- /dev/null +++ b/array/zipN.ts @@ -0,0 +1,7 @@ +export default (...xs: any[][]) => { + const [head = [], ...tail] = xs; + + return head.map((value, index) => + tail.reduce((x, xs) => [...x, xs[index]], [value]) + ); +};