-
-
Notifications
You must be signed in to change notification settings - Fork 717
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
Fix Issue 24541 - cartesianProduct should have a length for finite ranges #10657
base: master
Are you sure you want to change the base?
Fix Issue 24541 - cartesianProduct should have a length for finite ranges #10657
Conversation
Thanks for your pull request and interest in making D better, @nicolo-mn! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#10657" |
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.
Remove the trailing whitespace to fix the style failures
|
@thewilsonator Could you check my changes when you got some time? Thanks in advance! |
import std.array : join; | ||
auto pairs = map!((ElementType!R1 a) => zip(repeat(a), range2.save)) | ||
(range1); | ||
static if (!isInfinite!R1) return join(pairs); |
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.
This appears to turn cartesianProduct
from a lazy range into an eager range. Surely we should check if hasLength!R1 && hasLength!R2
(after taking care of infinite ranges) and compute that.
@CyberShadow do you have a better solution?
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 was thinking of the same solution. Maybe we can use takeExactly
to set the length if we can know it.
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.
A hypothetical assumeLength
range wrapper might be even better (like assumeSorted
).
This Draft PR aims to address issue #9871 (Issue 24541 on BugZilla). As of now, I have added a
length
field to thecartesianProduct
implementation with two finite forward ranges. Since I've already built theunittest
s for it I'm publishing this Draft PR in case anyone wants to give some feedback. I'm still working on the case of two finite ranges where one of them is not a forward range, particularly to replace thejoiner
returned incartesianProduct
with a sized range.Changelog as of now:
Result
struct insidecartesianProduct
to include alength
field.length
inunittest
regardingcartesianProduct
with finite forward ranges.unittest
to ensure the correct calculation oflength
when popping elements.