Book Image

Functional Programming in Go

By : Dylan Meeus
Book Image

Functional Programming in Go

By: Dylan Meeus

Overview of this book

While Go is a multi-paradigm language that gives you the option to choose whichever paradigm works best for the particular problem you aim to solve, it supports features that enable you to apply functional principles in your code. In this book, you’ll learn about concepts central to the functional programming paradigm and how and when to apply functional programming techniques in Go. Starting with the basic concepts of functional programming, this Golang book will help you develop a deeper understanding of first-class functions. In the subsequent chapters, you’ll gain a more comprehensive view of the techniques and methods used in functional languages, such as function currying, partial application, and higher-order functions. You’ll then be able to apply functional design patterns for solving common programming challenges and explore how to apply concurrency mechanisms to functional programming. By the end of this book, you’ll be ready to improve your code bases by applying functional programming techniques in Go to write cleaner, safer, and bug-free code.
Table of Contents (17 chapters)
1
Part 1: Functional Programming Paradigm Essentials
7
Part 2: Using Functional Programming Techniques
11
Part 3: Design Patterns and Functional Programming Libraries

What this book covers

In Chapter 1, Introducing Functional Programming, we are going to take a bird’s eye view of the what and why behind functional programming. To start, we will take a brief look at the history and contemporary state of functional programming methodologies. Then we will take a look at how functional programming compares to the more traditional object-oriented programming.

In Chapter 2, Treating Functions as First-Class Citizens, we are going to cover exactly why functions are powerful in languages that treat them as first-class citizens. Go has functions as first-class citizens out of the box, meaning we get this functionality. We are going to see how this allows us to create function-centered constructs that improve the readability and testability of our code.

In Chapter 3, Higher-Order Functions, we are going to explore the concept of function composition through higher-order functions. There are a variety of new concepts that are introduced here, such as closures, partial application, and function currying. We will take a look at some practical examples and real-world use cases for these.

In Chapter 4, Write Testable Code with Pure Functions, we will take a look at what it means for a language, and a function, to be considered pure. We will take a look at some of the tradeoffs between function purity and impurity, and explore how pure functions help us write testable code.

In Chapter 5, Immutability, we cover what exactly it means to be immutable, and how the Go language can help preserve immutability at the struct level. To understand how this works, we will look at how Go handles pointers and references to objects, what the performance implications are, and how to decide between the pointer-reference trade-offs. We will also dive into the implications for garbage collection, unit testing, and pure functional programming.

In Chapter 6, Three Common Categories of Functions, we are going to look at some practical implementations of functions that leverage the concepts of functional programming covered up to now. We will build Filter functions, Map functions and Reducers.

In Chapter 7, Recursion, we are going to talk about recursion. This is a topic that all programmers encounter sooner or later, as it’s not exclusive to the functional paradigm. Any language in which you can express function calls also allows you to express functions that are recursive in nature. But in functional languages, these take center stage. We will look at the implications for this in Go.

In Chapter 8, Readable Function Composition with Fluent Programming, we are going to look at different methods for chaining functions in functional programming. The end goal here is to write code that is easier to read and creates less visual clutter. We will look at three ways for achieving this. First, we will take a look at how we can use type aliases to attach methods to container types, allowing us to create chained functions with the familiar dot notation. Next, we will look at continuation-passing style programming and consider the trade-offs of each approach.

In Chapter 9, Functional Design Patterns, we will move to a higher level of abstraction. Rather than talking about individual functions and operations, we will look at design patterns. While we will not extensively explain each design pattern, we will take a look at how the object-oriented pattern translates to the functional world.

In Chapter 10, Concurrency and Functional Programming, we consider how concurrency is all around us, both in the real world as well as the virtual one. In this chapter we will start by looking at concurrency, parallelism, and distributed computation. Next, we will focus on how the concurrency mechanisms in Go can help us write functional code.

In Chapter 11, Functional Programming Libraries, we will explore several libraries that can help us build programs in the functional paradigm. We will look both at pre-generic libraries and post-generic libraries.