https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-06-10T13:12:23ZRuby Issue Tracking SystemRuby master - Feature #16946: Add an `intersperse` methodhttps://bugs.ruby-lang.org/issues/16946?journal_id=860632020-06-10T13:12:23Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Interesting idea.</p>
<p>In particular that use case:</p>
<p>'Hello'.intersperse('-') # => "H-e-l-l-o"</p>
<p>I actually had that use case every now and then, in a related manner.</p>
<p>For example in bioinformatics, you may have a long nucleotide sequence of A T C G<br>
stored in flat files (usually).</p>
<p>Then, you may wish to show a given sequence like:</p>
<pre><code>ATCAGGCAT
</code></pre>
<p>I tend to modify it via:</p>
<pre><code>ATC|AGG|CAT
</code></pre>
<p>for display purposes sometimes. This makes it easier to<br>
show where a new codon (triplet) begins. (I also show<br>
numbers on top, to make this even easier to count visually.)</p>
<p>So the '|' in the above example I tend to use sometimes because<br>
it simply makes it easier to read the result.</p>
<p>(I am aware that your suggestion is to split on every char by<br>
default, so it would be A|T|C etc... instead, but we could keep<br>
this method flexible, and allow both at how many chars it may<br>
add the intersperse-character, in addition to specifying which<br>
character it is, such as '-','|' or '|'.)</p>
<p>For example:</p>
<pre><code>"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
</code></pre>
<blockquote>
<p>I'm aware that I can achieve the above with built-in methods, but it's quite<br>
cumbersome: (requiring regular expressions / intermediate arrays)</p>
</blockquote>
<p>Not sure if it is cumbersome; I do it currently as-is. But even if it is not<br>
cumbersome, I think your proposal still has merit.</p>
<p>So personally I am slightly in favour of it. I should add that I don't that often<br>
have a need for the described use case, but sometimes I have. Then I tend to<br>
google for stack overflow answers and copy/paste them ... I am only half-kidding<br>
actually. :P</p>
<p>May be useful if other folks can say whether they have had a use case; this may<br>
help the ruby team to assess how beneficial such a method would be, objectively.</p>
<p>(I have no particular opinion on the method on Arrays though. Not sure if I had a<br>
use case for Arrays, but for Strings, most definitely. I am also curious what<br>
sawa thinks of the idea if he is still active on the issue tracker.)</p> Ruby master - Feature #16946: Add an `intersperse` methodhttps://bugs.ruby-lang.org/issues/16946?journal_id=860802020-06-11T00:58:39Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Hello, this request sounds interesting to me.</p>
<p>sos4nt (Stefan Schüßler) wrote:</p>
<blockquote>
<p>Haskell has an <code>intersperse</code> function which adds a separator between elements of a list.</p>
<p>It would be pretty useful to have such method(s) in Ruby, too.</p>
</blockquote>
<p>This is the key point. Can you show us the "pretty useful"-ness a bit more, possibly by telling us how it is practically used in Haskell?</p> Ruby master - Feature #16946: Add an `intersperse` methodhttps://bugs.ruby-lang.org/issues/16946?journal_id=860822020-06-11T01:47:08Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>The example for <code>Array</code> looks simple, but it would be more complicated for <code>String</code>.<br>
Is it OK by "char", not by "grapheme cluster"?</p> Ruby master - Feature #16946: Add an `intersperse` methodhttps://bugs.ruby-lang.org/issues/16946?journal_id=991432022-09-15T11:21:41ZVegard (Vegard Itland)
<ul></ul><p>Just came across this while attempting to see if there was an elegant way to do this. My use case is that I want to generate an array with separator elements -- specifically, I want to generate a nested array of "cellable" objects in Prawn, interspersed with empty rows, for use with the <code>table</code> method. My ideal code is something like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">input</span><span class="p">)</span>
<span class="n">rows</span> <span class="o">=</span> <span class="n">input</span><span class="p">.</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="nb">method</span><span class="p">(</span><span class="ss">:to_table_row</span><span class="p">)).</span><span class="nf">intersperse</span><span class="p">(</span><span class="n">empty_row</span><span class="p">)</span>
<span class="n">pdf</span><span class="p">.</span><span class="nf">table</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>
<span class="k">end</span>
<span class="c1"># dummy implementation</span>
<span class="k">def</span> <span class="nf">to_table_row</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>
<span class="p">[</span>
<span class="p">{</span> <span class="ss">content: </span><span class="s2">"Description"</span> <span class="p">},</span>
<span class="p">{</span> <span class="ss">content: </span><span class="n">element</span> <span class="p">},</span>
<span class="p">]</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">empty_row</span>
<span class="p">[{</span> <span class="ss">content: </span><span class="s2">""</span><span class="p">,</span> <span class="ss">colspan: </span><span class="mi">2</span><span class="p">,</span> <span class="ss">borders: </span><span class="sx">%i[top]</span><span class="p">,</span> <span class="ss">height: </span><span class="mi">10</span> <span class="p">}]</span>
<span class="k">end</span>
</code></pre>
<p>Of course it's trivial to make some sort of method that does this, or just inline it, but I thought I'd pitch in with a use case :)</p>