# Note that these are trivial examples for pedagogical purposes. Due to their
# trivial nature, most of these examples can be implemented more concisely
# using built-in R features.
# This is optional except when naming is ambiguous
#logarithm <- function(...) UseFunction('logarithm', ...)
# Abbreviated form (recommended)
# The expression must operate on arguments declared in the concrete function.
guard(logarithm.1, is.numeric(x))
logarithm.1 <- function(x) logarithm(x, exp(1))
# Explicit form (only necessary for special cases)
guard(logarithm.base, function(x,y) is.numeric(x) && is.numeric(y))
logarithm.base <- function(x,y) log(x, base=y)
ensure(logarithm.base, ! is.nan(result) && ! is.infinite(result))
guard(logarithm.default2, TRUE)
logarithm.default2 <- function(x,y) logarithm(as.numeric(x), as.numeric(y))
# Uses all arguments in assertion
guard(f, is.numeric(a) & is.numeric(b) & b > 1)
f <- function(a,b) a + b
ensure(f, result == a + b)
# View the function variants for this abstract function
ensures(logarithm)Run the code above in your browser using DataLab