reduce
definition
def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1demo
val donutPrices: Seq[Double] = Seq(1.5, 2.0, 2.5)
// find the sum of elements using reduce function explicitly
val sum: Double = donutPrices.reduce(_ + _)
val sum1: Double = donutPrices.reduce((a, b) => a + b)
// find the cheapest donut using reduce function
val priceMin = donutPrices.reduce(_ min _)
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
// concatenate the elements from the sequence using reduce function
val donutsConcat = donuts.reduce((left, right) => left + ", " + right)demo1
demo2
Last updated