D slicing

bearophile bearophileHUGS at lycos.com
Mon Jun 17 16:44:27 PDT 2013


Colin Grogan:

> Reading the articles on array slicing its not clear if its 
> possible.

I presume Walter thinks that slicing with a stride is a not 
common enough operation to put it into D. His choices on such 
things are a bit arbitrary.

One way to do it:


import std.stdio, std.array, std.range;

void main() {
     auto orig = [1, 2, 3, 4, 5, 6, 7];
     auto sliceOdd = orig.stride(2).array;
     sliceOdd.writeln;
     auto sliceEven = orig[1..$].stride(2).array;
     sliceEven.writeln;
}


stride() is just a function used with UFCS.

Don't use ".array" if you just need a lazy sequence.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list