• Share Ideas, Start Something Good.

### Scala – Fold

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

## Scala Tutorial Fold Function Example

Fold takes a twofold activity which consolidates every one of the components from the assortment and returns a solitary worth. The folds permits to characterize an underlying worth. Because of this property, fold can likewise oversee void assortments. On the off chance that the collection is unfilled, the value introduced turns into the last answer. Because of this we can likewise return an alternate worth from the arrangement of assortment utilizing beginning worth of some other datatype.

The `fold` function in Scala is a fundamental higher-order function used to combine elements of a collection into a single result by applying a binary operation. Understanding `fold` is essential for functional programming in Scala, as it enables concise and expressive ways to process collections.

The `fold` function takes an initial value (often referred to as an accumulator) and a binary operation as parameters. It then sequentially combines each element of the collection with the accumulator using the specified binary operation. The result is the final accumulated value after processing all elements of the collection.

In this example, `fold(0)(_ + _)` starts with an initial accumulator value of `0` and adds each element of the List to the accumulator using the addition operation (`_ + _`). The result (`sum`) will be `15`, which is the sum of all elements in the List.

By mastering the `fold` function, Scala developers can efficiently perform complex transformations and aggregations on collections, leading to more concise and functional code.Scala Tutorial Fold Function Example

#### use case 1:

``````// Scala program sum of elements
// using fold function
// Creating object
object prwatech {
// Main method
def main(arg:Array[String])
{
// initialize a sequence of elements
val seq_elements: Seq[Double] = Seq(.5, 2.0, 1.1)
println(s"Elements = \$seq_elements")

// find the sum of the elements using fold function
val sum: Double = seq_elements.fold(0.0)((a, b) => a + b)
println(s"Sum of elements = \$sum")
}
}
``````

output:

``````Elements = List(0.5, 2.0, 1.1)
Sum of elements = 3.6
``````

use case 2:

``````object prwatech {
// Main method
def main(arg:Array[String])
{
// initialize a sequence of elements
val seq_elements: Seq[Double] = Seq(.5, 2.0, 1.1)
println(s"Elements = \$seq_elements")

// find the difference of the elements using fold function
val sum: Double = seq_elements.fold(0.0)((a, b) => a - b)
println(s"difference of elements = \$sum")
}
}
``````

output:

``````Elements = List(0.5, 2.0, 1.1)
difference of elements = -3.6
``````

use case 3:

``````object prwatech {
// Main method
def main(arg:Array[String])
{
// initialize a sequence of strings
val str_elements : Seq[String] = Seq("hello", "Prwa", "Tech", "Bangalore")
println(s"Elements = \$str_elements")

// Concatenate strings with fold function
val concat: String = str_elements.fold("")(
(a, b) => a + "-" + b)
println(s"After concatenation = \$concat")
}
}
``````

output:

``````Elements = List(hello, Prwa, Tech, Bangalore)
After concatenation = -hello-Prwa-Tech-Bangalore
``````

0
0