Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wherein i learn about my first monad.
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
type alias Model =
{ count : Int }
initialModel : Model
initialModel =
{ count = 0 }
type Msg
= Increment
| Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
{ model | count = model.count + 1 }
Decrement ->
{ model | count = model.count - 1 }
view : Model -> Html Msg
view model =
div []
[ button [ onClick Increment ] [ text "+1" ]
, div [] [ text <| String.fromInt model.count ]
, button [ onClick Decrement ] [ text "-1" ]
]
main : Program () Model Msg
main =
Browser.sandbox
{ init = initialModel
, view = view
, update = update
}
mappy : (a -> b) -> Maybe.Maybe a -> Maybe.Maybe b
mappy f m =
case m of
Nothing -> Nothing
Just x -> Just (f x)
flatMappy : (a -> Maybe.Maybe b) -> Maybe.Maybe a -> Maybe.Maybe b
flatMappy f m =
case m of
Nothing -> Nothing
Just x -> f x
functionPipeline value f g h =
value
|> f
|> g
|> h
monadicPipeline m f g h =
m
|> (Maybe.andThen f)
|> Maybe.andThen g
|> Maybe.andThen h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment