tomasmikula / hasheq

Typeclass-based HashSet/HashMap for Scala

GitHub

hasheq

This library provides typeclass-based implementation of HashSet and HashMap data structures, both mutable and immutable, for Scala.

This library is based on the source code of the Scala standard library and is distributed under the same license. Thanks to all the original contributors!

Try it out

Include the following line in your build.sbt:

libraryDependencies += "com.github.tomasmikula" %% "hasheq" % "0.3"

Equal and Hash typeclasses

To work with HashSet[A]/HashMap[A] from this library, you will need to define equality and hash-code for type A via an (implicit) instance of Equal[A] and Hash[A], respectively.

Abstracting over set/map implementation

You may notice that the data structures in this library do not implement some common interface for sets/maps, such as scala.collection.Set/scala.collection.Map. To write generic code that doesn't care about the underlying implementation of set/map (such as HashSet, ListSet, TreeSet, ...), you can

  • use a type parameter S[_] and an (implicit) instance of SetRepr[S, A] to abstract over set implementation;
  • use a type parameter M[_, _] and an (implicit) instance of MapRepr[M, K] to abstract over map implementation.

Equivalence-awareness

Data structures in this library are equivalence aware. It is a type error to mix up two data structures that use a different equivalence relation on their elements. Read more about equivalence versus equality.

Status

This is work in progress. Both HashSet and HashMap, mutable and immutable, have been ported from the Scala standard library (equals/hashCode-based) to typeclass-style. However, currently only immutable.HashSet is tested for correctness. Before using the others in production, you might want to contribute tests for them. Also, only immutable.HashSet is currently parametric in the equivalence used (the others require equality; see Equivalence-awareness above).