RandomAccessRange: Finite
Learn the use of the finite RandomAccessRange.
We'll cover the following...
The following are all of the requirements of a RandomAccessRange that is based on a finite BidirectionalRange:
-
empty,frontandpopFront()required by InputRange -
saverequired by ForwardRange -
backandpopBack()required by BidirectionalRange -
opIndex()required by RandomAccessRange -
lengthwhich provides the length of the range
Example
As an example of a finite RandomAccessRange, let’s define a range that works similar to std.range.chain. The chain() function presents the elements of a number of separate ranges as if they are elements of a single larger range. Although chain() works with any type of element and any type of range, to keep the example short, let’s implement a range that works only with int slices.
Let’s name this range Together and expect the following behavior from it:
auto range = Together([ 1, 2, 3 ], [ 101, 102, 103]);
writeln(range[4]);
When constructed with the two ...