Modeling the update tuple as a Monad similar to Writer
Transform the Model the Cmd will be left untouched
Combine 2 Returns with a function
map2
(\modelA modelB -> { modelA | foo = modelB.foo })
retA
retBMap an Return into a Return containing a Model function
Transform the Model of and add a new Cmd to the queue
Map on the 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 FooModelDrop the current Cmd and replace with an empty thunk
Create a Return from a given Model
-- arbitrary function to demonstrate
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` fooHere 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.
Go point free with andThen chaining. Looking at the example from andThen
resulting : Return msg { model | bar : Int }
resulting =
myReturn `andThen` foo
`andThen` foo
`andThen` foothis code roughly becomes:
doFoo3Times : { model | bar : Int } -> Return msg { model | bar : Int }
doFoo3Times =
foo >>> foo >>> fooCmdsConstruct a new Return from parts
Add a Cmd to a Return, the Model is uneffected
Add a Cmd to a Return based on its Model, the Model will not be effected