Jagadeeswar
Jagadeeswar

Reputation: 597

What will be the result for the dropWhile in scala

I am using dropWhile in scala below is my problem. Problem:

val list = List(87, 44, 5, 4, 200)
list.dropWhile(_ < 100) should be(/*result*/)

My Answer:

val list = List(87, 44, 5, 4, 200)
list.dropWhile(_ < 100) should be(List(44,5,4,200))

As per the documentation on dropWhile will continually drop elements until a predicate is no longer satisfied: In my list the first element will satisfy the predicate so i removed the first element from the list.

val list = List(87, 44, 5, 4, 200)
list.dropWhile(_ < 100) should be(/*result*/)

I am expecting a result of List(44,5,4,200) But it is not.

Upvotes: 1

Views: 209

Answers (1)

uh_big_mike_boi
uh_big_mike_boi

Reputation: 3470

You are kind of going in the wrong direction. The head of the list is 87. The next element is 44, etc. dropWhile will continue to drop elements from the list until it hits that 200. If you initialize the list with more elements to the right of the 200, say

val list = List(87, 44, 5, 4, 200, 54, 60)

Then list.dropWhile(_ < 100) will return dropped: List[Int] = List(200, 54, 60)

Upvotes: 7

Related Questions