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

______________________________________________
R-help@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.

Reply via email to