www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - range slicing

reply "kuba" <kuba456 gmail.com> writes:
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
Feb 11 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Feb 11 2014
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
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.
Feb 11 2014
prev sibling next sibling parent "Brian Schott" <briancschott gmail.com> writes:
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
Feb 11 2014
prev sibling next sibling parent "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
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.
Feb 11 2014
prev sibling parent "kuba" <kuba456 gmail.com> writes:
Thanks for quick feedback and sorry, my bad. Will post to D.learn 
next time.
kuba
Feb 11 2014