elm-community / result-extra

Convenience functions for working with Result.

Home Page:http://package.elm-lang.org/packages/elm-community/result-extra/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add function for lazy version of fromMaybe

hedgehogface opened this issue · comments

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.