Feature #6499
closedArray::zip
Added by prijutme4ty (Ilya Vorontsov) over 12 years ago. Updated almost 12 years ago.
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 = first_row.zip(second_row,third_row,fourth_row)
have more natural analog:
cols = Array.zip(first_row,second_row,third_row,fourth_row)
Implementation is obvious:
def Array.zip(first,*rest)
first.zip(*rest)
end
Files
array_product_zip.pdf (53.9 KB) array_product_zip.pdf | claytrump (Clay Trump), 07/01/2012 07:59 AM |
Updated by mame (Yusuke Endoh) over 12 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by trans (Thomas Sawyer) over 12 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?
Updated by marcandre (Marc-Andre Lafortune) over 12 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.
Updated by duerst (Martin Dürst) over 12 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.
Updated by duerst (Martin Dürst) over 12 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.
Updated by prijutme4ty (Ilya Vorontsov) over 12 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.
Updated by marcandre (Marc-Andre Lafortune) over 12 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?
Updated by claytrump (Clay Trump) over 12 years ago
- File array_product_zip.pdf array_product_zip.pdf added
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.
--
Updated by mame (Yusuke Endoh) over 12 years ago
Received. Thank you!
--
Yusuke Endoh mame@tsg.ne.jp
Updated by mame (Yusuke Endoh) over 12 years 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
Updated by trans (Thomas Sawyer) over 12 years ago
If it is any consolation, I will add these to Facets.
Updated by marcandre (Marc-Andre Lafortune) almost 12 years 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.
Updated by marcandre (Marc-Andre Lafortune) over 10 years ago
- Is duplicate of Feature #8970: Array.zip and Array.product added
Updated by duerst (Martin Dürst) almost 2 years ago
- Related to Feature #19324: Enumerator.product => Enumerable#product added