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-Status: No, score=-2.6 required=3.0 tests=AWL,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,UNPARSEABLE_RELAY 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 B49351F55B for ; Wed, 10 Jun 2020 13:12:33 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 086901209B1; Wed, 10 Jun 2020 22:12:03 +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 3D663120992 for ; Wed, 10 Jun 2020 22:11:59 +0900 (JST) Received: by filterdrecv-p3iad2-784dbb6bd8-pfkxc with SMTP id filterdrecv-p3iad2-784dbb6bd8-pfkxc-19-5EE0DC37-31 2020-06-10 13:12:23.271578681 +0000 UTC m=+579928.591735255 Received: from herokuapp.com (unknown) by ismtpd0020p1iad2.sendgrid.net (SG) with ESMTP id Yk9G-kjkTlKv732-AEsnBQ for ; Wed, 10 Jun 2020 13:12:23.246 +0000 (UTC) Date: Wed, 10 Jun 2020 13:12:23 +0000 (UTC) From: shevegen@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74504 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 16946 X-Redmine-Issue-Author: sos4nt X-Redmine-Sender: shevegen 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?6lbdtOg4RDRLuxD00eQtQKgoNAsge5d4xND7cbMQd0yY0ygdmjE0AJWZdmq9ss?= =?us-ascii?Q?OGjJIosdM3DmlmQ6DsXkIULOlz4MkYEL1saHyRw?= =?us-ascii?Q?eS8rrfHCAkTPgjmzDGxw4W2dO=2FhuqGslIZfIyUF?= =?us-ascii?Q?7YcbmhbREHRcm=2FrWT=2F1POSkTbHI9ZbZjgmttxEg?= =?us-ascii?Q?yavB+d77bC3+fHRv8mOKRbY9TjyWcew4gELaQTp?= =?us-ascii?Q?MNMGZaZ3fmyR6A8AM=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 98712 Subject: [ruby-core:98712] [Ruby master Feature#16946] Add an `intersperse` method 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="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16946 has been updated by shevegen (Robert A. Heiler). Interesting idea. In particular that use case: 'Hello'.intersperse('-') # =3D> "H-e-l-l-o" I actually had that use case every now and then, in a related manner. For example in bioinformatics, you may have a long nucleotide sequence of A= T C G stored in flat files (usually). Then, you may wish to show a given sequence like: ATCAGGCAT I tend to modify it via: ATC|AGG|CAT for display purposes sometimes. This makes it easier to = show where a new codon (triplet) begins. (I also show numbers on top, to make this even easier to count visually.) So the '|' in the above example I tend to use sometimes because it simply makes it easier to read the result. (I am aware that your suggestion is to split on every char by default, so it would be A|T|C etc... instead, but we could keep this method flexible, and allow both at how many chars it may add the intersperse-character, in addition to specifying which character it is, such as '-','|' or '|'.) For example: "ATGCCG".intersperse('|', 3) # Or something like that; I may have # miscounted but the 3 would mean to # split at every 3 chars; default is # 1 for every char. And if I miscounted # then it would be 2, and 0. Off by one # errors ... :P > I'm aware that I can achieve the above with built-in methods, but it's qu= ite > cumbersome: (requiring regular expressions / intermediate arrays) Not sure if it is cumbersome; I do it currently as-is. But even if it is not cumbersome, I think your proposal still has merit. So personally I am slightly in favour of it. I should add that I don't that= often have a need for the described use case, but sometimes I have. Then I tend to google for stack overflow answers and copy/paste them ... I am only half-ki= dding actually. :P May be useful if other folks can say whether they have had a use case; this= may help the ruby team to assess how beneficial such a method would be, objecti= vely. (I have no particular opinion on the method on Arrays though. Not sure if I= had a use case for Arrays, but for Strings, most definitely. I am also curious wh= at sawa thinks of the idea if he is still active on the issue tracker.) ---------------------------------------- Feature #16946: Add an `intersperse` method https://bugs.ruby-lang.org/issues/16946#change-86063 * Author: sos4nt (Stefan Sch=FC=DFler) * Status: Open * Priority: Normal ---------------------------------------- Haskell has an `intersperse` function which adds a separator between elemen= ts of a list. It would be pretty useful to have such method(s) in Ruby, too. Examples for `Array` and `String`: ```ruby [1, 2, 3].intersperse(0) #=3D> [1, 0, 2, 0, 3] 'Hello'.intersperse('-') #=3D> "H-e-l-l-o" ``` I'm aware that I can achieve the above with built-in methods, but it's quit= e cumbersome: (requiring regular expressions / intermediate arrays) ```ruby [1, 2, 3].flat_map { |i| [i, 0] }[0...-1] #=3D> [1, 0, 2, 0, 3] 'Hello'.gsub(/(?<=3D.)./, '-\0') #=3D> "H-e-l-l-o" 'Hello'.chars.join('-') #=3D> "H-e-l-l-o" ``` -- = https://bugs.ruby-lang.org/ Unsubscribe: