Editing Topic: RCR230 Project: RCR | RCRchive home

RCR 230: 'for' for multiple arrays

submitted by mchase on Fri Mar 12 2004 03:29:27 PM -0800

Status: pending


Abstract

Give syntax for using the 'for' shortcut on multiple arrays.

Problem

Iteration over multiple enumerables at once is not simple or intuitive, involving explicit indexing into each of the enums (fine for people who came from C), and what is being iterated over is not well described by the loop control of the iteration (for example: '[a.length, b.length].max.times do |i| puts a[i],b[i] end').

Proposal

Allow comma delimited list of Enumerables to be zipped together and iterated over.

for x,y,z in enum_1,enum_2,enum_3
  puts x,y,z
end
would be a shortcut to
enum_1.zip(enum_2, enum_3).each do |x,y,z|
  puts x,y,z
end

Analysis

The shortcut wouldn't carry over and be able to help the normal use of #each, as 'for' is already doing some magic, and it would have to be doing more with this.

To be able to mix Hashes, Arrays, &c., further magic would have to happen, such as calling #to_a on all enums passed as arguments to #zip (possibly moving that behavior to #zip itself).

Implementation

*pending grokery of parse.y*


Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog