Ask HN: What are some cool but obscure data structures you know about?
Not a very deep CS-y one, but still one of my favourite data structures: Promise Maps.<p>It only works in languages where promises/futures/tasks are a first-class citizen. Eg JavaScript.<p>When caching the result of an expensive computation or a network call, don't actually cache the result, but cache the promise that awaits the result. Ie don't make a<p><pre><code>    Map<Key, Result>
</code></pre>
but a<p><pre><code>    Map<Key, Promise<Result>>
</code></pre>
This way, if a new, uncached key gets requested twice in rapid succession, ie faster than the computation takes, you avoid computing/fetching the same value twice. This trick works because:<p>- promises hold on to their result value indefinitely (until they're GC'ed)<p>- you can await (or .then()) an existing promise as many times as you want<p>- awaiting an already-resolved promise is a very low-overhead operation.<p>In other words, the promise acts as a mutex around the computation, and the resulting code is understandable even by people unfamiliar with mutexes, locks and so on.