On 06/21/2010 04:06 PM, bearophile wrote:
Adam Ruppe:
foreach(i; retro(iota(0, 10))) { }

Oh, right! Or even just:

foreach (i; retro(iota(10))) {}

But abstraction has a cost, see below. I have written three test programs.

Nice work.

iota() currently uses the formula initial + i * step to compute the ith element. This is to make sure that iota works properly with floating point numbers as well as integers.

We should specialize iota for integrals to use increment, which should count for some efficiency gains (currently the optimizer cannot figure out the equivalence, so it generates the multiply and add in the loop).

If efficiency is still sub-par, retro could detect that it's working with iota and generate specialized code. That's not too difficult; for integers, retro(iota(a, b)) could actually be a rewrite to iota(b - 1, a, -1). Figuring out all corner cases, steps greater than 1, and what to do for floating point numbers is doable but not trivial either, and works against modularity. Anyway, it does look like it's all about an implementation matter.


Andrei

Reply via email to