Ranges
pascal111
judas.the.messiah.111 at gmail.com
Thu Aug 4 13:08:21 UTC 2022
In next code from
"https://www.tutorialspoint.com/d_programming/d_programming_ranges.htm", we have two issues:
1) Why the programmer needs to program "empty()", "front()", and
"popFront()" functions for ranges while they exist in the
language library? 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.
'''D
import std.stdio;
import std.string;
struct Student {
string name;
int number;
string toString() const {
return format("%s(%s)", name, number);
}
}
struct School {
Student[] students;
}
struct StudentRange {
Student[] students;
this(School school) {
this.students = school.students;
}
@property bool empty() const {
return students.length == 0;
}
@property ref Student front() {
return students[0];
}
void popFront() {
students = students[1 .. $];
}
}
void main() {
auto school = School([ Student("Raj", 1), Student("John",
2), Student("Ram", 3)]);
auto range = StudentRange(school);
writeln(range);
writeln(school.students.length);
writeln(range.front);
range.popFront;
writeln(range.empty);
writeln(range);
}
'''
More information about the Digitalmars-d-learn
mailing list