Updated March 29, 2023
Introduction to Scala Fold
Scala Fold is a very important and powerful operation over Scala Collections that accepts the input operation and returns a single value from a collection.
Having an initialized initial value can also work over an empty collection, giving the initialized value only as a result. We are folding a collection from both sides, left or right, with the use of the SCALA FOLD function over the collection. The order over which the operation is to be performed overfold is non-deterministic.
Syntax
FOLD
deffold[b >: b1](z: b)(op: (b, b) => b): b // First Argument as the Initial Value
FOLD LEFT
deffoldLeft[B](z: B)(op: (B, A) => B): B // Second Argument as the Operation with return value.
FOLD RIGHT
final deffoldRight[B](z: B)(op: (A, B) => B): B
Scala Fold Functioning
Fold function takes up the initial value and the operation provided with the scala collection as the input and gives back the result over iterating over the collection.
It has an initial value that can be used further with the statement or else will be returned if an empty collection is given.
We can even change the data type of the value that we want to get out as the output by changing the Data type of the Initial Value variable.
Since the order is not preserved in the fold method, it uses two functions, FOLD LEFT AND FOLD RIGHT, to define the order over which the operation is to be performed.
These operations apply to both Scala Mutable as well as Non-mutable collections.
It takes on two arguments as its input parameter. The First argument is the initial value that we give in, and the second is the function inside which our operations are written over.
Whenever an execution starts, the first argument goes on with the first item of the second argument over a list, and a return statement is generated. Once that return statement is generated, it moves to the next item with the return statement as the first input parameter, and further traversal goes on.
So by this, when the end of the loop is achieved, the last returned value is the output generated.
To preserve the order, we use the Fold Right and Fold Left Functions.
Let us check now with Examples:
This is a small scala add program with the help of a Fold function.
scala> val a = List(1,2,3,4,5,6)
a: List[Int] = List(1, 2, 3, 4, 5, 6)
scala> val b = a.fold(2){
| (c,d) => c+d
| }
b: Int = 23
So the above code takes up the initial value function and operates that to produce results.
Code:
SCALA FOLD LEFT does the traversing of elements from a collection from left to right. So the order will be from left to right, the elements extreme left will be taken into consideration first while traversing it from left to right.
Let us check that with an example:-
scala> val a = List("Ram","is","happy!!")
a: List[String] = List(Ram, is, happy!!)
scala> val b = a.fold
fold foldLeft foldRight
scala> val b = a.foldLeft("After Concatatination :- "){
| (g,h)=>(g+h)
| }
b: String = After Concatatination :- Ramishappy!!
scala>
scala> val b = a.foldLeft("After Concatatination :- "){ (g,h) => (g +" "+h) }
b: String = After Concatatination :- Ram is happy.
SCALA FOLD RIGHT does the traversing of elements from a collection from right to left. So the order will be from right to left, the elements extreme right will be taken into consideration first while traversing it from right to left.
Let us check that with an example:-
scala> val a = List("Ram","is","happy!!")
a: List[String] = List(Ram, is, happy!!)
scala> val b = a.foldRight("After Conatatination :- ")((v,c)=>(v+" "+c))
b: String = "Ram is happy!! After Conatatination :- "
scala> val b = a.foldRight("After Conatatination But using Fold right:- ")((v,c)=>(v+" "+c))
b: String = "Ram is happy!! After Conatatination But using Fold right:- "
scala> val a = List(3,4,5,6)
a: List[Int] = List(3, 4, 5, 6)
scala> val b = a.fold(0)((a,b)=>a+b)
b: Int = 18
scala> val b = a.foldLeft(0)((a,b)=>a+b)
b: Int = 18
scala> val b = a.foldRight(0)((a,b)=>a+b)
b: Int = 18
Here we see the results are the same, but the order of execution for the code while execution is different.
scala> val b = a.foldRight(0.0)((a,b)=>a+b)
b: Double = 18.0
scala> val b = a.foldLeft(0.0)((a,b)=>a+b)
b: Double = 18.0
scala> val b = a.foldLeft(0.0)((a,b)=>a+b)
b: Double = 18.0
Even as stated above, the data type can be changed only by changing the data type of the initial variable used. So from the above example, we can see that the Data Type is changed to Double for an Integer Variable.
Code:
So by this article, we tried to see how scala Fold is used in Scala Application and the various functionalities over Scala Application.
Conclusion
Here from the above article, we saw how we can use the scala fold method and use the operation for various Scala Functioning. With the help of examples, we also saw how scala takes the initial values and does the operation over that function; we also checked how the order is achieved with the help of the Fold Left and Fold Right option over the scala collection.
So from the above article, we can conclude that the Scala Fold function is a powerful Scala operation for various object-oriented programming approaches.
Recommended Articles
This is a guide to Scala fold. Here we discuss the Scala Fold Functioning and examples of how scala takes the initial values along with the codes. You may also have a look at the following articles to learn more –