Binary-class {aoos}R Documentation

Binary-class

Description

This is a virtual class to be contained in other class definitions. It can be used to define binary operators, e.g. + or -, inside an aoos class definition (defineClass).

Details

At the moment you can define binary operators as methods by naming them as .<binaryOperator> (see the example). This is implemented for the following operators: +, -, *, /, %%, ^, <, >, ==, >=, <=, &.

Examples

Rational <- defineClass("Rational", contains = c("Show", "Binary"), {

  numer <- 0
  denom <- 1
  .g <- 1
  
  .gcd <- function(a, b) if(b == 0) a else Recall(b, a %% b)

  init <- function(numer, denom) {
    .self$.g <- .gcd(numer, denom)
    .self$numer <- numer / .g
    .self$denom <- denom / .g
  }
  
  show <- function() {
    cat(paste0(.self$numer, "/", .self$denom, "\n"))
  }
  
  ".+" <- function(that) {
    Rational(numer = numer * that$denom + that$numer * denom,
             denom = denom * that$denom)
  }
  
  neg <- function() {
    Rational(numer = -.self$numer,
             denom = .self$denom)
  }
  
  ".-" <- function(that) {
     .self + that$neg()
  }
  
})

rational <- Rational(2, 3)
rational + rational
rational$neg()
rational - rational

[Package aoos version 0.5.0 Index]