Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save obenjiro/e13a5628cd317aae74fd to your computer and use it in GitHub Desktop.

Select an option

Save obenjiro/e13a5628cd317aae74fd to your computer and use it in GitHub Desktop.
smallest multifunctional composition
var compose = function(array) {
return array.reduce(function(a,b) { return function(c) { return b(a(c)) } })
}
var get = function(prop) {
return function(obj) {
return obj && obj[prop]
}
}
var getpath = compose([ get('a'), get('b') ]);
console.log(getpath({ a: { b: 1 } }));
/* ============================================================= */
var composeArgs = function() {
return [].reduce.call(arguments, function(a,b) { return function(c) { return b(a(c)) } })
}
var get = function(prop) {
return function(obj) {
return obj && obj[prop]
}
}
var getpath = composeArgs(get('a'), get('b'));
console.log(getpath({ a: { b: 1 } }));
compose = (array) -> array.reduce (a, b) -> (c) -> b a(c)
get = (prop) -> (obj) -> obj and obj[prop]
getpath = compose [get('a'), get('b')]
alert getpath(a: b: 2)
# ============================================= #
composeArgs = -> [].reduce.call arguments, (a, b) -> (c) -> b a(c)
get = (prop) -> (obj) -> obj and obj[prop]
getpath = composeArgs get('a'), get('b')
alert getpath(a: b: 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment