From: shyouhei@... Date: 2020-06-11T00:58:39+00:00 Subject: [ruby-core:98724] [Ruby master Feature#16946] Add an `intersperse` method 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: