Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Last active November 13, 2018 14:30
Show Gist options
  • Save Phrogz/1a74e3fcfe0a37432a46aed96a7f45b3 to your computer and use it in GitHub Desktop.
Save Phrogz/1a74e3fcfe0a37432a46aed96a7f45b3 to your computer and use it in GitHub Desktop.
A useful JS function for running maps on objects; similar to Ruby's Symbol#to_proc &:foo syntax, but more powerful.
function ƒ(name){
let v,params=Array.prototype.slice.call(arguments,1);
return o=>(typeof (v=o[name])==='function' ? v.apply(o,params) : v);
}
a = ["foo", "barmitzvah"]
a.map(ƒ('length')) // [3, 10]
a.map(ƒ('toUpperCase')) // ["FOO", "BARMITZVAH"]
a.map(ƒ('slice',1)) // ["oo", "armitzvah"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment