Scala fold, foldLeft and foldRight

foldLeft

foldLeft takes 2 parameters. First one is a starting value, an empty string in our case. Second one is a function { (m: String, n: String) => m + n } , which in turn takes 2 parameters.

The first parameter (m) is an accumulator with an initial value of ” “. The second parameter (n) is the value from the list starting with “a”.

The following are the values for parameters m, n and the result at the end of each iteration.

Note:
The start value is passed as the first parameter to the function and list is evaluated from left to right.

foldRight

foldRight takes 2 parameters. First one is a starting value, an empty string. Second one is a function { (m: String, n: String) => m + n }‘ , which in turn takes 2 parameters.

The First parameter (m) is the value from the list starting with “c“. The second parameter (n) is an accumulator starting with the start value ” “. This is different from foldLeft.

The following are the values for parameters m, n and result at the end of each iteration.

Note:
The start value is passed as the second parameter to the function and list is evaluated from right to left. This is different from foldLeft.

fold

fold works same as above, except the order in which operations are performed is unspecified, because of this, there are 2 additional conditions on fold.

  1. Starting value must be neutral. e.g., Nil for list concatenation, 0 for addition, or 1 for multiplication. This is necessary as fold operation can work in parallel.
  2. Fold is done on supertype of values in a given collection.

Nested Functions and Methods in Scala

In Scala, you can nest methods and functions inside a method. This is a useful feature when you want to encapsulate a part of the logic specific to the enclosing method. The following is an example of a nested method.

When you run this code, it will print ‘I am a nested function’.

Similarly, you can do nested functions as below.

Passing Functions as Parameters in Scala & Java

Passing a function as a parameter helps in dividing complexity logically. For example, a function that iterates through a list of words and converts them to the upper case can be passed to any method that needs this functionality without exposing the list. It lets you delegate the complexity to the function.  Here is the code both in Scala and Java.

Scala

First define a method that takes a function as a parameter.

The above function fn, takes no parameters and outputs a String. Let’s define a function that takes no parameters and outputs a String, so we can pass it to methodA.

Pass functionB to functionA as below.

You should see “Hi, I am functionB from Scala, I am passed to functionA as a parameter.” in your browser. You can download the complete working example for both Scala and Java from my Git repo below. Here is the video version of this post.

Let’s implement the same thing in Java.

Java

Pass functionB to functionA as below.

You should see “Hi, I am functionB from Java, I am passed to functionA as a parameter.” in your browser. Download the working code from Git repo and run it. Go to http://localhost:8080/scala/passfunc and http://localhost:8080/java/passfunc to see the outputs from both Scala and Java respectively.