about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarijn Haverbeke <marijnh@gmail.com>2012-01-31 13:37:06 +0100
committerMarijn Haverbeke <marijnh@gmail.com>2012-02-06 09:56:41 +0100
commit91da710d86a58f4596b6ea22ccf35dd896445ccf (patch)
tree612c394d43b5a7d51a0dd4ca7d7f8db4bff40a2d /src
parente0fa5cd2edbbb611ff3759a31357a70ca9582245 (diff)
downloadrust-91da710d86a58f4596b6ea22ccf35dd896445ccf.tar.gz
rust-91da710d86a58f4596b6ea22ccf35dd896445ccf.zip
Add monad iface test
Diffstat (limited to 'src')
-rw-r--r--src/test/run-pass/monad.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/test/run-pass/monad.rs b/src/test/run-pass/monad.rs
new file mode 100644
index 00000000000..bdfb6ff8b70
--- /dev/null
+++ b/src/test/run-pass/monad.rs
@@ -0,0 +1,31 @@
+iface monad<A> {
+    fn bind<B>(fn(A) -> self<B>) -> self<B>;
+}
+
+impl <A> of monad<A> for [A] {
+    fn bind<B>(f: fn(A) -> [B]) -> [B] {
+        let r = [];
+        for elt in self { r += f(elt); }
+        r
+    }
+}
+
+impl <A> of monad<A> for option<A> {
+    fn bind<B>(f: fn(A) -> option<B>) -> option<B> {
+        alt self {
+          some(a) { f(a) }
+          none { none }
+        }
+    }
+}
+
+fn transform(x: option<int>) -> option<str> {
+    x.bind {|n| some(n + 1)}.bind {|n| some(int::str(n))}
+}
+
+fn main() {
+    assert transform(some(10)) == some("11");
+    assert transform(none) == none;
+    assert ["hi"].bind {|x| [x, x + "!"]}.bind {|x| [x, x + "?"]} ==
+        ["hi", "hi?", "hi!", "hi!?"];
+}