10

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?

Mirco Widmer
  • 2,139
  • 1
  • 20
  • 44
synapse
  • 5,588
  • 6
  • 35
  • 65

3 Answers3

14

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)
dhg
  • 52,383
  • 8
  • 123
  • 144
0

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

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
0

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)
Ashalynd
  • 12,363
  • 2
  • 34
  • 37