Last active
August 29, 2015 14:23
-
-
Save joneshf/aef59a07d3b27e8f86dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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