Monadic-style interface for state updates.
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 an Update.
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.
Run an update if the given condition is True, otherwise do nothing.
Sequential composition of updates. This function is especially useful in conjunction
with the forward pipe operator (|>), for writing code in the style of pipelines. To chain
updates, we compose functions of the form 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 having 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.
Combine the state of two Updates using a function of two arguments.
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.
Combine the state of three Updates using a function of three arguments.
Combine the state of four Updates using a function of four arguments.
Combine the state of five Updates using a function of five arguments.
Combine the state of six Updates using a function of six arguments.
Combine the state of seven Updates using a function of seven arguments.
Callbacks allow for information to be passed up in the update tree.
Append a callback to the list of functions that subsequently get applied to the returned value using runCallbacks.
Compose and apply the list of monadic functions (callbacks) produced by a nested update call.
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.
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))Shortcut for andThen << apply
Shortcut for andThen << addCmd
Shortcut for \fun -> andThen << with fun
Shortcut for andThen << using
Shortcut for \cond -> andThen << when cond
Type wrapper for Elm's
( model, Cmd msg )tuple.