#1100 Some Float and Range methods implementation for JavaScript

Yuri Strot Mon 24 May 2010

Hi,

I'm looking for implementation of the several methods:

  1. sys::Float: random, negate, increment, decrement
  2. sys::Range: isEmpty, min, max, first, last

Could you please add it? The following patch can be helpful:

diff -r 2fbc1e62ece0 src/sys/js/fan/Float.js
--- a/src/sys/js/fan/Float.js	Wed May 19 10:35:37 2010 -0400
+++ b/src/sys/js/fan/Float.js	Mon May 24 22:47:43 2010 +0700
@@ -70,6 +70,7 @@
 fan.sys.Float.ceil  = function(self) { return fan.sys.Float.make(Math.ceil(self)); }
 fan.sys.Float.exp   = function(self) { return fan.sys.Float.make(Math.exp(self)); }
 fan.sys.Float.floor = function(self) { return fan.sys.Float.make(Math.floor(self)); }
+fan.sys.Float.random = function(self) { return fan.sys.Float.make(Math.random()); }
 fan.sys.Float.log   = function(self) { return fan.sys.Float.make(Math.log(self)); }
 fan.sys.Float.log10 = function(self) { return fan.sys.Float.make(Math.log(self) / Math.LN10); }
 fan.sys.Float.min   = function(self, that) { return fan.sys.Float.make(Math.min(self, that)); }
@@ -80,6 +81,10 @@
 fan.sys.Float.sqrt  = function(self) { return fan.sys.Float.make(Math.sqrt(self)); }
 
 // arithmetic
+fan.sys.Float.negate    = function(self) { return -self; }
+fan.sys.Float.increment = function(self) { return self+1; }
+fan.sys.Float.decrement = function(self) { return self-1; }
+
 fan.sys.Float.plus  = function(a,b) { return fan.sys.Float.make(a+b); }
 fan.sys.Float.minus = function(a,b) { return fan.sys.Float.make(a-b); }
 fan.sys.Float.mult  = function(a,b) { return fan.sys.Float.make(a*b); }
diff -r 2fbc1e62ece0 src/sys/js/fan/Range.js
--- a/src/sys/js/fan/Range.js	Wed May 19 10:35:37 2010 -0400
+++ b/src/sys/js/fan/Range.js	Mon May 24 22:51:17 2010 +0700
@@ -64,6 +64,36 @@
 fan.sys.Range.prototype.inclusive = function() { return !this.m_exclusive; }
 fan.sys.Range.prototype.exclusive = function() { return this.m_exclusive; }
 
+fan.sys.Range.prototype.isEmpty = function() { return this.m_exclusive && this.m_start == this.m_end; }
+
+fan.sys.Range.prototype.min = function()
+{
+  if (this.isEmpty()) return null;
+  if (this.m_end < this.m_start) return this.m_exclusive ? this.m_end + 1 : this.m_end;
+  return this.m_start;
+}
+
+fan.sys.Range.prototype.max = function()
+{
+  if (this.isEmpty()) return null;
+  if (this.m_end < this.m_start) return this.m_start;
+  return this.m_exclusive ? this.m_end - 1 : this.m_end;
+}
+
+fan.sys.Range.prototype.first = function()
+{
+  if (this.isEmpty()) return null;
+  return this.m_start;
+}
+
+fan.sys.Range.prototype.last = function()
+{
+  if (this.isEmpty()) return null;
+  if (!this.m_exclusive) return this.m_end;
+  if (this.m_start < this.m_end) return this.m_end - 1;
+  return this.m_end + 1;
+}
+
 fan.sys.Range.prototype.contains = function(i)
 {
   if (this.m_start < this.m_end)

andy Tue 25 May 2010

Thanks ystrot - pushed these fixes as well as a few others to complete sys::Range API and mostly complete sys::Float API under JavaScript.

Login or Signup to reply.