From: "Vegard (Vegard Itland)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109900] [Ruby master Feature#16946] Add an `intersperse` method
Date: Thu, 15 Sep 2022 11:21:42 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-99143.20220915112141.7176@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16946.20200610100008.7176@ruby-lang.org
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/
prev parent reply other threads:[~2022-09-15 11:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Vegard (Vegard Itland) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-99143.20220915112141.7176@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).