Optional Types and Pattern Matching

Introduces optional types with some() and none, as well as match_with() from functional languages.


Build Status CRAN_Status_Badge CRAN\ Downloads

Installation

Installation

Installation

You can install this package from this repository by running devtools::install_github("antoinechampion/optional"), or from the CRAN by running install.packages("optional").

Introduction

This package adds an optional type, similar to Option in F#, OCaml and Scala, to Maybe in Haskell, and to nullable types in C#.

It should be used instead of NULL for values that might be missing or otherwise invalid.

This package also introduces pattern matching.

Using the optional type

optional is an object wrapper which indicates whether the object is valid or not.

Declaring an optional object

An optional variable can be set to some(object) or to none.

a <- some(5)
class(a)
## [1] "optional"

Operators and print will have the same behavior with an optional than with its base type.

a == 5
## [1] TRUE
a
## [1] 5

Note that some(some(obj)) equals some(obj) and that some(none) equals FALSE.

Optionals on functions

Given a function f(), to handle properly optional arguments and wraps its return type into an optional, one should use make_opt() the following way:

f_opt <- make_opt(f)
  1. 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 ?make_opt).
  2. If f() returns null, or if an error is thrown during its execution, then f_opt() returns none. Else it will return some(f(...)).

For instance:

c_opt <- make_opt(c)
c_opt(some(2), none, some(5))
## [1] 2 5
c_opt()
## [1] "None"

Pattern matching

Patterns are used in many functional languages in order to process variables in an exhaustive way.

The syntax is the following:

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() rather than a conditional statement.

library(magrittr)

a <- 5 match_with(a, . %>% some(.), paste, none, function() "Error!" )

## [1] "5"
  1. 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.
  2. 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.
a <- 4
match_with(a,
  1,                 function() "Matched exact value",
  list(2, 3, 4),     fallthrough(function() "Matched in list"),
  . %>% if (. > 3)., function(x) paste0("Matched in condition: ",x,">3")
)
## [1] "Matched in list"           "Matched in condition: 4>3"

News

optional 2.0

  • NOT COMPATIBLE WITH VERSIONS 1.x
  • Replace function some() with the type wrapper option()
  • some() is now used to check the status of an optional object

optional 1.1

  • Add a fall-through possibility for pattern matching with the fallthrough() function
  • match_with() now returns a concatenation of return values from matched result-functions
  • Values comparison in match_with() are now direct object comparisons instead of near equality checks

optional 1.0

Initial release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("optional")

2.0 by Antoine Champion, 7 months ago


Browse source code at https://github.com/cran/optional


Authors: Antoine Champion


Documentation:   PDF Manual  


BSL license


Imports methods, magrittr

Suggests knitr, rmarkdown


See at CRAN