Search⌘ K

Use of Ranges in D

Explore how ranges function in D programming, focusing on their role in abstracting algorithms from data structures. Understand the integration of Phobos library ranges, the lazy evaluation of temporary range objects like filter(), and how ranges simplify algorithm implementation across different data structures. This lesson equips you to enhance your data handling skills in D by mastering range usage.

Ranges are an integral part of D

D’s slices happen to be implementations of the most powerful range RandomAccessRange, and there are many range features in Phobos. It is essential to understand how ranges are used in Phobos.

Note: Phobos is the standard run time library for D. Many Phobos algorithms return temporary range objects. For example, filter(), which chooses elements that are greater than 10 in the following code, actually returns a range object, not an array:

D
import std.stdio;
import std.algorithm;
void main() {
int[] values = [ 1, 20, 7, 11 ];
writeln(values.filter!(value => value > 10)); // A range object is returned here.
}

writeln uses that range ...