https://github.com/laserpants/elm-update-pipeline
Elm interface for sequential composition of updates in the style of pipelines.
https://github.com/laserpants/elm-update-pipeline
applicative elm monad update
Last synced: 3 months ago
JSON representation
Elm interface for sequential composition of updates in the style of pipelines.
- Host: GitHub
- URL: https://github.com/laserpants/elm-update-pipeline
- Owner: laserpants
- License: bsd-3-clause
- Created: 2019-12-01T13:10:35.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-20T11:17:47.000Z (over 5 years ago)
- Last Synced: 2025-03-29T04:24:42.957Z (4 months ago)
- Topics: applicative, elm, monad, update
- Language: Elm
- Homepage: https://package.elm-lang.org/packages/laserpants/elm-update-pipeline/latest/
- Size: 51.8 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Elm Update Pipeline
[](https://travis-ci.org/laserpants/elm-update-pipeline)
[](http://elm-lang.org/)A library for sequential composition of updates in the convenient style of _pipelines_, where functions are chained together using the pipe operator.
For example;```elm
update msg model =
case msg of
SomeMsg someMsg ->
save model
|> andThen (setPower 100)
|> andAddCmd someCmd
```Monadic functions of type `a -> ( b, Cmd msg )` form the building blocks of a pipeline.
We use `save` to create an update without any commands, and `andThen` to extract the model from a result and pass it as input to the next function in the pipeline.```elm
showToast : String -> Model -> ( Model, Cmd msg )
showToast = ...setColor : Color -> Model -> ( Model, Cmd msg )
setColor color model =
save { model | color = color }update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
model
|> setColor Orange
|> andThen (showToast "Color changed to orange")
```The applicative interface, `map2`, `map3`, etc., together with `andMap`, addresses the need to map functions with more than one parameter over `( model, Cmd msg )` inputs.
```elm
type alias Model =
{ menuOpen : Bool, session : Session, router : Router.Model }initSession : Flags -> ( Session, Cmd Msg )
initSession = ...init : Flags -> ( Model, Cmd Msg )
init flags =
save Model
|> andMap (save False)
|> andMap (initSession flags)
|> andMap initRouter
```In this example, `init` can also be defined as
```elm
init flags =
map3 Model (save False) (initSession flags) initRouter
```