Earlier quoted context omitted.
> reading the title brought "Lua table"[2] to my mind, the most flexible data structure I have worked with, by far. Which is not necessarily a good thing. PHP's array and JS's Object are essentially the same thing.
Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. Plus all the metatables goodies: weak key and/or value references, prototype inheritance, ...
The Universal Data Structure
41–50 of 108 posts
Re: The Universal Data Structure
#42Earlier quoted context omitted.
It's even tagged "bad-theory." I think it's pretty clearly a joke! A really good one!
"And we can’t forget our favorite JavaScript interview question of all time: If you only had twenty-four hours to implement arrays in JavaScript, how would you do it?"
for (k in ['a', 'b']) {
console.log(k);
}
and get back 0, 1? However: var a = {0:'a', 1:'b'};
for (k in a) {
console.log(k);
}
also outputs 0, 1.Edit: turns out, you can even have doubles, too:
var weird = {3.14:'hello', 6.28:'world'};
// for loop above emits: 3.14, 6.28
console.log(weird[3.14]); // emits 'hello'Re: The Universal Data Structure
#43This is a perfect example of the kind of humor that belongs on HN. Actually had me nodding along in parts, then screwing up my face at others. By the time I was sure it was satire, I was committed enough to see it through to the end. Best of all, I expect sincere discussion of the merits of the argument in this thread. Well executed, and heh heh .
Re: The Universal Data Structure
#44"Hashes are always O(1) reads, inserts and writes." Maybe, once you've found a location to read, insert, or write to. The author neglects the runtime cost required for the hash algorithm itself, which may not be trivial; computing the hash of a string key is typically an O(n) operation. Furthermore, unless a suitable table size is selected, integer keys (should one use a map like an array) will eventually hash to the…
By the time you hash a key, you could have likely already inserted it into a trie.
Lookups on hashes are also not O(1) for similar reasons. You have to hash the search string, then compare the value at whatever location it hashed to (usually a string-comparison operation which aren't O(1)) and depending on the collision strategy, do more things if it doesn't match, but isn't an empty value.
Re: The Universal Data Structure
#45Re: The Universal Data Structure
#46Earlier quoted context omitted.
> reading the title brought "Lua table"[2] to my mind, the most flexible data structure I have worked with, by far. Which is not necessarily a good thing. PHP's array and JS's Object are essentially the same thing.
Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. Plus all the metatables goodies: weak key and/or value references, prototype inheritance, ...
That sounds... completely normal? Python dictionaries will do that too. So will Java HashMaps.
Re: The Universal Data Structure
#47Earlier quoted context omitted.
"And we can’t forget our favorite JavaScript interview question of all time: If you only had twenty-four hours to implement arrays in JavaScript, how would you do it?"
Aren't they just objects that have integer properties; that's why you can for (k in ['a', 'b']) { console.log(k); } and get back 0, 1? However: var a = {0:'a', 1:'b'}; for (k in a) { console.log(k); } also outputs 0, 1. Edit: turns out, you can even have doubles, too: var weird = {3.14:'hello', 6.28:'world'}; // for loop above emits: 3.14, 6.28 console.log(weird[3.14]); // emits 'hello'
Re: The Universal Data Structure
#48Earlier quoted context omitted.
Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. Plus all the metatables goodies: weak key and/or value references, prototype inheritance, ...
> Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. That sounds... completely normal? Python dictionaries will do that too. So will Java HashMaps.
Re: The Universal Data Structure
#49Earlier quoted context omitted.
Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. Plus all the metatables goodies: weak key and/or value references, prototype inheritance, ...
> Lua tables accept arbitrary objects as keys, and make a difference between `foo[1]` and `foo["1"]`. That sounds... completely normal? Python dictionaries will do that too. So will Java HashMaps.
Re: The Universal Data Structure
#50Earlier quoted context omitted.
"And we can’t forget our favorite JavaScript interview question of all time: If you only had twenty-four hours to implement arrays in JavaScript, how would you do it?"
Aren't they just objects that have integer properties; that's why you can for (k in ['a', 'b']) { console.log(k); } and get back 0, 1? However: var a = {0:'a', 1:'b'}; for (k in a) { console.log(k); } also outputs 0, 1. Edit: turns out, you can even have doubles, too: var weird = {3.14:'hello', 6.28:'world'}; // for loop above emits: 3.14, 6.28 console.log(weird[3.14]); // emits 'hello'
That's because Javascript doesn't actually have integers, it just has "Number":
> The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic