VOOZH about

URL: https://www.geeksforgeeks.org/scala/scala-reduce-fold-or-scan/

⇱ Scala | Reduce, fold or scan - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scala | Reduce, fold or scan

Last Updated : 29 Mar, 2019
In this tutorial we will learn about Reduce, Fold and Scan functions in Scala.
  1. Reduce : Reduce function is applied on collection data structure in scala that contains lists, sets, maps, sequence and tuples. Parameter in the reduce function is a binary operation which merges all the elements from the collection and returns a single value. The first two values is combined with the binary operation and the resultant of that operation combines with the next value of the collection and atlast we obtain a single value.
    👁 Image
    Using Reduce to Calculate Sum of all the numbers in a List
    This code implements the Sum of elements in a sequence using reduce function. Example : Output:
    Elements = List(3.5, 5.0, 1.5)
    Sum of elements = 10.0
    
    This code finds the maximum and minimum element in the sequence using reduce function Example : Output:
    Elements = List(3.5, 5.0, 1.5)
    Maximum element = 5.0
    Minimum element = 1.5
    
  2. Fold : Like reduce fold also takes a binary operation which merges all the elements from the collection and returns a single value. The difference is that fold allows us to define an initial value. Due to this property, fold can also manage empty collections. If the collection is empty, the value initialized becomes the final answer. Due to this we can also return a different value from the set of collection using initial value of some other datatype. Reduce can only return the value of the same type because its initial value is the first value from the collection. 👁 Image
    This code implements the Sum of elements in a sequence using fold function. Here initial value is taken as 0.0 as the sequence is in datatype Double. Example : Output:
    Elements = List(3.5, 5.0, 1.5)
    Sum of elements = 10.0
    
    This code concatenate the strings with hyphen. We use initial value as empty string. So our fold method will apply the operator on empty string as well where as with reduce we would not get the hyphen before the first value of the collection. Example : Output:
    Elements = List(hello, Geeks, For, Geeks)
    After concatenation = -hello-Geeks-For-Geeks
    
  3. Scan : Scan function takes the binary operation as parameter and returns the value for each element in collection for that operation. It returns each iteration for that binary operator in the collection. In scan also we can define the initial value. 👁 Image
    This code implements iterations of sum of all elements using scan function. Example : Output:
    Elements of numbers = List(4, 2, 1, 6, 9)
    Running total of all elements in the collection = List(0, 4, 6, 7, 13, 22)
    
    This is the implementation of concatenation of the strings with hyphen and shows the iterations. Example : Output:
    Elements = List(hello, Geeks, For, Geeks) After concatenation = List(, -hello, -hello-Geeks, -hello-Geeks-For, -hello-Geeks-For-Geeks)
Comment
Article Tags:
Article Tags:

Explore