Last active
July 13, 2019 15:54
-
-
Save fancellu/ec184ab05c41c76e7d614310af508fe1 to your computer and use it in GitHub Desktop.
case class equality: Making scala only check some params, not all
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
case class Product(name: String)(val price: Double) | |
// we can choose to only supply the name, i.e. partially applied | |
val banana=Product("banana") _ | |
val banana10=banana(10) | |
val bananafree=banana(0) | |
val apple=Product("apple")(5) | |
val list=List(banana10, apple, bananafree) | |
// Note, price is not used for equality, but is still very much present | |
list.filter(_==banana10).map(_.price) | |
list.filter(_==bananafree).map(_.price) | |
list.filter(_==apple).map(_.price) |
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
list: List[Product] = List(Product(banana), Product(apple), Product(banana)) | |
res1: List[Double] = List(10.0, 0.0) | |
res2: List[Double] = List(10.0, 0.0) | |
res3: List[Double] = List(5.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment