Skip to content

Instantly share code, notes, and snippets.

@furf
Last active January 26, 2021 05:25
Show Gist options
  • Save furf/07bcb8f19e42291c1c63499825a8ab4f to your computer and use it in GitHub Desktop.
Save furf/07bcb8f19e42291c1c63499825a8ab4f to your computer and use it in GitHub Desktop.
furf’s functional funpack
/**
* Introducing fu: furf's functional funpack.
* "Putting the F-U back in JS!"
*
* fu.Array.map([1, 2, 3], a => a * 2) => [2, 4, 6]
* fu.String.repeat('fu ', 3) => "fu fu fu"
*/
this.fu = (function() {
const call = Function.prototype.call;
const bind = call.bind.bind(call);
function invert(Native) {
const prototype = Native.prototype;
return Object.getOwnPropertyNames(prototype).reduce(function(map, key) {
const val = prototype[key];
map[key] = (typeof val === 'function') ? bind(val) : val => val[key];
return map;
}, {});
}
return [Array, Number, Object, String].reduce(function(obj, Native) {
obj[Native.name] = invert(Native);
return obj;
}, {});
})();
this.fu=(f=>f.bind.bind(f.call))(u=>u)
@cowboy
Copy link

cowboy commented Dec 4, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment