Skip to Content
Scala Functional Programming Patterns
book

Scala Functional Programming Patterns

by Atul S. Khot
December 2015
Intermediate to advanced content levelIntermediate to advanced
298 pages
5h 30m
English
Packt Publishing
Content preview from Scala Functional Programming Patterns

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in Scala

Functional Programming in Scala

Runar Bjarnason, Paul Chiusano
Scala Programming Projects

Scala Programming Projects

Mikael Valot, Nicolas Jorand

Publisher Resources

ISBN: 9781783985845Supplemental Content