Hacker News new | ask | show | jobs
by aidenn0 3709 days ago
How do you represent hierarchical data in a single hashmap?
2 comments

Depending on your problem domain, you might be able to flatten it by just combining keys together (subject to keys being combinable like with string concatenation, using separators to ensure uniqueness of combined keys, etc.)

E.g., turn {"foo": {"bar": "moop"}} into {"foo-bar": "moop"}. This also requires writing accessor fns, but it might be worth it, depending.

Right, that works fine so long as you only ever go up the hierarchy, but going down the hierarchy would require a prefix search, which is the one place where trees actually do win over hashes.
You can frequently buy a performance increase by representing nested calculation logic in hashmaps via memoization.