Scala - How to use reduce method? Code Example

The code for How to use reduce method?

object Demo {
   def main(args: Array[String]) = {
      val nums = List(10, 20, 30, 40)
      
      // Using reduce() method to get the sum 
	  // of all elements of the list
      val result = nums.reduce(_ + _)
      
      // Printing result
      println(result)
   }
}

/*
Output:
100
*/
Code by IncludeHelp, on August 13, 2022 20:46

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.