O'Reilly logo

Scala Functional Programming Patterns by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Traversals – Mapping/Filtering/Folding/Reducing

We, software developers, love creating data structures and traversing them. We traverse structures so as to visit elements therein. A traversal typically uses a loop.

However, doesn't writing a for loop (or a foreach loop) seem like a routine job, also known as a boilerplate? I would rather concentrate on the element and let the language figure out and write the looping for me. Again, what I wish to do with each element depends on the context (that is, it varies). On the other hand, writing a for loop seems like typing the same chars again and again.

IDEs like Eclipse provide helpful completions for such routine and boring stuff. However, could we have the loop abstracted away, and instead ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required