Issue #16147 has been updated by sammomichael (Samuel Michael).


In reiteration of my main points, Ruby is a dynamic multi-paradigm language which should when possible embrace a variety of modes to achieve the same result.    

SO what would be the benefits of list comprehension in Ruby and what would it look like? 

1. Creating an array with only [] square brackets is fun and makes intuitive sense for beginners
2. "For .. in .." syntax is succinct, flexible, and has a good spoken rhythm/easy to verbalize
3. Easier for users of other languages to assimilate into Ruby as they don't need to know all our enumerable library right away.

Ruby already has functioning list comprehension syntax but it could be optimized:

[for x in 1..10 do x**2 if x%2==0 end] # this is Ruby but we can't capture the value 
[for x in 1..10; x**2 if x.even? end] # since 2.7 Ruby it would also be better if it called Filter_Map under the hood instead of each

# proposal to use some syntax to use a splat or additional indicator to allow a smoother shorthand for list comprehensions

[for x in 1..10 do x**2 if x.even? end] #=> 1..10  (normal Ruby)

# below we propose a syntax in which we splat the for loop to return the stored result not the caller

[*for x in 1..10 do x**2 if x.even? end] #=> [4, 16, 36, 64, 100] 

# under the hood this could be syntactic sugar for 

(1..10).filter_map{@1**2 if @1.even?}  #=> [4, 16, 36, 64, 100] 

# the key differences to notice. although there are less keystrokes with filter_map, you had to know the method name, and the exact arrangement of parentheses and curly braces, and whether to use a block or numbered parameter. If you decided to use pre-2.7 ruby you would have said:

(1..10).filter_map{|x|x**2 if x.even?}

vs with proposed list syntax:

[*for x in 1..10; x**2 if x.even? end] 

# I think both could and should be standard ruby syntax of achieving the same result of quickly generating a filter mapped array of the given elements.







----------------------------------------
Feature #16147: List Comprehensions in Ruby
https://bugs.ruby-lang.org/issues/16147#change-81654

* Author: sammomichael (Samuel Michael)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## List comprehensions are present in many languages and programmers are quite fond of their simplicity and power. Add to that the fact that Ruby has a for...in loop that is rarely used but could possibly be repurposed. 

### Currently we can already do a hack like this to make Ruby support list comprehension syntax:

``` ruby
S = [for x in 0...9 do $* << x*2 if x.even? end, $*][1]
# [0, 4, 8, 12, 16]
```

Still, it would be far nicer if the for...in loop would return the desired array automatically, this is one way to approach that taking advantage of lambda bracket invocation syntax:

``` ruby
c = -> x do $*.clear             
  if x['if'] && x[0] != 'f' .  
    y = x[0...x.index('for')]    
    x = x[x.index('for')..-1]
    (x.insert(x.index(x.split[3]) + x.split[3].length, " do $* << #{y}")
    x.insert(x.length, "end; $*")
    eval(x)
    $*)
  elsif x['if'] && x[0] == 'f'
    (x.insert(x.index(x.split[3]) + x.split[3].length, " do $* << x")
    x.insert(x.length, "end; $*")
    eval(x)
    $*)
  elsif !x['if'] && x[0] != 'f'
    y = x[0...x.index('for')]
    x = x[x.index('for')..-1]
    (x.insert(x.index(x.split[3]) + x.split[3].length, " do $* << #{y}")
    x.insert(x.length, "end; $*")
    eval(x)
    $*)
  else
    eval(x.split[3]).to_a
  end
end 

```

so basically we are converting a string to proper ruby syntax for loop then we can use python syntax in a string to do:

``` ruby

c['for x in 1..10']
c['for x in 1..10 if x.even?']
c['x**2 for x in 1..10 if x.even?']
c['x**2 for x in 1..10']

# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# [2, 4, 6, 8, 10]
# [4, 16, 36, 64, 100]
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>