Jump to content

Monads/List monad: Difference between revisions

Added Wren
m (Promote to task, lots of examples, little controversy)
(Added Wren)
Line 681:
[3,4,5].bind_comp(listy_doub, listy_inc) #=> [8, 10, 12]
</lang>
 
=={{header|Wren}}==
{{trans|Go}}
<lang ecmascript>class Mlist {
construct new(value) { _value = value }
 
value { _value }
 
bind(f) { f.call(_value) }
 
static unit(lst) { Mlist.new(lst) }
}
 
var increment = Fn.new { |lst|
var lst2 = lst.map { |v| v + 1 }.toList
return Mlist.unit(lst2)
}
 
var double = Fn.new { |lst|
var lst2 = lst.map { |v| v * 2 }.toList
return Mlist.unit(lst2)
}
 
var ml1 = Mlist.unit([3, 4, 5])
var ml2 = ml1.bind(increment).bind(double)
System.print("%(ml1.value) -> %(ml2.value)")</lang>
 
{{out}}
<pre>
[3, 4, 5] -> [8, 10, 12]
</pre>
 
=={{header|zkl}}==
9,486

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.