Skip to content

Added scan() #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/__tests__/__snapshots__/scan-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transmute/scan scans a List 1`] = `
Immutable.List [
Immutable.List [],
Immutable.List [
2,
],
Immutable.List [
2,
3,
],
Immutable.List [
2,
3,
4,
],
]
`;

exports[`transmute/scan scans a Map 1`] = `
Immutable.List [
Immutable.Map {},
Immutable.Map {
"one": 2,
},
Immutable.Map {
"one": 2,
"two": 3,
},
Immutable.Map {
"one": 2,
"two": 3,
"three": 4,
},
]
`;

exports[`transmute/scan scans a Seq 1`] = `
Immutable.List [
Immutable.Seq [],
Immutable.Seq [
2,
],
Immutable.Seq […],
Immutable.Seq […],
]
`;

exports[`transmute/scan scans a Set 1`] = `
Immutable.List [
Immutable.Set [],
Immutable.Set [
2,
],
Immutable.Set [
2,
3,
],
Immutable.Set [
2,
3,
4,
],
]
`;

exports[`transmute/scan scans an Array 1`] = `
Immutable.List [
Immutable.List [],
Immutable.List [
2,
],
Immutable.List [
2,
3,
],
Immutable.List [
2,
3,
4,
],
]
`;

exports[`transmute/scan scans an Object 1`] = `
Immutable.List [
Immutable.List [],
Immutable.List [
2,
],
Immutable.List [
2,
3,
],
Immutable.List [
2,
3,
4,
],
]
`;

exports[`transmute/scan scans to a primitive 1`] = `
Immutable.List [
0,
1,
3,
6,
]
`;
51 changes: 51 additions & 0 deletions src/__tests__/scan-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { List, Map, Seq, Set } from 'immutable';
import scan from '../scan';

describe('transmute/scan', () => {
it('scans an Array', () => {
const scanner = jest.fn((acc, n) => acc.push(n + 1));
expect(scan(List(), scanner, [1, 2, 3])).toMatchSnapshot();
expect(scanner.mock.calls.length).toBe(3);
expect(scanner.mock.calls[0]).toEqual([List(), 1, 0, [1, 2, 3]]);
expect(scanner.mock.calls[1]).toEqual([List.of(2), 2, 1, [1, 2, 3]]);
expect(scanner.mock.calls[2]).toEqual([List.of(2, 3), 3, 2, [1, 2, 3]]);
});

it('scans a List', () => {
expect(
scan(List(), (acc, n) => acc.push(n + 1), List.of(1, 2, 3))
).toMatchSnapshot();
});

it('scans a Map', () => {
expect(
scan(
Map(),
(acc, n, k) => acc.set(k, n + 1),
Map({ one: 1, two: 2, three: 3 })
)
).toMatchSnapshot();
});

it('scans an Object', () => {
expect(
scan(List(), (acc, n) => acc.push(n + 1), { one: 1, two: 2, three: 3 })
).toMatchSnapshot();
});

it('scans a Seq', () => {
expect(
scan(Seq(), (acc, n) => acc.concat([n + 1]), Seq([1, 2, 3]))
).toMatchSnapshot();
});

it('scans a Set', () => {
expect(
scan(Set(), (acc, n) => acc.add(n + 1), Set.of(1, 2, 3))
).toMatchSnapshot();
});

it('scans to a primitive', () => {
expect(scan(0)((acc, n) => acc + n)(List.of(1, 2, 3))).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions src/internal/TransmuteCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ export const reduce = protocol({
name: 'reduce',
});

/**
* @private
* Returns a list of new values by applying `mapper` to each item in `subject`.
*
* @param {Function} mapper
* @param {TYPE} subject
* @return {TYPE}
*/
export const scan = protocol({
args: [
isAnyValue, // accumulator
isFunction, // reducer
protocol.TYPE, // subject
],
name: 'scan',
});

/**
* @private
* Set the `value` of `key` in `subject`.
Expand Down
18 changes: 18 additions & 0 deletions src/internal/_scan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Iterable, List } from 'immutable';
import { scan } from './TransmuteCollection';
import entrySeq from './_entrySeq';

const scanEntries = (into, operation, src) =>
entrySeq(src).reduce(
([acc, results], [key, val]) => {
const res = operation(acc, val, key, src);
return [res, results.push(res)];
},
[into, List([into])]
)[1];

scan.implement(Object, scanEntries);
scan.implement(Array, scanEntries);
scan.implementInherited(Iterable, scanEntries);

export default scan;
25 changes: 25 additions & 0 deletions src/scan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import curry from './curry';
import _scan from './internal/_scan';

/**
* scan is similar to reduce, but returns a list of successive reduced values,
* including the initial value
*
* @example
* scan(0, (acc, val) => acc + val, [1, 2, 3]);
* // returns List [ 0, 1, 3, 6 ]
*
* @example
* scan(
* List(),
* (acc, val) => acc.push(val),
* Map({ one: 1, two: 2, three: 3 })
* );
* // returns List [ List [ ], List [ 1 ], List [ 1, 2 ], List [ 1, 2, 3 ] ]
*
* @param {any} into
* @param {Function} operation
* @param {Iterable} subject [description]
* @return {Iterable}
*/
export default curry(_scan);