From ca1683e9b76cb5be725dfa5ce650c40bbb9b35bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Mon, 30 Dec 2019 13:34:27 +0100 Subject: [PATCH 1/5] Add zipN method --- README.md | 28 +++++++++++++++++++++++ array/README.md | 28 +++++++++++++++++++++++ array/index.js | 3 +++ array/index.ts | 3 +++ array/zipN.js | 2 ++ array/zipN.json | 20 ++++++++++++++++ array/zipN.md | 27 ++++++++++++++++++++++ array/zipN.test.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++ array/zipN.ts | 2 ++ 9 files changed, 170 insertions(+) create mode 100644 array/zipN.js create mode 100644 array/zipN.json create mode 100644 array/zipN.md create mode 100644 array/zipN.test.ts create mode 100644 array/zipN.ts diff --git a/README.md b/README.md index 99c5185f..e42b5998 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[], ...ys: 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..6cab5748 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[], ...ys: 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..a7e11f75 --- /dev/null +++ b/array/zipN.js @@ -0,0 +1,2 @@ +export default (xs, ...ys) => + xs.map((value, index) => ys.reduce((x, xs) => [...x, xs[index]], [value])); diff --git a/array/zipN.json b/array/zipN.json new file mode 100644 index 00000000..575f81be --- /dev/null +++ b/array/zipN.json @@ -0,0 +1,20 @@ +{ + "name": "zipN", + "description": "Zips given arrays", + "signature": "(xs: any[], ...ys: 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..cc8e93d1 --- /dev/null +++ b/array/zipN.md @@ -0,0 +1,27 @@ +# zipN + +Zips given arrays + +## Type signature + + +```typescript +(xs: any[], ...ys: 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..6ecd56b3 --- /dev/null +++ b/array/zipN.test.ts @@ -0,0 +1,57 @@ +/* eslint-env jest */ +// @ts-ignore ambiguous import +import zipN from "./zipN.ts"; + +describe("zip", () => { + it("should zips with pair constructor", () => { + expect(zipN([1, 2, 3], [4, 5, 6])).toEqual([ + [1, 4], + [2, 5], + [3, 6] + ]); + }); + + it("should zips 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 zips 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 zips 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..b6813c57 --- /dev/null +++ b/array/zipN.ts @@ -0,0 +1,2 @@ +export default (xs: any[], ...ys: any[]) => + xs.map((value, index) => ys.reduce((x, xs) => [...x, xs[index]], [value])); From fdc20311bedc566b027bf9904a870d56835012f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 13:58:53 +0100 Subject: [PATCH 2/5] Update after code review --- README.md | 2 +- array/README.md | 2 +- array/zipN.js | 9 +++++++-- array/zipN.json | 2 +- array/zipN.md | 2 +- array/zipN.test.ts | 16 ++++++++++++---- array/zipN.ts | 9 +++++++-- 7 files changed, 30 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e42b5998..9f6227ee 100644 --- a/README.md +++ b/README.md @@ -509,7 +509,7 @@ Zips given arrays ```typescript -(xs: any[], ...ys: any[]) => any[] +(...xs: any[]) => any ``` diff --git a/array/README.md b/array/README.md index 6cab5748..a7508079 100644 --- a/array/README.md +++ b/array/README.md @@ -498,7 +498,7 @@ Zips given arrays ```typescript -(xs: any[], ...ys: any[]) => any[] +(...xs: any[]) => any ``` diff --git a/array/zipN.js b/array/zipN.js index a7e11f75..4b8323fb 100644 --- a/array/zipN.js +++ b/array/zipN.js @@ -1,2 +1,7 @@ -export default (xs, ...ys) => - xs.map((value, index) => ys.reduce((x, xs) => [...x, xs[index]], [value])); +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 index 575f81be..2f34fc18 100644 --- a/array/zipN.json +++ b/array/zipN.json @@ -1,7 +1,7 @@ { "name": "zipN", "description": "Zips given arrays", - "signature": "(xs: any[], ...ys: any[]) => any[]", + "signature": "(...xs: any[]) => any", "examples": [ { "language": "javascript", diff --git a/array/zipN.md b/array/zipN.md index cc8e93d1..371c00a0 100644 --- a/array/zipN.md +++ b/array/zipN.md @@ -6,7 +6,7 @@ Zips given arrays ```typescript -(xs: any[], ...ys: any[]) => any[] +(...xs: any[]) => any ``` diff --git a/array/zipN.test.ts b/array/zipN.test.ts index 6ecd56b3..c438023d 100644 --- a/array/zipN.test.ts +++ b/array/zipN.test.ts @@ -3,7 +3,7 @@ import zipN from "./zipN.ts"; describe("zip", () => { - it("should zips with pair constructor", () => { + it("should zip with pair constructor", () => { expect(zipN([1, 2, 3], [4, 5, 6])).toEqual([ [1, 4], [2, 5], @@ -11,7 +11,7 @@ describe("zip", () => { ]); }); - it("should zips up to the left arrays length", () => { + 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], @@ -28,7 +28,15 @@ describe("zip", () => { ]); }); - it("should zips three arrays with the same lengths", () => { + 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 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], @@ -36,7 +44,7 @@ describe("zip", () => { ]); }); - it("should zips three arrays with different lengths", () => { + 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], diff --git a/array/zipN.ts b/array/zipN.ts index b6813c57..9e4c1390 100644 --- a/array/zipN.ts +++ b/array/zipN.ts @@ -1,2 +1,7 @@ -export default (xs: any[], ...ys: any[]) => - xs.map((value, index) => ys.reduce((x, xs) => [...x, xs[index]], [value])); +export default (...xs: any[]) => { + const [head, ...tail] = xs; + + return head.map((value, index) => + tail.reduce((x, xs) => [...x, xs[index]], [value]) + ); +}; From 1d496ee9701a430b3a8b7d63aad7ebf1792ce9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 15:26:13 +0100 Subject: [PATCH 3/5] Update type --- README.md | 2 +- array/README.md | 2 +- array/zipN.json | 2 +- array/zipN.md | 2 +- array/zipN.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9f6227ee..69f8a34b 100644 --- a/README.md +++ b/README.md @@ -509,7 +509,7 @@ Zips given arrays ```typescript -(...xs: any[]) => any +(...xs: any[][]) => any[][] ``` diff --git a/array/README.md b/array/README.md index a7508079..6a225332 100644 --- a/array/README.md +++ b/array/README.md @@ -498,7 +498,7 @@ Zips given arrays ```typescript -(...xs: any[]) => any +(...xs: any[][]) => any[][] ``` diff --git a/array/zipN.json b/array/zipN.json index 2f34fc18..b6394d6a 100644 --- a/array/zipN.json +++ b/array/zipN.json @@ -1,7 +1,7 @@ { "name": "zipN", "description": "Zips given arrays", - "signature": "(...xs: any[]) => any", + "signature": "(...xs: any[][]) => any[][]", "examples": [ { "language": "javascript", diff --git a/array/zipN.md b/array/zipN.md index 371c00a0..33cc9d8f 100644 --- a/array/zipN.md +++ b/array/zipN.md @@ -6,7 +6,7 @@ Zips given arrays ```typescript -(...xs: any[]) => any +(...xs: any[][]) => any[][] ``` diff --git a/array/zipN.ts b/array/zipN.ts index 9e4c1390..6c75a4e7 100644 --- a/array/zipN.ts +++ b/array/zipN.ts @@ -1,4 +1,4 @@ -export default (...xs: any[]) => { +export default (...xs: any[][]) => { const [head, ...tail] = xs; return head.map((value, index) => From 3a2ebaee996bf46d9175812ea534283825ea0c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 15:44:17 +0100 Subject: [PATCH 4/5] Update after code review --- array/zipN.js | 2 +- array/zipN.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/array/zipN.js b/array/zipN.js index 4b8323fb..bd6f462e 100644 --- a/array/zipN.js +++ b/array/zipN.js @@ -1,5 +1,5 @@ export default (...xs) => { - const [head, ...tail] = xs; + const [head = [], ...tail] = xs; return head.map((value, index) => tail.reduce((x, xs) => [...x, xs[index]], [value]) diff --git a/array/zipN.ts b/array/zipN.ts index 6c75a4e7..2b926720 100644 --- a/array/zipN.ts +++ b/array/zipN.ts @@ -1,5 +1,5 @@ export default (...xs: any[][]) => { - const [head, ...tail] = xs; + const [head = [], ...tail] = xs; return head.map((value, index) => tail.reduce((x, xs) => [...x, xs[index]], [value]) From 24bc9cf1428c6e74b9e69c431b47be722e8e687e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 15:57:08 +0100 Subject: [PATCH 5/5] Add additional test case --- array/zipN.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/array/zipN.test.ts b/array/zipN.test.ts index c438023d..67a7c18e 100644 --- a/array/zipN.test.ts +++ b/array/zipN.test.ts @@ -36,6 +36,10 @@ describe("zip", () => { 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],