Area 73 Blog

this is a description of my new blog created with Gatsbyjs


    Blog Post
    functional/functional-programing / patterns/mixins

    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" } }
    • Container is an object with one property.

    • Lots of containers just hold one thing, though they aren't limited to one. We've arbitrarily named its property __value.

    • The __value cannot be one specific type or our container would hardly live up to the name.

    • Once data goes into the Container it stays there. We could get it out by using .__value , but that would defeat the purpose.



    Bibliogrphy: