[R] Partial function application in R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Jan 17 12:25:53 CET 2009


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




More information about the R-help mailing list