range slicing

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Tue Feb 11 13:57:55 PST 2014


On Tuesday, 11 February 2014 at 21:48:29 UTC, kuba wrote:
> Hi everyone,
> This is my first post here :)
> In order to get a slice of a range (to access some initial 
> elements) I have to convert a range into array first. I wonder 
> what is preferred approach? Should I use slicing or stick with 
> for loops? Also turning the range into array forces the range 
> to be computed entirely, killing the lazy nature of the 
> dirEntries function. Could it be a bottleneck down the road?
>
> auto dir = "/data/home/kuba";
> auto dirs = dirEntries(dir,SpanMode.shallow);
> auto arr =  array(dirs);
> writeln(arr[2..4]);
>
> Thank you,
> kuba

Welcome to the D forums.

The idiomatic way of taking a subset of a range in D is use 
something like filter:

http://dlang.org/phobos/std_algorithm.html#filter

To implement your specific example (report the 2nd and 3rd 
elements) would be a bit tricky, is that really what you need to 
do?

Finally, this might be a better candidate for D.learn.


More information about the Digitalmars-d mailing list