Feature #6499

Array::zip

Added by Ilya Vorontsov almost 2 years ago. Updated over 1 year ago.

[ruby-core:45252]
Status:Rejected
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:2.0.0

Description

Sometimes it's more natural to have Array::zip rather than Array#zip. Otherwise first array of list became unnecessary dedicated
For example
cols = firstrow.zip(secondrow,thirdrow,fourthrow)
have more natural analog:
cols = Array.zip(firstrow,secondrow,thirdrow,fourthrow)

Implementation is obvious:
def Array.zip(first,rest)
first.zip(
rest)
end

array_product_zip.pdf (53.9 KB) Clay Trump, 07/01/2012 07:59 AM


Related issues

Duplicates ruby-trunk - Feature #8970: Array.zip and Array.product Open 10/01/2013

History

#1 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#2 Updated by Thomas Sawyer almost 2 years ago

This is probably true of any Array method that take one or more other arrays as argument. Are there other's besides #zip?

#3 Updated by Marc-Andre Lafortune almost 2 years ago

Hi

trans (Thomas Sawyer) wrote:

This is probably true of any Array method that take one or more other arrays as argument. Are there other's besides #zip?

I agree and would like Array.product to be considered as a worthy addition.

#4 Updated by Martin Dürst almost 2 years ago

The problem becomes even more obvious in the cases where one starts with an array of arrays:
New way:
Array.zip(arrays)
Old way:
arrays[0].zip(
arrrays[1..-1])
The new way is clear and concise. The old way is a mess. I have definitely met this pattern for zip and product, maybe others. I always wished there had been a better way, but the idea I came up with was something like arrays.zips_many or so. That might work, too, but Array.zip is quite neat, too.

#5 Updated by Martin Dürst almost 2 years ago

duerst (Martin Dürst) wrote:

I always wished there had been a better way, but the idea I came up with was something like arrays.zips_many or so. That might work, too, but Array.zip is quite neat, too.

Sorry, it should have been arrays.zip_many above.

#6 Updated by Ilya Vorontsov almost 2 years ago

marcandre (Marc-Andre Lafortune) wrote:

Hi

trans (Thomas Sawyer) wrote:

This is probably true of any Array method that take one or more other arrays as argument. Are there other's besides #zip?

I agree and would like Array.product to be considered as a worthy addition.

Actually! I had such a problem with #product too, but have already forgotten about it.

#7 Updated by Marc-Andre Lafortune almost 2 years ago

  • Category set to core
  • Target version set to 2.0.0

Wouldn't it be best if both Array.product and Array.zip were lazy?

Will someone produce a one-minute slide-show for this?

#8 Updated by Clay Trump almost 2 years ago

I've had the same issue in the past, so here's a slide for this feature.

On Fri, Jun 1, 2012 at 2:54 AM, prijutme4ty (Ilya Vorontsov) <
prijutme4ty@gmail.com> wrote:

Issue #6499 has been updated by prijutme4ty (Ilya Vorontsov).

Actually! I had such a problem with #product too, but have already
forgotten about it.

--

#9 Updated by Yusuke Endoh almost 2 years ago

Received. Thank you!

Yusuke Endoh mame@tsg.ne.jp

#10 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Assigned to Rejected

Ilya Vorontsov and Clay Trump,

Sorry but this proposal was rejected at the developer meeting (7/21).

Here is a discussion summary:

  • Matz was NOT positive to this kind of proposals that is for the
    sake of symmetric notation.

  • Matz wants to save those who want Enumerator-version of Array#zip
    and product, but it does not require the new-style API.
    We should discuss it as another request.

  • The behavior of Array.zip(*ary) is not trivial if ary == [].
    The spec candidates are:

    • returns an array whose size is the same as the first element, or
    • returns an array whose size is the same as the shortest element

    If ary == [], both do not make sense because neither the first
    element or the shortest one exist.
    It may be intuitive to return an infinite array: [[], [], [], ...]

Yusuke Endoh mame@tsg.ne.jp

#11 Updated by Thomas Sawyer over 1 year ago

If it is any consolation, I will add these to Facets.

#12 Updated by Marc-Andre Lafortune over 1 year ago

I've thought a bit about this, and the proposed Array.zip basically already exists if all arrays have the same length.

It's called Array#transpose :-)

Array.zip([1,2,3], [4,5,6]) == [[1,2,3], [4,5,6]].transpose

Note that the case of Array.zip() could be debated, but, as explained in https://bugs.ruby-lang.org/issues/6852, [] == [].transpose is the best answer.

Because of this, it seems there is a lesser need for Array.zip.

On the other hand, I am still confident that Array.product or similar, would be helpful. I hope I won't offend anyone by opening a new request.

#13 Updated by Marc-Andre Lafortune about 1 month ago

Also available in: Atom PDF