Created
July 30, 2013 22:32
-
-
Save earldouglas/6117664 to your computer and use it in GitHub Desktop.
List#contains as a type class: https://groups.google.com/forum/#!topic/scala-functional/UKoFkCiABtc
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> List("a", "b", "c").has(42) | |
<console>:10: error: type mismatch; | |
found : Int(42) | |
required: String | |
List("a", "b", "c").has(42) | |
^ | |
scala> List("a", "b", "c").has("b") | |
res1: Boolean = true |
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
trait Container[A] { | |
def has(a: A): Boolean | |
} | |
implicit def listContainer[A](xs: List[A]): Container[A] = | |
new Container[A] { | |
override def has(a: A): Boolean = xs.contains(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment