Curry: proposed new functional programming, er, function.
Hi,
On 5 May 2011 02:56, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
On 04/05/2011 10:40 AM, Ravi Varadhan 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)
Two objections: 1. ?I don't see how that is preferable to adaptIntegrate(function(x) f(x, a=0.2), lower=0, upper=2)
In this particular case I don't think it is. The reason I proposed it
yesterday for the cubature example was that in some cases it can make
the code more concise and clearer in my opinion. This is the case when
you override a considerable number of arguments in a function.
Consider this,
read.table2 = Curry(read.table, header = TRUE, na.strings = "9999",
colClasses = "numeric", nrows = 23)
vs
read.table2 = function(..., header = TRUE, na.strings = "9999",
colClasses = "numeric", nrows = 23)
{
read.table(header = header, na.strings = na.strings,
colClasses = colClasses, nrows = nrows, ...)
}
Whether or not this should be called curry, I have no idea. In any
case it seems like a useful function to have in a base package,
regardless of its implementation details.
baptiste
2. ?There seems to be confusion about what currying means. ?The Wikipedia page <http://en.wikipedia.org/wiki/Currying> indicates that the function Curry() defined above is really doing "partial function application", not currying. ?I'm in no position to judge whether Byron got it right or Wikipedia did, but this suggests to me that the name "Curry" is inappropriate, since at least some people who know what currying is would not guess that it does what it does. Duncan Murdoch
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
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel