Scala | Reduce, fold or scan
Last Updated :
29 Mar, 2019
In this tutorial we will learn about Reduce, Fold and Scan functions in Scala.
- 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.
This code implements the Sum of elements in a sequence using reduce function.
Example :
object geeks
{
def main(arg : Array[String])
{
val seq _ elements : Seq[Double] = Seq( 3.5 , 5.0 , 1.5 )
println(s "Elements = $seq_elements" )
val sum : Double = seq _ elements.reduce((a, b) => a + b)
println(s "Sum of elements = $sum" )
}
}
|
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 :
object geeks
{
def main(arg : Array[String])
{
val seq _ elements : Seq[Double] = Seq( 3.5 , 5.0 , 1.5 )
println(s "Elements = $seq_elements" )
val maximum : Double = seq _ elements.reduce( _ max _ )
println(s "Maximum element = $maximum" )
val minimum : Double = seq _ elements.reduce( _ min _ )
println(s "Minimum element = $minimum" )
}
}
|
Output:
Elements = List(3.5, 5.0, 1.5)
Maximum element = 5.0
Minimum element = 1.5
- 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.
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 :
object geeks
{
def main(arg : Array[String])
{
val seq _ elements : Seq[Double] = Seq( 3.5 , 5.0 , 1.5 )
println(s "Elements = $seq_elements" )
val sum : Double = seq _ elements.fold( 0.0 )((a, b) => a + b)
println(s "Sum of elements = $sum" )
}
}
|
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 :
object geeks
{
def main(arg : Array[String])
{
val str _ elements : Seq[String] = Seq( "hello" ,
"Geeks" , "For" , "Geeks" )
println(s "Elements = $str_elements" )
val concat : String = str _ elements.fold( "" )(
(a, b) => a + "-" + b)
println(s "After concatenation = $concat" )
}
}
|
Output:
Elements = List(hello, Geeks, For, Geeks)
After concatenation = -hello-Geeks-For-Geeks
- 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.
This code implements iterations of sum of all elements using scan function.
Example :
object geeks
{
def main(arg : Array[String])
{
val numbers : Seq[Int] = Seq( 4 , 2 , 1 , 6 , 9 )
println(s "Elements of numbers = $numbers" )
val iterations : Seq[Int] = numbers.scan( 0 )( _ + _ )
println( "Running total of all elements" +
s "in the collection = $iterations" )
}
}
|
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 :
object geeks
{
def main(arg : Array[String])
{
val str _ elements : Seq[String] = Seq( "hello" ,
"Geeks" , "For" , "Geeks" )
println(s "Elements = $str_elements" )
val concat : Seq[String]
= str _ elements.scan( "" )((a, b) => a + "-" + b)
println(s "After concatenation = $concat" )
}
}
|
Output:
Elements = List(hello, Geeks, For, Geeks)
After concatenation = List(, -hello, -hello-Geeks, -hello-Geeks-For, -hello-Geeks-For-Geeks)
Share your thoughts in the comments
Please Login to comment...