title: Scala List object methods implementation
body: <p>I need to implement my own List class in Scala. I've implemented:</p>
  
  <pre><code>trait List[+A] {
    /** The first element */
    def head: A
    /** The rest of the elements */
    def tail: List[A]
  
    def map[B](f: A =&gt; B): List[B]
    def flatMap[B](f: A =&gt; List[B]): List[B]
    def filter(f: A =&gt; Boolean): List[A]
  
    // Concatenate two lists
    def concat[B &gt;: A](that: List[B]): List[B] = this match {
      case Empty =&gt; that
      case NonEmpty(head, tail) =&gt; NonEmpty(head, tail concat that)
    }
  }
  
  /** The empty list, also known as Nil */
  case object Empty extends List[Nothing] {
    def head = throw new UnsupportedOperationException("Empty.head")
    def tail = throw new UnsupportedOperationException("Empty.tail")
  
    def map[B](f: Nothing =&gt; B): List[B] = Empty
    def flatMap[B](f: Nothing =&gt; List[B]): List[B] = Empty
    def filter(f: Nothing =&gt; Boolean): List[Nothing] = Empty
  
    override def toString = "Empty"
  }
  </code></pre>
  
  <p>And now I need to implement filter, flatMap and Map methods:</p>
  
  <pre><code>case class NonEmpty[A](head: A, tail: List[A]) extends List[A] {
  
  
      //def map[B](f: A =&gt; B): List[B] = ???
        //def flatMap[B](f: A =&gt; List[B]): List[B] = ???
        def filter(predicate: A =&gt; Boolean): List[A] = {        
        }
  </code></pre>
  
  <p>For instance method <code>filter(predicate: A =&gt; Boolean): List[A]</code> how can I iterate through every element in this list? How can I check if given predicate is true or false? (tried <code>if(predicate(head))</code> - doesn't work for some reason.)
  Thank you for help.</p>