Book Image

Scala Functional Programming Patterns

By : S. Khot
Book Image

Scala Functional Programming Patterns

By: S. Khot

Overview of this book

Scala is used to construct elegant class hierarchies for maximum code reuse and extensibility and to implement their behavior using higher-order functions. Its functional programming (FP) features are a boon to help you design “easy to reason about” systems to control the growing software complexities. Knowing how and where to apply the many Scala techniques is challenging. Looking at Scala best practices in the context of what you already know helps you grasp these concepts quickly, and helps you see where and why to use them. This book begins with the rationale behind patterns to help you understand where and why each pattern is applied. You will discover what tail recursion brings to your table and will get an understanding of how to create solutions without mutations. We then explain the concept of memorization and infinite sequences for on-demand computation. Further, the book takes you through Scala’s stackable traits and dependency injection, a popular technique to produce loosely-coupled software systems. You will also explore how to currying favors to your code and how to simplify it by de-construction via pattern matching. We also show you how to do pipeline transformations using higher order functions such as the pipes and filters pattern. Then we guide you through the increasing importance of concurrent programming and the pitfalls of traditional code concurrency. Lastly, the book takes a paradigm shift to show you the different techniques that functional programming brings to your plate. This book is an invaluable source to help you understand and perform functional programming and solve common programming problems using Scala’s programming patterns.
Table of Contents (13 chapters)
12
Index

What this book covers

Chapter 1, Grokking the Functional Way, gives you an eagle's eye view of functional programming and its advantages: succinct and readable code. Also, this chapter compares the command pattern in Java and Scala.

Chapter 2, Singletons, Factories, and Builders, covers singletons and Null Objects as specialized singletons. Scala Options are null objects. This also covers Scala's support for factory method and builders.

Chapter 3, Recursion and Chasing Your Own Tail, discusses the concept of recursion and Scala's support for it. It also looks at how recursion advocates immutability and the concept of structural sharing.

Chapter 4, Lazy Sequences – Being Lazy, Being Good, talks about eager versus lazy evaluation and the proxy design pattern. It also talks about Scala's streams and infinite lists.

Chapter 5, Taming Multiple Inheritance with Traits, covers Scala traits, mix-ins, and stackable modifications. It also covers dependency injection and the Cake pattern.

Chapter 6, Currying Favors with Your Code, covers lexical scope, closures, partially applied functions, and currying. This chapter also discusses the loan pattern, template method pattern, and another way to implement decorators.

Chapter 7, Of Visitors and Chains of Responsibilities, covers the Visitor pattern and its application. The other topics that are discussed are Scala's pattern matching capabilities and the chain of responsibility pattern. We will also learn Scala implementation using orElse and the collect idiom.

Chapter 8, Traversals – Mapping/Filtering/Folding/Reducing, covers iterators and functional iteration using map, filter, fold, and reduce. This chapter introduces Monads and explains ReduceLeft and ReduceRight.

Chapter 9, Higher Order Functions, discusses the strategy pattern and Scala version using higher order functions. It covers map as a functor, flatMap as a Monad, and foldLeft as Monoids. Here, you will also learn how to iterate lazy collections.

Chapter 10, Actors and Message Passing, showcases a case study to recursively grep a directory for files that contain matching text. It also covers the producer consumer pattern and the Master Slave pattern. It explains the concept of poison pills, event-driven programming, immutability, and concurrency. It also talks about Akka and Actors and how to reimplement recursive grep using Actors.

Chapter 11, It's a Paradigm Shift, teaches you how to sort in Scala and the Schwarzian transform implemented in Scala. It discusses functional error handling with Try/Success/Failure. And talks about Java Threads versus Scala's Futures. Scala's Parser Combinators are also discussed here.