Skip to content

Instantly share code, notes, and snippets.

@boombang
Last active September 26, 2019 20:32
Show Gist options
  • Save boombang/c1ac289a2d098b6cb1a744e0861fe42a to your computer and use it in GitHub Desktop.
Save boombang/c1ac289a2d098b6cb1a744e0861fe42a to your computer and use it in GitHub Desktop.
type Maybe a
= Just a
| Nothing
-- Partial Functions
-- toFloat : String -> Maybe Float
a =
String.toFloat "123"
-- Just 123.0
b =
String.toFloat "-42"
-- Just -42.0
c =
String.toFloat "31a"
-- Nothing
-- List.head : List a -> Maybe a
d =
List.head [ 1, 2, 3 ]
-- Just 1
e =
List.head []
-- Nothing
numbers : List Int
-- filterMap : (a -> Maybe b) -> List a -> List b
numbers =
List.filterMap String.toInt [ "3", "hi", "12", "4th", "May" ]
-- numbers == [3, 12]
-- Optional Fields
type alias User =
{ name : String
, age : Maybe Int
}
sue : User
sue =
{ name = "Sue", age = Nothing }
tom : User
tom =
{ name = "Tom", age = Just 24 }
canBuyAlcohol : User -> Bool
canBuyAlcohol user =
case user.age of
Nothing ->
False
Just age ->
age >= 21
type Result error value
= Ok value
| Err error
type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus Int
| BadBody String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment