Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fast/short array creation syntax #1107

Open
determin1st opened this issue Jul 4, 2020 · 6 comments
Open

fast/short array creation syntax #1107

determin1st opened this issue Jul 4, 2020 · 6 comments

Comments

@determin1st
Copy link

determin1st commented Jul 4, 2020

any ideas of making fast array creation/fill syntax?
here is the target javascript code:

Array(n).fill(x)

where n is the number of elements, and x is the fill value. maybe:

[n of x]
@determin1st determin1st changed the title Fast array creation syntax fast/short array creation syntax Jul 4, 2020
@ceremcem
Copy link

ceremcem commented Jul 4, 2020

Possible complications of introducing a new syntax might be upheld by "you may ignore it if you find it unhandy" claim, but eventually it will concern every one of us because we eventually will have to read someone else's code.

The richest "language" regarding to the "shorthand" is possibly the RegEx. It's considered as a "write-only language".

So, I prefer not to have it at all. That's of course only my idea.

@vendethiel
Copy link
Contributor

Isn’t that just [x] * n?

@determin1st
Copy link
Author

determin1st commented Jul 4, 2020

@vendethiel

works well when n is given straight and small, but:

repeatArray$([x], n);
function repeatArray$(arr, n){
  for (var r = []; n > 0; (n >>= 1) && (arr = arr.concat(arr)))
    if (n & 1) r.push.apply(r, arr);
  return r;
}

Isn't it way too arcane? :]
Actually my "use-case" is zero-fill array with a small, but unknown length..

@ceremcem

how do you create and fill/initialize array?

PS: oh, it's actually in the docs, operators -> List section.. didn't know

@rhendric
Copy link
Collaborator

rhendric commented Jul 4, 2020

In a version of LiveScript that assumed (or was instructed to have) an ES6 target, I think it could make sense to optimize specifically [x] * n to Array(n).fill(x). I wouldn't want there to be another syntax that overlaps with the existing one in functionality.

Absent such a version of LiveScript, this doesn't seem that important to do; writing Array n .fill x yourself isn't bad, if you care enough about the efficiency of your code to want that. It has the advantage of being crystal clear both about what it's doing and about what it requires from the runtime; the only cost is eight more characters.

@determin1st
Copy link
Author

@rhendric

So you mean that LiveScript should be upgraded to have some compile option that enables higher JS/ES runtime versions first?

@rhendric
Copy link
Collaborator

rhendric commented Jul 4, 2020

I'm not saying such a compile option would necessarily be a good idea. (A while back there was some talk about integrating LiveScript with Babel (#821), which I think would be a better approach for targeting more modern runtimes.)

But to take a compilation that targets ES5 (or is this even ES3?) and change it to only run on ES6 is more breaking than I want to do without some sort of a bigger plan for ES6+ support in general. If such a plan ever comes into play, I think I'd be happy for this optimization to be a part of it.

@rhendric rhendric added the ES6+ label Jul 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants