Skip to content

Instantly share code, notes, and snippets.

@joneshf
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joneshf/aef59a07d3b27e8f86dd to your computer and use it in GitHub Desktop.
Save joneshf/aef59a07d3b27e8f86dd to your computer and use it in GitHub Desktop.
'use strict';
var R = require('ramda');
var T1 = require('transducers.js');
var T2 = require('transducers-js');
function Identity(x) {
if (!(this instanceof Identity)) {
return new Identity(x);
};
this.runIdentity = function() {
return x;
};
}
Identity.prototype.toString = function() {
return 'Identity(' + this.runIdentity().toString() + ')';
};
Identity.prototype.map = function(f) {
return Identity(f(this.runIdentity()));
};
Identity.prototype[Symbol.iterator] = function*() {
yield this.runIdentity();
};
Identity.prototype['@@transducer/init'] = function() {
return this;
};
Identity.prototype['@@transducer/result'] = function(x) {
return x;
};
Identity.prototype['@@transducer/step'] = function(x, y) {
return Identity(y);
};
var i0 = Identity(0);
var i3 = Identity(3);
console.log('%s', R.map(R.add(1), i3));
// The following two lines fail if the iterator spec is not implemented.
console.log('%s', R.into(i0, R.map(R.add(1)), i3));
console.log('%s', T1.into(i0, R.map(R.add(1)), i3));
// This line is always Identity(0);
console.log('%s', T2.into(i0, R.map(R.add(1)), i3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment