Skip to content

Commit 91da710

Browse files
committed
Add monad iface test
1 parent e0fa5cd commit 91da710

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/test/run-pass/monad.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)