import std.algorithm : filter; import std.range : take, takeExactly; struct PowersOfTwo(bool shouldSave) { size_t i = 1; void popFront() pure nothrow { i *= 2; } @property size_t front() const pure nothrow { return i + 0; } @property bool empty() const pure nothrow { return false; } static if (shouldSave) { @property PowersOfTwo save() const pure nothrow { return cast(typeof(return)) this; } } } assert(last([1, 2, 3]) == 3); assert(last(PowersOfTwo!true(1).takeExactly(5)) == 16); assert(last(PowersOfTwo!true(1).take(5)) == 16); assert(last(PowersOfTwo!false(1).take(5)) == 16);
Get the last element in range assuming it to be non-empty.