Proposal: Signed variables

Adam Shannon adam at ashannon.us
Tue Jan 3 10:03:28 PST 2012


When working on Array.prototype.range (among many others) I noticed
that being able to specify the sign of a numeric would quicken code's
readability. Take the following proof of concept from
Array.prototype.range and compare it to the signed variable example.

Array.prototype.range = function (value, start, end, step) {
      if (typeof value === "number")  {
         end = (start >= 0 && value >= 0) ? Math.min(value, start) : 0;
         start = (start >= 0 && value >= 0) ? Math.max(value, start);
// What if value < start < 0, what's the default then?
         step = (arguments.length == 3) ? end : 1;
         value = "";
      }

      if (typeof value !== "number") {
         start ?? 0;
         //end ?? // Again, what should be the default? this.length?
         step ?? 1;
      }

       var result = [];
       for(var i=start; i < end; i++) {
           result.push(value);
       }
       return result;
   }

----

Array.prototype.range = function (value, start+, end+, step+) {
      if (typeof value === "number")  {
         end = Math.min(value, start);
         start = Math.max(value, start);
         step = (arguments.length == 3) ? end : 1;
         value = "";
      }

      if (typeof value !== "number") {
         start ?? 0;
         //end ?? // Again, what should be the default? this.length?
         step ?? 1;
      }

       var result = [];
       for(var i=start; i < end; i++) {
           result.push(value);
       }
       return result;
   }

----

The idea of signed numerics comes up a lot in cases, imagine for loops
that are optimized because inside of the binary there is no need for
sign conversion. Also, the range of variables would be far increased,
if we only allow one sign.

Also, if you wanted to represent a negative numeric, you would do so like:

var neg- = -12e7;
var pos+ = 12e7;

-- 
Adam Shannon
Web Developer
University of Northern Iowa
Sophomore -- Computer Science B.S. & Mathematics
http://ashannon.us


More information about the es-discuss mailing list