Skip to content

Commit

Permalink
Test transformer protocol with transduce
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelmer committed Apr 5, 2017
1 parent 04e287b commit ee9c68f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 15 deletions.
107 changes: 107 additions & 0 deletions __tests__/transformerProtocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
///<reference path='../resources/jest.d.ts'/>

import * as jasmineCheck from 'jasmine-check';
import * as t from 'transducers-js';
jasmineCheck.install();

import { List, Map, Set, Stack } from '../';

describe('Transformer Protocol', () => {

it('transduces Stack without initial values', () => {
let s = Stack.of(1, 2, 3, 4);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let s2 = t.transduce(xform, Stack(), s);
expect(s.toArray()).toEqual([1, 2, 3, 4]);
expect(s2.toArray()).toEqual([5, 3]);
});

it('transduces Stack with initial values', () => {
let v1 = Stack.of(1, 2, 3);
let v2 = Stack.of(4, 5, 6, 7);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let r = t.transduce(xform, Stack(), v1, v2);
expect(v1.toArray()).toEqual([1, 2, 3]);
expect(v2.toArray()).toEqual([4, 5, 6, 7]);
expect(r.toArray()).toEqual([7, 5, 1, 2, 3]);
});

it('transduces List without initial values', () => {
let v = List.of(1, 2, 3, 4);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let r = t.transduce(xform, List(), v);
expect(v.toArray()).toEqual([1, 2, 3, 4]);
expect(r.toArray()).toEqual([3, 5]);
});

it('transduces List with initial values', () => {
let v1 = List.of(1, 2, 3);
let v2 = List.of(4, 5, 6, 7);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let r = t.transduce(xform, List(), v1, v2);
expect(v1.toArray()).toEqual([1, 2, 3]);
expect(v2.toArray()).toEqual([4, 5, 6, 7]);
expect(r.toArray()).toEqual([1, 2, 3, 5, 7]);
});

it('transduces Map without initial values', () => {
let m1 = Map({a: 1, b: 2, c: 3, d: 4});
let xform = t.comp(
t.filter(([k, v]) => v % 2 === 0),
t.map(([k, v]) => [k, v * 2]),
);
let m2 = t.transduce(xform, Map(), m1);
expect(m1.toObject()).toEqual({a: 1, b: 2, c: 3, d: 4});
expect(m2.toObject()).toEqual({b: 4, d: 8});
});

it('transduces Map with initial values', () => {
let m1 = Map({a: 1, b: 2, c: 3});
let m2 = Map({a: 4, b: 5});
let xform = t.comp(
t.filter(([k, v]) => v % 2 === 0),
t.map(([k, v]) => [k, v * 2]),
);
let m3 = t.transduce(xform, Map(), m1, m2);
expect(m1.toObject()).toEqual({a: 1, b: 2, c: 3});
expect(m2.toObject()).toEqual({a: 4, b: 5});
expect(m3.toObject()).toEqual({a: 8, b: 2, c: 3});
});

it('transduces Set without initial values', () => {
let s1 = Set.of(1, 2, 3, 4);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let s2 = t.transduce(xform, Set(), s1);
expect(s1.toArray()).toEqual([1, 2, 3, 4]);
expect(s2.toArray()).toEqual([3, 5]);
});

it('transduces Set with initial values', () => {
let s1 = Set.of(1, 2, 3, 4);
let s2 = Set.of(2, 3, 4, 5, 6);
let xform = t.comp(
t.filter(x => x % 2 === 0),
t.map(x => x + 1),
);
let s3 = t.transduce(xform, Set(), s1, s2);
expect(s1.toArray()).toEqual([1, 2, 3, 4]);
expect(s2.toArray()).toEqual([2, 3, 4, 5, 6]);
expect(s3.toArray()).toEqual([1, 2, 3, 4, 5, 7]);
});

});
22 changes: 22 additions & 0 deletions dist/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,13 @@ MapPrototype[IS_MAP_SENTINEL] = true;
MapPrototype[DELETE] = MapPrototype.remove;
MapPrototype.removeIn = MapPrototype.deleteIn;
MapPrototype.removeAll = MapPrototype.deleteAll;
MapPrototype['@@transducer/init'] = MapPrototype.asMutable;
MapPrototype['@@transducer/step'] = function(result, arr) {
return result.set(arr[0], arr[1]);
};
MapPrototype['@@transducer/result'] = function(obj) {
return obj.asImmutable();
};

// #pragma Trie Nodes

Expand Down Expand Up @@ -3118,6 +3125,11 @@ ListPrototype.withMutations = MapPrototype.withMutations;
ListPrototype.asMutable = MapPrototype.asMutable;
ListPrototype.asImmutable = MapPrototype.asImmutable;
ListPrototype.wasAltered = MapPrototype.wasAltered;
ListPrototype['@@transducer/init'] = ListPrototype.asMutable;
ListPrototype['@@transducer/step'] = function(result, arr) {
return result.push(arr);
};
ListPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];

var VNode = function VNode(array, ownerID) {
this.array = array;
Expand Down Expand Up @@ -3912,6 +3924,11 @@ StackPrototype.wasAltered = MapPrototype.wasAltered;
StackPrototype.shift = StackPrototype.pop;
StackPrototype.unshift = StackPrototype.push;
StackPrototype.unshiftAll = StackPrototype.pushAll;
StackPrototype['@@transducer/init'] = StackPrototype.asMutable;
StackPrototype['@@transducer/step'] = function(result, arr) {
return result.unshift(arr);
};
StackPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];

function makeStack(size, head, ownerID, hash) {
var map = Object.create(StackPrototype);
Expand Down Expand Up @@ -4197,6 +4214,11 @@ SetPrototype.mergeDeepWith = SetPrototype.mergeWith;
SetPrototype.withMutations = MapPrototype.withMutations;
SetPrototype.asMutable = MapPrototype.asMutable;
SetPrototype.asImmutable = MapPrototype.asImmutable;
SetPrototype['@@transducer/init'] = SetPrototype.asMutable;
SetPrototype['@@transducer/step'] = function(result, arr) {
return result.add(arr);
};
SetPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];

SetPrototype.__empty = emptySet;
SetPrototype.__make = makeSet;
Expand Down
Loading

0 comments on commit ee9c68f

Please sign in to comment.