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 zip and zip? to Enumerable and make it work with any number of Indexable or Iterable or Iterator #7453

Merged
merged 1 commit into from
Mar 30, 2019

Conversation

asterite
Copy link
Member

@asterite asterite commented Feb 18, 2019

Because Enumerable only provides internal iteration (there's no next, unlike Iterator) and I think because at the time zip was written there was no Iterator at all, zip used to only work against Indexable things.

This PR changes that. First, zip is moved to Enumerable because the only requirement the receiver has is to be able to traverse its elements. However, the arguments can now be any of Indexable, Iterable or Iterator, and there can be any number of them.

Some examples:

a = [1, 2, 3]
b = "a".."c"
c = 9.downto(1)

a.zip(b) # => [{1, "a"}, {2, "b"}, {3, "c"}]

b.zip(a) # => [{"a", 1}, {"b", 2}, {"c", 3}]

a.zip(b, c) # => [{1, "a", 9}, {2, "b", 8}, {3, "c", 7}]

# Don't do this at home :-P
(1..5).zip(Dir.open(".")) #=> [{1, "."}, {2, ".."}, {3, "man"}, {4, "CODE_OF_CONDUCT.md"}, {5, "compiler_spec"}] 

All of the above also work with the block version.

It's nice to see that the above snippet also works exactly the same as in Ruby :-)

It might be a bit strange that the receiver has to be Enumerable but the arguments have to be of different types, but the truth is that, at least in the std many/most of the type implement Enumerable, Iterable and Iterator at the same time.

Is this useful? I'm pretty sure it has its usecases (zipping two or three sequences at the same time). It can also be good for "competition" problems. But ultimately, this is a more complete and powerful version of the existing zip, and the implementation isn't that complex.

Bonus snippet: another way to do each_with_index:

['a', 'b', 'c'].zip(1..) do |e, i|
  puts "#{i}) #{e}"
end

(but it's probably a bit slower than each_with_index)

@asterite asterite force-pushed the feature/zip-superpower branch from 017dab1 to dec2fe3 Compare February 19, 2019 13:15
…r of `Indexable` or `Iterable` or `Iterator`
@asterite asterite force-pushed the feature/zip-superpower branch from dec2fe3 to b7f7482 Compare February 19, 2019 13:16
@asterite
Copy link
Member Author

@r00ster91 Thanks! I found it easier to apply the suggestions locally and then force push everything. Let me know if it's good now.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Sounds good! 👍

# a.zip(b, c) # => [{1, 4, 8}, {2, 5, 7}, {3, 6, 6}]
# ```
def zip(*others : Indexable | Iterable | Iterator)
pairs = Array(typeof(zip(*others) { |e| break e }.not_nil!)).new(size)
Copy link
Contributor

Choose a reason for hiding this comment

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

How does that type resolution handle a other collection whose elements are nilable?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the tuple that becomes nilable because the compiler can't know whether the block will be called (or something like that). This doesn't affect individual elements.

@asterite asterite requested a review from bcardiff March 27, 2019 17:32
@asterite asterite added this to the 0.28.0 milestone Mar 30, 2019
@asterite asterite merged commit 4402703 into crystal-lang:master Mar 30, 2019
@asterite asterite deleted the feature/zip-superpower branch March 30, 2019 16:31
@bcardiff
Copy link
Member

I’m late to the review. But I wanted to say that i liked the flexibility this PR brings. And also the tricks of using typeof of the yielding methods to be able to type the resulting array.

@asterite
Copy link
Member Author

@bcardiff Oops, sorry I didn't wait for your review. I remembered you said it was approved but I forgot you wanted to review it.

Yeah, typeof keeps rocking it :-)

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

Successfully merging this pull request may close these issues.

5 participants