[Javascript] Functor Basic Intro

本文介绍了一种称为Functor的数据结构,通过定义map方法来处理包含的数据,并演示了如何利用这一特性进行链式调用。同时介绍了Curry化map函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
var _Container = function(val){
  this.val = val;
}

var Container = function(x){
  return new _Container(x);
}

console.log(Container(3)) // _Container( {val: 3})

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.

_Container.prototype.map = function(f){
  return Container(f(this.val))
}
var res = Container("flamethrower").map(function(s){ return _.capitalize(s) })
console.log(res) //_Container( {val: "Flamethrower"} )

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:

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(13)

 

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:

var map = R.curry(function(f, obj) {
  return obj.map(f) 
})

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:

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 what we have seen so far, we give a name call "Functor".

 

Functor

“An object or data structure you 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 functor
console.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)
var ex1 = 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 list
var xs = Identity(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
console.log("--------Start exercise 2--------")


var ex2 = map(_.head)


assertDeepEqual(Identity('do'), ex2(xs))
console.log("exercise 2...ok!")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值