r/javascript • • Jul 19 '16

JavaScript Functors Explained

http://freecontent.manning.com/javascript-functors-explained/
5 Upvotes

14 comments sorted by

View all comments

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.