www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to iterate over range two items at a time

reply Adnan <relay.public.adnan outlook.com> writes:
What is the equivalent of Rust's chunks_exact()[1] method in D? I 
want to iterate over a spitted string two chunks at a time.


[1] 
https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
Feb 16 2020
next sibling parent mipri <mipri minimaltype.com> writes:
On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote:
 What is the equivalent of Rust's chunks_exact()[1] method in D? 
 I want to iterate over a spitted string two chunks at a time.


 [1] 
 https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
$ rdmd --eval '"hello world".chunks(2).each!writeln' he ll o wo rl d $ rdmd --eval '"hello world".chunks(2).map!"a.array.length".each!writeln' 2 2 2 2 2 1 Note the .array; each chunk is a range type. $ rdmd --eval 'typeid("hello world".chunks(2).take(1)).writeln' std.range.Take!(Chunks!string).Take
Feb 16 2020
prev sibling next sibling parent mipri <mipri minimaltype.com> writes:
On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote:
 What is the equivalent of Rust's chunks_exact()[1] method in D? 
 I want to iterate over a spitted string two chunks at a time.


 [1] 
 https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
And, after actually reading the Rust doc, I see that you want to iterate over a string two chars at a time while discarding any remaining 1-length chunk. There isn't an equivalent of that. A quick one is auto chunks_exact(R)(R r, size_t n) { return r.take(r.length - r.length%n).chunks(n); } but to get the exact same behavior you'll want to return a range type that stores the final chunk.
Feb 16 2020
prev sibling parent reply Mitacha <mateusz.mitaszka gmail.com> writes:
On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote:
 What is the equivalent of Rust's chunks_exact()[1] method in D? 
 I want to iterate over a spitted string two chunks at a time.


 [1] 
 https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
It sounds similar to `slide` https://dlang.org/phobos/std_range.html#slide
Feb 16 2020
parent reply Adnan <relay.public.adnan outlook.com> writes:
On Monday, 17 February 2020 at 07:50:02 UTC, Mitacha wrote:
 On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote:
 What is the equivalent of Rust's chunks_exact()[1] method in 
 D? I want to iterate over a spitted string two chunks at a 
 time.


 [1] 
 https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
It sounds similar to `slide` https://dlang.org/phobos/std_range.html#slide
The key difference here is that slide seems to overlap, which is a big no-no for my current problem. I have just gone with a classic for loop. Error prone, but just works.
Feb 17 2020
parent Basile B. <b2.temp gmx.com> writes:
On Monday, 17 February 2020 at 09:41:35 UTC, Adnan wrote:
 On Monday, 17 February 2020 at 07:50:02 UTC, Mitacha wrote:
 On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote:
 What is the equivalent of Rust's chunks_exact()[1] method in 
 D? I want to iterate over a spitted string two chunks at a 
 time.


 [1] 
 https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact
It sounds similar to `slide` https://dlang.org/phobos/std_range.html#slide
The key difference here is that slide seems to overlap, which is a big no-no for my current problem. I have just gone with a classic for loop. Error prone, but just works.
Yeah your analysis is right. slide is specifically made to overlapp, if overlapping is not needed slide is not needed.
Feb 17 2020