-
-
Notifications
You must be signed in to change notification settings - Fork 739
std.range: explain difference between lockstep and zip #4054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
51fe30f
to
d7019e8
Compare
ranges in lockstep. For example, the following code sorts two arrays | ||
in parallel: | ||
ranges in lockstep. | ||
For example, the following code sorts two arrays in parallel: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could move this sentence to a comment at the top of the unit test, then the example doesn't need to be in the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, please try to stick to documented unit tests as much as possible. From past experience (i.e. before the feature existed), the danger of bit rot is just too high otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed - just didn't know a nice way to fit it into the text. I will try again ;-)
While you are at it, could you also have a look at the corresponding comment in
is rather confusing to me. It's Edit: Huh, GitHub didn't show those lines as being added with this commit. |
@@ -3877,7 +3894,6 @@ pure unittest | |||
assert(b == [ "c", "b", "a" ]); | |||
} | |||
|
|||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should undocument this, I actually think it should come before the sort example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep at least one example that shows explicitly how the different subranges can be indexed. The other example just has [0]
, which might not be enough to tip off users as to how that works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the sort example, I suggest we rename e to tup as it's a tuple.
Since the phrasing seems to be off for both the functions – did you somehow mix up the descriptions for the two See Also blocks? |
588d1b1
to
078b299
Compare
Please have a look at example 2. There are three ways that we could recommend - they all work. As we now have an entire PR about it. Let me clarify - we have
Moreover I added another nice example for
|
70f1d0a
to
235d5bb
Compare
LGTM 👍 |
@ntrel - thanks for your helpful feedback and review. The examples now look a lot better: side note: the formatting of the ddocs output could be improved a bit (e.g. no duplicates "Examples")- see issue 15769 |
The actual reason auto left = new int[](3);
auto right = new int[](3);
foreach(ref lhs, ref rhs; lockstep(left, right))
{
lhs = 1;
rhs = 2;
}
assert(left == [1, 1, 1]);
assert(right == [2, 2, 2]); External iteration ala |
Okay then I will update the docs to point this out even more clearly :) Btw having idexing in foreachs |
What do you mean? If you mean iterating with an index, std.range.enumerate is designed to work well with zip. |
Yes I know, but I was more referring that
|
Updates:
|
|
||
idx = 0; | ||
// it supports automatic tuple unpacking too | ||
foreach (t1, t2; zip(a, b)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I suggest we use e1, e2
as they're not tuples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done - I also renamed the loop parameter for lockstep
512a439
to
4227fd3
Compare
I think that's due to lockStep's opApply. I'm not sure that tuple unpacking for zip would work correctly if there was an index element but no index variable. BTW foreach tuple unpacking isn't documented AFAICT: |
That's the wrong way in my opinion, if it is slow due to the tuple expansion, we should either
and not hope that people might not try it. |
There already was an example with |
Thanks @ntrel - merged with this branch :) |
Simplify zip examples more: |
Should I squash the commits? |
Sure, go ahead. |
816bb42
to
b86ae29
Compare
squashed and rebased to master. @ntrel - it's a shame git doesn't allow joint commit messages :/ |
b86ae29
to
69346b1
Compare
ping @ reviewers - @ntrel and I have worked on this minor update to the documentation which yields a better understanding of the difference between |
Absolutely - you could/can change it back to your name. I don't mind either. Thanks for pushing this @ntrel, but I guess the main reason the inactivity is due to the weekend? |
Auto-merge toggled on |
std.range: explain difference between lockstep and zip
this is a follow-up to the discussions from the forum
foreach
example forzip
- this is too confusing!zip
unittest in the documentation as otherwise the order with See_Also would be messed up :S