https://commitlogs.com/2016/09/10/scala-fold-foldleft-and-foldright/
Code copied from the above link.
Print statements added to show the intermediate results.
val inputList: List[Int] = List(1, 3, 5)
inputList.foldLeft(0) { (acc, i) => acc + i }
def op(acc: Int, i: Int): Int = acc + i
op(acc = 0, i = 1) // 1
op(acc = 1, i = 3) // 4
op(acc = 4, i = 5) // 9
op(op(op(0, 1), 3), 5)
op(op(0 + 1, 3), 5)
op(op(1, 3), 5)
op(1 + 3, 5)
op(4, 5)
(4 + 5)
9
_________________________________________________________________________________
def len(list: List[Any]): Int = list.foldLeft(0) { (count, _) => count + 1 }
op(0, 1) // 1
op(1, 3) // 2
op(2, 5) // 3
op(op(op(0, 1), 3), 5)
op(op(1, 3), 5)
op(2, 5)
3
_________________________________________________________________________________
def len(list: List[Any]): Int = list.foldLeft(0) { (count, i) => println(s"""c: $count i: $i"""); count + 1 }
op(0, 1) // 1
op(1, 3) // 2
op(2, 5) // 3
op(op(op(0, 1), 3), 5)
op(op(1, 3), 5)
op(2, 5)
3
_________________________________________________________________________________
scala> def len(list: List[Any]): Int = list.foldLeft(0) { (count, i) => println(s"""c: $count i: $i"""); count + 1 }
len: (list: List[Any])Int
scala> val inputList: List[Int] = List(1, 3, 5)
inputList: List[Int] = List(1, 3, 5)
scala> len(inputList)
c: 0 i: 1
c: 1 i: 3
c: 2 i: 5
res0: Int = 3
_________________________________________________________________________________
scala> def last[A](list: List[A]): A = list.foldLeft[A](list.head) { (x, cur) => println(s"""x: $x cur: $cur"""); cur }
last: [A](list: List[A])A
scala> last(inputList)
x: 1 cur: 1
x: 1 cur: 3
x: 3 cur: 5
res1: Int = 5
_________________________________________________________________________________
scala> def average(list: List[Double]): Double = list match {
| case head :: tail => tail.foldLeft((head, 1.0)) { (avg, cur) =>
| ((avg._1 * avg._2 + cur)/(avg._2 + 1.0), avg._2 + 1.0)
| }._1
| case Nil => Double.NaN
| }
average: (list: List[Double])Double
scala> average(inputList.map(_.toDouble))
res4: Double = 3.0
_________________________________________________________________________________
scala> def average(list: List[Double]): Double = list match {
| case head :: tail => tail.foldLeft((head, 1.0)) { (avg, cur) =>
| println(s"""sum: ${avg._1 * avg._2 + cur} count: ${avg._2 + 1.0}""");
| ((avg._1 * avg._2 + cur)/(avg._2 + 1.0), avg._2 + 1.0)
| }._1
| case Nil => Double.NaN
| }
average: (list: List[Double])Double
scala>
scala> average(inputList.map(_.toDouble))
sum: 4.0 count: 2.0
sum: 9.0 count: 3.0
res7: Double = 3.0
_________________________________________________________________________________
scala> def reverse[A](list: List[A]): List[A] =
| list.foldLeft(List[A]()) { (r,c) => println(s"""r: ${r} c: ${c} """); c :: r }
reverse: [A](list: List[A])List[A]
scala> val l = List(1,3,5,7,9,11,13,15,17)
l: List[Int] = List(1, 3, 5, 7, 9, 11, 13, 15, 17)
scala> reverse(l)
r: List() c: 1
r: List(1) c: 3
r: List(3, 1) c: 5
r: List(5, 3, 1) c: 7
r: List(7, 5, 3, 1) c: 9
r: List(9, 7, 5, 3, 1) c: 11
r: List(11, 9, 7, 5, 3, 1) c: 13
r: List(13, 11, 9, 7, 5, 3, 1) c: 15
r: List(15, 13, 11, 9, 7, 5, 3, 1) c: 17
res10: List[Int] = List(17, 15, 13, 11, 9, 7, 5, 3, 1)
_________________________________________________________________________________
scala> def reverse[A](list: List[A]): List[A] =
| list.foldLeft(List[A]()) { (r,c) => println(s"""r: ${r} c: ${c} """); c :: r }
reverse: [A](list: List[A])List[A]
scala> val l = List(1,3,5,7,9,11,13,15,17)
l: List[Int] = List(1, 3, 5, 7, 9, 11, 13, 15, 17)
scala> reverse(l)
r: List() c: 1
r: List(1) c: 3
r: List(3, 1) c: 5
r: List(5, 3, 1) c: 7
r: List(7, 5, 3, 1) c: 9
r: List(9, 7, 5, 3, 1) c: 11
r: List(11, 9, 7, 5, 3, 1) c: 13
r: List(13, 11, 9, 7, 5, 3, 1) c: 15
r: List(15, 13, 11, 9, 7, 5, 3, 1) c: 17
res10: List[Int] = List(17, 15, 13, 11, 9, 7, 5, 3, 1)