From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.8 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id EA7F61F4BD for ; Thu, 3 Oct 2019 17:58:06 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 9E5CF120A32; Fri, 4 Oct 2019 02:57:56 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 13B0E12091B for ; Fri, 4 Oct 2019 02:57:53 +0900 (JST) Received: by filter0073p3iad2.sendgrid.net with SMTP id filter0073p3iad2-28379-5D9636A2-9 2019-10-03 17:57:54.167778557 +0000 UTC m=+169591.059566814 Received: from herokuapp.com (unknown [54.161.249.108]) by ismtpd0069p1iad1.sendgrid.net (SG) with ESMTP id Rus8OpvBQoa_IQ8VfFyPZg for ; Thu, 03 Oct 2019 17:57:54.053 +0000 (UTC) Date: Thu, 03 Oct 2019 17:57:54 +0000 (UTC) From: sammomichael@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70787 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16147 X-Redmine-Issue-Author: sammomichael X-Redmine-Sender: sammomichael X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?slqsTT=2F+PuUcADLY9nggyMdJj+djGZd32YYUR+KNBmZulH8HUiQgmIDlQyyCWj?= =?us-ascii?Q?3y8SDo0JESmJJpyixJwQiZxDDeD9izh7j6D=2Fr87?= =?us-ascii?Q?NlAgc3f+08FwabfmQVv8TLVvUuxqe5KUxrCd0Aa?= =?us-ascii?Q?GjcWtTgFdGB7pjXX2TU3rzPhI5YQMmskACFSDlq?= =?us-ascii?Q?2LgLfCRltDbtIxm02v8PDqIFJI3EHo9Mmow=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95200 Subject: [ruby-core:95200] [Ruby master Feature#16147] List Comprehensions in Ruby X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16147 has been updated by sammomichael (Samuel Michael). 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. > > 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-81876 * 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/