Definition of foldLeft and foldRight

foldLeft

foldLeft

please notice the definition of op : (op: (B, A) => B)

here, A is the element of the caller, B is the intermedia result of function op

foldRight

foldRight

Example

foldLeft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Foo(val name: String, val age: Int, val sex: Symbol)

object Foo {
def apply(name: String, age: Int, sex: Symbol) = new Foo(name, age, sex)
}

val fooList = Foo("Hugh Jass", 25, 'male) ::
Foo("Biggus Dickus", 43, 'male) ::
Foo("Incontinentia Buttocks", 37, 'female) ::
Nil


val stringList = fooList.foldLeft[List[String]](List[String]()) { (z, f) =>
val title = f.sex match {
case 'male => "Mr."
case 'female => "Ms."
}
z :+ s"$title ${f.name}, ${f.age}"
}

the result is :

1
stringList: List[String] = List(Mr. Hugh Jass, 25, Mr. Biggus Dickus, 43, Ms. Incontinentia Buttocks, 37)

foldRight

1
2
3
4
5
6
7
val stringList2 = fooList.foldRight[List[String]](List[String]()) { (z, f) =>
val title = f.sex match {
case 'male => "Mr."
case 'female => "Ms."
}
z :+ s"$title ${f.name}, ${f.age}"
}

you would get error:

1
2
Error:(34, 23) value sex is not a member of List[String]
val title = f.sex match {

why ?

notice this code snippet : { (z, f) => }

for foldRight, f is the intermedia result of the function, of course he would have the sex symbol.

so we need to switch them like this:

1
2
3
4
5
6
7
val stringList2 = fooList.foldRight[List[String]](List[String]()) { (f, z) =>
val title = f.sex match {
case 'male => "Mr."
case 'female => "Ms."
}
z :+ s"$title ${f.name}, ${f.age}"
}

result:

1
stringList2: List[String] = List(Ms. Incontinentia Buttocks, 37, Mr. Biggus Dickus, 43, Mr. Hugh Jass, 25)