Generate a case expression!
Here's an example for extracting a Maybe Int
Elm.Case.maybe myMaybe
{ nothing = Elm.int 0
, just =
( "value"
, \content ->
Elm.plus (Elm.int 5) content
)
}Generates
case myMaybe of
Nothing ->
0
Just value ->
value + 5Elm.Case.result myResult
{ ok =
Tuple.pair "ok" <|
\ok ->
Elm.string "No errors"
, err =
Tuple.pair "err" <|
\err ->
err
}Generates
case myResult of
Ok ok ->
"No errors"
Err err ->
errLet's unpack the first value from a list.
Elm.Case.list myList
{ empty = Elm.int 0
, nonEmpty =
\top remaining ->
Elm.plus (Elm.int 5) top
}Generates
case myList of
[] ->
0
top :: remaining ->
top + 5Note if you want more control over unpacking lists, check out branchList
Elm.Case.tuple myTuple
"first"
"second"
(\one two ->
Elm.plus (Elm.int 5) two
)Generates
case myTuple of
( first, second ) ->
5 + secondElm.Case.triple myTriple
"one"
"two"
"three"
(\one two three ->
Elm.plus (Elm.int 5) two
)Generates
case myTriple of
( one, two, three ) ->
5 + twoElm.Case.custom maybeString
(Elm.Annotation.maybe Elm.Annotation.string)
[ Elm.Case.branch0 "Nothing"
(Elm.string "It's nothing, I swear!")
, Elm.Case.branch1 "Just" ( "val", Elm.Annotation.string ) <|
\val ->
Elm.append (Elm.string "Actually, it's: ") val
]Generates
case maybeString of
Nothing ->
"It's nothing, I swear!"
Just just ->
"Actually, it's: " ++ justA catchall branch in case you want the case to be nonexhaustive.