Skip to content

Instantly share code, notes, and snippets.

@rvcas
Last active May 3, 2017 16:04
Show Gist options
  • Save rvcas/091d039099b248270f3ef52e59f500e6 to your computer and use it in GitHub Desktop.
Save rvcas/091d039099b248270f3ef52e59f500e6 to your computer and use it in GitHub Desktop.
Some nonsense in Elm
module Main exposing (..)
import Html
type Maybe a
= Just a
| Nothing
mapMaybe : (a -> b) -> Maybe a -> Maybe b
mapMaybe f perhaps =
case perhaps of
Nothing ->
Nothing
Just a ->
Just (f a)
maybeToList : (a -> b) -> List (Maybe a) -> List b
maybeToList f perhaps =
case perhaps of
x :: xs ->
let
result =
mapMaybe f x
in
case result of
Nothing ->
maybeToList f xs
Just a ->
a :: maybeToList f xs
[] ->
[]
main : Html.Html a
main =
Html.text <|
case test of
[ 2, 4 ] ->
"pass"
_ ->
"fail"
succ : Int -> Int
succ =
(+) 1
test : List Int
test =
let
maybe_list =
[ Just 1, Nothing, Just 3, Nothing ]
in
maybeToList succ maybe_list
@rvcas
Copy link
Author

rvcas commented Apr 29, 2017

running elm-reactor in the directory containing this file should be enough to run it

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