5 ms·
Some good javascript tips & handy functions
- jauco 17y agoGenerator expressions: [ y for ( y in [5,6,7,8,9] ) ] // is [0,1,2,3,4] and [ y for each ( y in [5,6,7,8,9] ) ] // is [5,6,7,8,9] Because in for extracts index names, and for each extracts the values.
- tumult 17y agoOh dude, you're the man. I didn't even know you could do comprehensions in JS.
- tlrobinson 17y agoOf course it's not cross-browser, but if you're doing server-side JS it might be useful.
- mpk 17y agoIt's not just server-side or client side these days. Using Rhino I'm using JS for compressing JS files for production and generating documentation for said JS files.
- jamongkad 17y agoYou can do list comprehensions in JS now??
- jeroen 17y agoSince 1.8: http://en.wikipedia.org/wiki/List_comprehension#Javascript_1.8 http://en.wikipedia.org/wiki/List_comprehension#Javascript_1... It will probably be a long time before 1.8 is supported by all browsers.
- boucher 17y agoit will probably be never. unless all browsers ceases to include internet explorer at some point.
- zackattack 17y agoWhat are generator expressions?
- jauco 17y agoA generator expression is an easy way the create a function that performs a small part of the calculation each time it's run (and has memory of the state it's in). Since code often uses a one item at a time, this is less memory intensive then first generating the complete list and storing it in memory. http://www.python.org/dev/peps/pep-0289/ http://www.python.org/dev/peps/pep-0289/ The generator part in the example is the '(y in [])' the rest is a list comprehension (a short way of writing for loops that can be implemented very efficiently in a language and will return a list). [UPDATE] Found a better explanation that also explains expression closures at http://ejohn.org/blog/javascript-18-progress/ http://ejohn.org/blog/javascript-18-progress/
- pmjordan 17y agoRegarding "shuffle the Array": list = list.sort(function() Math.random() > 0.5 ? 1 : -1); I'm wondering just how reliably random the result is. Does JavaScript make any guarantee whether the comparison function will only be called once for a given pair of values, and if not, could this affect the result?
- jauco 17y agoWell, even if they compare multiple times the order will still be 'random' as in not easily predictable
- pmjordan 17y agoOr it could derail the sort algorithm in some way (infinite loop?) - the point is, it makes some assumptions and it's not clear that these assumptions hold.
- fh 17y agoOn the contrary, I bet that depending on the exact search algorithm, it would be possible to make some very specific predictions. To pick a simple example, say you use this technique on quicksort of the "pivot on the first element" variety. Given the nature of the algorithm, the first source element will end up near the center of the "randomized" list with high probability, not at all what you'd expect from an unbiased random permutation. Granted, no real world sort is that simple, and it's even possible that the "clever technique" will produce an unbiased permutation for some sorting algorithms. But why waste time thinking about this when the Knuth shuffle, a proper way to randomly permute a list, only takes a few lines of code more and is so well-documented?
- jjs 17y agoIt's not random; the mean value for the first half of the new list (originally [0,1,2,3,4,5,6,7,8,9]) is 10, while the mean for the second half is 35. (Or at least, it came out that way reliably after several runs of a million iterations each time). Since a value sorted this way has a decreasing chance to be swapped an increasing number of positions, this result makes perfect sense.
- 17y ago
- TweedHeads 17y agoThe more I read the less I know
- soubok 17y agoLatest Mozilla JavaScript has also Generators (yield keyword), Destructuring assignment (var [a,b,c] = [1,2,3]) and just-in-time compilation.