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

2012-01-03 Thread Axel Rauschmayer
On Jan 3, 2012, at 15:46 , Greg Smith wrote:

 What is the use case for .repeat? Trying to imagine some code where I'd need 
 it so I can get a feel for how it should work.

So beauty alone does not count? ;-)

It’s true – there are not a lot of use cases for Array.repeat().

But I keep thinking that there should be a way to create an array of a given 
length *with* content in it. Rationale: such an array is nice to have as a 
starting point for a transformation via Array.prototype.map() or an array 
comprehension [1]. How about the following?

Array.range = function (start, end) {
if (arguments.length  1 || arguments.length  2) {
throw new TypeError(Need one or two arguments);
}
if (arguments.length === 1) {
end = start;
start = 0;
}
var result = [];
for(var i=start; i  end; i++) {
result.push(i);
}
return result;
}

Interaction:

 Array.range(4)
[ 0, 1, 2, 3 ]
 Array.range(4).map(function () { return * })
[ '*', '*', '*', '*' ]

I’m not yet sure how many use cases there are (suggestions welcome), but it 
does fill a hole (IIRC, Python has something similar).

TODO: This method probably makes more sense as an iterator (e.g. implemented 
via a generator). Then one could even omit the upper limit and produce an 
unlimited sequence.

[1] http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Rick Waldron
I think it's fairly common for range implementations to provide an optional
`step` parameter

On Tue, Jan 3, 2012 at 12:08 PM, Axel Rauschmayer a...@rauschma.de wrote:

 On Jan 3, 2012, at 15:46 , Greg Smith wrote:

  What is the use case for .repeat? Trying to imagine some code where I'd
 need it so I can get a feel for how it should work.

 So beauty alone does not count? ;-)

 It’s true – there are not a lot of use cases for Array.repeat().

 But I keep thinking that there should be a way to create an array of a
 given length *with* content in it. Rationale: such an array is nice to have
 as a starting point for a transformation via Array.prototype.map() or an
 array comprehension [1]. How about the following?

Array.range = function (start, end) {
if (arguments.length  1 || arguments.length  2) {
throw new TypeError(Need one or two arguments);
}
if (arguments.length === 1) {
end = start;
start = 0;
}
var result = [];
for(var i=start; i  end; i++) {
result.push(i);
}
return result;
}

 Interaction:

 Array.range(4)
[ 0, 1, 2, 3 ]
 Array.range(4).map(function () { return * })
[ '*', '*', '*', '*' ]

 I’m not yet sure how many use cases there are (suggestions welcome), but
 it does fill a hole (IIRC, Python has something similar).

 TODO: This method probably makes more sense as an iterator (e.g.
 implemented via a generator). Then one could even omit the upper limit and
 produce an unlimited sequence.

 [1] http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions

 --
 Dr. Axel Rauschmayer
 a...@rauschma.de

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com




___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Axel Rauschmayer
 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
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Sean Eagan
I think step should be  0, and step towards end:

Array.range({start: 5, end: 0, step: 2}) - 5, 3, 1

On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@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
 a...@rauschma.de

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com




 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss




-- 
Sean Eagan
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Axel Rauschmayer
Either way is fine with me. But it’s probably best to copy Python’s semantics:
http://docs.python.org/py3k/library/functions.html#range

On Jan 3, 2012, at 18:50 , Sean Eagan wrote:

 I think step should be  0, and step towards end:
 
 Array.range({start: 5, end: 0, step: 2}) - 5, 3, 1
 
 On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@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
 a...@rauschma.de
 
 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com
 
 
 
 
 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss
 
 
 
 
 -- 
 Sean Eagan
 

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Adam Shannon
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 a...@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
 a...@rauschma.de

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com




 ___
 es-discuss mailing list
 es-discuss@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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Rick Waldron
On Tue, Jan 3, 2012 at 12:50 PM, Sean Eagan seaneag...@gmail.com wrote:

 I think step should be  0, and step towards end:

 Array.range({start: 5, end: 0, step: 2}) - 5, 3, 1


This would be an unfortunate limitation, considering real world impl's
allow negative numbers...
http://documentcloud.github.com/underscore/#range



 On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@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
  a...@rauschma.de
 
  home: rauschma.de
  twitter: twitter.com/rauschma
  blog: 2ality.com
 
 
 
 
  ___
  es-discuss mailing list
  es-discuss@mozilla.org
  https://mail.mozilla.org/listinfo/es-discuss
 



 --
 Sean Eagan

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Maël Nison
 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.

Why use a new syntax for a simple function call ?
Afaik, there is no other standard function using an object for passing
parameters, is it ?

On 3 January 2012 18:42, Axel Rauschmayer a...@rauschma.de wrote:
 I think it's fairly common for range implementations to provide an optional
 `step` parameter


 [...]

 The defaults of start and end and the comparison operator depend on the sign
 of step.

 --
 Dr. Axel Rauschmayer
 a...@rauschma.de

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com




 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss




-- 
Maël Nison
Epitech 2014, Paris - Astek
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Michael A. Smith
When I first started learning JavaScript I didn't understand how

new Array(n);

worked, in that it creates an empty array with a length of n. What I
had expected was an array with n values (even if it wasn't
well-defined what those values should be). So of course my attempt to
create an array of random numbers:

var rands = (new Array(6)).map(function () { return
Math.floor(Math.random() * (1001)) }; );

fell flat. This is one case where the earlier Array.prototype.repeat
proposal had merit. Either way I think the Array.prototype.range
suggestion is useful, but I wish it could generate an array with
arbitrary values. Maybe we could create a shortcut syntax instead of
requiring .map if the *value* parameter in Adam's proposal could
alternatively be a callback? (Is it safe to assume nobody wants to use
range to create an array of functions? Maybe not, but it would surely
be useful.)

Michael A. Smith
Web Developer
True Action Network, an eBay Company


On Tue, Jan 3, 2012 at 1:12 PM, Rick Waldron waldron.r...@gmail.com wrote:


 On Tue, Jan 3, 2012 at 12:50 PM, Sean Eagan seaneag...@gmail.com wrote:

 I think step should be  0, and step towards end:

 Array.range({start: 5, end: 0, step: 2}) - 5, 3, 1


 This would be an unfortunate limitation, considering real world impl's allow
 negative numbers...
 http://documentcloud.github.com/underscore/#range



 On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@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
  a...@rauschma.de
 
  home: rauschma.de
  twitter: twitter.com/rauschma
  blog: 2ality.com
 
 
 
 
  ___
  es-discuss mailing list
  es-discuss@mozilla.org
  https://mail.mozilla.org/listinfo/es-discuss
 



 --
 Sean Eagan



 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss