Created
April 22, 2015 04:24
-
-
Save vqvu/07bebdab23fed81c2325 to your computer and use it in GitHub Desktop.
HL commute
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
var R = require('ramda'), | |
_ = require('highland'); | |
// Hack in partial Fantasy Land support. | |
var p = Object.getPrototypeOf(_()); | |
p.reduce = function (f, m) { | |
console.log('m', m._incoming); | |
return _.reduce(m, f, this); | |
}; | |
p.ap = function ap(ap) { | |
var self = this; | |
return ap.collect().flatMap(function (arr) { | |
return self.flatMap(function (f) { | |
return _(arr).map(f); | |
}); | |
}); | |
}; | |
_.of = function (x) { | |
return _([x]); | |
}; | |
// I wonder why we don't expose _.toArray... | |
var toArray = R.curry(function toArray(f, xs) { | |
return xs.toArray(f); | |
}); | |
console.log(R.commute(R.of, [[1, 2], [3, 4], [5, 6]])); | |
// => [ [ 1, 3, 5 ], | |
// [ 2, 3, 5 ], | |
// [ 1, 4, 5 ], | |
// [ 2, 4, 5 ], | |
// [ 1, 3, 6 ], | |
// [ 2, 3, 6 ], | |
// [ 1, 4, 6 ], | |
// [ 2, 4, 6 ] ] | |
R.commute(_.of, _([_([1, 2]), _([3, 4]), _([5, 6])])) | |
.each(toArray(_.log)); | |
// => [ [ 1, 3, 5 ], | |
// [ 2, 3, 5 ], | |
// [ 1, 4, 5 ], | |
// [ 2, 4, 5 ], | |
// [ 1, 3, 6 ], | |
// [ 2, 3, 6 ], | |
// [ 1, 4, 6 ], | |
// [ 2, 4, 6 ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment