xxxxxxxxxx
import std.stdio : writeln;
import std.range : iota, array;
import std.algorithm : map;
import std.range : ElementType, front, popFront, empty;
import std.traits : Unqual;
import std.functional : forward;
struct DiffRange(R)
{
private:
alias Range = Unqual!R;
alias El = Unqual!(ElementType!Range);
Range _range;
El _prev;
public:
this(R r)
{
assert(!r.empty);
_range = r;
_prev = _range.front;
_range.popFront();
}
bool empty() const
{
return _range.empty;
}
El front() const
{
return _range.front - _prev;
}
void popFront()
{
_prev = _range.front;
_range.popFront();
}
}
// First difference
auto diff1(R)(auto ref R r)
{
return DiffRange!(R)(forward!r);
}
void main()
{
{
// OK
const(int)[] range = [0,1,2,3];
auto x = range.diff1;
range.writeln;
x.writeln;
}
{
// Error:
// mutable method std.algorithm.iteration.MapResult!(unaryFun, int[]).MapResult.empty
// is not callable using a const object
// Consider adding const or inout to std.algorithm.iteration.MapResult!(unaryFun, int[]).MapResult.empty
auto range = [0,1,2,3].map!"a^^2";
auto x = range.diff1;
range.writeln;
x.writeln;
}
}