#### Overview of this book

Scala Functional Programming Patterns
Credits
Aknowledgement
www.PacktPub.com
Preface
Free Chapter
Grokking the Functional Way
Singletons, Factories, and Builders
Recursion and Chasing your Own Tail
Lazy Sequences – Being Lazy, Being Good
Taming Multiple Inheritance with Traits
Of Visitors and Chains of Responsibilities
Traversals – Mapping/Filtering/Folding/Reducing
Higher Order Functions
Actors and Message Passing
Index

## Foreach–sugary sweetener

We briefly touched on the `for` expression. We also noted how the `for` expressions are similar to the map version. In fact, the Scala compiler translates the `for` expression into a form using map, flatMap, and filter. The following are how the forms of the expression are translated.

### One generator

This is a simple case that can be readily translated into a form that uses a map:

```scala> for (p <- List(1,2,3)) yield(p+1)
res10: List[Int] = List(2, 3, 4)
```

Gets translated into the following:

```scala> List(1,2,3) map (p => p + 1)
res11: List[Int] = List(2, 3, 4)
```

### A generator and a filter

Let's try printing pretty numbers from a list, such that each number is divisible by 3:

```scala> val p = (1 to 20).toList
p: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
scala> for (k <- p if k % 3 == 0) yield("<" + k + ">")
res12: List[String] = List(<3>, <6>, <9>, <12>, <15>, <18>)
```

This expression...