▶Book Description
Functional programming is a popular programming paradigm that is used to simplify many tasks and will help you write flexible and succinct code. It allows you to decompose your programs into smaller, highly reusable components, without applying conceptual restraints on how the software should be modularized.
This book bridges the language gap for Golang developers by showing you how to create and consume functional constructs in Golang.
The book is divided into four modules. The first module explains the functional style of programming; pure functional programming (FP), manipulating collections, and using high-order functions. In the second module, you will learn design patterns that you can use to build FP-style applications. In the next module, you will learn FP techniques that you can use to improve your API signatures, to increase performance, and to build better Cloud-native applications. The last module delves into the underpinnings of FP with an introduction to category theory for software developers to give you a real understanding of what pure functional programming is all about, along with applicable code examples.
By the end of the book, you will be adept at building applications the functional way.
▶What You Will Learn
⦁ Learn how to compose reliable applications using high-order functions
⦁ Explore techniques to eliminate side-effects using FP techniques such as currying
⦁ Use first-class functions to implement pure functions
⦁ Understand how to implement a lambda expression in Go
⦁ Compose a working application using the decorator pattern
⦁ Create faster programs using lazy evaluation
⦁ Use Go concurrency constructs to compose a functionality pipeline
⦁ Understand category theory and what it has to do with FP
▶Key Features
⦁ Write concise and maintainable code with streams and high-order functions
⦁ Understand the benefits of currying your Golang functions
⦁ Learn the most effective design patterns for functional programming and learn when to apply each of them
⦁ Build distributed MapReduce solutions using Go
▶Who This Book Is For
For the programming sections in this book, you should have at least one year programming experience. Proficiency with Go or Haskell is ideal, but experience with other languages such as C/C++, Python, Javascript, Java, Scala or Ruby is also sufficient. You should have some familiarity using the command line.
▶What this book covers
⦁ Chapter 1, Pure Functional Programming in Go, introduces the declarative style of programming and demonstrates recursion, memorization, and Go's concurrency constructs using the Fibonacci Sequence. We will learn how to benchmark/performance test your recursive code and we will get some bad news.
⦁ Chapter 2, Manipulating Collections, shows us how to use intermediate (Map, Filter, and Sort) and terminal (Reduce, GroupBy, and Join) functions to perform data transformations. We use a Mocha-like BDD Go framework to test predicate functions. Itertools helps us grasp the breadth of FP collection manipulating function and we look at a distributed MapReduce solution: Gleam = Go + LuaJIT + Unix Pipes.
⦁ Chapter 3, Using High-Order Functions, covers a list of 27 FP characteristics: Anonymous function, closures, currying, Either data type, first-class functions, functions, functional composition, Hindley-Milner type system, Idempotence, immutable state, immutable variables, Lambda expressions, List Monad, Maybe data type, Maybe Monad, Monadic error, handling, No side-effects, operator overloading, option type, parametric polymorphism, partial function application, recursion, referential transparency, sum or union types, Tail Call Optimization, typeclasses, and Unit type. It also covers an example of Generics, and illustrates its value to FP programmers. We implement the Map, Filter, and Reduce functions, as well as lazy evaluation using Goroutines and a Go channel.
⦁ Chapter 4, SOLID Design in Go, talks about why Gophers loath Java, principles of good software design, how to apply the Single Responsibility principle, function composition, the open/closed Principle, FP contracts, and duck typing. It also covers how to model behavior using interfaces, compose software using the Interface Segregation principle and embedded interfaces. We will learn about the law of Associativity with a purple Monoid chain and get the big reveal—.Monads chain continuations.
⦁ Chapter 5, Adding Functionality with Decoration, illustrates interface composition using Go's complimentary Reader and Writer interfaces. Next, we will learn how procedural design compares to functional Inversion of Control. We will implement the following decorators: authorization, logging, and load balancing. Also, we will add easy-metrics to our app to see our decorator pattern in action.
⦁ Chapter 6, Applying FP at the Architectural Level, builds an application framework using a layered architecture, which solves cyclical dependency errors. We will learn how to apply the Hollywood principle and the difference between the observer pattern and dependency injection. We will use Inversion of Control (IoC) to control the flow of logic and build a layered application. Also, we will build an effective table-driven framework to test our application's API.
⦁ Chapter 7, Functional Parameters, enlightens us as to why a lot of we've learned from Java and object-oriented programming does not apply to Go, teaches us a better way to refactor long parameter lists using functional options, and helps us understand the difference between currying and partial application. We will learn how to apply partial application to create another function with a smaller arity. We will use a context to gracefully shut down our server and see how to cancel and roll back a long-running database transaction using a context.
⦁ Chapter 8, Increase Performance Using Pipelining, covers data flow types (Read, Split, Transform, Merge, and Write) and teaches us when and how to build a data transformation pipeline. We use buffering to increase throughput, goroutines and channels to process data faster, improve API readability using interfaces, and implement some useful filters. We also implement and compare imperative and functional pipeline designs for processing credit card charges.
⦁ Chapter 9, Functors, Monoids, and Generics, gives us an appreciation for the lack of support for Generics in Go. We will see how to use a code generation tool to solve the repetitive boilerplate code problem. We will dive deep into function composition, implement a few functors, and learn how to map between worlds. We will also learn how to write a Reduce function to implement an invoice processing monoid.
⦁ Chapter 10, Monads, Type Classes, and Generics, shows us how a Monad works and teaches us how to compose functions using the Bind operation. It shows us how Monads process errors and deal with Input/Output (I/O). This chapter works through a monadic workflow implementation in Go. We cover what The Lambda Calculus is and what it has to do with Monads, see how The Lambda Calculus implements Recursion, and learn how the YCombinator works in Go. Next, we use the Y-Combinator to control a workflow and learn how to handle all errors at the end of the pipe. We will learn how type classes work and implement a few in Go. Finally, we review the pros and cons of generics in Go.
⦁ Chapter 11, Category Theory That Applies, gives us a working understanding of category theory. We will learn to appreciate the deep connection between category theory, logic, and type theory. We will increase our understanding with a journey through the history of FP. This chapter uses a Venn diagram to help explain various categories of programming languages. We come to understanding of what binding, currying, and application mean in the context of a lambda expression. This chapter shows us that the Lambda Calculus is like chocolate milk. This chapter covers the type system implications of FP, shows us different categories of homomorphisms and when to use them, and uses mathematics and the flight of a soccer ball to increase our understanding of morphisms. We will cover function composition with linear and quadratic functions, and we will learn about interface-driven development. We'll explore the value in knowledge-driven systems, and we will learn how to apply our understanding of category theory to build better applications.