[R] Partial function application in R

nosek nospamek at interia.pl
Thu Mar 12 12:50:02 CET 2009

Sorry for not answering long. After working a lot with new version of "bind"
function I find it actually really useful. Just to avoid any possible
hard-to-debug errors I try to stick to some good practices, like always
specifying default values for formal parameters and calling "bind" with
named arguments only.

And actually this feature reinvents

I find myself operating a lot with list-of-lists-like structures, so this
facility helps me really a lot. What about this one:

combine <- function( ... ) {
  funs <- list( ... )
  head <- funs[[1]]
  NULL -> funs[[1]]
  if( length(funs) > 0 )
    function( ... )
      do.call( combine, funs ) ( head( ... ) )
  else head

It helps me to avoid lapplying multiple functions one after another, which
was not really readable. Thus I came to writing code like this:

sorting.predicate <- combine( pure.perf, bind( "[[", i="V11"), bind(
quantile, 0.25 ) )

I wonder how bad is it? :)


Wacek Kusnierczyk wrote:
> nosek wrote:
>> Hello,
>> soon after my last posting to this thread I stumbled upon the do.call
>> function and came to the very much the same version as yours. It is good!
> at least for the sort of tasks as in the examples below.  but i haven't
> tested it beyond those, and in r you shouldn't rely on intuition.
>> However, it looks that both name clashes and mixing keyword and
>> positional
>> styles in argument binding and function calls may lead to very drastic
>> and
>> cryptic errors. Therefore I find this "functional" style of programming
>> not
>> too reliable yet.
> would be interesting to see what sorts of very drastic and cryptic
> errors you get.  not that i would be surprised if something intuitively
> correct does not if fact work.  it might be a semantic weirdo, but it
> might be that you use the solution in a way obviously destined to failure.
> vQ
>> Wacek Kusnierczyk wrote:
>>> czesc,
>>> looks like you want some sort of currying, or maybe partial currying,
>>> right?  anyway, here's a quick guess at how you can modify your bind,
>>> and it seems to work, as far as i get your intentions,  with the plot
>>> example you gave:
>>> bind = function(f, ...) {
>>>    args = list(...)
>>>    function(...) do.call(f, c(list(...), args)) }
>>> plotlines = bind(plot, type='l')
>>> plotlines(1:10, runif(10))
>>> plotredlines = bind(plotlines, col="red")
>>> plotredlines(runif(10))
>>> # careful about not overriding a named argument
>>> plotredpoints = bind(plotredlines, type="p")
>>> plotredpoints(runif(10))
>>> you may want to figure out how to get rid of the smart y-axis title.
>>> is this what you wanted?
>>> pzdr,
>>> vQ
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

View this message in context: http://www.nabble.com/Partial-function-application-in-R-tp21487269p22474710.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list