title: How to case match a View in scala
body: <p>I'm implementing a class to constrain the access on an iterable. Intermediate steps of the sequence (after some map, etc...) is expected to be too big for memory. Thus map (and the likes: scanLeft, reduce, ...) should be lazy. </p>
  
  <p>Internally I use <code>map(...) = iterable.view.map( ... )</code>. But it seems, <code>IterableView.view</code> is not it-self, which produce useless redirection when calling map multiple times. It is probably not critical, but I'd like to call <code>.view</code> only if the iterable is not already a view.</p>
  
  <p>So, how can I case-match a View?</p>
  
  <pre><code>class LazyIterable[A](iterable: Iterable[A]){
  
    def map[B](f: A =&gt; B) = {
      val mapped = iterable match {
        case v: View[A] =&gt; v              // what should be here?
        case i: Iterable[A] =&gt; i.view
      }.map( f ))
  
      new LazyIterable(mapped)
    }
  
    def compute() =  iterable.toList
  
  }
  </code></pre>
  
  <p>Note that I don't know what is the inputed Iterable, a concrete Seq (e.g. List, Vector) or a View. And if a View, I don't know on which concrete seq type (e.g. InterableView, SeqView, ...). And I got lost in the class hierarchy of View's &amp; ViewLike's.</p>