range slicing

Dicebot public at dicebot.lv
Tue Feb 11 13:55:44 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

auto dirs = dirEntries(dir,SpanMode.shallow);
writeln(dirs.drop(2).take(2));

slicing implies random access which is not possible for input 
range.


More information about the Digitalmars-d mailing list