Thread overview
range slicing
Feb 11, 2014
kuba
Feb 11, 2014
bearophile
Feb 11, 2014
kuba
Feb 11, 2014
Dicebot
Feb 11, 2014
Brian Schott
Feb 11, 2014
Craig Dillabaugh
February 11, 2014
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
February 11, 2014
kuba:

> auto dir = "/data/home/kuba";
> auto dirs = dirEntries(dir,SpanMode.shallow);
> auto arr =  array(dirs);
> writeln(arr[2..4]);

In Python standard library there is an islice(), that performs a lazy slicing. I think there's not a similar function in Phobos, but you can use ranges to skip the leading part. So you can use drop(2).take(2) to take the slice. All lazily.

I also suggest to ask such questions in D.learn.

Bye,
bearophile
February 11, 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.
February 11, 2014
On Tuesday, 11 February 2014 at 21:48:29 UTC, kuba wrote:
> Hi everyone,
> This is my first post here :)

In general questions about programming in D belong in D.learn.

http://forum.dlang.org/group/digitalmars.D.learn
February 11, 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.
February 11, 2014
Thanks for quick feedback and sorry, my bad. Will post to D.learn next time.
kuba