r/javascript • u/jrsinclair • Jul 19 '16
JavaScript Functors Explained
http://freecontent.manning.com/javascript-functors-explained/3
u/ggolemg2 Jul 19 '16
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
2
u/totemcatcher Jul 19 '16
Ever see that show "How it's made"? Most products are merely extruded and chopped by highly engineered automation machines which have replaced human labour, which makes for an incredibly uninteresting demonstration if you focus on "how". At that point we would rather watch a show called "Why it's made" which explains the machine making it, the economics of it, and the relevant convenience layers in industrial mass production applied in this particular context.
I wonder if the author understands the concept enough to produce an example with some useful application.
0
u/gcanti Jul 19 '16 edited Jul 19 '16
For me the simplest explanation of a functor F is "a procedure that, given a type, returns a type" (+ some laws which should hold). So Maybe<T> is the functor that, given the type T return the type ?T (aka the set T union the set {null}). List<T> is the functor that, given a type T return the type Array<T> (aka the set of all arrays whose elements are of type T). A functor is not a data structure (e.g. a "container"), is a mapping F between a type T and another type F(T). Now what happens to functions? Given a function f: A -> B, what's F(f)? Here comes the definition of fmap: ((A -> B) -> FA) -> FB. <= this is the usual signature but if you group the arguments as in fmap: (A -> B) -> (FA -> FB) now it should be clear that fmap defines how F maps functions, i.e. what's F(f)
2
u/crcrcrcrcrcrcrcrcr Jul 19 '16
This example makes sense as someone whose studied this at uni, but if you haven't studied an introductory course in category theory this isn't that simple at all. Arrows and mappings are fairly abstract, this isn't a good place to start if the people you're apparently trying to help haven't necessarily seen the notation.
0
u/binsmyth Jul 19 '16
There is a funfunfunction video on functors. According to the video, it is said that a functor is anything(e.g. object) that implements a map. I don't know how true it is but the video explains it in a simplest way. Note: There are two videos there. In the second video he corrects some of the mistakes he made in first video.
https://www.youtube.com/watch?v=YLIH8TKbAh4 https://www.youtube.com/watch?v=DisD9ftUyCk
20
Jul 19 '16
[removed] — view removed comment
3
u/jellatin Jul 19 '16 edited Jul 19 '16
Edit: totally forgot /u/jnd-au helped me understand this a few months ago: https://www.reddit.com/r/functionalprogramming/comments/4e9evj/trying_to_wrap_my_head_around_lift/
First things first, imo Professor Frisby's Mostly Adequate Guide to Functional Programming is the de facto standard for learning about FP in JavaScript. You don't get to Functors/Lifting until Chapter 8 which assumes understanding of the previous chapters but even without reading the prior chapters it's still more clear than the excerpt posted here.
The other problem here is that the author describes "lifting" incorrectly. Lifting is the idea of "teaching" something, say a function, how to work with values inside of a container.
Simple example:
function add2(n) { return n + 2; }Our
add2function understands how to work on single values like Numbers. If you give it a number, you will get a new number back that's 2 higher than the one you gave it. Great.But what if I want to do this for a number inside of a container?
var myArr = [5]; add2(myArr) // error (in actuality you're going to get "52" as JS will cast to string and + will concat, but I think it's safe to say this is not our desired behavior)The problem here is that
add2doesn't understand container/functor space. It needs to be lifted into this space, which is exactly whatmapcan do for us.map(myArr, add2); // [7](Side Note: If you want to get a
mapfunction rather than method try this:var map = Function.prototype.call.bind(Array.prototype.map);)Ignore the fact that people usually use map to work on multiple values inside of an array, it's irrelevant for this example. What you see here is a function being lifted into container/functor context.
Now that you see this working, understand that it can work on all sorts of different containers that can be built to do other cool things for you.
This guide may be slightly more advanced than where you're at but the pictures might help you understand what's happening with the containers: http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
5
u/cwmma Jul 19 '16
I am honestly more confused after reading this and I occasionally program in Erlang. Is an array a functor if so why not just use them in the explanation instead of some unexplained wrap function that uses fmap instead of map which everyone understands. Yes I understand that this might not be 100% canonical pure functors but I'm ok with that if it lets me understand the concept.
This seems like another example of how the main thing holding back functional programing is functional programmers.