Last active
August 29, 2015 14:22
-
-
Save obenjiro/e13a5628cd317aae74fd to your computer and use it in GitHub Desktop.
smallest multifunctional composition
This file contains hidden or 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 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 } })); |
This file contains hidden or 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
| 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