Skip to content
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

Move Array#product to Enumerable #3794

Closed
wants to merge 1 commit into from
Closed

Conversation

yxhuvud
Copy link
Contributor

@yxhuvud yxhuvud commented Dec 28, 2016

Issue #3790

@yxhuvud
Copy link
Contributor Author

yxhuvud commented Dec 28, 2016

Thoughts? Is checking for #size ok?

@@ -9,7 +9,7 @@
## Build the compiler in release mode
## $ make crystal release=1
## Run all specs in verbose mode
## $ make specs verbose=1
## $ make spec verbose=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate this fix from this pull request!

This allows doing cartesian products on things that are not arrays. It
should also reduce the confusion of Array having product methods from
two different locations.
#
# An empty array will be returned if either collection is empty.
def product(ary : Enumerable(U)) forall U
result = if ary.responds_to?(:size)
Copy link
Contributor

@RX14 RX14 Dec 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use this syntax instead:

if ary.responds_to?(:size)
  result = Array({T, U}).new(size * ary.size)
else
  result = Array({T, U}).new
end

It's much cleaner with the indenting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not quite correct. Enumerable also has a size method, that merely iterates through all of the elements to count them. So this will traverse the enumerable many times.

I think product is fine in Array, and shouldn't be available in Enumerable.

We'll eventually revisit all of the std design, in one go, instead of deciding the fate of each method case by case. I don't think I'll be able to merge this PR or take any other decision about this right now.

Copy link
Contributor Author

@yxhuvud yxhuvud Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, that would be a bad side effect without any obvious solution. Should I close or leave the pull request?

Or should I separate out the documentation additions as those may be nice even if it isn't moved to enumerator?

# An empty array will be returned if either collection is empty.
def product(ary : Enumerable(U)) forall U
result = if ary.responds_to?(:size)
Array({T, U}).new(size * ary.size)
Copy link
Contributor

@RX14 RX14 Dec 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you assume both ary and this class respond to size, but only check for ary having size.


product(ary) do |x, y|
result << {x, y}
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank space after this line.

@RX14
Copy link
Contributor

RX14 commented Dec 28, 2016

I think it would be extremely wise to rename this cartesian_product, considering that array.product(foo) will do entirely different thigs depending on whether foo is an Enumerable or a Number.

@miketheman
Copy link
Contributor

#codetriage
@asterite This appears to have been left with a question directed to you, whether this should be closed, or what the next steps should be.

@sdogruyol
Copy link
Member

Any update on this? WDYT @asterite ?

@asterite
Copy link
Member

Let's close this PR as the issue regarding size has no clear solution right now. Please reopen if there's a good solution to this. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants