Skip to content

Instantly share code, notes, and snippets.

@fancellu
Last active July 13, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/ec184ab05c41c76e7d614310af508fe1 to your computer and use it in GitHub Desktop.
Save fancellu/ec184ab05c41c76e7d614310af508fe1 to your computer and use it in GitHub Desktop.
case class equality: Making scala only check some params, not all
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)
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