Title: | Optional Types and Pattern Matching |
---|---|
Description: | Introduces optional types with some() and none, as well as match_with() from functional languages. |
Authors: | Antoine Champion |
Maintainer: | Antoine Champion <[email protected]> |
License: | BSL |
Version: | 2.0.1 |
Built: | 2025-03-05 03:16:11 UTC |
Source: | https://github.com/antoinechampion/optional |
Permit a pattern matching to continue even if its argument is executed.
fallthrough(fun)
fallthrough(fun)
fun |
A result function used in |
fallthrough(fun)
can be applied to a result function fun
inside a
match_with()
pattern.
If there is a match, this will make the pattern matching
continue through the other conditions at the end of the result function fun
.
match_with(variable,
pattern, fallthrough(result-function),
...
library(magrittr) a <- 4 match_with(a, . %>% if (. %% 2 == 0)., fallthrough( function() "This number is even" ), . %>% if ( sqrt(.) == round(sqrt(.)) )., function() "This number is a perfect square" ) ## [1] "This number is even" "This number is a perfect square"
library(magrittr) a <- 4 match_with(a, . %>% if (. %% 2 == 0)., fallthrough( function() "This number is even" ), . %>% if ( sqrt(.) == round(sqrt(.)) )., function() "This number is a perfect square" ) ## [1] "This number is even" "This number is a perfect square"
Make an existing function accepting and returning optionals.
make_opt(fun, stop_if_none = FALSE, fun_if_none = NULL)
make_opt(fun, stop_if_none = FALSE, fun_if_none = NULL)
fun |
The function to make optional, might be any function. |
stop_if_none |
If true, |
fun_if_none |
If not null, will be executed if an argument
is |
Every optional argument passed to f_opt()
will be
converted to its original type before being sent
to f()
. If one or more of them is none
,
several behaviors are available (see argument list).
If f()
returns null, or if an error is thrown
during its execution, then f_opt()
returns
none
. Else it will return option(f(...))
.
The optional function. To be used with the
same parameters than fun()
.
option(), none(), match_with()
c_opt <- make_opt(c) c_opt(option(2), none, option(5)) ## [1] 2 5 c_opt() ## [1] "None"
c_opt <- make_opt(c) c_opt(option(2), none, option(5)) ## [1] 2 5 c_opt() ## [1] "None"
Function to check a variable using pattern matching.
match_with(x, ...)
match_with(x, ...)
x |
The variable to pattern-match |
... |
Pairs of one pattern (value or list or magrittr sequence) and one result function |
match_with(variable,
pattern, result-function,
...
If variable
matches a pattern
, result-function
is called. For comparing optional types, it is a better habit to
use match_with
than a conditional statement.
Each pattern
can be either:
an object or a primitive type (direct comparison with variable
),
a list (match if variable
is in the list),
a magrittr
functional sequence that matches if it returns variable
. The dot .
denotes the variable to be matched.
If result-function
takes no arguments, it will be called as is. Else, the only argument that will be sent is variable
.
You can also use the fallthrough function fallthrough()
to permit the matching to continue even if the current pattern is matched.
option(), none
library(magrittr) a <- 5 match_with(a, . %>% option(.), paste, none, function() "Error!" ) ## [1] 5 match_with(a, 1, function() "Matched exact value", list(2, 3, 4), function(x) paste("Matched in list:", x), . %>% if (. > 4) ., function(x) paste("Matched in condition:", x) ) ## [1] "Matched in condition: 5"
library(magrittr) a <- 5 match_with(a, . %>% option(.), paste, none, function() "Error!" ) ## [1] 5 match_with(a, 1, function() "Matched exact value", list(2, 3, 4), function(x) paste("Matched in list:", x), . %>% if (. > 4) ., function(x) paste("Matched in condition:", x) ) ## [1] "Matched in condition: 5"
Indicates an invalid variable.
Might be returned by an optional function
(see ?make_opt()
)
none
none
An object of class optional
of length 1.
option(), opt_unwrap()
a <- none a ## [1] None
a <- none a ## [1] None
Cast an optional object to its base type.
opt_unwrap(opt)
opt_unwrap(opt)
opt |
The optional variable to cast back |
Since an optional can be used the same way as its base type, there is no known scenario where this function might be useful.
The object wrapped in opt
.
NULL
if opt
is none
.
make_opt(), match_with()
a <- option(5) class(a) ## [1] "optional" a <- opt_unwrap(a) class(a) ## [1] "numeric"
a <- option(5) class(a) ## [1] "optional" a <- opt_unwrap(a) class(a) ## [1] "numeric"
Make a variable optional.
option
is an object wrapper which indicates
whether the object is valid or not.
option(arg)
option(arg)
arg |
The variable to make optional |
Note that option(option(i)) == option(i)
and option(none) == FALSE
Operators and print will have the same behavior with an optional than with its base type.
arg
as optional
none, opt_unwrap(), make_opt()
a <- option(5) class(a) ## [1] "optional" a == 5 ## [1] TRUE a ## [1] 5
a <- option(5) class(a) ## [1] "optional" a == 5 ## [1] TRUE a ## [1] 5
Check if a optional object equals none
some(arg)
some(arg)
arg |
The variable to check existence |
TRUE if arg
is an optional variable
and if it is not none, else returns FALSE
option(), none a <- option(1) some(a) ## [1] TRUE b <- none some(b) ## [1] FALSE