Book Image

Mastering Functional Programming

Book Image

Mastering Functional Programming

Overview of this book

Functional programming is a paradigm specifically designed to deal with the complexity of software development in large projects. It helps developers to keep track of the interdependencies in the code base and changes in its state in runtime. Mastering Functional Programming provides detailed coverage of how to apply the right abstractions to reduce code complexity, so that it is easy to read and understand. Complete with explanations of essential concepts, practical examples, and self-assessment questions, the book begins by covering the basics such as what lambdas are and how to write declarative code with the help of functions. It then moves on to concepts such as pure functions and type classes, the problems they aim to solve, and how to use them in real-world scenarios. You’ll also explore some of the more advanced patterns in the world of functional programming such as monad transformers and Tagless Final. In the concluding chapters, you’ll be introduced to the actor model, which you can implement in modern functional languages, and delve into parallel programming. By the end of the book, you will be able to apply the concepts of functional programming and object-oriented programming (OOP)in order to build robust applications.
Table of Contents (17 chapters)

The Type Class pattern

Sometimes, the effect type that we are going to use is not known in advance. Consider the problem of logging. Logging can be performed to a list or a file. Logging to a list can be implemented using the Writer effect type.

A Writer is an abstraction of a pair of a result and a log generated by a computation. In its simplest form, a Writer can be understood as a pair of a list of strings and an arbitrary result. We can define the Writer effect type as follows:

case class SimpleWriter[A](log: List[String], value: A) {
def flatMap[B](f: A => SimpleWriter[B]): SimpleWriter[B] = {
val wb: SimpleWriter[B] = f(value)
SimpleWriter(log ++ wb.log, wb.value)
}
def map[B](f: A => B): SimpleWriter[B] =
SimpleWriter(log, f(value)
}

Notice that we have also defined the familiar map and flatMap methods for this effect type.

A few words should be said about...