Open In App
Related Articles

Scala List dropWhile() method with example

Improve Article
Improve
Save Article
Save
Like Article
Like

The dropWhile() method is utilized to drops the longest prefix of elements that satisfies the stated condition.

Method Definition: def dropWhile(p: (A) => Boolean): List[A]

Return Type: It returns all the elements of the list except the dropped ones.

Example #1:




// Scala program of dropWhile()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 3, 5, 4, 2)
          
        // Applying dropWhile method
        val res = m1.dropWhile(x=>{x % 2 != 0})
          
        // Displays output
        println(res)
      
    }
}

Output:

List(4, 2)

Example #2:




// Scala program of dropWhile()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(3, 6, 5, 4, 2)
          
        // Applying dropWhile method
        val res = m1.dropWhile(x=>{x % 3 == 0})
          
        // Displays output
        println(res)
      
    }
}

Output:

List(5, 4, 2)

Last Updated : 26 Jul, 2019
Like Article
Save Article
Similar Reads