[R-pkgs] Introducing the futile.paradigm, a package for functional dispatching in R

From: Nurometic R Help <r_at_nurometic.com>
Date: Tue, 26 Oct 2010 20:48:00 -0400


Hello useRs,

I'm pleased to announce the general availability of the R package futile.paradigm, which is a language extension that implements functional dispatching in R. This is an alternative to the current object-oriented styles, replacing them with a functional programming style that provides a clean, fine-grained declarative syntax for function definitions. The core of the package consists of two operators, %when% and %must% that implement guard statements that define when a function variant is executed and post assertions that define the criteria for success on individual functions. The package also implements custom type constructors, which are declared using the 'create' function, which is predefined in the futile.paradigm.

Here are some examples to give you an idea of the syntax.

# Factorial function in the 'fac' namespace fac.0 %when% (x == 0) # guard statement fac.0 <- function(x) 1 # function variant

fac.n %when% (x > 0)       # guard statement
fac.n %must% (result >= x) # post-assertion
fac.n <- function(x) x * fac(x-1) # function variant

> fac(4)

[1] 24
> fac(-2)

Error in UseFunction("fac", ...) : No valid function for 'fac/1' : -2

# Numerical optimization with custom type constructors # Define a function and its derivatives

fx <- function(x) x^2 - 4
f1 <- function(x) 2*x
f2 <- function(x) 2

# Define the numerical optimization harness converged <- function(x1, x0, tolerance=1e-6) abs(x1 - x0) < tolerance minimize <- function(x0, algo, max.steps=100) {
  step <- 0
  old.x <- x0
  while (step < max.steps)
  {
    # Calls abstract function 'iterate' with definitions below     new.x <- iterate(old.x, algo)
    if (converged(new.x, old.x)) break
    old.x <- new.x
  }
  new.x
}

# Implement Newton-Raphson
iterate.nr %when% (algo %isa% NewtonRaphson) iterate.nr <- function(x, algo) x - algo$f1(x) / algo$f2(x)

# Implement Gradient Descent
iterate.gd %when% (algo %isa% GradientDescent) iterate.gd <- function(x, algo) x - algo$step * algo$f1(x)

# Create a custom type constructor
create.GradientDescent <- function(T, f1, step=0.01) list(f1=f1,step=step)

> algo <- create(GradientDescent, f1)
> minimize(3, algo)

[1] 3.677989e-06

# Execute using a dynamic type constructor
> algo <- create(NewtonRaphson, f1=f1,f2=f2)
> minimize(3, algo)

[1] 0

More documentation is available in the package help files and also at https://nurometic.com/quantitative-finance/futile/paradigm

The current version is version 1.2.0 and is available on CRAN.

Regards,
Brian Lee Yung Rowe



R-packages mailing list
R-packages_at_r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages Received on Wed 27 Oct 2010 - 17:35:30 EST

This archive was generated by hypermail 2.2.0 : Wed 27 Oct 2010 - 17:40:01 EST