Scala – Reduce

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

Scala Tutorial Reduce Function Example

 

Function on assortment information structure in scala that contains records, sets, guides, succession and tuples.

The reduce function in Scala is a higher-order function use to combine elements of a collection into a single result by applying a binary operation repeatedly. This function is particularly useful for aggregating values, computing cumulative results, or summarizing data in a concise manner.

In Scala, the reduce function takes a binary operator as an argument and applies it sequentially to elements of a collection. 

In this example, reduce(_ + _) iteratively applies addition (_ + _) to combine elements of the list, resulting in the sum of all elements.

Understanding how to use the reduce function is essential for functional programming in Scala, as it promotes concise and expressive code for data aggregation and computation. By leveraging reduce, developers can streamline data processing tasks and harness the power of functional programming paradigms effectively.Scala Tutorial Reduce Function Example etc

Understanding how to use the reduce function is essential for functional programming in Scala, as it enables concise and elegant solutions to data aggregation tasks. By leveraging reduce, developers can perform complex computations on collections with minimal code and maximum readability.

use case 1:

In this example:

  • numbers is a List containing integers [1, 2, 3, 4, 5].
  • The reduce function is on the numbers list.
  • The function (x, y) => x + y is passed as an argument to reduce.
    • Here, x and y are successive elements of the list.
    • The function x + y is applied iteratively to combine elements from the list.
  • The result of reduce is the sum of all elements in the list, computed as:
    • 1 + 2 (resulting in 3)
    • 3 + 3 (resulting in 6)
    • 6 + 4 (resulting in 10)
    • 10 + 5 (resulting in 15)
// Scala program sum of elements 
// using reduce function 
// Creating object
object prwatech {
// Main method
    def main(arg:Array[String])
    {
      // initialize a sequence of elements
        val seq_elements: Seq[Double] = Seq(3.1, 2.0, 1.5)
        println(s"Elements = $seq_elements") 
  
        // find the sum of the elements
        // using reduce function
        val sum: Double = seq_elements.reduce((a, b) => a + b)
        println(s"Sum of elements = $sum")
    }   
}

output:

Elements = List(3.1, 2.0, 1.5)
Sum of elements = 6.6

use case 2:

// Scala program to find maximum and minimum 
// using reduce function 
// Creating object
object prwatech {
// Main method
    def main(arg:Array[String])
    {
      // initialize a sequence of elements
        val seq_elements : Seq[Double] = Seq(2.1, 2.0, 1.5)
        println(s"Elements = $seq_elements")
  
        // find the maximum element using reduce function
        val maximum : Double = seq_elements.reduce(_ max _)
        println(s"Maximum element = $maximum")
  
        // find the minimum element using reduce function
        val minimum : Double = seq_elements.reduce(_ min _)
        println(s"Minimum element = $minimum")
    }
}

output:

Elements = List(2.1, 2.0, 1.5)
Maximum element = 2.1
Minimum element = 1.5

use case 3:

object prwatech {
// Main method
    def main(arg:Array[String])
    {
      // initialize a sequence of elements
        val seq_elements: Seq[Double] = Seq(3.5, 2.0)
        println(s"Elements = $seq_elements") 
  
        // find the diff of the elements
        // using reduce function
        val diff: Double = seq_elements.reduce((a, b) => a - b)
        println(s"diff of elements = $diff")
    }   
}

output:

Elements = List(3.5, 2.0)
diff of elements = 1.5
0
0

Quick Support

image image