Timeline for Tips for Golfing in ECMAScript 6 and above
Current License: CC BY-SA 3.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 17, 2020 at 9:04 | history | edited | CommunityBot |
Commonmark migration
|
|
May 1, 2019 at 4:52 | comment | added | nderscore |
If you need to map over a 0...n range, you can usually save a byte using Array.from (ex: [...Array(N).keys()].map(i=>i*2) vs Array.from(Array(N),(_,i)=>i*2) )
|
|
May 4, 2017 at 2:26 | comment | added | Mwr247 |
@yonatanmn Very nice! Only comments would be 1) the 1/4 example would be shorter written out [0,0,0,0] , and 2) stringified functions are implementation specific, so won't return a reliable length (Map is 32 bytes in Chrome, but 36 bytes in Firefox).
|
|
May 3, 2017 at 22:59 | comment | added | yonatanmn |
also 4: [...1/4+''] ; 18: [...1/3+''] ; 19: [...1/7+''] ; 32:[...Map+''] ; etc..;
|
|
May 3, 2017 at 22:50 | comment | added | yonatanmn |
got 1 byte shorter! [...9**x+''] works up to 22; for array of 8 you can use [...1/0+'']
|
|
Oct 4, 2016 at 1:22 | comment | added | ETHproductions |
You can also use generators: x=>[...function*(n){for(;n--;)yield n}(x)] generates [x-1,...,0] . Of course, x=>[...Array(x)].map(_=>--x) and x=>[for(i of Array(x))--x] are still a good bit shorter. (Maybe you could add a section about using array comprehensions?)
|
|
Jan 21, 2016 at 21:01 | comment | added | ETHproductions | This deserves more upvotes! It's very useful in many scenarios. | |
Dec 22, 2015 at 20:20 | history | edited | Mwr247 | CC BY-SA 3.0 |
added 91 characters in body
|
Dec 22, 2015 at 20:17 | comment | added | Mwr247 | Weird that I forgot to mention that one (since it was really the whole point of this answer)... I was focusing on what you could do with it (like with keys), rather than it's simplest form. I'll edit that in, thanks =) | |
Dec 22, 2015 at 20:02 | comment | added | ETHproductions |
A new trick I've learned: [...Array(x)] works just as well as Array(x).fill() , and is 2 bytes shorter. x=>[...Array(x)].map((a,i)=>i)
|
|
Oct 23, 2015 at 15:04 | history | edited | Mwr247 | CC BY-SA 3.0 |
deleted 3 characters in body
|
Oct 23, 2015 at 12:45 | comment | added | ETHproductions |
Also, for the last example, a=>a%2-1 works fine, as does a=>a%2<1 .
|
|
Oct 23, 2015 at 2:51 | comment | added | Mwr247 | @ETHproductions You're right, I forgot the 0 isn't needed in the fill before map. This makes it 1 character shorter than your suggested one though, so I'll keep it like that. Thanks! | |
Oct 23, 2015 at 2:49 | history | edited | Mwr247 | CC BY-SA 3.0 |
deleted 1 character in body
|
Oct 23, 2015 at 2:47 | comment | added | ETHproductions |
Here's a suggestion for the second example: x=>Array(x).fill(i=0).map(a=>i++) Also, I'm not sure that the 0 in .fill(0) is necessary. Have you tried it without?
|
|
Oct 22, 2015 at 21:38 | history | answered | Mwr247 | CC BY-SA 3.0 |