#### 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

## Iterating the Scala way

In the imperative Java world, we are used to writing loops. For example:

```int[] arr = new int[10];
for (int i = 0; i < 10; ++i) {
arr[i] = arr[i] + 1;
}
System.out.println(Arrays.toString(arr));```

This changes the array in place (that is, the original data structure is being mutated). The Scala way is:

```scala> val list = List.fill(10)(0)
list: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> list map (a => a + 1) foreach { a => print(" " + a) }
1 1 1 1 1 1 1 1 1 1
scala> list
res3: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
```

Instead of coding the loop, we pass in a function to a construct, to building blocks called combinators. These building blocks aka combinators allow themselves to be composed into a higher-level combinator. These combinators allow you to think in terms of higher-level operations and their interactions, instead of low-level details. This is an example of the power of higher order functions.

Also note that the original...