Skip to content

Instantly share code, notes, and snippets.

@vqvu
Created April 22, 2015 04:24
Show Gist options
  • Save vqvu/07bebdab23fed81c2325 to your computer and use it in GitHub Desktop.
Save vqvu/07bebdab23fed81c2325 to your computer and use it in GitHub Desktop.
HL commute
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