-
Notifications
You must be signed in to change notification settings - Fork 0
/
control-monad.red
198 lines (176 loc) · 4.01 KB
/
control-monad.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Red [
Title: "Control.Monad"
Author: "unchartedworks"
File: %control-monad.red
Tabs: 4
Rights: "unchartedworks. All rights reserved."
License: "MIT"
]
sequencially-compose-pass*: function [
"Sequentially compose two actions, passing any value produced by the first as an argument to the second."
x [object! series!]
f [function!]
][
case [
object? x (sequencially-compose-pass-object* x :f)
series? x (sequencially-compose-pass-series* x :f)
]
]
sequencially-compose-pass-object*: function [
x [object!]
f [any-function!]
][
case [
(isLeft x) x
(isRight x) (f fromRight x)
(isNothing x) Nothing
(isJust x) (f fromJust x)
]
]
sequencially-compose-pass-series*: function [
xs [series!]
f [any-function!]
][
case [
(empty? xs) xs
(series? xs) (concatMap :f xs)
]
]
>>=: make op! :sequencially-compose-pass*
sequencially-compose-discard*: function [
"Sequentially compose two actions, passing any value produced by the first as an argument to the second."
x [object! series!]
y [object! series!]
][
case [
((object? x) && (object? y)) (sequencially-compose-discard-object* x y)
((series? x) && (series? y)) (sequencially-compose-discard-series* x y)
]
]
sequencially-compose-discard-object*: function [
x [object!]
y [object!]
][
case [
(isLeft x) x
(isRight x) y
(isNothing x) Nothing
(isJust x) y
]
]
sequencially-compose-discard-series*: function [
xs [series!]
ys [series!]
][
case [
(empty? xs) xs
(series? xs) ys
]
]
>>: make op! :sequencially-compose-discard*
;;Basic Monad functions
; mapM: function [
; "Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results. For a version that ignores the results see mapM_."
; f: [any-function!]
; xs: [object! series!]
; ][
; case [
; object? x (mapM-object* :f xs)
; series? x (mapM-series* :f xs)
; ]
; ]
; mapM-object: function [
; f: [any-function!]
; xs: [object! series!]
; ][
; ]
; mapM-object: function [
; f: [any-function!]
; xs: [object! series!]
; ][
; foldl :f
; ]
;;Monadic lifting operators
liftM: function [
"Promote a function to a monad."
f [any-function!]
mx [object! series!]
][
mx >>= (function [x] [liftM* mx :f x])
]
liftM*: function [
mx [object! series!]
f [any-function!]
x
][
case [
(object? mx) (liftM-object* mx :f x)
(series? mx) (liftM-series* mx :f x)
]
]
liftM-object*: function [
mx [object!]
f [any-function!]
x
][
case [
((isJust mx) || (isNothing mx)) (Just (f x))
((isLeft mx) || (isRight mx)) (Right (f x))
]
]
liftM-series*: function [
mx [series!]
f [any-function!]
x
][
either (string? mx) [to-string f x][reduce [f x]]
]
liftM2: function [
"Promote a function to a monad, scanning the monadic arguments from left to right."
f [any-function!]
mx [object! series!]
my [object! series!]
][
;;function [x] reduce ['function [y] 'reduce ['add :x 'y] ]
g: (function [x] reduce ['function [y] 'reduce ['f :x 'y]])
print mold :g
h: function [x][g x]
;print mold :h
;i: h 2
;print mold (i 2)
;print mold :i
s: (liftM :g [1 2])
print mold :s
[]
;my >>= :fs
]
; liftM2*: function [
; mx [object! series!]
; f [any-function!]
; x
; y
; ][
; case [
; (object? mx) (liftM-object* mx :f x)
; (series? mx) (liftM-series* mx :f x)
; ]
; ]
; liftM-object*: function [
; mx [object!]
; f [any-function!]
; x
; y
; ][
; case [
; ((isJust mx) || (isNothing mx)) (Just (f x))
; ((isLeft mx) || (isRight mx)) (Right (f x))
; ]
; ]
; liftM-series*: function [
; mx [series!]
; f [any-function!]
; x
; y
; ][
; either (string? mx) [to-string f x][reduce [f x]]
; ]