Skip to content

Commit

Permalink
implement Map and Reduce (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisugi authored and terkelg committed Mar 6, 2019
1 parent 619753d commit 997f44f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
62 changes: 62 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ ECMAScript 6 sets have no methods for computing the union (∪), intersection (
- **** symmetric difference
- **** subset
- **** superset
- map
- filter
- reduce


## Install
Expand Down Expand Up @@ -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`

Expand All @@ -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) ∪
Expand Down Expand Up @@ -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`

Expand All @@ -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)
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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();
});

0 comments on commit 997f44f

Please sign in to comment.