Skip to content
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

update to transducer protocol. fixes #1003 #1006

Merged
merged 1 commit into from
Apr 5, 2015
Merged
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
2 changes: 1 addition & 1 deletion src/internal/_isTransformer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function _isTransformer(obj) {
return typeof obj.step === 'function' && typeof obj.result === 'function';
return typeof obj['@@transducer/step'] === 'function';
};
18 changes: 9 additions & 9 deletions src/internal/_reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ module.exports = (function() {
function _arrayReduce(xf, acc, list) {
var idx = -1, len = list.length;
while (++idx < len) {
acc = xf.step(acc, list[idx]);
if (acc && acc.__transducers_reduced__) {
acc = acc.value;
acc = xf['@@transducer/step'](acc, list[idx]);
if (acc && acc['@@transducer/reduced']) {
acc = acc['@@transducer/value'];
break;
}
}
return xf.result(acc);
return xf['@@transducer/result'](acc);
}

function _iterableReduce(xf, acc, iter) {
var step = iter.next();
while (!step.done) {
acc = xf.step(acc, step.value);
if (acc && acc.__transducers_reduced__) {
acc = acc.value;
acc = xf['@@transducer/step'](acc, step.value);
if (acc && acc['@@transducer/reduced']) {
acc = acc['@@transducer/value'];
break;
}
step = iter.next();
}
return xf.result(acc);
return xf['@@transducer/result'](acc);
}

function _methodReduce(xf, acc, obj) {
return xf.result(obj.reduce(bind(xf.step, xf), acc));
return xf['@@transducer/result'](obj.reduce(bind(xf['@@transducer/step'], xf), acc));
}

var symIterator = (typeof Symbol !== 'undefined') ? Symbol.iterator : '@@iterator';
Expand Down
6 changes: 5 additions & 1 deletion src/internal/_reduced.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = function(x) {
return x && x.__transducers_reduced__ ? x : {value: x, __transducers_reduced__: true};
return x && x['@@transducer/reduced'] ? x :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, let's use:

return x != null && ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. i'll know that i have arrived when i get no review comments from you 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't really count, as the comment relates to code not modified in this pull request.

{
'@@transducer/value': x,
'@@transducer/reduced': true
};
};
18 changes: 9 additions & 9 deletions src/internal/_stepCat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ var merge = require('../merge');

module.exports = (function() {
var _stepCatArray = {
init: Array,
step: function(xs, x) { return _concat(xs, [x]); },
result: _identity
'@@transducer/init': Array,
'@@transducer/step': function(xs, x) { return _concat(xs, [x]); },
'@@transducer/result': _identity
};
var _stepCatString = {
init: String,
step: _add,
result: _identity
'@@transducer/init': String,
'@@transducer/step': _add,
'@@transducer/result': _identity
};
var _stepCatObject = {
init: Object,
step: function(result, input) {
'@@transducer/init': Object,
'@@transducer/step': function(result, input) {
return merge(
result,
isArrayLike(input) ? _createMapEntry(input[0], input[1]) : input
);
},
result: _identity
'@@transducer/result': _identity
};

return function _stepCat(obj) {
Expand Down
14 changes: 7 additions & 7 deletions src/internal/_xall.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = (function() {
this.f = f;
this.all = true;
}
XAll.prototype.init = function() {
return this.xf.init();
XAll.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XAll.prototype.result = function(result) {
XAll.prototype['@@transducer/result'] = function(result) {
if (this.all) {
result = this.xf.step(result, true);
result = this.xf['@@transducer/step'](result, true);
}
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XAll.prototype.step = function(result, input) {
XAll.prototype['@@transducer/step'] = function(result, input) {
if (!this.f(input)) {
this.all = false;
result = _reduced(this.xf.step(result, false));
result = _reduced(this.xf['@@transducer/step'](result, false));
}
return result;
};
Expand Down
14 changes: 7 additions & 7 deletions src/internal/_xany.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = (function() {
this.f = f;
this.any = false;
}
XAny.prototype.init = function() {
return this.xf.init();
XAny.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XAny.prototype.result = function(result) {
XAny.prototype['@@transducer/result'] = function(result) {
if (!this.any) {
result = this.xf.step(result, false);
result = this.xf['@@transducer/step'](result, false);
}
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XAny.prototype.step = function(result, input) {
XAny.prototype['@@transducer/step'] = function(result, input) {
if (this.f(input)) {
this.any = true;
result = _reduced(this.xf.step(result, true));
result = _reduced(this.xf['@@transducer/step'](result, true));
}
return result;
};
Expand Down
10 changes: 5 additions & 5 deletions src/internal/_xdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ module.exports = (function() {
this.xf = xf;
this.n = n;
}
XDrop.prototype.init = function() {
return this.xf.init();
XDrop.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XDrop.prototype.result = function(result) {
return this.xf.result(result);
XDrop.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](result);
};
XDrop.prototype.step = function(result, input) {
if (this.n > 0) {
this.n -= 1;
return result;
}
return this.xf.step(result, input);
return this.xf['@@transducer/step'](result, input);
};

return _curry2(function _xdrop(n, xf) { return new XDrop(n, xf); });
Expand Down
10 changes: 5 additions & 5 deletions src/internal/_xdropWhile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ module.exports = (function() {
this.xf = xf;
this.f = f;
}
XDropWhile.prototype.init = function() {
return this.xf.init();
XDropWhile.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XDropWhile.prototype.result = function(result) {
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XDropWhile.prototype.step = function(result, input) {
XDropWhile.prototype['@@transducer/step'] = function(result, input) {
if (this.f) {
if (this.f(input)) {
return result;
}
this.f = null;
}
return this.xf.step(result, input);
return this.xf['@@transducer/step'](result, input);
};

return _curry2(function _xdropWhile(f, xf) { return new XDropWhile(f, xf); });
Expand Down
12 changes: 6 additions & 6 deletions src/internal/_xfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module.exports = (function() {
this.xf = xf;
this.f = f;
}
XFilter.prototype.init = function() {
return this.xf.init();
XFilter.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XFilter.prototype.result = function(result) {
return this.xf.result(result);
XFilter.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](result);
};
XFilter.prototype.step = function(result, input) {
return this.f(input) ? this.xf.step(result, input) : result;
XFilter.prototype['@@transducer/step'] = function(result, input) {
return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
};

return _curry2(function _xfilter(f, xf) { return new XFilter(f, xf); });
Expand Down
12 changes: 6 additions & 6 deletions src/internal/_xfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = (function() {
this.f = f;
this.found = false;
}
XFind.prototype.init = function() {
XFind.prototype['@@transducer/init'] = function() {
return this.xf.init();
};
XFind.prototype.result = function(result) {
XFind.prototype['@@transducer/result'] = function(result) {
if (!this.found) {
result = this.xf.step(result, void 0);
result = this.xf['@@transducer/step'](result, void 0);
}
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XFind.prototype.step = function(result, input) {
XFind.prototype['@@transducer/step'] = function(result, input) {
if (this.f(input)) {
this.found = true;
result = _reduced(this.xf.step(result, input));
result = _reduced(this.xf['@@transducer/step'](result, input));
}
return result;
};
Expand Down
14 changes: 7 additions & 7 deletions src/internal/_xfindIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ module.exports = (function() {
this.idx = -1;
this.found = false;
}
XFindIndex.prototype.init = function() {
return this.xf.init();
XFindIndex.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XFindIndex.prototype.result = function(result) {
XFindIndex.prototype['@@transducer/result'] = function(result) {
if (!this.found) {
result = this.xf.step(result, -1);
result = this.xf['@@transducer/step'](result, -1);
}
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XFindIndex.prototype.step = function(result, input) {
XFindIndex.prototype['@@transducer/step'] = function(result, input) {
this.idx += 1;
if (this.f(input)) {
this.found = true;
result = _reduced(this.xf.step(result, this.idx));
result = _reduced(this.xf['@@transducer/step'](result, this.idx));
}
return result;
};
Expand Down
10 changes: 5 additions & 5 deletions src/internal/_xfindLast.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module.exports = (function() {
this.xf = xf;
this.f = f;
}
XFindLast.prototype.init = function() {
return this.xf.init();
XFindLast.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XFindLast.prototype.result = function(result) {
return this.xf.result(this.xf.step(result, this.last));
XFindLast.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
};
XFindLast.prototype.step = function(result, input) {
XFindLast.prototype['@@transducer/step'] = function(result, input) {
if (this.f(input)) {
this.last = input;
}
Expand Down
10 changes: 5 additions & 5 deletions src/internal/_xfindLastIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ module.exports = (function() {
this.idx = -1;
this.lastIdx = -1;
}
XFindLastIndex.prototype.init = function() {
return this.xf.init();
XFindLastIndex.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XFindLastIndex.prototype.result = function(result) {
return this.xf.result(this.xf.step(result, this.lastIdx));
XFindLastIndex.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
};
XFindLastIndex.prototype.step = function(result, input) {
XFindLastIndex.prototype['@@transducer/step'] = function(result, input) {
this.idx += 1;
if (this.f(input)) {
this.lastIdx = this.idx;
Expand Down
16 changes: 8 additions & 8 deletions src/internal/_xgroupBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ module.exports = (function() {
this.f = f;
this.inputs = {};
}
XGroupBy.prototype.init = function() {
return this.xf.init();
XGroupBy.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XGroupBy.prototype.result = function(result) {
XGroupBy.prototype['@@transducer/result'] = function(result) {
var key;
for (key in this.inputs) {
if (_has(key, this.inputs)) {
result = this.xf.step(result, this.inputs[key]);
if (result.__transducers_reduced__) {
result = result.value;
result = this.xf['@@transducer/step'](result, this.inputs[key]);
if (result['@@transducer/reduced']) {
result = result['@@transducer/value'];
break;
}
}
}
return this.xf.result(result);
return this.xf['@@transducer/result'](result);
};
XGroupBy.prototype.step = function(result, input) {
XGroupBy.prototype['@@transducer/step'] = function(result, input) {
var key = this.f(input);
this.inputs[key] = this.inputs[key] || [key, []];
this.inputs[key][1] = _append(input, this.inputs[key][1]);
Expand Down
12 changes: 6 additions & 6 deletions src/internal/_xmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module.exports = (function() {
this.xf = xf;
this.f = f;
}
XMap.prototype.init = function() {
return this.xf.init();
XMap.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XMap.prototype.result = function(result) {
return this.xf.result(result);
XMap.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](result);
};
XMap.prototype.step = function(result, input) {
return this.xf.step(result, this.f(input));
XMap.prototype['@@transducer/step'] = function(result, input) {
return this.xf['@@transducer/step'](result, this.f(input));
};

return _curry2(function _xmap(f, xf) { return new XMap(f, xf); });
Expand Down
14 changes: 7 additions & 7 deletions src/internal/_xtake.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ module.exports = (function() {
this.xf = xf;
this.n = n;
}
XTake.prototype.init = function() {
return this.xf.init();
XTake.prototype['@@transducer/init'] = function() {
return this.xf['@@transducer/init']();
};
XTake.prototype.result = function(result) {
return this.xf.result(result);
XTake.prototype['@@transducer/result'] = function(result) {
return this.xf['@@transducer/result'](result);
};
XTake.prototype.step = function(result, input) {
XTake.prototype['@@transducer/step'] = function(result, input) {
this.n -= 1;
return this.n === 0 ? _reduced(this.xf.step(result, input))
: this.xf.step(result, input);
return this.n === 0 ? _reduced(this.xf['@@transducer/step'](result, input))
: this.xf['@@transducer/step'](result, input);
};

return _curry2(function _xtake(n, xf) { return new XTake(n, xf); });
Expand Down
Loading