Re: [R] functional (?) programming in r

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Mon, 17 Nov 2008 19:44:16 -0800 (PST)

On Mon, 17 Nov 2008, jim holtman wrote:

> You can use the 'local' function to make sure you create a value of
> 'i' that is defined when the function is defined:
>
>> funcs = lapply(1:5, function(i)local({i; function(){ i}}))

You can use pretty much any other function, too   funcs = lapply(1:5, function(i) {cos(i); function() i}) or no function at all
  funcs = lapply(1:5, function(i) {i=i; function() i})

The only requirement is to make sure that i is evaluated. The force() function was created for exactly this situation, to make it obvious that you are just forcing lazy evaluation.

  funcs = lapply(1:5, function(i) {force(i); function() i})

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley_at_u.washington.edu	University of Washington, Seattle

______________________________________________
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. Received on Tue 18 Nov 2008 - 03:49:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Nov 2008 - 04:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive