[#98645] [Ruby master Misc#16933] DevelopersMeeting20200618Japan — mame@...

Issue #16933 has been reported by mame (Yusuke Endoh).

14 messages 2020/06/04

[#98663] [Ruby master Bug#16936] `make check TESTS="-n !/Foo#method/"` not skipping the test case — jaruga@...

Issue #16936 has been reported by jaruga (Jun Aruga).

13 messages 2020/06/05

[#98772] [Ruby master Bug#16959] Weakmap has specs and third-party usage despite being a private API — headius@...

Issue #16959 has been reported by headius (Charles Nutter).

13 messages 2020/06/12

[#98826] [Ruby master Feature#16963] Remove English.rb from Ruby 2.8/3.0 — hsbt@...

Issue #16963 has been reported by hsbt (Hiroshi SHIBATA).

9 messages 2020/06/16

[#98920] [Ruby master Bug#16978] Ruby should not use realpath for __FILE__ — v.ondruch@...

Issue #16978 has been reported by vo.x (Vit Ondruch).

24 messages 2020/06/23

[#98947] [Ruby master Feature#16986] Anonymous Struct literal — ko1@...

Issue #16986 has been reported by ko1 (Koichi Sasada).

66 messages 2020/06/26

[#98964] [Ruby master Feature#16989] Sets: need ♥️ — marcandre-ruby-core@...

Issue #16989 has been reported by marcandre (Marc-Andre Lafortune).

33 messages 2020/06/26

[#98965] [Ruby master Feature#16990] Sets: operators compatibility with Array — marcandre-ruby-core@...

Issue #16990 has been reported by marcandre (Marc-Andre Lafortune).

11 messages 2020/06/26

[#98968] [Ruby master Feature#16993] Sets: from hash keys using Hash#key_set — marcandre-ruby-core@...

Issue #16993 has been reported by marcandre (Marc-Andre Lafortune).

10 messages 2020/06/26

[#98997] [Ruby master Feature#17000] 2.7.2 turns off deprecation warnings by deafult — mame@...

Issue #17000 has been reported by mame (Yusuke Endoh).

16 messages 2020/06/30

[ruby-core:98939] [Ruby master Feature#16147] List Comprehensions in Ruby

From: jon@...
Date: 2020-06-25 18:23:37 UTC
List: ruby-core #98939
Issue #16147 has been updated by emptyflask (Jon Roberts).


In my opinion, the more useful types of list comprehensions are when multiple inputs are needed.
The Ruby syntax in example one looks best, but requires more array manipulation, even if Enumerable#filter_map replaces select.map. Example four is the kind of method we should be comparing with list comps.

``` ruby
# Nice syntax, but slow:
def one(a,b,c)
  a.product(b, c)
    .select {|x, y, z| x < y && y > z }
    .map(&:sum)
end

# Combine select and map in Ruby 2.7
def two(a,b,c)
  a.product(b, c)
    .filter_map {|x, y, z| x+y+z if x < y && y > z }
end

# Ugly but more efficient, requires a mutable result array:
def three(a,b,c)
  res = []
  a.product(b, c) do |x, y, z|
    res << x+y+z if x < y && y > z
  end
  res
end

# About the same performance as three, slightly cleaner:
def four(a,b,c)
  [].tap do |res|
    a.product(b, c) do |x, y, z|
      res << x+y+z if x < y && y > z
    end
  end
end

#       user     system      total        real
#  1.566321   0.100999   1.667320 (  1.667341)
#  0.884384   0.016958   0.901342 (  0.901343)
#  0.708852   0.005994   0.714846 (  0.714862)
#  0.714095   0.000005   0.714100 (  0.714138)

```
```
-- List comprehension (Haskell syntax):
five a b c = [ x+y+z | x <- a,
                       y <- b,
                       z <- c,
                       x < y, y > z ]

```

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

* Author: sammomichael (Samuel Michael)
* Status: Open
* Priority: Normal
----------------------------------------
## 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>

In This Thread

Prev Next