ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: duerst@it.aoyama.ac.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95483] [Ruby master Feature#16261] Enumerable#each_tuple
Date: Tue, 22 Oct 2019 22:52:04 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82255.20191022225203.6125fccfbeb9bb1b@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16261.20191018101149@ruby-lang.org

Issue #16261 has been updated by duerst (Martin Dürst).


Dan0042 (Daniel DeLorme) wrote:
> It's worth pointing out the desired difference with regards to lambdas a bit more explicitly:
> 
> ```ruby
> [1, 2, 3].zip([4, 5, 6]).map(&:+)            # ArgumentError (wrong number of arguments (given 0, expected 1))
> [1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9]
> ```

What you want to do here is in many other languages done with `zip_with`:

```ruby
[1, 2, 3].zip_with([4, 5, 6], :+) # => [5, 7, 9]
```

There is already an issue for this, issue #4539, which is open and waits for Matz's approval.

----------------------------------------
Feature #16261: Enumerable#each_tuple
https://bugs.ruby-lang.org/issues/16261#change-82255

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
New method proposal.

Prototype code:
```ruby
module Enumerable
  def each_tuple
    return to_enum(__method__) unless block_given?
    each { |item| yield(*item) } # unpacking possible array into several args
  end
end
```

Supposed documentation/explanation:

> For enumerable with Array items, passes all items in the block provided as a separate arguments. t could be useful if the provided block has lambda semantics, e.g. doesn't unpack arguments automatically. For example:

  ```ruby
files = ["README.md", "LICENSE.txt", "Contributing.md"]
content = [fetch_readme, fetch_license, fetch_contributing] # somehow make a content for the files

files.zip(content).each_tuple(&File.:write) # writes to each file its content
```

> When no block passed, returns enumerator of the tuples:

  ```ruby
[1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9] 
```



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

  parent reply	other threads:[~2019-10-22 22:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16261.20191018101149@ruby-lang.org>
2019-10-18 10:11 ` [ruby-core:95419] [Ruby master Feature#16261] Enumerable#each_tuple zverok.offline
2019-10-18 13:58 ` [ruby-core:95422] " shevegen
2019-10-18 20:26 ` [ruby-core:95424] " shannonskipper
2019-10-22 13:44 ` [ruby-core:95464] " daniel
2019-10-22 14:10 ` [ruby-core:95465] " zverok.offline
2019-10-22 18:01 ` [ruby-core:95473] " daniel
2019-10-22 19:33 ` [ruby-core:95477] " eregontp
2019-10-22 20:52 ` [ruby-core:95479] " daniel
2019-10-22 22:52 ` duerst [this message]
2019-10-23  7:33 ` [ruby-core:95493] " zverok.offline
2019-11-28  6:24 ` [ruby-core:96010] [Ruby master Feature#16261] Enumerable#each_splat and Enumerator#splat knu
2019-11-28  8:15 ` [ruby-core:96015] " matz

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-82255.20191022225203.6125fccfbeb9bb1b@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).