Idiomatic adjacent_difference

Guillaume Chatelet via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 16 04:43:14 PDT 2015


On Friday, 16 October 2015 at 11:38:35 UTC, John Colvin wrote:
> import std.range, std.algorithm;
>
> auto slidingWindow(R)(R r, size_t n)
> if(isForwardRange!R)
> {
> 	//you could definitely do this with less overhead
> 	return roundRobin(r.chunks(n), r.save.drop(1).chunks(n))
> 		.filter!(p => p.length == n);
> }
>
> auto adjacentDiff(R)(R r)
> {
> 	return r.slidingWindow(2).map!"a[1] - a[0]";
> }

Nice !
I wanted to use lockstep(r, r.dropOne) but it doesn't return a 
Range :-/
It has to be used in a foreach.


More information about the Digitalmars-d-learn mailing list