1 USING: tools.test math kernel sequences lists promises monads ;
5 [ 5 ] [ 1 identity-monad return [ 4 + ] fmap run-identity ] unit-test
6 [ "OH HAI" identity-monad fail ] must-fail
9 111 just [ 6 * ] fmap [ ] [ "OOPS" throw ] if-maybe
13 111 just [ maybe-monad fail ] bind
17 5 either-monad return [ 10 * ] [ 20 * ] if-either
20 [ T{ left f "OOPS" } ] [
21 5 either-monad return >>= [ drop "OOPS" either-monad fail ] swap call
25 { 1 2 3 } [ 10 * ] fmap
29 { 1 2 3 } [ drop "OOPS" array-monad fail ] bind
33 5 state-monad return "initial state" run-st
37 5 state-monad return [ 3 + state-monad return ] bind
38 "initial state" run-st
42 5 state-monad return >>=
43 [ 3 + state-monad return ] swap call
44 "initial state" run-st
48 f state-monad return >>=
49 [ drop get-st ] swap call
74 [ " bye" append just ]
80 LAZY: nats-from ( n -- list )
81 dup 1 + nats-from cons ;
83 : nats ( -- list ) 0 nats-from ;
88 [ dup 3 = [ list-monad return ] [ list-monad fail ] if ]
101 [ 3 + reader-monad return ]
107 f reader-monad return [ drop ask ] bind [ 1 + ] local 5 run-reader
111 5 writer-monad return
112 [ drop { 1 2 3 } tell ] bind
116 [ T{ identity f 7 } ]
118 4 identity-monad return
119 [ 3 + ] identity-monad return
124 5 just nothing maybe-monad apply
128 5 just [ 10 + ] just maybe-monad apply