digitalmars.D.learn - std.range: Lockstep vs. Zip
- Manuel Maier (10/10) Mar 02 2016 Hi there,
- HSteffenhagen (8/19) Mar 02 2016 I'm not entirely sure, but I get the impression lockstep is a
- Alex Parrill (7/18) Mar 03 2016 zip uses the InputRange protocol, and bundles up all the values
- Seb (5/26) Mar 04 2016 That helps a lot - it is funny that the documentation shows an
Hi there, I was wondering why I should ever prefer std.range.lockstep over std.range.zip. In my (very limited) tests std.range.zip offered the same functionality as std.range.lockstep, i.e. I was able to iterate using `foreach(key, value; std.range.zip(...)) {}` which, according to the docs, is what std.range.lockstep was supposed to be designed for. On top of that, std.range.zip is capable of producing a RandomAccessRange, but std.range.lockstep only produces something with opApply. Cheers
Mar 02 2016
On Wednesday, 2 March 2016 at 08:51:07 UTC, Manuel Maier wrote:Hi there, I was wondering why I should ever prefer std.range.lockstep over std.range.zip. In my (very limited) tests std.range.zip offered the same functionality as std.range.lockstep, i.e. I was able to iterate using `foreach(key, value; std.range.zip(...)) {}` which, according to the docs, is what std.range.lockstep was supposed to be designed for. On top of that, std.range.zip is capable of producing a RandomAccessRange, but std.range.lockstep only produces something with opApply. CheersI'm not entirely sure, but I get the impression lockstep is a special case of zip. I suspect if you don't need anything zip offers over lockstep (mutability, random access) you won't gain anything from using it. I'm usually using zip in this circumstance, but if all you're doing is iterating there doesn't seem to be a reason to not use lockstep.
Mar 02 2016
On Wednesday, 2 March 2016 at 08:51:07 UTC, Manuel Maier wrote:Hi there, I was wondering why I should ever prefer std.range.lockstep over std.range.zip. In my (very limited) tests std.range.zip offered the same functionality as std.range.lockstep, i.e. I was able to iterate using `foreach(key, value; std.range.zip(...)) {}` which, according to the docs, is what std.range.lockstep was supposed to be designed for. On top of that, std.range.zip is capable of producing a RandomAccessRange, but std.range.lockstep only produces something with opApply. Cheerszip uses the InputRange protocol, and bundles up all the values in a Tuple. lockstep uses the opApply protocol, and doesn't bundle the values. Lockstep is useful for foreach loops since you don't need to unpack a tuple, but zip is compatible with all of the std.range and std.algorithm functions that take ranges.
Mar 03 2016
On Thursday, 3 March 2016 at 15:00:10 UTC, Alex Parrill wrote:On Wednesday, 2 March 2016 at 08:51:07 UTC, Manuel Maier wrote:That helps a lot - it is funny that the documentation shows an example with foreach an `zip`. Your insights also helped me and I thought it should also help other people, so I made a PR ;-) https://github.com/D-Programming-Language/phobos/pull/4054Hi there, I was wondering why I should ever prefer std.range.lockstep over std.range.zip. In my (very limited) tests std.range.zip offered the same functionality as std.range.lockstep, i.e. I was able to iterate using `foreach(key, value; std.range.zip(...)) {}` which, according to the docs, is what std.range.lockstep was supposed to be designed for. On top of that, std.range.zip is capable of producing a RandomAccessRange, but std.range.lockstep only produces something with opApply. Cheerszip uses the InputRange protocol, and bundles up all the values in a Tuple. lockstep uses the opApply protocol, and doesn't bundle the values. Lockstep is useful for foreach loops since you don't need to unpack a tuple, but zip is compatible with all of the std.range and std.algorithm functions that take ranges.
Mar 04 2016