Skip to content

Commit 6864ec3

Browse files
committed
---
yaml --- r: 14088 b: refs/heads/try c: 91da710 h: refs/heads/master v: v3
1 parent 9416153 commit 6864ec3

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: e0fa5cd2edbbb611ff3759a31357a70ca9582245
5+
refs/heads/try: 91da710d86a58f4596b6ea22ccf35dd896445ccf
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
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)