We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e0fa5cd commit 91da710Copy full SHA for 91da710
src/test/run-pass/monad.rs
@@ -0,0 +1,31 @@
1
+iface monad<A> {
2
+ fn bind<B>(fn(A) -> self<B>) -> self<B>;
3
+}
4
+
5
+impl <A> of monad<A> for [A] {
6
+ fn bind<B>(f: fn(A) -> [B]) -> [B] {
7
+ let r = [];
8
+ for elt in self { r += f(elt); }
9
+ r
10
+ }
11
12
13
+impl <A> of monad<A> for option<A> {
14
+ fn bind<B>(f: fn(A) -> option<B>) -> option<B> {
15
+ alt self {
16
+ some(a) { f(a) }
17
+ none { none }
18
19
20
21
22
+fn transform(x: option<int>) -> option<str> {
23
+ x.bind {|n| some(n + 1)}.bind {|n| some(int::str(n))}
24
25
26
+fn main() {
27
+ assert transform(some(10)) == some("11");
28
+ assert transform(none) == none;
29
+ assert ["hi"].bind {|x| [x, x + "!"]}.bind {|x| [x, x + "?"]} ==
30
+ ["hi", "hi?", "hi!", "hi!?"];
31
0 commit comments