Question

How to implement Scala equivalent to Java Iterable<T> and C# IEnumerable<T>? Basically, I want my collection to be mappable, filterable etc. What traits should the collection class extend and are there easy ways (something like yield return and yield break in C#) to create enumerator?

Was it helpful?

Solution

Implement the Iterable trait. All that is required is the iterator method. All the other methods (map, filter, etc) come for free.

class MyIterable[T](xs: Vector[T]) extends Iterable[T] { 
  override def iterator = xs.iterator 
}

val a = new MyIterable(Vector(1,2,3))
a.map(_+1) // res0: Iterable[Int] = List(2, 3, 4)
a.filter(_%2==1) // res1: Iterable[Int] = List(1, 3)

OTHER TIPS

You can use scala.collection.Iterable trait or its immutable version which are pretty comprehensive and powerful. Hope that will help.

Regarding the enumerators, there is Enumeration in Scala but using sealed traits + derived classes seems to be more flexible, for example:

sealed trait MyEnum {
  lazy val value = 0
}
case object MyEnumA extends MyEnum {
  override lazy val value = 1
}
case object MyEnumB extends MyEnum {
  override lazy val value = 2
}

scala> val a = MyEnumB
a: MyEnumB.type = MyEnumB

scala> a.value
res24: Int = 2

scala>  val l = List(MyEnumA,MyEnumB)
l: List[Product with Serializable with MyEnum] = List(MyEnumA, MyEnumB)

scala> l.map(_.value)
res29: List[Int] = List(1, 2)

You can use these objects without any internal structure as well, if you don't care about mapping them to anything except their string representations:

sealed trait MyEnum
case object MyEnumA extends MyEnum
case object MyEnumB extends MyEnum

scala> val a = MyEnumA
a: MyEnumA.type = MyEnumA

scala> a.toString
res21: String = MyEnumA

scala> val l = List(MyEnumA,MyEnumB)
l: List[Product with Serializable with MyEnum] = List(MyEnumA, MyEnumB)

scala> l.map(_.toString)
res30: List[String] = List(MyEnumA, MyEnumB)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top