Hello, in a desperate desire of using partial function application in R I fried out the following piece of code: bind <- function( f, ... ) { args <- list(...) function(...) f( ..., unlist(args) ) } Its purpose, if not clear, is to return a function with part of its arguments bound to specific values, so that I can for example create and use functions like this: q1 <- bind( quantile, 0.25 ) lapply( some_list, q1 ) It's been a lot of work and unfortunately is not perfect. My bind applies arguments only using positional rule. What I dream of is a function bind2 that would apply keyword arguments, like: plot_lines <- bind2( plot, type="l" ) which would return function(...) plot( type="l", ... ) How to do this in R? Regards, nosek -- View this message in context: http://www.nabble.com/Partial-function-application-in-R-tp21487269p21487269.html Sent from the R help mailing list archive at Nabble.com.
How is function() not the correct approach? > plot_lines <- function(x, ...) plot(x, type="l", ...) > > plot_lines(1:10, xlim = c(1,5)) > plot_lines(1:10, 11:20, xlim = c(1,5)) Still seems to get the unnamed optional y argument to the plotting machinery. -- David Winsemius On Jan 15, 2009, at 4:25 PM, nosek wrote:> > Hello, > > in a desperate desire of using partial function application in R I > fried out > the following piece of code: > > bind <- function( f, ... ) { > args <- list(...) > function(...) f( ..., unlist(args) ) > } > > Its purpose, if not clear, is to return a function with part of its > arguments bound to specific values, so that I can for example create > and use > functions like this: > q1 <- bind( quantile, 0.25 ) > lapply( some_list, q1 ) > > It's been a lot of work and unfortunately is not perfect. My bind > applies > arguments only using positional rule. What I dream of is a function > bind2 > that would apply keyword arguments, like: > plot_lines <- bind2( plot, type="l" ) > which would return > function(...) plot( type="l", ... ) > > How to do this in R? > > Regards, > nosek > -- > View this message in context: http://www.nabble.com/Partial-function-application-in-R-tp21487269p21487269.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Have a look at the setDefaults package. It will set the default arguments of a function to whatever you specify so that if you omit them then those are the values you get for them. On Thu, Jan 15, 2009 at 4:25 PM, nosek <nospamek at interia.pl> wrote:> > Hello, > > in a desperate desire of using partial function application in R I fried out > the following piece of code: > > bind <- function( f, ... ) { > args <- list(...) > function(...) f( ..., unlist(args) ) > } > > Its purpose, if not clear, is to return a function with part of its > arguments bound to specific values, so that I can for example create and use > functions like this: > q1 <- bind( quantile, 0.25 ) > lapply( some_list, q1 ) > > It's been a lot of work and unfortunately is not perfect. My bind applies > arguments only using positional rule. What I dream of is a function bind2 > that would apply keyword arguments, like: > plot_lines <- bind2( plot, type="l" ) > which would return > function(...) plot( type="l", ... ) > > How to do this in R? > > Regards, > nosek > -- > View this message in context: http://www.nabble.com/Partial-function-application-in-R-tp21487269p21487269.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Maybe Matching Threads
- insert_html inserting fragment twice
- put procmail between postfix and dovecot
- How to make big MySQL database more diffable/rsyncable? (aka rsyncing big files)
- [PATCH] [dovecot 2.2.9] Quota warnings ignored with FS quotas
- [PATCH] [dovecot 2.2.9] Quota warnings ignored with FS quotas