Creating a custom iota()

realhet real_het at hotmail.com
Thu May 12 21:02:59 UTC 2022


On Thursday, 12 May 2022 at 17:06:39 UTC, Ali Çehreli wrote:
> I don't care whether it is good practice or not. :) The 
> following is what you meant anyway and seems to work.

I restricted the parameter types to the ones I wanted to use.
And for the standard iota behavior I used a public import.

     public import std.range : iota;

     auto iota(in DateTime begin, in DateTime end, in Time step){
       
//https://forum.dlang.org/post/ivskeghrhbuhpiytesas@forum.dlang.org -> Ali's solution

       static struct Result{
         DateTime current, end;
         Time step;

         @property bool empty(){ return current >= end; }
         @property auto front(){ return current; }
         void popFront(){ assert(!empty); current += step; }
       }

       return Result(begin, end, step);
     }

     ...

     iota(st, en, day).each!writeln; //works
     iota(1, 10, 0.5).each!writeln;  //also works

It works perfectly, Thank You very much!

Although that general implementation of iota is a bit complex for 
me, this specialized one is simple.

note(0): no cast() was needed here, worked with a const DateTime{ 
ulong ticks; ... }


More information about the Digitalmars-d-learn mailing list