ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / Atom feed
From: marcandre-ruby-core@marc-andre.ca
To: ruby-core@ruby-lang.org
Subject: [ruby-core:99425] [Ruby master Feature#17097] `map_min`, `map_max`
Date: Sat, 01 Aug 2020 00:56:32 +0000 (UTC)
Message-ID: <redmine.journal-86878.20200801005631.2963@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-17097.20200731123339.2963@ruby-lang.org>

Issue #17097 has been updated by marcandre (Marc-Andre Lafortune).


@nobu is right, we're not going to add `map_` for everything.

Eregon (Benoit Daloze) wrote in #note-6:
> Then repeating it is not elegant and is duplicated code.

I don't see why there would be repetition.

Just do `enum.map { very_long_expression }.max`...

Please benchmark the time it takes to generate the intermediate array. My guess is that it's typically negligible.



----------------------------------------
Feature #17097: `map_min`, `map_max`
https://bugs.ruby-lang.org/issues/17097#change-86878

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
`min`, `min_by`, `max`, `max_by` return the element that leads to the minimum or the maximum value, but I think it is as, or even more, frequent that we are interested in the minimum or the maximum value itself rather than the element. For example, to get the length of the longest string in an array, we do:

```ruby
%w[aa b cccc dd].max_by(&:length).length # => 4
%w[aa b cccc dd].map(&:length).max # => 4
```

I propose to have methods that return the minimum or the maximum value. Temporarily calling them `map_min`, `map_max`, they should work like this:

```ruby
%w[aa b cccc dd].map_max(&:length) # => 4
```

`map_min`, `map_max` are implementation-centered names, so perhaps better names should replace them, just like `yield_self` was replaced by `then`.



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

  parent reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-31 12:33 [ruby-core:99418] " sawadatsuyoshi
2020-07-31 12:37 ` [ruby-core:99419] " nobu
2020-07-31 13:51 ` [ruby-core:99420] " grzegorz.jakubiak
2020-07-31 18:03 ` [ruby-core:99421] " eregontp
2020-07-31 18:06 ` [ruby-core:99422] " eregontp
2020-08-01  0:56 ` marcandre-ruby-core [this message]
2020-08-01  3:53 ` [ruby-core:99428] " sawadatsuyoshi

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

ruby-core@ruby-lang.org archive (unofficial mirror)

Archives are clonable: git clone --mirror https://public-inbox.org/ruby-core

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.core
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.core
	nntp://news.gmane.io/gmane.comp.lang.ruby.core

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git