Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Created April 4, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syntacticsugar/331f195293d02555054fc32f3e89feb3 to your computer and use it in GitHub Desktop.
Save syntacticsugar/331f195293d02555054fc32f3e89feb3 to your computer and use it in GitHub Desktop.
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