last

Get the last element in range assuming it to be non-empty.

ElementType!Range
last
(
Range
)
(
Range range
)
if (
isInputRange!Range
)

Examples

1 import std.algorithm : filter;
2 import std.range : take, takeExactly;
3 
4 struct PowersOfTwo(bool shouldSave)
5 {
6     size_t i = 1;
7 
8     void popFront() pure nothrow
9     {
10         i *= 2;
11     }
12 
13     @property size_t front() const pure nothrow
14     {
15         return i + 0;
16     }
17 
18     @property bool empty() const pure nothrow
19     {
20         return false;
21     }
22 
23     static if (shouldSave)
24     {
25         @property PowersOfTwo save() const pure nothrow
26         {
27             return cast(typeof(return)) this;
28         }
29     }
30 }
31 
32 assert(last([1, 2, 3]) == 3);
33 assert(last(PowersOfTwo!true(1).takeExactly(5)) == 16);
34 assert(last(PowersOfTwo!true(1).take(5)) == 16);
35 assert(last(PowersOfTwo!false(1).take(5)) == 16);

Meta