ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method
@ 2020-06-10 10:00 mail
  2020-06-10 13:12 ` [ruby-core:98712] " shevegen
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mail @ 2020-06-10 10:00 UTC (permalink / raw)
  To: ruby-core

Issue #16946 has been reported by sos4nt (Stefan Schüßler).

----------------------------------------
Feature #16946: Add an `intersperse` method
https://bugs.ruby-lang.org/issues/16946

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
----------------------------------------
Haskell has an `intersperse` function which adds a separator between elements 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)
#=> [1, 0, 2, 0, 3]

'Hello'.intersperse('-')
#=> "H-e-l-l-o"
```

I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays)

```ruby
[1, 2, 3].flat_map { |i| [i, 0] }[0...-1]
#=> [1, 0, 2, 0, 3]

'Hello'.gsub(/(?<=.)./, '-\0')
#=> "H-e-l-l-o"

'Hello'.chars.join('-')
#=> "H-e-l-l-o"
```




-- 
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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:98712] [Ruby master Feature#16946] Add an `intersperse` method
  2020-06-10 10:00 [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method mail
@ 2020-06-10 13:12 ` shevegen
  2020-06-11  0:58 ` [ruby-core:98724] " shyouhei
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: shevegen @ 2020-06-10 13:12 UTC (permalink / raw)
  To: ruby-core

Issue #16946 has been updated by shevegen (Robert A. Heiler).


Interesting idea.

In particular that use case:

  'Hello'.intersperse('-') # => "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 quite
> 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-kidding
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, objectively.

(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 what
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üßler)
* Status: Open
* Priority: Normal
----------------------------------------
Haskell has an `intersperse` function which adds a separator between elements 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)
#=> [1, 0, 2, 0, 3]

'Hello'.intersperse('-')
#=> "H-e-l-l-o"
```

I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays)

```ruby
[1, 2, 3].flat_map { |i| [i, 0] }[0...-1]
#=> [1, 0, 2, 0, 3]

'Hello'.gsub(/(?<=.)./, '-\0')
#=> "H-e-l-l-o"

'Hello'.chars.join('-')
#=> "H-e-l-l-o"
```




-- 
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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:98724] [Ruby master Feature#16946] Add an `intersperse` method
  2020-06-10 10:00 [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method mail
  2020-06-10 13:12 ` [ruby-core:98712] " shevegen
@ 2020-06-11  0:58 ` shyouhei
  2020-06-11  1:47 ` [ruby-core:98726] " nobu
  2022-09-15 11:21 ` [ruby-core:109900] " Vegard (Vegard Itland)
  3 siblings, 0 replies; 5+ messages in thread
From: shyouhei @ 2020-06-11  0:58 UTC (permalink / raw)
  To: ruby-core

Issue #16946 has been updated by shyouhei (Shyouhei Urabe).


Hello, this request sounds interesting to me.

sos4nt (Stefan Schüßler) wrote:
> Haskell has an `intersperse` function which adds a separator between elements of a list.
> 
> It would be pretty useful to have such method(s) in Ruby, too.

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?

----------------------------------------
Feature #16946: Add an `intersperse` method
https://bugs.ruby-lang.org/issues/16946#change-86080

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
----------------------------------------
Haskell has an `intersperse` function which adds a separator between elements 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)
#=> [1, 0, 2, 0, 3]

'Hello'.intersperse('-')
#=> "H-e-l-l-o"
```

I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays)

```ruby
[1, 2, 3].flat_map { |i| [i, 0] }[0...-1]
#=> [1, 0, 2, 0, 3]

'Hello'.gsub(/(?<=.)./, '-\0')
#=> "H-e-l-l-o"

'Hello'.chars.join('-')
#=> "H-e-l-l-o"
```




-- 
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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:98726] [Ruby master Feature#16946] Add an `intersperse` method
  2020-06-10 10:00 [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method mail
  2020-06-10 13:12 ` [ruby-core:98712] " shevegen
  2020-06-11  0:58 ` [ruby-core:98724] " shyouhei
@ 2020-06-11  1:47 ` nobu
  2022-09-15 11:21 ` [ruby-core:109900] " Vegard (Vegard Itland)
  3 siblings, 0 replies; 5+ messages in thread
From: nobu @ 2020-06-11  1:47 UTC (permalink / raw)
  To: ruby-core

Issue #16946 has been updated by nobu (Nobuyoshi Nakada).


The example for `Array` looks simple, but it would be more complicated for `String`.
Is it OK by "char", not by "grapheme cluster"?

----------------------------------------
Feature #16946: Add an `intersperse` method
https://bugs.ruby-lang.org/issues/16946#change-86082

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
----------------------------------------
Haskell has an `intersperse` function which adds a separator between elements 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)
#=> [1, 0, 2, 0, 3]

'Hello'.intersperse('-')
#=> "H-e-l-l-o"
```

I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays)

```ruby
[1, 2, 3].flat_map { |i| [i, 0] }[0...-1]
#=> [1, 0, 2, 0, 3]

'Hello'.gsub(/(?<=.)./, '-\0')
#=> "H-e-l-l-o"

'Hello'.chars.join('-')
#=> "H-e-l-l-o"
```




-- 
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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:109900] [Ruby master Feature#16946] Add an `intersperse` method
  2020-06-10 10:00 [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method mail
                   ` (2 preceding siblings ...)
  2020-06-11  1:47 ` [ruby-core:98726] " nobu
@ 2022-09-15 11:21 ` Vegard (Vegard Itland)
  3 siblings, 0 replies; 5+ messages in thread
From: Vegard (Vegard Itland) @ 2022-09-15 11:21 UTC (permalink / raw)
  To: ruby-core

Issue #16946 has been updated by Vegard (Vegard Itland).


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 `table` method. My ideal code is something like this:

``` ruby
def call(input)
  rows = input.map(&method(:to_table_row)).intersperse(empty_row)
  pdf.table(rows)
end

# dummy implementation
def to_table_row(element)
  [
    { content: "Description" },
    { content: element },
  ]
end

def empty_row
  [{ content: "", colspan: 2, borders: %i[top], height: 10 }]
end
```

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 :)

----------------------------------------
Feature #16946: Add an `intersperse` method
https://bugs.ruby-lang.org/issues/16946#change-99143

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
----------------------------------------
Haskell has an `intersperse` function which adds a separator between elements 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)
#=> [1, 0, 2, 0, 3]

'Hello'.intersperse('-')
#=> "H-e-l-l-o"
```

I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays)

```ruby
[1, 2, 3].flat_map { |i| [i, 0] }[0...-1]
#=> [1, 0, 2, 0, 3]

'Hello'.gsub(/(?<=.)./, '-\0')
#=> "H-e-l-l-o"

'Hello'.chars.join('-')
#=> "H-e-l-l-o"
```




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-15 11:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-10 10:00 [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method mail
2020-06-10 13:12 ` [ruby-core:98712] " shevegen
2020-06-11  0:58 ` [ruby-core:98724] " shyouhei
2020-06-11  1:47 ` [ruby-core:98726] " nobu
2022-09-15 11:21 ` [ruby-core:109900] " Vegard (Vegard Itland)

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).