这是单子吗? [英] Is this a monad?
问题描述
我试图理解单子的概念,我想知道这个代码是否是这个概念的实现(在JavaScript中)。
我有函数返回带有set方法的新对象,创建包装方法的M返回值:
var foo = M()。set('getX', function(){
return this.x;
})。set('setX',function(x){
this.x = x;
})。set 'addX',函数(x){
this.x + = x;
});
然后我可以链接foo的方法
foo.setX(10).addX(20).addX(30).getX()
将返回60
,如果我有方法对象并用此对象调用M,则返回相同的结果。
$ b
var foo = {
x:10,
add:function(x){
this.x + = x ;
}
};
(foo).add(10).add(20).add(30).x
将返回70
函数包装在M对象中,所以此方法内部的上下文始终是M对象。
$ b $
f = M({x:20})。set('getX',function(){
return this.x; $ b (x){
this.x + = x;
})。addX(10).getX
所以f是函数与M包装的对象的上下文 - 如果我调用 f()
它会返回30。
我的理解是否正确?是M monad?
编辑修改后的代码位于github上 https://github.com/jcubic/monadic
这是一个monoid模式。每个状态更新操作,例如 .setX(10)
, .addX(20)
等等,是一个转换一个对象的计算。 (为了在语法上有效,你必须把它写成一个单参数函数 function(x){x.addX(20);}
,但我认为它更清晰如果我使用简短的形式。)
有两件事情使它成为一个monoid。首先,存在一个标识元素: .addX(0)
对其对象没有任何作用。其次,任何两个操作可以合并。例如, .setX(10).addX(20)
也是一个可以转换一个对象的计算。
这不是一个monad。您的方法支持的计算仅限于编写和更新 this.x
。 ( .getX()
不是monoid的成员,因为你不能在它之后链接任何东西)。例如,对于monad,您可以让一个操作链中的一个成员执行if-then-else来决定链中的下一个内容。你的方法不能这样做。
I'm trying to understand the concept of monads and I want to know if this code is an implementation of this concept (in JavaScript).
I have function M which return new object that have set method which create wrapper method
var foo = M().set('getX', function() {
return this.x;
}).set('setX', function(x) {
this.x = x;
}).set('addX', function(x) {
this.x += x;
});
And then I can chain method of foo
foo.setX(10).addX(20).addX(30).getX()
will return 60
and the same if I have object with methods and call M with this object.
var foo = {
x: 10,
add: function(x) {
this.x += x;
}
};
M(foo).add(10).add(20).add(30).x
will return 70
Functions are wrapped inside M object so the this context inside method is always that M object.
f = M({x: 20}).set('getX', function() {
return this.x;
}).set('addX', function(x) {
this.x += x;
}).addX(10).getX
so f is function with context of object wrapped by M — if I call f()
it will return 30.
Am I understand this correctly? Is M a monad?
EDIT modified code is on github https://github.com/jcubic/monadic
This is a monoid pattern. Each state-updating operation, such as .setX(10)
, .addX(20)
, and so forth, is a computation that transforms one object. (To be syntactically valid, you would have to write it as a one-parameter function function(x) {x.addX(20);}
, but I think it's clearer if I use the short form.)
Two things make this a monoid. First, there is an identity element: .addX(0)
does nothing to its object. Second, any two operations can be combined. For example, .setX(10).addX(20)
is also a computation that transforms one object.
It is not a monad. The computations supported by your methods are limited to writing and updating this.x
. (.getX()
is not a member of the monoid because you can't chain anything after it). For example, with a monad you can have one member of a chain of operations execute an if-then-else to decide what comes next in the chain. Your methods can't do that.
这篇关于这是单子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!