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

guard

ocharles opened this issue · comments

Any interest in something like:

guard : a -> (a -> Bool) -> Maybe a
guard a pred = if pred a then Just a else Nothing

I'm actually using it in the sense of guard someHtmlView (always someCondition), so I don't strictly need a predicate function, a single bool would do. But the more general case seems like it might be the better addition.

I also use the very similar when all the time:

when : Bool -> a -> Maybe a
when test value =
    if test then
        Just value
    else
        Nothing

I would love to see one or both of these two functions added to the library.

@ccapndave I have the very same function, also unless as the inverse of it (mainly because it reads better).

Decided not to include this after discussion in #49