Skip to content

Commit

Permalink
new function: withDefaultLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
skyqrose committed Dec 17, 2021
1 parent 71d222b commit 8288c1d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Maybe/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Maybe.Extra exposing
( isJust, isNothing, join, filter
, unwrap, unpack
, withDefaultLazy, unwrap, unpack
, or, orElse, orList, orLazy, orElseLazy, orListLazy, oneOf
, values
, combine, traverse, combineArray, traverseArray
Expand All @@ -22,7 +22,7 @@ Work with 1 `Maybe`
# Get a value out of a `Maybe`
@docs unwrap, unpack
@docs withDefaultLazy, unwrap, unpack
# Or
Expand Down Expand Up @@ -165,6 +165,29 @@ filter f m =
-- Get a value out of a `Maybe`


{-| Lazy version of [Maybe.withDefault](https://package.elm-lang.org/packages/elm/core/latest/Maybe#withDefault).
It will only calculate the default if needed.
Examples:
withDefaultLazy (\() -> 2 + 2) Nothing
--> 4
withDefaultLazy (\() -> Debug.todo "Expensive calculation") (Just 4)
--> 4
-}
withDefaultLazy : (() -> a) -> Maybe a -> a
withDefaultLazy default m =
case m of
Nothing ->
default ()

Just a ->
a


{-| Like using a `case`.
Give a function that says what to do if the input is `Just`,
and a value to use if the input is `Nothing`.
Expand Down

0 comments on commit 8288c1d

Please # to comment.