*foldLeft*

1 2 |
val letters = List("a", "b", "c") letters.foldLeft(" ") { (m: String, n: String) => m + n } will output 'abc'. |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
1st iteration ------------- Starts with " " and adds 'a' from the list. m:" " n: a = a 2nd iteration ------------- Starts with the result of the 1st iteration 'a' and adds 'b' from the list. m: a n: b = ab 3rd iteration ------------- Starts with the result of the 2nd iteration 'ab' and adds 'c' from the list. m: ab n: c = abc |

*Note:*

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

*foldRight*

1 2 |
val letters = List("a", "b", "c") letters.foldRight("") { (m: String, n: String) => m + n } will output 'abc'. |

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)* i*s* 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
1st iteration -------------- Starts with 'c' from the list and adds the start value " ". m: c n:" " = c 2nd iteration ------------- Starts with 'b' from the list and adds result from 1st iteration 'c'. m: b n: c = bc 3rd iteration ------------- Starts with 'a' from the list and adds result from 2nd iteration 'bc'. m: a n: bc = abc |

*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

- 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.
Fold is done on supertype of values in a given collection.

1 2 |
val letters = List("a", "b", "c") letters.foldRight("") { (m: String, n: String) => m + n } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Iteration A ------------ Starts with " " and adds 'a' from the list. m:"" n: a = a ------------- Iteration B in parallel with Iteration A Starts with " " and adds 'b' from the list. m:"" n: b = b Iteration C in parallel with Iteration B and Iteration A -------------------------------------------------------- Starts with " " string and adds 'b' from the list. m:"" n: c = c output abc |