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

## Lazy collections

All these combinators chaining are lovely, but there is a problem. For very large input data structures, this ends up creating intermediate copies.

Let's look at this aspect closely; you'll find that the solution is pretty revealing. Here is the Java code, just to set the stage:

```public class LargeLists {
public static void main(final String[] args) {
final int take = 5;
for (int i = 1000, n = 0; i < 1000000 && n < take; ++i) {
final int j = i + 1;
final int k = j * 2;
if (k % 4 != 0) {
System.out.println(k);
++n;
}
}
}
}```

Here, we iterate a range of numbers from `1000` to `1000000`. And for each number, we add `1` and then multiply the result by `2`. If the result is divisible by `4`, we discard it. Otherwise, we print the first five numbers from the result.

For example:

```scala> val list = (1000 to 1000000).toList
scala> list.map(_ + 1).map(_ * 2).filter(_ % 4 != 0).take(5)
res0: List[Int] = List(2002, 2006, 2010...```