Search⌘ K

std.range and std.algorithm Modules

Explore the std.range and std.algorithm modules in D to manipulate data using range types. Learn to apply algorithms like swapFront and filter with predicates and lambda expressions for efficient data handling and flexibility in your programs.

We'll cover the following...

A great benefit of defining our types as ranges is being able to use them not only with our own functions, but with Phobos and other libraries as well.

std.range includes a large number of range functions, structs, and classes.

std.algorithm includes many algorithms that are commonly found also in the standard libraries of other languages.

Example

To see an example of how our types can be used with standard modules, let’s use School with the std.algorithm.swapFront algorithm. swapFront() swaps the front elements of two InputRange ranges.

This requires the front elements of the two ranges to be swappable. Arrays satisfy that condition.

D
import std.algorithm;
//...
auto turkishSchool = School( [ Student("Ebru", 1),
Student("Derya", 2) ,
Student("Damla", 3) ] );
auto americanSchool = School( [ Student("Mary", 10),
Student("Jane", 20) ] );
swapFront(turkishSchool.studentsOf,
americanSchool.studentsOf);
print(turkishSchool.studentsOf);
print(americanSchool.studentsOf);

The first ...