Ali Çehreli acehreli at yahoo.com
Thu Aug 4 22:07:56 UTC 2022

On 8/4/22 06:08, pascal111 wrote:
 > In next code from
 > "https://www.tutorialspoint.com/d_programming/d_programming_ranges.htm",

That page seems to be adapted from this original:


 > we have two issues:
 > 1) Why the programmer needs to program "empty()", "front()", and
 > "popFront()" functions for ranges

The programmer almost never needs to implement those functions. Existing 
data structures and algorithms are almost always sufficient. (I did need 
to implement them but really rarely.)

I tried to explain what those functions do. I don't like my Students 
example much because wrapping a D slice does not make much sense. Again, 
I just try to explain them.

 > while they exist in the language
 > library?

The existing front, popFronh, etc. are only for arrays (slices).

 > it seems there's no need to exert efforts for that.


 > "https://dlang.org/phobos/std_range_primitives.html"
 > 2) "front()", and "popFront()" are using fixed constants to move forward
 > the range, while they should use variables.

Well, 0 is always the first element and 1..$ are always the rest. 
Variables would not add any value there.

However, the example could use the existing library function that you 

 >         @property bool empty() const {
 >            return students.length == 0;


              import std.array : empty;
              return students.empty;

 >         }
 >         @property ref Student front() {
 >            return students[0];


              import std.array : front;
              return students.front;

 >         }
 >         void popFront() {
 >            students = students[1 .. $];


              import std.array : popFront;

But I think those implementations might confuse the reader.


More information about the Digitalmars-d-learn mailing list