Firefox 3 even has reduce(), apparently. My nipples are perky! I don't like that their implementation of Arrays seems to make shift()/unshift() seem to run in O(n).
My personal rule-of-thumb is that if it takes more than two lines, don't make it a lambda function. I claim that as a legibility and style decision more than anything else.
Javascript arrays are just hashes with numeric keys, so yeah, shifting up or down requires changing all the keys.
Go go gadget functional programming. One of many reasons I dislike Python (which is Javascriptish in many ways) is Guido's active discouragement of functional idioms...
See, I think having to explicitly change all the keys indicates a lack of either priority on the part of the implementers. "array" is just an interface to a data structure that supports a pile of operations (access child nodes by numeric index, push, pop, shift, unshift, splice, join, map, reduce, etc).
I can easily imagine implementing a JS array as a lookup table with fixed size records, and pointers off to the actual data payloads. The actual keys don't exist anywhere - they're just translated into offsets into the lookup table. This makes push, pop, shift, and unshift all constant-time operations - you just move the last or first entry in the lookup table, respectively.
Now, that does introduce some issues with making sure you've got enough empty space before and after the lookup table to accommodate this. I think it's doable.
Comments 6
Reply
...though it'd be cooler if you used anonymous functions instead of named ones. If you're only ever going to call it once, and from map, then just do
my_array.map( function(item) { do stuff with item } )
:)
Reply
My personal rule-of-thumb is that if it takes more than two lines, don't make it a lambda function. I claim that as a legibility and style decision more than anything else.
Reply
Go go gadget functional programming. One of many reasons I dislike Python (which is Javascriptish in many ways) is Guido's active discouragement of functional idioms...
Reply
I can easily imagine implementing a JS array as a lookup table with fixed size records, and pointers off to the actual data payloads. The actual keys don't exist anywhere - they're just translated into offsets into the lookup table. This makes push, pop, shift, and unshift all constant-time operations - you just move the last or first entry in the lookup table, respectively.
Now, that does introduce some issues with making sure you've got enough empty space before and after the lookup table to accommodate this. I think it's doable.
Reply
Leave a comment