Monadic-style interface for state updates.
In standard use, you will not need the third type parameter of
Update. SeeBurrito.Update.Simplein that case.
Lifts a value into the Update context. For example,
save modelcorresponds to ( model, Cmd.none ) in code that doesn't use this library.
Add a command to an Update pipeline. For example;
update msg state =
case msg of
SomeMsg someMsg ->
state
|> addCmd someCommand
|> andThen (addCmd someOtherCommand)
|> andThen (setStatus Done)In this example, andThen (addCmd someOtherCommand) can also be shortened to
andAddCmdsomeOtherCommand.
Apply a function to the state portion of a value.
Map over the Cmd contained in the provided Update.
Remove one level of monadic structure. It may suffice to know that some other
functions in this library are implemented in terms of join. In particular, andThen f = join << map f
Right-to-left (Kleisli) composition of two functions that return Update values,
passing the state part of the first return value to the second function.
Sequential composition of updates. This function is especially useful in combination
with the forward pipe operator (|>), for writing code in the style of pipelines. To chain
updates, we compose functions of the form something -> State -> Update State msg t:
say : String -> State -> Update State msg t
say what state = ...
save state
|> andThen (say "hello")
|> andThen doSomethingElseAside: andThen is like the monadic bind (>>=) operator in Haskell, but with the arguments interchanged.
Take a list of a -> Update a msg t values and run them sequentially, in a left-to-right manner.
These functions address the need to map over functions of more than one argument.
Trying to map over a function number -> number -> number,
map (+) (save 4)we end up with a result of type Update (number -> number) msg t. To apply the function inside this value to another Update number msg t value, we can write…
map (+) (save 4) |> andMap (save 5)in elm repl, we can verify that the result is what we expect:
> (map (+) (save 4) |> andMap (save 5)) == save 9
True : BoolThis pattern scales in a nice way to functions of any number of arguments:
let
f x y z =
x + y + z
in
map f (save 1)
|> andMap (save 1)
|> andMap (save 1)If not sooner, you'll need this when you want to mapN and N > 7.
This function is like andMap, but with the arguments interchanged.
Apply a function of two arguments to the state portion of a value.
Equivalently, we can think of this as taking a function a -> b -> c and
transforming it into a “lifted” function of type Update a msg t -> Update b msg t -> Update c msg t.
Apply a function of three arguments to the state portion of a value.
Apply a function of four arguments to the state portion of a value.
Apply a function of five arguments to the state portion of a value.
Apply a function of six arguments to the state portion of a value.
Apply a function of seven arguments to the state portion of a value.
Translate a function that returns an Update into one that returns a plain ( model, cmd ) pair.
Same as run, but for functions of two arguments.
Same as run, but for functions of three arguments.
Shortcut for andThen << addCmd
Combinator useful for pointfree style. For example;
nextPage state =
goToPage (state.current + 1) statecan be changed to
nextPage =
using (\{ current } -> goToPage (current + 1))Combinator useful for pointfree style. For example, to get rid of the lambda in the following code;
update msg state =
case msg of
Click ->
state
|> updateSomething
|> andThen (\s -> setCounterValue (s.counter + 1) s)we can write:
update msg state =
case msg of
Click ->
state
|> updateSomething
|> andThen (with .counter (setCounterValue << (+) 1))
Type wrapper for Elm's
( model, Cmd msg )tuple.