5 ms·
Sets are just like hashes where the value is always "true" for each key.
by xyzzy4 9y ago
Sets are just like hashes where the value is always "true" for each key.
- mcpherrinm 9y agoWhile that's true you could implement one that way, it's very nice to have the set operations implemented. Intersection, union, subtraction, etc. And having a uniform set type makes API signatures more consistent. Plus you may be able to optimize Set<T> to use less space than Map<T, bool>.
- slig 9y ago> While that's true you could implement one that way, it's very nice to have the set operations implemented Unrelated, but does anyone know why the new JavaScript set implementation is so limited? Why didn't they bother doing this right?
- douche 9y agoBecause it is JavaScript, and there's some kind of unspoken rule about not doing things properly and instead releasing broken things.
- floogtheunfound 9y agoI’m shocked this isn’t being aggressively down voted...oh wait... it’s bashing js and not bashing haskell...nvm
- TheAceOfHearts 9y agoI think I remember reading a claim that they pushed for a small API surface in order to make sure it got through. Now that it's part of the language, anyone else can work on getting those extra features in. You can implement most basic functionality easily enough, MDN even has an example [0]. Although I agree that it should really be part of the language. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Implementing_basic_set_operations https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- deckarep 9y agoThis exactly, I hear over and over: just use a hashtable...totally misses the point.
- OJFord 9y agoDoes it really matter what the 'value' is? A set is surely implementable as a supertype of a hash, where the value is totally arbitrary; it only matters that the entry exists. With: {'A': true, 'B': false} you seem to be suggesting `B` is 'not in the set'. What's `C`?
- jackweirdy 9y agoNot in the set. You're describing the same thing as the parent comment, but you're saying "arbitrary value" which they substituted for "True"
- freyr 9y agoThey're not really the same thing then. In OJFord's implementation, there's a one-to-one correspondence between states: the element is a member of the set iff it exists as a key in the hash table. In the true/false implementation, two distinct hash table states (false or key not set) map to one set state (not a member). The program must check whether the key is set, and then check its value.