How to extend trait Iterable

Hey Guys,

I created an implicit class to extend List with a function:

find_uniquely_like( ... )

I would like to use this function for Set, List and Array. What’s the best way to do that? I was thinking I could somehow extend the Iterable trait to do that…

An implicit class that extends Iterable will work.
Here’s a quick example:

scala> implicit class IterableWithFind(iterable: Iterable[String]) {
     |   def findStartsWithA = iterable.filter(_.startsWith("A"))
     | }
defined class IterableWithFind

scala> val list = List("Alpha", "Beta")
list: List[String] = List(Alpha, Beta)

scala> list.findStartsWithA
res0: Iterable[String] = List(Alpha)

scala> val set = Set("Alpha", "Beta")
set: scala.collection.immutable.Set[String] = Set(Alpha, Beta)

scala> set.findStartsWithA
res1: Iterable[String] = Set(Alpha)
2 Likes

That worked! Thanks for the prompt response, pjfanning.