[ruby-trunk - Feature #5549][Open] Comparable#min, Comparable#max

Issue #5549 has been reported by Tsuyoshi Sawada.


Feature #5549: Comparable#min, Comparable#max

Author: Tsuyoshi Sawada
Status: Open
Priority: Normal
Assignee:
Category:
Target version:

I request the following methods

Comparable#min(other)
Comparable#max(other)

which will work the same as

[self, other].min
[self, other].max

and can be defined as

module Comparable

def min other; [self, other].min end
def max other; [self, other].max end
end

Example use case: If you need to ensure that some calculated value
becomes at least zero within a method chain.

Issue #5549 has been updated by Yukihiro M…

Status changed from Open to Rejected

なぜ、それが必要だと思いましたか?
この提案だと a.min(b)
というスタイルになり、Enumerableの「もっとも小さい(大きい)要素を返す」という意味でなく、「ふたつの要素のうちより小さい(大きい)ものを返す」と記述しています。そのような記述のための語彙としてはmin,maxではなく、比較演算子

, < を使うのだと思いますし、そのメソッドはすでにComparableに定義されています。


Feature #5549: Comparable#min, Comparable#max

Author: Tsuyoshi Sawada
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:

I request the following methods

Comparable#min(other)
Comparable#max(other)

which will work the same as

[self, other].min
[self, other].max

and can be defined as

module Comparable

def min other; [self, other].min end
def max other; [self, other].max end
end

Example use case: If you need to ensure that some calculated value
becomes at least zero within a method chain.

$B1sF#$G$9!#(B

2011$BG/(B11$B7n(B7$BF|(B11:34 Nobuyoshi N. [email protected]:

$B$?$@!“$3$NNc$G$O(Bmin,max$B$H$$$&L>A0$O$”$^$j$U$5$o$7$/$J$$$h$&$K;W(B
$B$$$^$9!#%5%A%e%l!<%7%g%s$H$+%/%j%C%T%s%0$H$+8F$P$l$k$3$H$,B?$$5$(B
$B$,$7$^$9!#(B

some_method.clip_by(0…100)

#4574 $B$G$^$5$K$=$&$$$&A:n$r$9$k(B Numeric#within $B$H$$$&%a%=%C%I$r(B $BDs0F$7$^$7$?$,!"$=$3$GJ9$$$?OC$@$H!"$3$&$$$&A:n$O0lHLE*$K(B clamp
$B$H8F$P$l$k$=$&$G$9!#(B

なかだです。

At Thu, 3 Nov 2011 19:52:44 +0900,
Yukihiro M. wrote in [ruby-dev:44793]:

この提案だと a.min(b) というスタイルになり、Enumerableの「もっ
とも小さい(大きい)要素を返す」という意味でなく、「ふたつの要素
のうちより小さい(大きい)ものを返す」と記述しています。そのよう
な記述のための語彙としてはmin,maxではなく、比較演算子 >, < を
使うのだと思いますし、そのメソッドはすでにComparableに定義され
ています。

これは

Example use case: If you need to ensure that some calculated
value becomes at least zero within a method chain.

ということで、メソッドチェーンに追加する形で使いたいということが
大きいのでしょう。

some_method.and_another.yet_another.min(0).max(100)

ただ、この例ではmin,maxという名前はあまりふさわしくないように思
います。サチュレーションとかクリッピングとか呼ばれることが多い気
がします。

some_method.clip_by(0…100)

とか。片方向のみの場合にうまくありませんが。