Add function for lazy version of fromMaybe
hedgehogface opened this issue · comments
hedgehogface commented
Could we add a new function fromMaybeLazy.
{-| Lazy version of `Result.fromMaybe`
Only calculates the error case on Nothing
maybe
|> Result.fromMaybe (\() -> Debug.todo "expensive calcuation")
-}
fromMaybeLazy : (() -> x) -> Maybe a -> Result x a
fromMaybeLazy fErr maybe =
case maybe of
Just a ->
Ok a
Nothing ->
fErr () |> Err
I used Result.fromMaybe in my code and was computing a complicated error message from a long list, didn't realize the error case was evaluated for every value even if there were no errors. I then needed the lazy version shown here.