Created
May 2, 2013 06:47
-
-
Save larsrh/5500542 to your computer and use it in GitHub Desktop.
Sets are not Functors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem here is that
toB
doesn't respect the equivalence relation defined by the auto-generatedA#equals
method.