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

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Wed May 4 17:14:26 CEST 2011


On Wed, 4 May 2011, Ravi Varadhan wrote:

> I too would like this (being an Indian!).

I would not.

> 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(...)))
> }

This has quite different behavior with respect to evaluation/lazy
evaluation than an analogous anonymous function. In addition, do.call
has some fairly strange aspect so it with respect to how it intereacts
with sys.xyz functions, and does not do what you want in many cases I
care about when quote = FALSE, as is the default.  Adding this would
create more problems than is solves.

> 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)

Yes -- as do a number of other variations.

> # but this doesn't work
> rm(a)
> adaptIntegrate(function(x, argA=a) f(x, a=argA), lower=0, upper=2, a=0.2)

Of course not -- why would anyone think it would?

>
>
> # Use of Curry
> adaptIntegrate(Curry(f, a=0.2), lower=0, upper=2)

The _concept_ of currying is useful, and maybe more can be done to
provide guidance and education on how to do it, but adding a function
that sometimes works and somesimes does surprising things is not the
way to go.

Best,

luke

>
> Best,
> Ravi.
>
> -------------------------------------------------------
> Ravi Varadhan, Ph.D.
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University
>
> Ph. (410) 502-2619
> email: rvaradhan at jhmi.edu
>
>
> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of Hadley Wickham
> Sent: Wednesday, May 04, 2011 10:29 AM
> To: Byron Ellis
> Cc: R Development Mailing List
> Subject: Re: [Rd] Curry: proposed new functional programming, er, function.
>
> I thought I might bring this up again - it now seems like Curry would
> be a natural fit with Reduce, Filter, Find, Map, Negate and Position.
> Any chance we might see this in a future version of R?
>
> Hadley
>
> On Thu, Nov 1, 2007 at 2:00 PM, Byron Ellis <byron.ellis at gmail.com> wrote:
>> Hi all (especially R-core) I suppose,
>>
>> With the introduction of the new functional programming functions into
>> base I thought I'd ask for a Curry() function. I use a simple one that
>> looks this:
>>
>> Curry = function(FUN,...) { .orig = list(...);function(...)
>> do.call(FUN,c(.orig,list(...))) }
>>
>> This comes in really handy when using say, heatmap():
>>
>> heatmap(mydata,hclustfun=Curry(hclust,method="average"))
>>
>> or other functions where there are ... arguments, but it's not clear
>> where they should end up.
>>
>> --
>> Byron Ellis (byron.ellis at gmail.com)
>> "Oook" -- The Librarian
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>
>
>

-- 
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list