文章出處
文章列表
// 參考
// 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是一個起子,用來打開容器。
// 把容器里的函數應用到容器里的值。
文章列表
全站熱搜