logo       

Re: Empty Iterators: msg#00484

lang.scala

Subject: Re: Empty Iterators

For what it's worth...

new Array[String](0).elements


Miles Sabin wrote:
What's the canonical way of getting an empty Iterator[A]? Iterator.empty 
only wants to give me an Iterator[Nothing], which isn't quite what I'm 
after.

The context is as the zero for a fold,

  class DisjointUnion[A](family : List[Set[A]])
    extends Set[A]
  {
    val size = ...
  
     def contains(elem: A) = ...  

     def elements = family.foldLeft(???)(
       (i : Iterator[A], s : Set[A]) => i ++ s.elements)
  }

It feels like the '???' should be replaced by something like,

     def elements = family.foldLeft(Iterator.empty[A])(
       (i : Iterator[A], s : Set[A]) => i ++ s.elements)

where Iterator.empty[A] has the obvious definition,

  def empty[A] : Iterator[A] = /* old Iterator.empty renamed */

Could this be done without too much breakage elsewhere in the Scala 
libraries?

Cheers,


Miles
  
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise