programming functors

January 1, 2019

Functors

Functor is simply an interface with a contract.
We could have just as easily named it Mappable, but now, where's the fun in that?.
Professor Frisby's

A functor is nothing more than a data structure that you can map functions with the purpose of lifting values intro a wrapper, modifying them, and then putting them back into a wrapper.

It is a design pattern that defines semantics for how fmap should work

fmap :: (A -> B) -> Wrapper(A) -> Wrapper(B)

Lets see an example:

// NOTE: we can't use arrow function if we are referencing this inside the function (no "new" for arrow functions)
var Container = function(x) {
  this.__value = x;
}

Container.of = function(x) { return new Container(x); };

Container.of(3); // => Contaier(3) === { "__value": 3 }
Container.of(Container.of("pepinillos")) ; // => Container(Container("pepinillos")) === { "__value": { "__value": "pepinillos" } }



Bibliogrphy:

comments powered by Disqus