Skip to content

Instantly share code, notes, and snippets.

@larsrh
Created May 2, 2013 06:47
Show Gist options
  • Save larsrh/5500542 to your computer and use it in GitHub Desktop.
Save larsrh/5500542 to your computer and use it in GitHub Desktop.
Sets are not Functors
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class A(x: Int)(y: Int) { def toB = B(x, y) }
case class B(x: Int, y: Int) { def toA = A(x)(y) }
// Exiting paste mode, now interpreting.
defined class A
defined class B
scala> val set = Set(B(1,1), B(1,2))
set: scala.collection.immutable.Set[B] = Set(B(1,1), B(1,2))
scala> val s1 = set.map(_.toA).map(_.toB)
s1: scala.collection.immutable.Set[B] = Set(B(1,1))
scala> val s2 = set.map(_.toA.toB)
s2: scala.collection.immutable.Set[B] = Set(B(1,1), B(1,2))
@greenrd
Copy link

greenrd commented May 2, 2013

The problem here is that toB doesn't respect the equivalence relation defined by the auto-generated A#equals method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment