by Sean1708 · 7y ago · 0 comments
Interestingly enough some languages actually do the opposite. In Rust for example a set is literally just a dictionary with unit as the value[0] and unit is essentially a way of expressing the absence of a value (it takes up no space in memory, and you just can't do anything with it).
[0]: https://doc.rust-lang.org/stable/src/std/collections/hash/se...
For posterity, the above link shows:
pub struct HashSet<T, S = RandomState> {
map: HashMap<T, (), S>,
}FWIW, I'm usually using map[T]bool and only ever inserting `true` values. It uses a bit more space, but membership checks read like
if set[key] {
instead of if _, ok := set[key]; ok {Of course you can represent sets as dictionaries with empty values (ask anyone who programmed Perl). You're supporting my point that dicts logically subclass sets, because they can represent sets where the values can be other things as well.
You're also getting at what the behavior should be if you union a dictionary and a set. Hypothetical Python:
>>> s = {'a','b','c'}
>>> d = {'d': 'D'}
>>> d | s
>>> {'d': 'D', 'a': None, 'b': None, 'c': None}