digitalmars.D - range slicing
- kuba (14/14) Feb 11 2014 Hi everyone,
- bearophile (8/12) Feb 11 2014 In Python standard library there is an islice(), that performs a
- kuba (3/3) Feb 11 2014 Thanks for quick feedback and sorry, my bad. Will post to D.learn
- Dicebot (5/19) Feb 11 2014 auto dirs = dirEntries(dir,SpanMode.shallow);
- Brian Schott (3/5) Feb 11 2014 In general questions about programming in D belong in D.learn.
- Craig Dillabaugh (9/23) Feb 11 2014 Welcome to the D forums.
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
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
Thanks for quick feedback and sorry, my bad. Will post to D.learn next time. kuba
Feb 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, kubaauto 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
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
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, kubaWelcome 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