Last active
December 20, 2015 21:09
-
-
Save buzzdecafe/6196021 to your computer and use it in GitHub Desktop.
working out composition problems in Ramda
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
// 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