Issue #6073 has been updated by Shouichi KAMIYA.


Hi,

Thank you for your quick response.

Shugo Maeda wrote:
> 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?

Yes Scala's yield feature in for loop is powerful and it'll be good if we can use that feature in Ruby.
However, it seems like that for loop feature will be pretty different from current for loop feature right?
Do you think it's a good idea?
----------------------------------------
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/