guard
ocharles opened this issue · comments
Ollie Charles commented
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.
Dave Keen commented
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.
Ian Phillips commented
@ccapndave I have the very same function, also unless
as the inverse of it (mainly because it reads better).