Purely functional equivalent of weakhashmap?

Posted by Jon Harrop on Stack Overflow See other posts from Stack Overflow or by Jon Harrop
Published on 2011-01-17T13:53:29Z Indexed on 2011/01/17 14:53 UTC
Read the original article Hit count: 138

Weak hash tables like Java's weak hash map use weak references to track the collection of unreachable keys by the garbage collector and remove bindings with that key from the collection. Weak hash tables are typically used to implement indirections from one vertex or edge in a graph to another because they allow the garbage collector to collect unreachable portions of the graph.

Is there a purely functional equivalent of this data structure? If not, how might one be created?

This seems like an interesting challenge. The internal implementation cannot be pure because it must collect (i.e. mutate) the data structure in order to remove unreachable parts but I believe it could present a pure interface to the user, who could never observe the impurities because they only affect portions of the data structure that the user can, by definition, no longer reach.

© Stack Overflow or respective owner

Related posts about data-structures

Related posts about weak-references