Skip to content

Instantly share code, notes, and snippets.

@rofrol
Last active March 25, 2020 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rofrol/cacb386d71ce62399826be22e45c4dd6 to your computer and use it in GitHub Desktop.
Save rofrol/cacb386d71ce62399826be22e45c4dd6 to your computer and use it in GitHub Desktop.
Explain Monad using Elm - WIP

Explain Monad using Elm

Try to guess what will the code display before before clicking Details.

Inspired by https://janiczek.github.io/silent-teacher/.

text

main =
    Html.text "Hello"
Displayed: Hello

Run this example

toString

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (Debug.toString maybeHead)
Displayed: Just "one"

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (Debug.toString maybeHead)
Displayed: Nothing

Run this example

case

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (case maybeHead of
            Just head ->
                head

            Nothing ->
                ""
        )
Displayed: one

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (case maybeHead of
            Just head ->
                head

            Nothing ->
                ""
        )
Displayed:

Run this example

withDefault

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "" maybeHead)
Displayed: one

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "" maybeHead)
Displayed:

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "Empty list" maybeHead)
Displayed: Empty list

Run this example

map

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "Empty list" (Maybe.map (\head -> "Head: " ++ head) maybeHead))
Displayed: Head: one

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "Empty list" (Maybe.map (\head -> "Head: " ++ head) maybeHead))
Displayed: Empty list

Run this example

list =
    []


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "Empty list" (Maybe.map (\head -> "Head: " ++ head) maybeHead))
Displayed: Empty list

Run this example

andThen

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.withDefault "Empty list" (Maybe.andThen (\head -> Just ("Head: " ++ head)) maybeHead))
Displayed: Head: one

Run this example

|>

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (Maybe.map (\head -> "Head: " ++ head) maybeHead |> Maybe.withDefault "Empty list")
Displayed: Head: one

Run this example

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    Html.text
        (maybeHead |> Maybe.map (\head -> "Head: " ++ head) |> Maybe.withDefault "Empty list")
Displayed: Head: one

Run this example

list =
    [ "one", "two" ]


maybeHead =
    List.head list


main =
    maybeHead |> Maybe.map (\head -> "Head: " ++ head) |> Maybe.withDefault "Empty list" |> Html.text
Displayed: Head: one

Run this example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment