[#37730] [Ruby 1.9 - Bug #4962][Open] come back gem_prelude! — Yusuke Endoh <mame@...>

24 messages 2011/07/02

[#37840] [Ruby 1.9 - Feature #4985][Open] Add %S[] support for making a list of symbols — Aaron Patterson <aaron@...>

23 messages 2011/07/07

[#37866] [Backport87 - Feature #4996][Open] About 1.8.7 EOL — Shyouhei Urabe <shyouhei@...>

22 messages 2011/07/08

[#37913] [Ruby 1.9 - Bug #5003][Open] Enumerator#next segfaults in OS X Lion (10.7) — Ganesh Gunasegaran <ganesh.gunas@...>

16 messages 2011/07/09

[#37917] [Ruby 1.9 - Feature #5005][Open] Provide convenient access to original methods — Lazaridis Ilias <ilias@...>

13 messages 2011/07/09

[#37932] [Ruby 1.9 - Feature #5008][Open] Equal rights for Hash (like Array, String, Integer, Float) — Suraj Kurapati <sunaku@...>

31 messages 2011/07/09

[#37936] [Ruby 1.9 - Feature #5010][Open] Add Slop(-like) in stdlib and deprecate current OptionParser API — Rodrigo Rosenfeld Rosas <rr.rosas@...>

29 messages 2011/07/09

[#37968] [Ruby 1.9 - Bug #5015][Open] method_added" is called in addition to "method_undefined — Lazaridis Ilias <ilias@...>

14 messages 2011/07/10

[#38096] [Ruby 1.9 - Feature #5033][Open] PATCH: 1.9: gc_mark_children: Avoid gc_mark() tail recursion, use goto again. — Kurt Stephens <ks.ruby@...>

14 messages 2011/07/16

[#38109] [Ruby 1.9 - Bug #5034][Open] C Source Code formatting — Lazaridis Ilias <ilias@...>

18 messages 2011/07/16

[#38171] [Ruby 1.9 - Bug #5047][Open] Segfault (most likely involving require) — Jack Christensen <jack@...>

21 messages 2011/07/18

[#38182] [Ruby 1.9 - Feature #5054][Open] Compress a sequence of ends — ANDO Yasushi ANDO <andyjpn@...>

68 messages 2011/07/19

[#38197] [Ruby 1.9 - Feature #5056][Open] About 1.9 EOL — Shyouhei Urabe <shyouhei@...>

39 messages 2011/07/19
[#38900] [Ruby 1.9 - Feature #5056] About 1.9 EOL — Shota Fukumori <sorah@...> 2011/08/10

[#38902] Re: [Ruby 1.9 - Feature #5056] About 1.9 EOL — Yukihiro Matsumoto <matz@...> 2011/08/10

Hi,

[#39048] Re: [Ruby 1.9 - Feature #5056] About 1.9 EOL — SASADA Koichi <ko1@...> 2011/08/22

Hi,

[#39055] Re: [Ruby 1.9 - Feature #5056] About 1.9 EOL — Lucas Nussbaum <lucas@...> 2011/08/23

On 23/08/11 at 06:50 +0900, SASADA Koichi wrote:

[#38295] [Ruby 1.9 - Feature #5064][Open] HTTP user-agent class — Eric Hodel <drbrain@...7.net>

15 messages 2011/07/21

[#38391] [Ruby 1.9 - Bug #5076][Open] Mac OS X Lion Support — Yui NARUSE <naruse@...>

17 messages 2011/07/22

[#38503] [Ruby 1.9 - Feature #5096][Open] offer Logger-compatibility for ext — Eric Wong <normalperson@...>

16 messages 2011/07/25

[#38510] [Ruby 1.9 - Feature #5097][Assigned] Supported platforms of Ruby 1.9.3 — Yui NARUSE <naruse@...>

42 messages 2011/07/26

[#38526] [Backport92 - Backport #5099][Open] Backport r31875 load path performance problem — Aaron Patterson <aaron@...>

19 messages 2011/07/26

[#38538] [Ruby 1.9 - Feature #5101][Open] allow optional timeout for TCPSocket.new — Eric Wong <normalperson@...>

15 messages 2011/07/27

[#38610] [Ruby 1.9 - Feature #5120][Open] String#split needs to be logical — Alexey Muranov <muranov@...>

18 messages 2011/07/30

[#38623] [Ruby 1.9 - Feature #5123][Open] Alias Hash 1.9 as OrderedHash — Alexey Muranov <muranov@...>

14 messages 2011/07/31

[ruby-core:37956] [Ruby 1.9 - Feature #4574][Assigned] Numeric#within

From: Motohiro KOSAKI <kosaki.motohiro@...>
Date: 2011-07-10 06:15:28 UTC
List: ruby-core #37956
Issue #4574 has been updated by Motohiro KOSAKI.

Category set to core
Status changed from Open to Assigned
Target version changed from 1.9.3 to 1.9.x

It's too late for 1.9.3.

----------------------------------------
Feature #4574: Numeric#within
http://redmine.ruby-lang.org/issues/4574

Author: Yusuke Endoh
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: core
Target version: 1.9.x


=begin
Hello,

Many people have written programs that limits an integer/float
within a range like:

  v = [[v, min].max, max].min

or

  v = v < min ? min : (v < max ? v : max)

or

  if v < min
    v = min
  elsif max < v
    v = max
  end

.  But these are all too complex in spite of frequent idiom.
"[min, v, max].sort[1]" is cool, but slightly cryptic.


So I propose Numeric#within.

  v = v.within(min, max)

Examples:

  p 0.within(2, 5)  #=> 2
  p 2.within(2, 5)  #=> 2
  p 3.within(2, 5)  #=> 3
  p 5.within(2, 5)  #=> 5
  p 6.within(2, 5)  #=> 6

What do you think?


Some Japanese committers agree with this idea, and proposed
other candidates of method name:

  - Numeric#bound
  - Numeric#limit
  - Numeric#clip
  - Numeric#into
  - Numeric#crop
  - Numeric#trim
  - Range#bound   # usage: (2..5).bound(0)

Anyway, I think that the length should be less than 9, because
it should be shorter than the sort idiom:

  [min, v, max].sort[1]
  v.xxxxxxxxx(min, max)


Here is a patch including both Numeric#within and Range#bound.

diff --git a/numeric.c b/numeric.c
index 34c378b..dad485f 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1600,6 +1600,43 @@ num_round(int argc, VALUE* argv, VALUE num)
     return flo_round(argc, argv, rb_Float(num));
 }
 
+static int
+r_le(VALUE a, VALUE b)
+{
+    int c;
+    VALUE r = rb_funcall(a, rb_intern("<=>"), 1, b);
+
+    if (NIL_P(r))
+	return (int)Qfalse;
+    c = rb_cmpint(r, a, b);
+    if (c == 0)
+	return (int)INT2FIX(0);
+    if (c < 0)
+	return (int)Qtrue;
+    return (int)Qfalse;
+}
+
+/*
+ *  call-seq:
+ *     num.within(min, max)  ->  new_num
+ *
+ *  Bounds <i>num</i> so that <i>min</i> <= new_num <= <i>max</i>.
+ *  Returns min when num < min, max when num > end, otherwise
+ *  num itself.
+ */
+
+static VALUE
+num_within(VALUE num, VALUE min, VALUE max)
+{
+    if (r_le(min, num)) {
+	if (r_le(max, num)) {
+	    return max;
+	}
+	return num;
+    }
+    return min;
+}
+
 /*
  *  call-seq:
  *     num.truncate  ->  integer
@@ -3424,6 +3461,7 @@ Init_Numeric(void)
     rb_define_method(rb_cNumeric, "round", num_round, -1);
     rb_define_method(rb_cNumeric, "truncate", num_truncate, 0);
     rb_define_method(rb_cNumeric, "step", num_step, -1);
+    rb_define_method(rb_cNumeric, "within", num_within, 1);
 
     rb_cInteger = rb_define_class("Integer", rb_cNumeric);
     rb_undef_alloc_func(rb_cInteger);
diff --git a/range.c b/range.c
index 1866df1..2dd99f5 100644
--- a/range.c
+++ b/range.c
@@ -923,6 +923,41 @@ range_cover(VALUE range, VALUE val)
     return Qfalse;
 }
 
+/*
+ *  call-seq:
+ *     rng.bound(val)  ->  new_val
+ *
+ *  Bounds val so that beg <= new_val <= end.  This method returns
+ *  beg when val < ben, end when val > end, otherwise, val itself.
+ *  Raises an exception if val >= end and the range is exclusive.
+ *
+ *     (2..5).bound(0)  #=> 2
+ *     (2..5).bound(2)  #=> 2
+ *     (2..5).bound(3)  #=> 3
+ *     (2..5).bound(5)  #=> 5
+ *     (2..5).bound(6)  #=> 5
+ *     (2...5).bound(6) #=> ArgumentError
+ */
+
+static VALUE
+range_bound(VALUE range, VALUE val)
+{
+    VALUE beg, end;
+
+    beg = RANGE_BEG(range);
+    end = RANGE_END(range);
+    if (r_le(beg, val)) {
+	if (r_le(end, val)) {
+	    if (EXCL(range)) {
+		rb_raise(rb_eArgError, "more than or equal to the excluded range");
+	    }
+	    return end;
+	}
+	return val;
+    }
+    return beg;
+}
+
 static VALUE
 range_dumper(VALUE range)
 {
@@ -1051,4 +1086,5 @@ Init_Range(void)
     rb_define_method(rb_cRange, "member?", range_include, 1);
     rb_define_method(rb_cRange, "include?", range_include, 1);
     rb_define_method(rb_cRange, "cover?", range_cover, 1);
+    rb_define_method(rb_cRange, "bound", range_bound, 1);
 }

-- 
Yusuke Endoh <mame@tsg.ne.jp>
=end



-- 
http://redmine.ruby-lang.org

In This Thread