Well, this stuff would be a little bit strange if you deal with it first time.
Container Object:
- Just a Wrapper/contianer for values
- No Method
- No nouns
var _container = function (val) { this. val = val;} var Container = function (x) { returnnew _container (x);} Console.log (Container (3// _container ({val:3})
Every time we use Container, it'll just add the value to the Container object and assign value to prop val< /c1>.
Map function on Container:
This map was not the map of what think it was ....
The map You think are the method on the Array.
Here the " map is" the one get into the Container, grep the value and then apply function to this value. The return value is still Container.
_container.prototype.map = function (f) { return Container (f (this. val))}
var res = Container ("flamethrower"return// _container ({val: "flamethrower"})
The example, map function goes into the Container, get the value ' flamethrower ', and apply the fun Ction ' _.capitialize ' to the value and return the new value to the Container.
Or you can write as this:
var capitalize = _.capitalize; var res = Container ("flamethrower"). Map (capitalize);
More examples:
Container ([1,2,3]). Map (reverse). Map (first)//= Container (3) Container ("flamethrower"). Map (length). Map (Add (1)) //= Container (+)
So "Container" ... what are feel about it? It's nothing ... you don ' t need it. Just every time to call map on it, the return value would still inside the Container, so then you can chain maps on it.
Curry Map:
Define a global map function, which use Ramda Curry method:
var map = R.curry (function (f, obj) { return obj.map (f)})
Later we'll pass Container as obj, and on our Container, we already defined map func tion, so we can use here as ' Obj.map '.
So, again, here ' obj.map '-and Means, goes into the obj, grep the value and apply function f.
So now, we can do:
Container (3). Map (Add (1// Container (4))map (Add (1), Container (3// Container (4), or map (add (1)) (Container (3)), since map is Curry method
More exmaples:
Map (R.compose (R.head, R.reverse), Container ("dog"))//= > Container ("G")
So all and we have the seen so far, we give a name "Functor".
Functor
"An object or data structure-can map over"
Function:map
//Exercise 1// ==========//Use _.add (x, y) and map (f,x) to make a function that increments a value inside a functorConsole.log ("--------Start Exercise 1--------")//map (): Go inside the object and run the function on the value of the object//map (): Here map is curry function, so take the function as first arguement and object as second arguement.//Map (_.add (1), Identity (2))--Identity (3)varEX1 = Map (_.add (1); Assertdeepequal (Identity (3), Ex1 (Identity (2)) ) Console.log ("Exercise 1...ok!")//Exercise 2// ==========//Use _.head to get the first element of the listvarxs = Identity ([' Do','Ray','Me','FA',' So','La','ti',' Do']) Console.log ("--------Start Exercise 2--------")varEX2 =map (_.head) assertdeepequal (Identity (' Do'), EX2 (XS)) Console.log ("Exercise 2...ok!")
[Javascript] Functor Basic Intro