Scala Collections FoldLeft Method
The foldLeft strategy takes an acquain twofold administrator work as boundary and will utilize it to implode components from the assortment.
The request for navigating the components in the assortment is from left to right and subsequently the name foldLeft. The foldLeft strategy permits you to likewise determine an underlying worth.
The foldLeft
method is a fundamental higher-order function in Scala collections that allows for iterative aggregation or reduction of elements in a collection.
use case 1:
scala> val prices: Seq[Double] = Seq(9.5, 3.0, 7)
scala> val sum = prices.foldLeft(0.0)(_ + _)
2use case 2:
scala> val eduprwa: Seq[String] = Seq("Data", "Science", "Prwatech")
scala> println(s"All eduprwa = ${eduprwa.foldLeft("")((a, b) => a + " eduprwa " + b)}")
use case 3:
scala> val prices: Seq[Double] = Seq(4.0, 2.0)
scala> val diff = prices.foldLeft(5.0)(_ - _)