Skip to content

Instantly share code, notes, and snippets.

@mikaelbr
Last active June 7, 2019 03:47
Show Gist options
  • Select an option

  • Save mikaelbr/3808aec0ac6b5d641c28fda5534f5ee2 to your computer and use it in GitHub Desktop.

Select an option

Save mikaelbr/3808aec0ac6b5d641c28fda5534f5ee2 to your computer and use it in GitHub Desktop.
const scope = {};
function def(id, fn) {
scope[id] = fn;
}
function explode(id) {
return scope[id] ? [id] : id.split('\u200d');
}
function getFns(id) {
return explode(id).map(function(ii) {
if (!scope[ii])
throw new Error(`undefined is not a function 🀑.
(But seriously, ${ii} isn't defined as a function in the scope.)`);
return scope[ii];
});
}
function compose(fns) {
return arg =>
fns.reduceRight(function(prev, fn) {
return fn(prev);
}, arg);
}
function invoke(id, arg) {
return compose([compose, getFns])(id)(arg);
}
def('πŸ‘¨', console.log);
def('πŸ’»', arg => arg.toUpperCase());
invoke('πŸ‘¨β€πŸ’»', 'hello, world!');
//=> HELLO, WORLD!
πŸ“© Example syntax:
πŸ‘¨ ⬅️ console.log
πŸ’» ⬅️ toUpperCase
"hello, world!" ➑️ πŸ‘¨β€πŸ’»
πŸ“© Outputs: "HELLO, WORLD!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment