1 USING: tools.test math math.functions kernel sequences lists
6 { 5 } [ 1 identity-monad return [ 4 + ] fmap run-identity ] unit-test
7 [ "OH HAI" identity-monad fail ] must-fail
10 111 <just> [ 6 * ] fmap [ ] [ "OOPS" throw ] if-maybe
14 111 <just> [ maybe-monad fail ] bind
18 5 either-monad return [ 10 * ] [ 20 * ] if-either
21 { T{ left f "OOPS" } } [
22 5 either-monad return >>= [ drop "OOPS" either-monad fail ] swap call
26 { 1 2 3 } [ 10 * ] fmap
30 { 1 2 3 } [ drop "OOPS" array-monad fail ] bind
34 5 state-monad return "initial state" run-st
38 5 state-monad return [ 3 + state-monad return ] bind
39 "initial state" run-st
43 5 state-monad return >>=
44 [ 3 + state-monad return ] swap call
45 "initial state" run-st
49 f state-monad return >>=
50 [ drop get-st ] swap call
75 [ " bye" append <just> ]
81 LAZY: nats-from ( n -- list )
82 dup 1 + nats-from cons ;
84 : nats ( -- list ) 0 nats-from ;
89 [ dup 3 = [ list-monad return ] [ list-monad fail ] if ]
102 [ 3 + reader-monad return ]
108 f reader-monad return [ drop ask ] bind [ 1 + ] local 5 run-reader
112 5 writer-monad return
113 [ drop { 1 2 3 } tell ] bind
119 { value 1.618033988749895 }
121 "Started with five, took square root, added one, divided by two."
126 [ 5 "Started with five, " <writer> ]
127 [ sqrt "took square root, " <writer> ]
128 [ 1 + "added one, " <writer> ]
129 [ 2 / "divided by two." <writer> ]
133 { T{ identity f 7 } }
135 4 identity-monad return
136 [ 3 + ] identity-monad return
141 5 <just> nothing maybe-monad apply
145 5 <just> [ 10 + ] <just> maybe-monad apply