Last active
January 26, 2021 05:25
-
-
Save furf/07bcb8f19e42291c1c63499825a8ab4f to your computer and use it in GitHub Desktop.
furf’s functional funpack
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
/** | |
* 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 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
this.fu=(f=>f.bind.bind(f.call))(u=>u) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll just leave this here https://twitter.com/cowboy/status/125423802975928320