[#107008] [Ruby master Bug#18465] Make `IO#write` atomic. — "ioquatix (Samuel Williams)" <noreply@...>
Issue #18465 has been reported by ioquatix (Samuel Williams).
16 messages
2022/01/09
[#107150] [Ruby master Feature#18494] [RFC] ENV["RUBY_GC_..."]= changes GC parameters dynamically — "ko1 (Koichi Sasada)" <noreply@...>
Issue #18494 has been updated by ko1 (Koichi Sasada).
4 messages
2022/01/17
[#107170] Re: [Ruby master Feature#18494] [RFC] ENV["RUBY_GC_..."]= changes GC parameters dynamically
— Eric Wong <normalperson@...>
2022/01/17
> https://bugs.ruby-lang.org/issues/18494
[#107302] [Ruby master Bug#18553] Memory leak on compiling method call with kwargs — "ibylich (Ilya Bylich)" <noreply@...>
Issue #18553 has been reported by ibylich (Ilya Bylich).
4 messages
2022/01/27
[#107346] [Ruby master Misc#18557] DevMeeting-2022-02-17 — "mame (Yusuke Endoh)" <noreply@...>
Issue #18557 has been reported by mame (Yusuke Endoh).
18 messages
2022/01/29
[ruby-core:106966] [Ruby master Feature#18181] Introduce Enumerable#min_with_value, max_with_value, and minmax_with_value
From:
"shan (Shannon Skipper)" <noreply@...>
Date:
2022-01-04 22:53:29 UTC
List:
ruby-core #106966
Issue #18181 has been updated by shan (Shannon Skipper).
For a single value, a "then" suffix might be nice.
``` ruby
%w(abcde fg hijk).min_by(&:size)
#=> "fg"
%w(abcde fg hijk).min_by_then(&:size)
#=> 2
```
It mirrors #min_by and #then with the same block.
``` ruby
%w(abcde fg hijk).min_by(&:size).then(&:size)
#=> 2
```
----------------------------------------
Feature #18181: Introduce Enumerable#min_with_value, max_with_value, and minmax_with_value
https://bugs.ruby-lang.org/issues/18181#change-95794
* 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]]
```
Corresponding to `#min(n)`, an integer argument can be passed to `#min_with_value` or `#max_with_value`.
``` ruby
%w(abcde fg hijk).min_with_value(2) { |e| e.size } # => [['fg', 2], ['hijk', 4]]
%w(abcde fg hijk).max_with_value(2) { |e| e.size } # => [['abcde', 5], ['hijk', 4]]
```
## 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.
## Benchmark
https://bugs.ruby-lang.org/issues/18181#note-2
## Example
Solving a traveling salesman problem in nearest neighbor algorithm.
``` ruby
require 'set'
Point = Struct.new(:x, :y)
points = Set.new([Point.new(1, 1), Point.new(2, 4), Point.new(3, 3), Point.new(2, 2), Point.new(0, 1)])
total = 0
current = points.first
points.delete(current)
path = [current]
until points.empty?
current, distance = points.min_with_value do |point|
Math.hypot(current.x - point.x, current.y - point.y)
end
total += distance
points.delete(current)
path << current
end
p path
p total
```
--
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>