-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReturn.elm
308 lines (222 loc) · 5.84 KB
/
Return.elm
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
module Return
exposing
( Return
, ReturnF
, andMap
, andThen
, andThenK
, command
, dropCmd
, effect_
, flatten
, map
, map2
, map3
, map4
, map5
, mapBoth
, mapCmd
, mapWith
, pipel
, pipelK
, piper
, piperK
, return
, sequence
, singleton
, zero
)
{-|
## Type
Modeling the `update` tuple as a Monad similar to `Writer`
@docs Return, ReturnF
## Mapping
@docs map, map2, map3, map4, map5, andMap, mapWith, mapCmd, mapBoth, dropCmd
## Piping
@docs piper, pipel, zero, piperK, pipelK
## Basics
@docs singleton, andThen, andThenK
## Write `Cmd`s
@docs return, command, effect_
## Fancy non-sense
@docs sequence, flatten
-}
import Respond exposing (..)
import Tuple
{-| -}
type alias Return msg model =
( model, Cmd msg )
{-| -}
type alias ReturnF msg model =
Return msg model -> Return msg model
{-| -}
piper : List (ReturnF msg model) -> ReturnF msg model
piper =
List.foldr (<<) zero
{-| -}
pipel : List (ReturnF msg model) -> ReturnF msg model
pipel =
List.foldl (>>) zero
{-| -}
zero : ReturnF msg model
zero =
identity
{-| Transform the `Model`, the `Cmd` will be left untouched
-}
map : (a -> b) -> Return msg a -> Return msg b
map f ( model, cmd ) =
( f model, cmd )
{-| Transform the `Model` of and add a new `Cmd` to the queue
-}
mapWith : (a -> b) -> Cmd msg -> Return msg a -> Return msg b
mapWith f cmd ret =
andMap ret ( f, cmd )
{-| Map an `Return` into a `Return` containing a `Model` function
-}
andMap : Return msg a -> Return msg (a -> b) -> Return msg b
andMap ( model, cmd_ ) ( f, cmd ) =
( f model, Cmd.batch [ cmd, cmd_ ] )
{-| Map over both the model and the msg type of the `Return`.
This is useful for easily embedding a `Return` in a Union Type.
For example
import Foo
type Msg = Foo Foo.Msg
type Model = FooModel Foo.Model
...
update : Msg -> Model -> Return Msg Model
update msg model =
case msg of
Foo foo -> Foo.update foo model.foo
|> mapBoth Foo FooModel
-}
mapBoth : (a -> b) -> (c -> d) -> Return a c -> Return b d
mapBoth f f_ ( model, cmd ) =
( f_ model, Cmd.map f cmd )
{-| Combine 2 `Return`s with a function
map2
(\modelA modelB -> { modelA | foo = modelB.foo })
retA
retB
-}
map2 :
(a -> b -> c)
-> Return msg a
-> Return msg b
-> Return msg c
map2 f ( x, cmda ) ( y, cmdb ) =
( f x y, Cmd.batch [ cmda, cmdb ] )
{-| -}
map3 :
(a -> b -> c -> d)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
map3 f ( x, cmda ) ( y, cmdb ) ( z, cmdc ) =
( f x y z, Cmd.batch [ cmda, cmdb, cmdc ] )
{-| -}
map4 :
(a -> b -> c -> d -> e)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
-> Return msg e
map4 f ( w, cmda ) ( x, cmdb ) ( y, cmdc ) ( z, cmdd ) =
( f w x y z, Cmd.batch [ cmda, cmdb, cmdc, cmdd ] )
{-| -}
map5 :
(a -> b -> c -> d -> e -> f)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
-> Return msg e
-> Return msg f
map5 f ( v, cmda ) ( w, cmdb ) ( x, cmdc ) ( y, cmdd ) ( z, cmde ) =
( f v w x y z, Cmd.batch [ cmda, cmdb, cmdc, cmdd, cmde ] )
{-| Create a `Return` from a given `Model`
-}
singleton : model -> Return msg model
singleton a =
( a, Cmd.none )
{-|
foo : Model -> Return Msg Model
foo ({ bar } as model) =
-- forking logic
if
bar < 10
-- that side effects may be added
then
( model, getAjaxThing )
-- that the model may be updated
else
( { model | bar = model.bar - 2 }, Cmd.none )
They are now chainable with `andThen`...
resulting : Return msg { model | bar : Int }
resulting =
myReturn
|> andThen foo
|> andThen foo
|> andThen foo
Here we changed up `foo` three times, but we can use any function of
type `(a -> Return msg b)`.
Commands will be accumulated automatically as is the case with all
functions in this library.
-}
andThen : (a -> Return msg b) -> Return msg a -> Return msg b
andThen f ( model, cmd ) =
let
( model_, cmd_ ) =
f model
in
( model_, Cmd.batch [ cmd, cmd_ ] )
{-| Construct a new `Return` from parts
-}
return : model -> Cmd msg -> Return msg model
return a b =
identity ( a, b )
{-| Add a `Cmd` to a `Return`, the `Model` is uneffected
-}
command : Cmd msg -> ReturnF msg model
command cmd ( model, cmd_ ) =
( model, Cmd.batch [ cmd, cmd_ ] )
{-| Add a `Cmd` to a `Return` based on its `Model`, the `Model` will not be effected
-}
effect_ : Respond msg model -> ReturnF msg model
effect_ f ( model, cmd ) =
( model, Cmd.batch [ cmd, f model ] )
{-| Map on the `Cmd`.
-}
mapCmd : (a -> b) -> Return a model -> Return b model
mapCmd f ( model, cmd ) =
( model, Cmd.map f cmd )
{-| Drop the current `Cmd` and replace with an empty thunk
-}
dropCmd : ReturnF msg model
dropCmd =
singleton << Tuple.first
{-| -}
sequence : List (Return msg model) -> Return msg (List model)
sequence =
let
f ( model, cmd ) ( models, cmds ) =
( model :: models, Cmd.batch [ cmd, cmds ] )
in
List.foldr f ( [], Cmd.none )
{-| -}
flatten : Return msg (Return msg model) -> Return msg model
flatten =
andThen identity
{-| Kleisli composition -}
andThenK : (a -> Return x b) -> (b -> Return x c) -> (a -> Return x c)
andThenK x y a =
x a |> andThen y
{-| Compose updaters from the left -}
pipelK : List (a -> Return x a) -> (a -> Return x a)
pipelK =
List.foldl andThenK singleton
{-| Compose updaters from the right -}
piperK : List (a -> Return x a) -> (a -> Return x a)
piperK =
List.foldr andThenK singleton