From 997f44f9f69d8917e0e1165db3eb9652d2b73bc1 Mon Sep 17 00:00:00 2001 From: HelloRusk <36184621+7ma7X@users.noreply.github.com> Date: Wed, 6 Mar 2019 10:17:31 +0900 Subject: [PATCH] implement Map and Reduce (#5) --- readme.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 17 ++++++++++++++ test/index.js | 26 +++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/readme.md b/readme.md index 7323740..07b715c 100755 --- a/readme.md +++ b/readme.md @@ -28,7 +28,9 @@ ECMAScript 6 sets have no methods for computing the union (∪), intersection ( - **⊖** symmetric difference - **⊆** subset - **⊇** superset +- map - filter +- reduce ## Install @@ -152,6 +154,21 @@ Type: `Zet|Set` Set of type `Zet` or `Set`. +### map(set, func) +Returns: `Zet|Set` + +Creates a set with the results of calling the provided function on every element. + +#### set +Type: `Zet|Set` + +Set of type `Zet` or `Set`. + +#### func +Type: `Function` + +Function that produces an element of the new set. + ### filter(set, func) Returns: `Zet|Set` @@ -166,6 +183,26 @@ Type: `Function` It is a predicate, to test each element of the set. +### reduce(set, func, *initializer*) +Returns: `Number` + +Reduces the set to a single value, by executing the provided function for each element in the set (from left-to-right). + +#### set +Type: `Zet|Set` + +Set of type `Zet` or `Set`. + +#### func +Type: `Function` + +Function to be executed for each element in the set. + +#### *initializer* +Type: `Number` + +Optional. A value to be passed to the function as the initial value. + ## Instance Methods ### union(...sets) ∪ @@ -228,6 +265,16 @@ Type: `Zet|Set` Set of type `Zet` or `Set`. +### map(func) +Returns: `Zet|Set` + +Creates a set with the results of calling the provided function on every element. + +#### func +Type: `Function` + +Function that produces an element of the new set. + ### filter(func) Returns: `Zet|Set` @@ -238,6 +285,21 @@ Type: `Function` It is a predicate, to test each element of the set. +### reduce(func, *initializer*) +Returns: `Number` + +Reduces the set to a single value, by executing the provided function for each element in the set (from left-to-right). + +#### func +Type: `Function` + +Function to be executed for each element in the set. + +#### *initializer* +Type: `Number` + +Optional. A value to be passed to the function as the initial value. + ## License MIT © [Terkel Gjervig](https://terkel.com) diff --git a/src/index.js b/src/index.js index 8b534df..788d2a4 100644 --- a/src/index.js +++ b/src/index.js @@ -26,10 +26,19 @@ class Zet extends Set { return [...setB].every(x => setA.has(x)); } + static map(set, func) { + return new Zet([...set].map(func)); + } + static filter(set, func) { return new Zet([...set].filter(func)); } + static reduce(set, func, initializer) { + if (initializer === undefined) return [...set].reduce(func); + return [...set].reduce(func, initializer); + } + union(...sets) { return Zet.union(this, ...sets); } @@ -54,9 +63,17 @@ class Zet extends Set { return Zet.superset(this, other); } + map(func) { + return Zet.map(this, func); + } + filter(func) { return Zet.filter(this, func); } + + reduce(func, initializer) { + return Zet.reduce(this, func, initializer); + } } export default Zet; diff --git a/test/index.js b/test/index.js index 1541123..f69786c 100755 --- a/test/index.js +++ b/test/index.js @@ -81,12 +81,25 @@ test('static :: superset ⊇', t => { t.end(); }); +test('static :: map', t => { + let x = new Zet([1,4,9,16]); + eq(t, Zet.map(x, i => i * 2), [2,8,18,32]); + t.end(); +}); + test('static :: filter', t => { let x = new Zet([1,2,3]); eq(t, Zet.filter(x, i => i % 2), [1, 3]); t.end(); }); +test('static :: reduce', t => { + let x = new Zet([1,2,3,4]); + t.is(Zet.reduce(x, (i, j) => i + j), 10); + t.is(Zet.reduce(x, (i, j) => i + j, 5), 15); + t.end(); +}); + test('instance :: union ∩', t => { let x = new Zet([1,2,3]); let y = new Zet([2,3,4]); @@ -150,8 +163,21 @@ test('instance :: superset ⊇', t => { t.end(); }); +test('instance :: map', t => { + let x = new Zet([1,4,9,16]); + eq(t, x.map(i => i * 2), [2,8,18,32]); + t.end(); +}); + test('instance :: filter', t => { let x = new Zet([1,2,3]); eq(t, x.filter(i => i % 2), [1, 3]); t.end(); }); + +test('instance :: reduce', t => { + let x = new Zet([1,2,3,4]); + t.is(x.reduce((i, j) => i + j), 10); + t.is(x.reduce((i, j) => i + j, 5), 15); + t.end(); +});