Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala implicit or explicit conversion from iterator to iterable

Does Scala provide a built-in class, utility, syntax, or other mechanism for converting (by wrapping) an Iterator with an Iterable?

For example, I have an Iterator[Foo] and I need an Iterable[Foo], so currently I am:

 val foo1: Iterator[Foo] = ....
 val foo2: Iterable[Foo] = new Iterable[Foo] {
   def elements = foo1
 }

This seems ugly and unnecessary. What's a better way?

like image 377
Landon Kuhn Avatar asked Apr 30 '10 18:04

Landon Kuhn


1 Answers

Iterator has a toIterable method in Scala 2.8.0, but not in 2.7.7 or earlier. It's not implicit, but you could define your own implicit conversion if you need one.

like image 153
Jay Conrod Avatar answered Oct 12 '22 01:10

Jay Conrod