Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to 'hash cons'?

When to use it and why?

My question comes from the sentence: "hash cons with some classes and compare their instances with reference equality"

like image 743
Boris Pavlović Avatar asked Apr 13 '09 14:04

Boris Pavlović


1 Answers

From Odersky, Spoon and Venners (2007), Programming in Scala, Artima Press, p. 243:

You hash cons instances of a class by caching all instances you have created in a weak collection. Then, any time you want a new instance of the class, you first check the cache. If the cache already has an element equal to the one you are about to create, you can reuse the existing instance. As a result of this arrangement, any two instances that are equal with equals() are also equal with reference equality.

like image 58
Topher Avatar answered Oct 13 '22 19:10

Topher