函数式functor的理解

// 参考
// http://jiyinyiyong.github.io/monads-in-pictures/
// https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch8.html
// https://zhuanlan.zhihu.com/p/21926955
// https://zhuanlan.zhihu.com/p/22094473

// functor就是一个容器
var Container = function(x) {
  this.__value = x;
}

Container.of = function(x) { return new Container(x); };
Container.of(3);

// map函数的参数是一个函数。这个函数用来运算容器里面的值。
Container.prototype.map = function(f){
  return Container.of(f(this.__value))
};
Container.of(2).map(function(two){ return two + 2 }).map(function(num){return num * 2 });




// 高级一点的functor,可以处理null的functor。
var Maybe = function(x) {
  this.__value = x;
};

Maybe.of = function(x) {
  return new Maybe(x);
};

Maybe.prototype.isNothing = function() {
  return (this.__value === null || this.__value === undefined);
};

Maybe.prototype.map = function(f) {
  return this.isNothing() ? Maybe.of(null) : Maybe.of(f(this.__value));
};

Maybe.prototype.join = function() {
  return this.isNothing() ? Maybe.of(null) : this.__value;
}

Maybe.of({name: "Boris"}).map(function(obj){return obj['age'] }).map(function(num){return num + 10});
Maybe.of(3).map(function(num){return num + 2; }).map(function(num){return num + 5});

console.log('点的连续运算', Maybe.of(3).map(function(num){return num + 2; }).map(function(num){return num + 5}) );
console.log('函数组合',Maybe.of(function(num){return num + 2}).map(function(num){return num + 3}));

// 尽管Maybe会返回null,但是我们不知道它从哪返回,也没有更多异常信息。
// 错误捕获
var Left = function(x) {
  this.__value = x;
}
var Right = function(x) {
  this.__value = x;
}

Left.of = function(x) {
  return new Left(x);
};
Right.of = function(x) {
  return new Right(x);
};

Left.prototype.map = function(f) {
  return this;
};
Right.prototype.map = function(f) {
  return Right.of(f(this.__value));
};

var either = function(f, g, e) {
  switch(e.constructor) {
    case Left: return f(e.__value);
    case Right: return g(e.__value);
  }
};

var a = Maybe.of({name: "Boris"}).map(function(obj){
    if(obj['name']){return Right.of('has age')}
    else{
        return Left.of('no age');
    } 
})
// .map(function(num){return num + 10}).map(function(num){return num + 1});
console.log('a',a);

// 另类functor,惰性求值,用来接触外面的世界。把副作用操作( IO操作、异步请求、DOM操作)包裹起来。
var IO = function(f) {
  this.__value = f;
};

IO.of = function(x) {
  return new IO(function() {
    return x;
  });
};

IO.prototype.map = function(f) {
  return new IO(f(this.__value));
};

// 可以链式调用。
// 可以进行函数组合。
// 处理副作用。常见的副作用包括异常和异步。
// 我们很快想到数组也是一个容器functor,该容器的map函数也是用来运算容器里面的值。
// promise也是一个容器functor。


// Applicative是一个起子,用来打开容器。
// 把容器里的函数应用到容器里的值。





原文地址:https://www.cnblogs.com/samwu/p/6053016.html