Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active December 20, 2015 21:09
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 buzzdecafe/6196021 to your computer and use it in GitHub Desktop.
Save buzzdecafe/6196021 to your computer and use it in GitHub Desktop.
working out composition problems in Ramda
// currently in Ramda:
var compose = R.compose = function() { // TODO: type check of arguments?
var fns = slice(arguments);
return function() {
return foldr(function(fn, args) {return [fn.apply(this, args)];}, slice(arguments), fns)[0];
};
};
//...
var useWith = R.useWith = _(function(fn /*, tranformers */) {
var tranformers = slice(arguments, 1);
return function() {
var args = [], idx = -1;
while (++idx < tranformers.length) {
args.push(tranformers[idx](arguments[idx]));
}
return fn.apply(this, args.concat(slice(arguments, tranformers.length)));
};
});
// This does work:
function max() { return Math.max.apply(Math, arguments); }
function add1(x) { return x + 1; }
function mult2(x) { return x * 2; }
function div3(x) { return x/3; }
useWith(max, add1, mult2, div3)(4,5,6,7,8,9); // => 10
// THE PROBLEM IS, THIS DOESN'T WORK, once we wrap useWith in curry(nAry(...)):
// Reasonable analog to SQL `select` statement.
//
// var kids = [
// {name: 'Abby', age: 7, hair: 'blond', grade: 2},
// {name: 'Fred', age: 12, hair: 'brown', grade: 7}
// ];
// project(['name', 'grade'], kids);
// //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
R.project = useWith(map, pick);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment