Skip to contents

Wrapping a function in maybe will modify it to return a maybe value. If the function would normally return an error or warning the modified function will return a 'Nothing' value, otherwise it will return a 'Just' value. If a predicate function is provided with the parameter ensure, if the predicate returns TRUE when evaluated on the return value of the function, then a 'Just' value will be returned by the modified function, otherwise it will return a 'Nothing' value.

Usage

maybe(.f, ensure = function(a) TRUE, allow_warning = FALSE)

Arguments

.f

A function to modify

ensure

A predicate function

allow_warning

Whether warnings should result in 'Nothing' values

Value

A function which returns maybe values

Examples

maybe(mean)(1:10)
#> Just
#> [1] 5.5
maybe(mean, allow_warning = TRUE)("hello")
#> Warning: argument is not numeric or logical: returning NA
#> Just
#> [1] NA
maybe(sqrt)("hello")
#> Nothing
maybe(sqrt, ensure = not_infinite)(-1)
#> Nothing