MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/4tk8sp/javascript_functors_explained/d5i1qk7/?context=3
r/javascript • u/jrsinclair • Jul 19 '16
14 comments sorted by
View all comments
3
Can anyone give an example in a non-FP es5 way?
2 u/jellatin Jul 19 '16 Tried to expand on this a bit here: https://www.reddit.com/r/javascript/comments/4tk8sp/javascript_functors_explained/d5ihbxd 2 u/inmatarian Jul 19 '16 Start with this function:function double(x) { return x * 2; }Now, image we have an object that's this:{ "awesomeness": 10 }Now lets say you wanted to get the double of the awesomeness of an object, that's easy, var thingsDoubleAwesome = double(thing.awesome);However, you really want it to be in another object with the awesomeness field filled, so you do this:function doubleAwesomeness(container) { return { "awesomeness": double(container.awesomeness) }; } var thingsDoubleAwesome = doubleAwesomeness(thing);An abstract reusable version of this looks like so:function applyAwesomeness(action, container) { return { "awesomeness": action(container.awesomeness) }; } var thingsDoubleAwesome = applyAwesomeness(double, thing);And a functor just makes this a partial function that returns another:function applyAwesomeness(action) { return (container) { return { "awesomeness": action(container.awesomeness) }; }; } var doubleAwesomeness = applyAwesomeness(double); var thingsDoubleAwesome = doubleAwesomeness(thing); 3 u/TexMax007 Jul 19 '16 I believe I saw a video about Functors on the FunFunFunction YouTube channel. -2 u/siegfryd Jul 19 '16 All of the code in the article is already in ES5 (not that there's much), what do you want for an example? 1 u/[deleted] Jul 19 '16 Arrow functions were in ES5?
2
Tried to expand on this a bit here: https://www.reddit.com/r/javascript/comments/4tk8sp/javascript_functors_explained/d5ihbxd
Start with this function:
function double(x) { return x * 2; }
Now, image we have an object that's this:
{ "awesomeness": 10 }
Now lets say you wanted to get the double of the awesomeness of an object, that's easy, var thingsDoubleAwesome = double(thing.awesome);
var thingsDoubleAwesome = double(thing.awesome);
However, you really want it to be in another object with the awesomeness field filled, so you do this:
function doubleAwesomeness(container) { return { "awesomeness": double(container.awesomeness) }; } var thingsDoubleAwesome = doubleAwesomeness(thing);
An abstract reusable version of this looks like so:
function applyAwesomeness(action, container) { return { "awesomeness": action(container.awesomeness) }; } var thingsDoubleAwesome = applyAwesomeness(double, thing);
And a functor just makes this a partial function that returns another:
function applyAwesomeness(action) { return (container) { return { "awesomeness": action(container.awesomeness) }; }; } var doubleAwesomeness = applyAwesomeness(double); var thingsDoubleAwesome = doubleAwesomeness(thing);
I believe I saw a video about Functors on the FunFunFunction YouTube channel.
-2
All of the code in the article is already in ES5 (not that there's much), what do you want for an example?
1 u/[deleted] Jul 19 '16 Arrow functions were in ES5?
1
Arrow functions were in ES5?
3
u/ggolemg2 Jul 19 '16
Can anyone give an example in a non-FP es5 way?