Suppose you have the following functions available for reference
sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]
Now consider the following dropWhile
function which supposedly
removes elements from List
prefix as long as they match a predicate.
def dropWhile[A](l: List[A], f: A => Boolean): List[A] =
l match {
case Cons(h, t) if f(h) => dropWhile(t, f)
case _ => l
}
I have the following test cases:
dropWhile(List(1, 2, 3), (x: Int) => x < 2) shouldBe List(2, 3)
dropWhile(List(1, 2, 3), (x: Int) => x > 2) shouldBe List(1, 2)
dropWhile(List(1, 2, 3), (x: Int) => x > 0) shouldBe Nil
dropWhile(Nil, (x: Int) => x > 0) shouldBe Nil
Question?
Test case (2) fails. Why is that the case?. The interpreter
actually gives me List(1, 2, 3)
the original list without having
dropped anything.
For context this is Exercise 3.5 on Functional Programming in Scala by Chuisano and Bjarnason. The authors themselves implement this function exactly the same way I have written on here. Is there something that I am not understanding here?.
Test case (2) is wrong. You are dropping elements while x > 2
. Since the first element fails this test, it does not drop anything. So List(1, 2, 3)
is the correct result.
The test case appears to be testing for filterNot
not dropWhile
.