On Sunday, 29 April 2012 at 01:06:54 UTC, bearophile wrote:
retro() can't replace foreach_reverse until the front-end demonstrability produces asm code equally efficient. Loops _must_ be fully efficient, they are a basic language construct, this is very important. Even foreach() is sometimes not equally efficient as a for() in some cases...

Slightly related: For this:

---
import core.stdc.stdio;
import std.algorithm;
import std.range;

void main() {
  int sum;
  foreach (i; retro(iota(11))) sum += i;
  printf("%d\n", sum);
}
---

LDC produces
---
__Dmain:
        pushq   %rax
        movl    $0x00000037,%esi
        xorb    %al,%al
        leaq    _.str24(%rip),%rdi # str24 = "%d\n"
        callq   _printf
        xorl    %esi,%esi
        movl    %eax,0x04(%rsp)
        movl    %esi,%eax
        popq    %rdx
        ret
---

Now that's constant folding… ;)

David

Reply via email to