File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
branches/try/src/test/run-pass Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 2
2
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
3
3
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4
4
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5
- refs/heads/try: e0fa5cd2edbbb611ff3759a31357a70ca9582245
5
+ refs/heads/try: 91da710d86a58f4596b6ea22ccf35dd896445ccf
6
6
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments