Cover....1
Preface....14
Title Page....2
Copyright....3
Dedication....4
Contributors....5
Table of Contents....8
Part 1: Functional Programming Paradigm Essentials....20
Chapter 1: Introducing Functional Programming....22
What is functional programming?....22
Introducing first-class functions....23
What are pure functions?....24
Say what you want, not how you want it ....26
A brief history of functional programming....28
Modern functional programming....30
The Go programming paradigm....31
Why functional programming?....32
Why not functional programming in Go?....33
Comparing FP and OOP....34
Summary....35
Chapter 2: Treating Functions as First-Class Citizens....36
Benefits of first-class functions....36
Defining types for functions....37
Type aliases for primitives....37
Type aliases for functions....40
Using functions as objects....42
Passing functions to functions....42
In-line function definitions....43
Anonymous functions....43
Returning functions from functions....44
Functions in var....45
Functions inside data structures....46
Functions inside structs....47
Example 1 – map dispatcher....49
Creating a simple calculator....49
Example 2 – mocking functions for testing....51
Summary....55
Chapter 3: Higher-Order Functions....56
Technical requirements....57
An introduction to higher-order functions....57
Closures and variable scoping....57
Variable scoping in Go....58
Capturing variable context in functions (closures)....61
Partial application....63
Function currying, or how to reduce n-ary functions to unary functions....67
Example: function currying....69
Example: server constructor....70
Summary....74
Chapter 4: Writing Testable Code with Pure Functions....76
Technical requirements....76
What is purity?....77
Demonstrating pure versus impure function calls....77
Referential transparency....78
Idempotence....80
Statelessness....80
Side effects....80
Why does purity improve our code?....80
Increases the testability of our code....81
Increases the confidence in our code....83
Improved confidence in function names and signatures....84
Safer concurrency....85
When not to write pure functions....86
Input/output operations....86
Non-determinism can be desired....87
When we really have to panic!....87
How do we create pure functions?....87
Avoid global state....87
Separate pure and impure functionality....88
Example 1 – hotdog shop....90
Bad hotdog shop....90
Better hotdog shop....92
Summary....97
Chapter 5: Immutability....98
Technical requirements....98
What is immutability?....99
Immutability at the data layer....99
How to write immutable code in Go....100
Writing immutable code for collection data types....103
Measuring performance in mutable and immutable code....104
Benchmarking functions....104
Understanding stacks, heaps, and garbage collection....106
When to write mutable functions....109
What are functors and monads?....110
What’s a functor?....110
From functor to monad....112
Summary....115
Part 2: Using Functional Programming Techniques....116
Chapter 6: Three Common Categories of Functions....118
Technical requirements....118
Predicate-based functions....119
Implementing a Filter function....119
Any or all....123
Implementing DropWhile and TakeWhile....124
Map/transformation functions....126
Transformations while maintaining the data type....127
Data reducing functions....130
Example – working with airport data....134
Summary....137
Chapter 7: Recursion....138
Technical requirements....138
What is recursion?....139
Why do functional languages favor recursion?....141
When to use recursive functions....141
Iterating over trees....143
Recursion and functions as first-class citizens....146
Limits of recursive functions....151
Measuring the performance of recursive versus iterative solutions....151
Space limitation of recursive functions....153
Tail recursion as a solution to stack limitations....155
Summary....157
Chapter 8: Readable Function Composition with Fluent Programming....158
Technical requirements....158
Chaining functions through dot notation....159
Chaining methods for object creation (builder pattern)....159
Dot notation to chain functions on slices....161
Infinite data structures and lazy evaluation....170
Continuation-passing style programming....171
CPS and goroutines....177
When to use CPS?....178
Summary....178
Part 3: Design Patterns and Functional Programming Libraries....180
Chapter 9: Functional Design Patterns....182
Technical requirements....182
Classical design patterns in a functional paradigm....182
The strategy pattern....183
The decorator pattern....191
The Hollywood principle....195
Functional design patterns....197
Summary....199
Chapter 10: Concurrency and Functional Programming....200
Technical requirements....200
Functional programming and concurrency....201
Concurrency, parallelism, and distributed computing....201
Functional programming and concurrency....203
Creating concurrent functions....207
Concurrent filter implementation....207
Concurrent Map and FMap implementation....210
The pipeline pattern....214
Summary....222
Chapter 11: Functional Programming Libraries....224
Technical requirements....224
Is the library alive – and do the examples still match it?....225
Legal requirements....225
Pre-generics libraries for creating common FP functions....226
Code generation libraries for pre-generics Go....226
Post-generics functional programming libraries....232
Pie with generics....232
Lodash, for Go....234
Mo, for go....236
Summary....237
Index....238
About Packt....244
Other Books You May Enjoy....245
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.
If you are a Go engineer with a background in traditionally object-oriented languages such as Java or C++ and want to broaden your knowledge of functional programming, this book is for you.