elm-community / maybe-extra

Convenience functions for working with Maybe.

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: lazy `or` and `orElse`

jvoigtlaender opened this issue · comments

Currently, there is:

or : Maybe b -> Maybe b -> Maybe b

In a call or someMaybe anotherMaybe the computation of anotherMaybe will be performed even if someMaybe evaluates to Nothing. That is, the behavior is unlike someBoolean || anotherBoolean. That could be fixed by revising or into:

or : Maybe b -> Lazy (Maybe b) -> Maybe b

using the elm-lang/lazy package.

As a complement, it would make sense to also introduce

orElse : Lazy (Maybe b) -> Maybe b -> Maybe b

That one would be more pipeline-friendly, for example one could then write stuff like:

    List.head []
    |> orElse (lazy (\() -> List.head [4]))

An alternative to using the elm-lang/lazy package would be to simply do a somewhat corresponding thing directly in this library here. That would mean the following types:

or : Maybe b -> (() -> Maybe b) -> Maybe b

orElse : (() -> Maybe b) -> Maybe b -> Maybe b

and an example use as follows:

    List.head []
    |> orElse (\() -> List.head [4])

For context, a related discussion is here: #20.