Scala – Fold Left

  • date 30th May, 2021 |
  • by Prwatech |
  • 0 Comments

The foldLeft strategy takes an acquainted 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.

use case 1:

scala> val prices: Seq[Double] = Seq(9.5, 3.0, 7)

scala> val sum = prices.foldLeft(0.0)(_ + _)

use 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)(_ - _)

Quick Support

image image