-4

In versions of JS upto and including ES6 are there any mechanisms available for the generation of sequences without loops or recursion?

For example:

[n, n*2...] // Hypothetical syntax that I know to be invalid JavaScript
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    Perhaps you should try http://codegolf.stackexchange.com/? – jonrsharpe Apr 15 '15 at 19:10
  • 1
    try http://codereview.stackexchange.com/ – cport1 Apr 15 '15 at 19:10
  • 2
    *"I am trying to find out if there is a shorthand for this kind of operation in the language"* What kind of operation? Printing a list of consecutive numbers? That's quite a specific application IMO and does not seem to require a syntax extension or API extension. – Felix Kling Apr 15 '15 at 19:13
  • 1
    `for(var x=0; x < 100; x++) { console.log(x); }` **is** shorter. What are you really asking for a shorthand for? Array comprehensions? – apsillers Apr 15 '15 at 19:14
  • I guess the numbers are a sequence, so my question would be better put - are there any built-in sequence handling functions in ES6? – Ben Aston Apr 15 '15 at 19:14
  • @apsillers: You are way too verbose: `for(var x=0;x<100;x++)console.log(x);` :P – Felix Kling Apr 15 '15 at 19:16
  • @Ben: So something like `range` in Python? No there is not afaik. – Felix Kling Apr 15 '15 at 19:17
  • I found this post http://ariya.ofilabs.com/2013/07/sequences-using-javascript-array.html – Ben Aston Apr 15 '15 at 19:17
  • If you refer to array comprehension, that was deferred to ES7. – Felix Kling Apr 15 '15 at 19:18
  • @FelixKling interestingly that post also provides examples using only `Array`, `apply` and `map`. But I take your point – Ben Aston Apr 15 '15 at 19:19
  • Either way, `Array`, `apply` and `map` already exist in ES5. You really should edit your question to reflect your actual intentions. – Felix Kling Apr 15 '15 at 19:20

1 Answers1

0

No there are no operators built-in to the language specifically for generating sequences, but you can re-purpose some existing methods for sequence generation.

For example:

[...Array(5)].map((x, y) => y*2); // Array [ 0, 2, 4, 6, 8 ]
Ben Aston
  • 53,718
  • 65
  • 205
  • 331