Issue #16946 has been reported by sos4nt (Stefan Sch=FC=DFler).

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

* 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: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>