Containers (not ioc)

And not docker either, for that matter.

After doing this for a while I’m realizing that this functional programming in javascript is basically like porting Haskell.  Or as much of it as our language will allow.  Building up the pieces parts that allow you to compose functions in a friendly sort of way.  Not that I know Haskell or anything about it, just sayin.

The first step to building functions that are easily composed is to your take value and put it in a container that can be treated the same way no matter what is inside it.  This homogenizing of values is similar to standarization that took place at the beginning of the industrial revolution.  Any container of a specific type can be treated the same way regardless of the contents within.  This may not be the best analogy, or even 100% accurate but I like it.

The simplest container ( more commonly and heretofore known as functors ) is the identity functor.   It looks like this

a -> a
function takes a type of "a" and returns a type of "a"
function(a) { return a; };

not very interesting, admittedly.  However, the definition of a functor, dumbed down for me, is an object ( here a value wrapped in a function ) that can be mapped to another object of the same type paraphrased from here.  it is typically shown as

(a->b) -> functor(a) -> functor(b)

So for instance

var whadYouSay = Identity("hell").map(x => x + "o!");
==> Identity("hello!")

A nice image of this can be found in this post which is probably a better article to read than this one.

chart

This shows an increment function being mapped over an identity of 1.

This is the basic building block upon which we can build an empire.  This notion of encapsulation, applicable rules and the idea of currying will allow us to build compositions that are agnostic of the content and immutable

In my next post I’ll write about Prop, SafeProp and Maybe.

 

 

 

Advertisements
Containers (not ioc)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s