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

From: Thomas Lumley <>
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 Lumley			Assoc. Professor, Biostatistics	University of Washington, Seattle

______________________________________________ mailing list PLEASE do read the posting guide 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 Please read the posting guide before posting to the list.

list of date sections of archive