天天看点

[Javascript] Functor Basic Intro

well, this stuff will be a little bit strange if you deal with it first time.

container object:

  just a wrapper / contianer for values

  no method

      no nouns

every time we use container, it will just add the value to the container object and assign value to prop val.

map function on container:

this map is not the map what you think it is.... 

the map you think is the method on the array.

here the map is the one get into the container, grep the value, then apply function on this value. the return value is still container.

so in the example, map function goes into the container, get the value "flamethrower", and apply the function '_.capitialize' to the value and return the new value to the container.

or you can write like this:

more examples:

so "container"... what you feel about it? it is nothing... you don't need to care about it. just every time you call map on it, the return value will still inside the container, so that you can chain map on it.

curry map:

define a global map function, which use ramda curry method:

later we will pass container as obj, and on our container, we already defined map function, so we can use here as 'obj.map'.

so, again, here 'obj.map' --> means, goes into the obj, grep the value and apply function f.

so now what we can do:

more exmaples:

so all what we have seen so far, we give a name call "functor".

functor

“an object or data structure you can map over”

 function: map

继续阅读