[Rd] Curry: proposed new functional programming, er, function.

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 4 16:59:17 CEST 2011


On Wed, May 4, 2011 at 10:40 AM, Ravi Varadhan <rvaradhan at jhmi.edu> wrote:
> I too would like this (being an Indian!).
>
> Here is an example that came up just yesterday with regards to solving a quadrature problem using the "cubature" package.  The adaptIntegrate function does not allow additional arguments via ...
>
> Uwe suggested a work around, but `Curry' would solve it nicely (and it also tastes better!):
>
> Curry = function(FUN,...) {
> .orig = list(...)
> function(...) do.call(FUN,c(.orig, list(...)))
> }
>
> require(cubature)
>
> f <- function(x, a) cos(2*pi*x*a)  # a simple test function
>
> # this works
> a <- 0.2
> adaptIntegrate(function(x, argA=a) f(x, a=argA), lower=0, upper=2)
>
> # but this doesn't work
> rm(a)
> adaptIntegrate(function(x, argA=a) f(x, a=argA), lower=0, upper=2, a=0.2)
>
>
> # Use of Curry
> adaptIntegrate(Curry(f, a=0.2), lower=0, upper=2)
>

Here is another approach. If we preface any function with gsubfn's fn$
then it will turn formulas (subject to certain rules to determine
which to pay attention to) into functions.

1. Here it is applied to the cubature example

> library(cubature)
> library(gsubfn)
Loading required package: proto
>
> fn$adaptIntegrate(~ f(x, 0.2), lower=0, upper=2)
$integral
[1] 0.4677446

$error
[1] 1.247382e-14

$functionEvaluations
[1] 15

$returnCode
[1] 0


2. and here is the heatmap example redone.  The example line is to
compute x so that the code is self contained:

library(gsubfn)
example(heatmap)

fn$heatmap(x, hclustfun = ~ hclust(x, method="average"))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-devel mailing list