From ggrothendieck at gmail.com  Wed May  4 16:59:17 2011
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Wed, 4 May 2011 10:59:17 -0400
Subject: [Rd] Curry: proposed new functional programming, er, function.
In-Reply-To: <79F23BA7BB084E4FA01A8B93904CD02CF669FAA4C9@WIGGUMVS.win.ad.jhu.edu>
References: <7098abec0711011200va3851o811f60bb100616ce@mail.gmail.com>
	<BANLkTi=jud5hhis5y-veQKZHzSYYoPqnBw@mail.gmail.com>
	<79F23BA7BB084E4FA01A8B93904CD02CF669FAA4C9@WIGGUMVS.win.ad.jhu.edu>
Message-ID: <BANLkTi=Nd7XEdr4GrOL_ULQchmnKq2stGg@mail.gmail.com>

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



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