Array.range() (was: Suggestion: Array.prototype.repeat)

Adam Shannon adam at ashannon.us
Tue Jan 3 09:57:55 PST 2012


I would be more in favor of something like the code below, its just a
proof of concept.

 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;
   }

On Tue, Jan 3, 2012 at 11:42, Axel Rauschmayer <axel at rauschma.de> wrote:
> I think it's fairly common for range implementations to provide an optional
> `step` parameter
>
>
> Good point. Maybe all of these parameters should be options:
>
> Array.range() -> 0, 1, 2, 3, ...
> Array.range({ start: 3 }) -> 3, 4, 5, 6, ...
> Array.range({ end: 5 }) -> 0, 1, 2, 3, 4
> Array.range({ step: 3 }) -> 0, 3, 6, 9, ...
> Array.range({ start: -2, step: -1 }) -> -2, -3, -4, -5, ...
> etc.
>
> The defaults of start and end and the comparison operator depend on the sign
> of step.
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
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