Scala equivalent of Python's "in" operator for sets?
Here is one example how to do this:
scala> implicit class InOperation[T](v: T) extends AnyVal { def in(s: Set[T]) = { s contains v } }
defined class InOperation
scala> val x = Set(1,2,3)
x: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
scala> 2 in x
res0: Boolean = true
It uses implicit class to add in
method (that takes Set[T]
) to arbitrary type T
and checks whether object is in the set.