List DropWhile

object DropWhile {

  def dropWhile[A](l: List[A], f: A => Boolean): List[A] = l match {
    case Nil    => Nil
    case h :: t => if (f(h)) dropWhile(t, f) else h :: t
  }

  def main(args: Array[String]): Unit = {
    println(dropWhile(Nil, (a: Int) => a < 0))
    println(dropWhile(List(-1, -3, -2, 3, 4, 5, 6), (a: Int) => a < 0))
  }
}

 

List()
List(3, 4, 5, 6)

 

posted on 2016-04-18 23:17  JonkeyGuan  阅读(277)  评论(0编辑  收藏  举报