From: sammomichael@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95213] [Ruby master Feature#16147] List Comprehensions in Ruby
Date: Fri, 04 Oct 2019 00:41:35 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-81887.20191004004133.4679dde17b57dc1e@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16147.20190906103822@ruby-lang.org
Issue #16147 has been updated by sammomichael (Samuel Michael).
sammomichael (Samuel Michael) wrote:
> sammomichael (Samuel Michael) wrote:
> > ## ruby_list_comprehension gem documentation
> > https://github.com/SammoMichael/Ruby_List_Comprehension
> > ## ruby_list_comprehension gem page
> > https://rubygems.org/gems/ruby_list_comprehension
> > Here is a gem to give a better idea what I mean.
> > [![Gem Version](https://badge.fury.io/rb/ruby_list_comprehension.svg)](https://badge.fury.io/rb/ruby_list_comprehension)
> > Instructions:
> > gem install ruby_list_comprehension
> >
> > `require 'ruby_list_comprehension'`
> >
> > `l - ListComprehension.new`
> >
> > ` l[for x in 1..10 do x end] (optional map/filter condition)`
> >
> > `l[for x in 1..10 do x + 5 || x - 3 if x != 3 end] #=> [6, 7, 9, 10, 11, 12, 13, 14, 15]`
> >
> > `l[for x in Set.new([1,2,2,3,4,5]) do x.to_s if x != 2 end] #=> ["1", "3", "4", "5"]`
> >
> > if you would like to try out a prototype I would appreciate feedback
----------------------------------------
Feature #16147: List Comprehensions in Ruby
https://bugs.ruby-lang.org/issues/16147#change-81887
* 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/
next prev parent reply other threads:[~2019-10-04 0:41 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-16147.20190906103822@ruby-lang.org>
2019-09-06 10:38 ` [ruby-core:94796] [Ruby master Bug#16147] List Comprehensions in Ruby sammomichael
2019-09-06 13:18 ` [ruby-core:94799] " daniel
2019-09-06 14:06 ` [ruby-core:94800] " shevegen
2019-09-06 16:41 ` [ruby-core:94804] [Ruby master Feature#16147] " sammomichael
2019-09-06 16:44 ` [ruby-core:94805] " sammomichael
2019-09-06 17:28 ` [ruby-core:94807] " sammomichael
2019-09-21 21:46 ` [ruby-core:95026] " sammomichael
2019-09-22 10:09 ` [ruby-core:95031] " nobu
2019-09-22 16:47 ` [ruby-core:95035] " eregontp
2019-09-23 21:16 ` [ruby-core:95047] " sammomichael
2019-09-23 22:06 ` [ruby-core:95049] " sammomichael
2019-09-29 2:26 ` [ruby-core:95144] " sammomichael
2019-10-03 17:57 ` [ruby-core:95200] " sammomichael
2019-10-04 0:41 ` sammomichael [this message]
2019-10-07 13:01 ` [ruby-core:95259] " sammomichael
2019-10-07 18:35 ` [ruby-core:95262] " sammomichael
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-81887.20191004004133.4679dde17b57dc1e@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).