Project

General

Profile

Actions

Feature #4633

closed

iterate method / extended version of for

Added by rbjl (Jan Lelis) almost 13 years ago. Updated almost 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:35947]

Description

=begin
The Ruby world is known for using each, but it does not always look nice (although in most cases it does).

I am proposing an iterate method that is nicely readable and allows easy iteration over multiple objects. It behaves like each for an single argument, but passes nils for Enumerables with multiple sizes:
iterate [1,2], [3,4,5] do |e,f|
puts "#{e},#{f}"
end

outputs

1,3

2,4

,5

A simple Ruby implementation:
def iterate(*params)
# params.shift.zip(*params).each{ |*elements| yield *elements }
raise ArgumentError, "wrong number of arguments (0)" if params.empty?

first = params.shift
if params.empty? # single param - like each
  if block_given?
    first.map{|e| yield e }
  else
    first.map.to_enum
  end
else # multiple params
  max_size = [first, *params].max_by(&:count).size
  padded_first = first.to_a + [nil]*(max_size - first.count)  # append nils
  obj = padded_first.zip *params
  if block_given?
    obj.map{|es| yield *es }
  else
    obj.map.to_enum
  end
end

end

A modified version of this request (no new method/statement) could be an alternative usage of for, something like:
for e,f in [1,2], [3,4,5]
puts "#{e},#{f}"
end

outputs

1,3

2,4

,5

This feature request does not add something needed, but I think, Ruby would look even more beautiful.
=end

Updated by mame (Yusuke Endoh) about 12 years ago

  • Description updated (diff)
  • Status changed from Open to Assigned
  • Assignee set to tarui (Masaya Tarui)

Updated by tarui (Masaya Tarui) almost 12 years ago

  • Status changed from Assigned to Rejected

Dear Jan,

Sorry that I didn't reply to you sooner.

Thank you for the precious proposal,
but I reject this proposal from the following points.

  1. This proposal becomes the change to Kernel, and
    adding the general name of "iterate" there has great
    influence.
    I think that it is neccessary to make it the method of Array at least.

  2. A use-case is not known.
    Which becomes happy by it?
    There are similar methods already.(zip & transpose)
    Although the difference from "zip" is the number of
    times of repeating, which is the case needed frequently?

If you can show that a suitable name as a method of Array and the use-case,
please create a ticket newly.

Regards,
Masaya

Actions

Also available in: Atom PDF

Like0
Like0Like0