Skip to content

Commit

Permalink
feat: reducers
Browse files Browse the repository at this point in the history
BREAKING CHANGE: countInIterable, everyInIterable and someInIterable have been removed, use
reduceIterable instead
  • Loading branch information
ivan7237d committed Dec 7, 2020
1 parent 5315714 commit aa92215
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 59 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ How-to:

- **Get a flag indicating if the element is the first element:** `zipIterables(firstIterable, yourIterable)` (returns an iterable of `[boolean, <element>]`).

- **Count elements in an iterable:**: `applyPipe(yourIterable, reduceIterable(countReducer, 0))`.

- **Check if every element in an iterable is true:** `applyPipe(yourIterableOfBooleans, reduceIterable(andReducer, true))` (iteration will not continue unnecessarily because of how [`reduceIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/reduceIterable.ts) and [`andReducer`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/reducer/andReducer.ts) are defined).

- **Check if some elements in an iterable are true:** `applyPipe(yourIterableOfBooleans, reduceIterable(orReducer, false))`.

- **Find the first element matching a predicate:** `applyPipe(yourIterable, filter(yourPredicate), firstInIterable)`.

- **Yield values while a condition holds:**
Expand All @@ -75,7 +81,7 @@ How-to:
);
```

(yields `1`, `2`, see [`scanIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/scanIterable.ts) and [`reduceIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/reduceIterable.ts)).
(yields `1`, `2`, see [`scanIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/scanIterable.ts)).

- **Also yield the value that broke the condition:**

Expand Down Expand Up @@ -114,6 +120,12 @@ It [provides](https://github.com/ivan7237d/antiutils/tree/master/src/internal/co

It also provides implementations of `EqualFunction` for objects, iterables, maps, and sets, and a function [`deepEqual`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/deepEqual.ts) that recursively delegates to those functions depending on the object type.

## Reducers

The library exports a type `Reducer` which is like a regular reducer, but can return `undefined` to stop the iteration short, and which is used by functions [`reduceIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/reduceIterable.ts) and [`scanIterable`](https://github.com/ivan7237d/antiutils/blob/master/src/internal/iterable/scanIterable.ts).

The library includes [basic implementations of this type](https://github.com/ivan7237d/antiutils/blob/master/src/internal/reducer), all of which except the boolean ones (`andReducer` and `orReducer`) can also be used with arrays and observables.

## Lenses

First let's talk about how we define a lens. When building React components, it's convenient to work with a type which we'll call `StateView`, a combination of a value and a setter:
Expand Down
6 changes: 0 additions & 6 deletions src/internal/iterable/countInIterable.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/internal/iterable/countInIterable.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/internal/iterable/everyInIterable.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/internal/iterable/everyInIterable.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/internal/iterable/someInIterable.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/internal/iterable/someInIterable.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/internal/reducer/andReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { andReducer } from './andReducer';

it('works', () => {
expect(applyPipe([true, true], reduceIterable(andReducer))).toEqual(true);
expect(applyPipe([true, false], reduceIterable(andReducer))).toEqual(false);
expect(applyPipe([false, true], reduceIterable(andReducer))).toEqual(false);
expect(applyPipe([false, false], reduceIterable(andReducer))).toEqual(false);
});
2 changes: 2 additions & 0 deletions src/internal/reducer/andReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const andReducer = (accumulator: boolean, value: boolean) =>
accumulator ? value : undefined;
7 changes: 7 additions & 0 deletions src/internal/reducer/countReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { countReducer } from './countReducer';

it('works', () => {
expect(applyPipe([1, 2], reduceIterable(countReducer))).toEqual(2);
});
1 change: 1 addition & 0 deletions src/internal/reducer/countReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const countReducer = (accumulator: number) => accumulator + 1;
7 changes: 7 additions & 0 deletions src/internal/reducer/maxReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { maxReducer } from './maxReducer';

it('works', () => {
expect(applyPipe([1, 2], reduceIterable(maxReducer))).toEqual(2);
});
2 changes: 2 additions & 0 deletions src/internal/reducer/maxReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const maxReducer = (accumulator: number, value: number) =>
Math.max(accumulator, value);
7 changes: 7 additions & 0 deletions src/internal/reducer/minReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { minReducer } from './minReducer';

it('works', () => {
expect(applyPipe([1, 2], reduceIterable(minReducer))).toEqual(1);
});
2 changes: 2 additions & 0 deletions src/internal/reducer/minReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const minReducer = (accumulator: number, value: number) =>
Math.min(accumulator, value);
10 changes: 10 additions & 0 deletions src/internal/reducer/orReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { orReducer } from './orReducer';

it('works', () => {
expect(applyPipe([true, true], reduceIterable(orReducer))).toEqual(true);
expect(applyPipe([true, false], reduceIterable(orReducer))).toEqual(true);
expect(applyPipe([false, true], reduceIterable(orReducer))).toEqual(true);
expect(applyPipe([false, false], reduceIterable(orReducer))).toEqual(false);
});
2 changes: 2 additions & 0 deletions src/internal/reducer/orReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const orReducer = (accumulator: boolean, value: boolean) =>
accumulator ? undefined : value;
7 changes: 7 additions & 0 deletions src/internal/reducer/sumReducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyPipe } from '../applyPipe';
import { reduceIterable } from '../iterable/reduceIterable';
import { sumReducer } from './sumReducer';

it('works', () => {
expect(applyPipe([1, 2], reduceIterable(sumReducer))).toEqual(3);
});
2 changes: 2 additions & 0 deletions src/internal/reducer/sumReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const sumReducer = (accumulator: number, value: number) =>
accumulator + value;

0 comments on commit aa92215

Please sign in to comment.