Hi all, I'm new to R. Having a functional background, I was wondering what's the idiomatic way to iterate. It seems that for loops are the default given there's no tail-call optimization. I'm curious to know whether there is a way to transform the following toy snippet into something that doesn't eat up gigabytes of memory (like it's for loop counterpart) using laziness: Reduce('+', seq(1,1e6)) Thanks! Best regards, A.S.
Alexander S?ndergaard wrote:> Hi all, > > I'm new to R. Having a functional background, I was wondering what's > the idiomatic way to iterate. It seems that for loops are the default > given there's no tail-call optimization. > > I'm curious to know whether there is a way to transform the following > toy snippet into something that doesn't eat up gigabytes of memory > (like it's for loop counterpart) using laziness:> Reduce('+', seq(1,1e6))In this case sum(as.numeric(seq(1, 1e6))) or according to Gauss 1e6 * (1e6+1) / 2 but for more complicated problems it might be sensible to go the loop way ... Uwe Ligges> Thanks! > > Best regards, > A.S. > > ______________________________________________ > 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.
On 27/11/2009 3:36 PM, Alexander S?ndergaard wrote:> Hi all, > > I'm new to R. Having a functional background, I was wondering what's > the idiomatic way to iterate. It seems that for loops are the default > given there's no tail-call optimization. > > I'm curious to know whether there is a way to transform the following > toy snippet into something that doesn't eat up gigabytes of memory > (like it's for loop counterpart) using laziness: > > Reduce('+', seq(1,1e6))I believe the iterators and foreach packages give ways to iterate without creating the whole array, so they might do what you want. But is the allocation really a problem on modern computers? The for loop version of your example, i.e. total <- 0 for (i in seq(1,1e6)) total <- total + i total uses about 4 megabytes of memory, not "gigabytes". If you increase the limit from 1e6 to 1e9 you'll get gigabytes, but it'll probably take tens of minutes to finish running on a system that can run it: the big problem is the interpreted looping, not the memory use. R has always followed the strategy of making it easy to jump out to C code when speed matters, so the natural idiom for your problem is to use sum(), not to program it yourself. In more complicated examples, or if you really do want to sum the integers from 1 to a billion on a system with limited memory, you'll have to write the C yourself, but it's not hard. Duncan Murdoch> > Thanks! > > Best regards, > A.S. > > ______________________________________________ > 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.
Duncan Murdoch murdoch at stats.uwo.ca wrote:> > > On 27/11/2009 3:36 PM, Alexander S??ndergaard wrote: > > I'm new to R. Having a functional background, I was wondering what's > > the idiomatic way to iterate. It seems that for loops are the default > > given there's no tail-call optimization. > > > > I'm curious to know whether there is a way to transform the following > > toy snippet into something that doesn't eat up gigabytes of memory > > (like it's for loop counterpart) using laziness: > > > > Reduce('+', seq(1,1e6)) > > I believe the iterators and foreach packages give ways to iterate > without creating the whole array, so they might do what you want.The OP may also be interested in the jit package, which currently just covers (see http://www.milbo.users.sonic.net/ra) loops, but might eventually extend to TCO. David Duffy. -- | David Duffy (MBBS PhD) ,-_|\ | email: davidD at qimr.edu.au ph: INT+61+7+3362-0217 fax: -0101 / * | Epidemiology Unit, Queensland Institute of Medical Research \_,-._/ | 300 Herston Rd, Brisbane, Queensland 4029, Australia GPG 4D0B994A v