From: Shugo Maeda Date: 2012-02-24T14:48:56+09:00 Subject: [ruby-core:42854] [ruby-trunk - Feature #6073] Proposal of extending syntax of for loop Issue #6073 has been updated by Shugo Maeda. Hi, Shouichi KAMIYA wrote: > I propose to extend syntax of for loop which allows us to write multiple loop with guard easily. > This extension is inspired by Scala. Here is a example I'm interested in your idea. # Feature #6070 was preparation for the same proposal. > for i in 1..4 when i % 2 == 0 > j in 5..8 when j % 4 == 0 > puts "#{i}, #{j}" > end > > Above code is same as following code. > > for i in 1..4 > if i % 2 == 0 > for j in 5..8 > if j % 4 == 0 > puts "#{i}, #{j}" > end > end > end > end Scala's for expression is more powerful. If yield in used in a for expression, nested loop is converted to a combination of map/flatMap/withFilter. For example, the following code: for z in (1..Float::INFINITY).lazy x in 1..z y in x..z when x**2 + y**2 == z**2 yield [x, y, z] end is equivalent to the following code: (1..Float::INFINITY).lazy.flat_map {|z| (1..z).flat_map {|x| (x..z).select {|y| x**2 + y**2 == z**2 }.map {|y| [x, y, z] } } } Please note that yield is already take in Ruby, so we need different Syntax. What do you think of that feature? ---------------------------------------- Feature #6073: Proposal of extending syntax of for loop https://bugs.ruby-lang.org/issues/6073 Author: Shouichi KAMIYA Status: Open Priority: Normal Assignee: Category: Target version: Hi, I propose to extend syntax of for loop which allows us to write multiple loop with guard easily. This extension is inspired by Scala. Here is a example for i in 1..4 when i % 2 == 0 j in 5..8 when j % 4 == 0 puts "#{i}, #{j}" end Above code is same as following code. for i in 1..4 if i % 2 == 0 for j in 5..8 if j % 4 == 0 puts "#{i}, #{j}" end end end end I already implemented this syntax and attached a patch. What do you think about this syntax guys? Thank you, Shouichi -- http://bugs.ruby-lang.org/