www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - staticIota is easy

reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
I thought I needed something like staticIota in a unittest to effect 
static foreach over a number range and I found one in druntime's 
implementation:

 
https://github.com/dlang/druntime/blob/master/src/core/internal/traits.d#L106

(I wonder why that one is implemented in divide-and-conquer fashion. 
Perhaps due to faster compilation that way?)

Then I realized that this is actually pretty easy with D:

template staticIota(size_t N) {
     import std.range: iota;
     import std.meta: aliasSeqOf;
     alias staticIota = aliasSeqOf!(N.iota);
}

unittest {
     size_t count = 0;
     foreach (i; staticIota!10) {
         mixin("++count;");
     }
     assert(count == 10);
}

void main() {
}

I realized that I don't actually need it but I wanted to share. :)

Ali

P.S. Related, I've been using D for a living since I started working for 
Weka.IO in June. (I think the only mention of that was in this blog 
post: https://dlang.org/blog/2016/06/). The more I use D, the more I 
like it but you already know it. ;)
Dec 09 2016
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 9 December 2016 at 18:52:59 UTC, Ali Çehreli wrote:
 I thought I needed something like staticIota in a unittest to 
 effect static foreach over a number range and I found one in 
 druntime's implementation:


 https://github.com/dlang/druntime/blob/master/src/core/internal/traits.d#L106

 (I wonder why that one is implemented in divide-and-conquer 
 fashion. Perhaps due to faster compilation that way?)
Yes it is. n log n instead of n^2
Dec 09 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/09/2016 05:34 PM, Stefan Koch wrote:
 On Friday, 9 December 2016 at 18:52:59 UTC, Ali Çehreli wrote:
 I thought I needed something like staticIota in a unittest to effect
 static foreach over a number range and I found one in druntime's
 implementation:


 https://github.com/dlang/druntime/blob/master/src/core/internal/traits.d#L106


 (I wonder why that one is implemented in divide-and-conquer fashion.
 Perhaps due to faster compilation that way?)
Yes it is. n log n instead of n^2
Makes sense. I was stopping my counting at n: both looked O(n) to me. :) How about my staticIota()? Is it n^2 inside the compiler? Ali
Dec 09 2016
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 10 December 2016 at 01:48:24 UTC, Ali Çehreli wrote:
 On 12/09/2016 05:34 PM, Stefan Koch wrote:
 On Friday, 9 December 2016 at 18:52:59 UTC, Ali Çehreli wrote:
 I thought I needed something like staticIota in a unittest to 
 effect
 static foreach over a number range and I found one in 
 druntime's
 implementation:


 https://github.com/dlang/druntime/blob/master/src/core/internal/traits.d#L106


 (I wonder why that one is implemented in divide-and-conquer 
 fashion.
 Perhaps due to faster compilation that way?)
Yes it is. n log n instead of n^2
Makes sense. I was stopping my counting at n: both looked O(n) to me. :) How about my staticIota()? Is it n^2 inside the compiler? Ali
it's O(n log n)*((n!)/some_really_large_number) Because the template-subsystem is it not really build for a abuse like AliasSeq. After a cut-off point the factorial factor will dominate.
Dec 09 2016