R provides many functional programming concepts mostly inherited from Scheme. Concepts like first class functions and lazy evaluation are key components to a functional language, yet R lacks some of the more advanced features of modern functional programming languages. Lambda.r introduces a syntax for writing applications using a declarative notation that facilitates reasoning about your program in addition to making programs modular and easier to maintain.
%as% symbol in place of <-.
Simple functions can be defined as simply as
f(x) %as% x
and can be called like any other function.
f(1)
Functions that have a more complicated body require braces. f(x) %as% { 2 * x }
g(x, y) %as% { z <- x + y sqrt(z) }
%when%
keyword, these parts can be easily captured.
abs(x) %when% { x < 0 } %as% -x
abs(x) %as% x
Any number of guard expressions can be in a guard block, such that all guard expressions must evaluate to true. abs(x) %when% { is.numeric(x) length(x) == 1 x < 0 } %as% -x
abs(x) %when% { is.numeric(x) length(x) == 1 } %as% x
If a guard is not satisfied, then the next clause is tried. If no function clauses are satisfied, then an error is thrown. }
Recursive functions can be defined simply using pattern matching. For example the famed Fibonacci sequence can be defined recursively. fib(0) %as% 1 fib(1) %as% 1 fib(n) %as% { fib(n-1) + fib(n-2) } This is also useful for conditionally executing a function. The reason you would do this is that it becomes easy to symbolically transform the code, making it easier to reason about. pad(x, length, TRUE) %as% c(rep(NA,length), x) pad(x, length, FALSE) %as% x
It is also possible to match on NULL and NA.
sizeof(NULL) %as% 0
sizeof(x) %as% length(x)
}
}
Point create its type constructor.
Point(x,y) %as% list(x=x,y=y)
Note that any built-in data structure can be used as a base type.
Lambda.r simply extends the base type with additional type information.
Types are then created by calling their type constructor. p <- Point(3,4)
To check whether an object is of a given type, use the %isa%
operator. p %isa% Point
}
Suppose we want to define a distance function for Point.
Since it is only meaningful for Points we do not want to
execute it for other types. This is achieved by using a type constraint,
which declares the function argument types as well as the
type of the return value. Type constraints are defined by declaring the
function signature followed by type arguments. distance(a,b) %::% Point : Point : numeric
distance(a,b) %as% { sqrt((b$x - a$x)^2 + (b$y - a$y)^2) }
With this type constraint distance will only be called if both arguments
are of type Point. After the function is applied, a further
requirement is that the return value must be of type numeric.
Otherwise lambda.r will throw an error.
Note that it is perfectly legal to mix and match lambda.r types with
S3 types in type constraints.
}
In the distance example, suppose we do not want to restrict the
function to just Points, but whatever type is used must
be consistent for both arguments. In this case a type variable is
sufficient. distance(a,b) %::% z : z : numeric
distance(a,b) %as% { sqrt((b$x - a$x)^2 + (b$y - a$y)^2) }
The letter z was used to avoid confusion with the names of
the arguments, although it would have been just as valid to use
a.
Type constraints and type variables can be applied to any lambda.r function, including type constructors. }
... within
the type constraint. For example, suppose you want a function that
multiplies the sum of a set of numbers. The ellipsis type tells
lambda.r to bind the types associated with the ellipsis type.
sumprod(x, ..., na.rm=TRUE) %::% numeric : ... : logical : numeric sumprod(x, ..., na.rm=TRUE) %as% { x * sum(..., na.rm=na.rm) }
> sumprod(4, 1,2,3,4) [1] 40
Alternatively, suppose you want all the values bound to the ellipsis to be of a certain type. Then you can append ```...``` to a concrete type.
sumprod(x, ..., na.rm=TRUE) %::% numeric : numeric... : logical : numeric sumprod(x, ..., na.rm=TRUE) %as% { x * sum(..., na.rm=na.rm) }
> sumprod(4, 1,2,3,4) [1] 40 > sumprod(4, 1,2,3,4,'a') Error in UseFunction(sumprod, "sumprod", ...) : No valid function for 'sumprod(4,1,2,3,4,a)'
If you want to preserve polymorphism but still constrain values bound to the ellipsis to a single type, you can use a type variable. Note that the same rules for type variables apply. Hence a type variable represents a type that is not specified elsewhere.
sumprod(x, ..., na.rm=TRUE) %::% a : a... : logical : a sumprod(x, ..., na.rm=TRUE) %as% { x * sum(..., na.rm=na.rm) }
> sumprod(4, 1,2,3,4) [1] 40 > sumprod(4, 1,2,3,4,'a') Error in UseFunction(sumprod, "sumprod", ...) : No valid function for 'sumprod(4,1,2,3,4,a)'
}
For example in f(x, y) %::% . : numeric : numeric, the type of
x will not be checked.
}
}
Suppose there are two kinds of Points: those defined as
Cartesian coordinates and those as Polar coordinates. Rather than
create a type hierarchy, you can attach an attribute to the object.
This keeps the data clean and separate from meta-data that only
exists to describe the data.
Point(r,theta, 'polar') %as% {
o <- list(r=r,theta=theta)
o@system <- 'polar'
o
}
Point(x,y, 'cartesian') %as% { o <- list(x=x,y=y) o@system <- 'cartesian' o }
Then the distance function can be defined according to the
coordinate system.
distance(a,b) %::% z : z : numeric
distance(a,b) %when% {
a@system == 'cartesian'
b@system == 'cartesian'
} %as% {
sqrt((b$x - a$x)^2 + (b$y - a$y)^2)
}
distance(a,b) %when% { a@system == 'polar' b@system == 'polar' } %as% { sqrt(a$r^2 + b$r^2 - 2 * a$r * b$r * cos(a$theta - b$theta)) } Note that the type constraint applies to both function clauses.
}
debug.lr as a drop-in
replacement for debug and undebug.lr for undebug.
In addition to being aware of multipart functions, lambda.r's
debugging system keeps track of what is being debugged, so you can
quickly determine which functions are being debugged. To see
which functions are currently marked for debugging, call
which.debug. Note that if you use debug.lr for
all debugging then lambda.r will keep track of all debugging in
your R session. Here is a short example demonstrating this.
> f(x) %as% x
> debug.lr(f)
> debug.lr(mean)
>
> which.debug()
[1] "f" "mean"
}
Project site: https://github.com/muxspace/lambda.r
%as%, describe, debug.lr,
%isa%is.wholenumber <-
function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol
## Use built in types for type checking
fib(n) %::% numeric : numeric
fib(0) %as% 1
fib(1) %as% 1
fib(n) %when% {
is.wholenumber(n)
} %as% {
fib(n-1) + fib(n-2)
}
fib(5)
## Using custom types
Integer(x) %when% { is.wholenumber(x) } %as% x
fib.a(n) %::% Integer : Integer
fib.a(0) %as% Integer(1)
fib.a(1) %as% Integer(1)
fib.a(n) %as% { Integer(fib.a(n-1) + fib.a(n-2)) }
fib.a(Integer(5))
## Newton-Raphson optimization
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)
{
new.x <- iterate(old.x, algo)
if (converged(new.x, old.x)) break
old.x <- new.x
}
new.x
}
iterate(x, algo) %::% numeric : NewtonRaphson : numeric
iterate(x, algo) %as% { x - algo$f1(x) / algo$f2(x) }
iterate(x, algo) %::% numeric : GradientDescent : numeric
iterate(x, algo) %as% { x - algo$step * algo$f1(x) }
NewtonRaphson(f1, f2) %as% list(f1=f1, f2=f2)
GradientDescent(f1, step=0.01) %as% list(f1=f1, step=step)
fx <- function(x) x^2 - 4
f1 <- function(x) 2*x
f2 <- function(x) 2
algo <- NewtonRaphson(f1,f2)
minimize(3, algo)
algo <- GradientDescent(f1, step=0.1)
minimize(3, algo)Run the code above in your browser using DataLab