Fresheyeball/elm-return/6.0.0

NOTE: this package is not compatible with Elm 0.19.1

Return

Type

Modeling the update tuple as a Monad similar to Writer

type alias Return msg model =
( model, Cmd msg )
type alias ReturnF msg model =
Return msg model -> Return msg model

Mapping

map : (a -> b) -> Return msg a -> Return msg b

Transform the Model the Cmd will be left untouched

map2 : (a -> b -> c) -> Return msg a -> Return msg b -> Return msg c

Combine 2 Returns with a function

map2
  (\modelA modelB -> { modelA | foo = modelB.foo })
  retA
  retB
(a -> b -> c -> d)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
(a -> b -> c -> d -> e)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
-> Return msg e
(a -> b -> c -> d -> e -> f)
-> Return msg a
-> Return msg b
-> Return msg c
-> Return msg d
-> Return msg e
-> Return msg f
andMap : Return msg a -> Return msg (a -> b) -> Return msg b

Map an Return into a Return containing a Model function

mapWith : (a -> b) -> Cmd msg -> Return msg a -> Return msg b

Transform the Model of and add a new Cmd to the queue

mapCmd : (a -> b) -> Return a model -> Return b model

Map on the Cmd.

mapBoth : (a -> b) -> (c -> d) -> Return a c -> Return b d

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
dropCmd : ReturnF msg model

Drop the current Cmd and replace with an empty thunk

Piping

piper : List (ReturnF msg model) -> ReturnF msg model
pipel : List (ReturnF msg model) -> ReturnF msg model
zero : ReturnF msg model

Basics

singleton : model -> Return msg model

Create a Return from a given Model

andThen : (a -> Return msg b) -> Return msg a -> Return msg b
-- 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` 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.

(>>>) : (a -> Return msg b) -> (b -> Return msg c) -> a -> Return msg c
(<<<) : (b -> Return msg c) -> (a -> Return msg b) -> a -> Return msg c

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` foo

this code roughly becomes:

doFoo3Times : { model | bar : Int } -> Return msg { model | bar : Int }
doFoo3Times =
  foo >>> foo >>> foo

Write Cmds

return : model -> Cmd msg -> Return msg model

Construct a new Return from parts

command : Cmd msg -> ReturnF msg model

Add a Cmd to a Return, the Model is uneffected

effect_ : Respond msg model -> ReturnF msg model

Add a Cmd to a Return based on its Model, the Model will not be effected

Fancy non-sense

sequence : List (Return msg model) -> Return msg (List model)
flatten : Return msg (Return msg model) -> Return msg model