ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max`
@ 2020-07-31 12:33 sawadatsuyoshi
  2020-07-31 12:37 ` [ruby-core:99419] " nobu
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: sawadatsuyoshi @ 2020-07-31 12:33 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been reported by sawa (Tsuyoshi Sawada).

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

* 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].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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99419] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
@ 2020-07-31 12:37 ` nobu
  2020-07-31 13:51 ` [ruby-core:99420] " grzegorz.jakubiak
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: nobu @ 2020-07-31 12:37 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by nobu (Nobuyoshi Nakada).


Then we'll need `map_` versions for all `Enumerable` methods.

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

* 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].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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99420] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
  2020-07-31 12:37 ` [ruby-core:99419] " nobu
@ 2020-07-31 13:51 ` grzegorz.jakubiak
  2020-07-31 18:03 ` [ruby-core:99421] " eregontp
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: grzegorz.jakubiak @ 2020-07-31 13:51 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by greggzst (Grzegorz Jakubiak).


nobu (Nobuyoshi Nakada) wrote in #note-1:
> Then we'll need `map_` versions for all `Enumerable` methods.

Exactly, I don't see any good use case in that apart from being lazy and just using one method call. It seems to me that this kind of proposal is too much. I mean there are more pressing issues or features of the language that the team has to focus on instead of dealing with proposals like `map_tally` or whatever.

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

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99421] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` 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 ` eregontp
  2020-07-31 18:06 ` [ruby-core:99422] " eregontp
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: eregontp @ 2020-07-31 18:03 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by Eregon (Benoit Daloze).


I actually regularly wanted such behavior, but min_by/max_by do not return the result of the block but the element (which makes sense).
And `.map {}.min/max` works but is inefficient by generating an intermediate Array.

Maybe we should add some keyword argument to min_by/max_by?

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

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99422] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (2 preceding siblings ...)
  2020-07-31 18:03 ` [ruby-core:99421] " eregontp
@ 2020-07-31 18:06 ` eregontp
  2020-08-01  0:56 ` [ruby-core:99425] " marcandre-ruby-core
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: eregontp @ 2020-07-31 18:06 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by Eregon (Benoit Daloze).


To put in context, consider that the expression might be much longer than `.length`.
Then repeating it is not elegant and is duplicated code.

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

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99425] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (3 preceding siblings ...)
  2020-07-31 18:06 ` [ruby-core:99422] " eregontp
@ 2020-08-01  0:56 ` marcandre-ruby-core
  2020-08-01  3:53 ` [ruby-core:99428] " sawadatsuyoshi
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: marcandre-ruby-core @ 2020-08-01  0:56 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:99428] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (4 preceding siblings ...)
  2020-08-01  0:56 ` [ruby-core:99425] " marcandre-ruby-core
@ 2020-08-01  3:53 ` sawadatsuyoshi
  2021-01-13  1:24 ` [ruby-core:102041] " universato
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: sawadatsuyoshi @ 2020-08-01  3:53 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by sawa (Tsuyoshi Sawada).


I do not understand why the proposal has to be extended to all other `Enumerable` methods.

My point is semantic. I do not see many use cases where I am interested in the element that is related to the min/max value but am not interested in the min/max value.

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

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:102041] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (5 preceding siblings ...)
  2020-08-01  3:53 ` [ruby-core:99428] " sawadatsuyoshi
@ 2021-01-13  1:24 ` universato
  2022-12-23  2:14 ` [ruby-core:111387] " jnchito (Junichi Ito) via ruby-core
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: universato @ 2021-01-13  1:24 UTC (permalink / raw)
  To: ruby-core

Issue #17097 has been updated by universato (Yoshimine Sato).


https://github.com/crystal-lang/crystal/pull/365
In Crystal language, these proposed methods are called `max_of`, `min_of`.
Note: Crystal has `max`, `max_by`, `max_of`.

This feature may not be innovative, but I think many Ruby users have the opportunity to use these methods.

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

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:111387] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (6 preceding siblings ...)
  2021-01-13  1:24 ` [ruby-core:102041] " universato
@ 2022-12-23  2:14 ` jnchito (Junichi Ito) via ruby-core
  2022-12-23  4:30 ` [ruby-core:111390] " baweaver (Brandon Weaver) via ruby-core
  2022-12-23 17:19 ` [ruby-core:111406] " Eregon (Benoit Daloze) via ruby-core
  9 siblings, 0 replies; 11+ messages in thread
From: jnchito (Junichi Ito) via ruby-core @ 2022-12-23  2:14 UTC (permalink / raw)
  To: ruby-core; +Cc: jnchito (Junichi Ito)

Issue #17097 has been updated by jnchito (Junichi Ito).


`max_of` method is very elegant! I really want the Ruby version of this method. 

For example, I need to write `column_width = @ls_files.map { |ls_file| ls_file.name.size }.max` to determine column width according to the longest file name (in the image below, it would be 19 because "credentials.yml.enc" is the longest, and see also [this](https://github.com/JunichiIto/ruby-practices/pull/2/files#diff-50ea26a97f1139dc7a41d65762e37a2ba50a801e32fb6af28b4a6f6263ed3b97R11))

![sample](https://user-images.githubusercontent.com/1148320/209256935-e849003a-1ac1-4244-a4c8-d21cb53aa5c5.png)

I'd be pretty if I could write `column_width = @ls_files.max_of { |ls_file| ls_file.name.size }` .

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

* 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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:111390] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (7 preceding siblings ...)
  2022-12-23  2:14 ` [ruby-core:111387] " jnchito (Junichi Ito) via ruby-core
@ 2022-12-23  4:30 ` baweaver (Brandon Weaver) via ruby-core
  2022-12-23 17:19 ` [ruby-core:111406] " Eregon (Benoit Daloze) via ruby-core
  9 siblings, 0 replies; 11+ messages in thread
From: baweaver (Brandon Weaver) via ruby-core @ 2022-12-23  4:30 UTC (permalink / raw)
  To: ruby-core; +Cc: baweaver (Brandon Weaver)

Issue #17097 has been updated by baweaver (Brandon Weaver).


For the sake of naming conventions I would personally lean towards `map_max` to match methods like `filter_map`, and while this does not necessarily extend to _all_ Enumerable methods I would wonder if there's another conversation later to be had about composite Enumerable methods.

On this particular ticket though I believe that the use-case is common enough, and in many cases may be what the developer intends.

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

* 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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:111406] [Ruby master Feature#17097] `map_min`, `map_max`
  2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
                   ` (8 preceding siblings ...)
  2022-12-23  4:30 ` [ruby-core:111390] " baweaver (Brandon Weaver) via ruby-core
@ 2022-12-23 17:19 ` Eregon (Benoit Daloze) via ruby-core
  9 siblings, 0 replies; 11+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2022-12-23 17:19 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #17097 has been updated by Eregon (Benoit Daloze).


IMHO we should just add `max_of` and `min_of`, like Crystal has.
`map` in the name feels out of place here because this method does not return a new array, it's just execute the block and remember the max/min value returned by the block for all elements.

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

* 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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-12-23 17:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` 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 ` [ruby-core:99425] " marcandre-ruby-core
2020-08-01  3:53 ` [ruby-core:99428] " sawadatsuyoshi
2021-01-13  1:24 ` [ruby-core:102041] " universato
2022-12-23  2:14 ` [ruby-core:111387] " jnchito (Junichi Ito) via ruby-core
2022-12-23  4:30 ` [ruby-core:111390] " baweaver (Brandon Weaver) via ruby-core
2022-12-23 17:19 ` [ruby-core:111406] " Eregon (Benoit Daloze) via ruby-core

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