Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for lazy version of fromMaybe #33

Open
hedgehogface opened this issue Oct 17, 2022 · 0 comments
Open

Add function for lazy version of fromMaybe #33

hedgehogface opened this issue Oct 17, 2022 · 0 comments

Comments

@hedgehogface
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant