[#105450] [Ruby master Feature#18228] Add a `timeout` option to `IO.copy_stream` — "byroot (Jean Boussier)" <noreply@...>
SXNzdWUgIzE4MjI4IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGJ5cm9vdCAoSmVhbiBCb3Vzc2llciku
11 messages
2021/09/27
[ruby-core:105354] [Ruby master Feature#18181] Introduce Enumerable#min_with_value, max_with_value, and minmax_with_value
From:
"kyanagi (Kouhei Yanagita)" <noreply@...>
Date:
2021-09-20 15:25:39 UTC
List:
ruby-core #105354
Issue #18181 has been reported by kyanagi (Kouhei Yanagita).
----------------------------------------
Feature #18181: Introduce Enumerable#min_with_value, max_with_value, and minmax_with_value
https://bugs.ruby-lang.org/issues/18181
* Author: kyanagi (Kouhei Yanagita)
* Status: Open
* Priority: Normal
----------------------------------------
PR is https://github.com/ruby/ruby/pull/4874
I propose `Enumerable#min_with_value`, `max_with_value` and `minmax_with_value`.
These methods work like this:
``` ruby
%w(abcde fg hijk).min_with_value { |e| e.size } # => ['fg', 2]
%w(abcde fg hijk).max_with_value { |e| e.size } # => ['abcde', 5]
%w(abcde fg hijk).minmax_with_value { |e| e.size } # => [['fg', 2], ['abcde', 5]]
```
Motivation:
When I use `Enumerable#min_by`, I sometimes want to get not only the minimum element
but also the value from the given block.
(e.g.: There are many points. Find the nearest point and get distance to it.)
``` ruby
elem = enum.min_by { |e| foo(e) }
value = foo(elem)
```
This works, but I'd like to avoid writing foo() twice. (Consider a more complex case.)
This can be written without repeated foo() like belows, but it is slightly complicated and needs extra arrays.
``` ruby
value, elem = enum.map { |e| [foo(e), e] }.min_by(&:first)
```
If the size of enum is enormous, it is hard to use intermediate arrays.
`Enumerable#min_with_value` solves this problem.
I think `min_with_value` is the best name I could think of, but any suggestions for better names would be appreciated.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>