Book description
Function literals, Monads, Lazy evaluation, Currying, and more
About This Book
- 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
This book is for Golang developers comfortable with OOP and interested in learning how to apply the functional paradigm to create robust and testable apps. Prior programming experience with Go would be helpful, but not mandatory.
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
In Detail
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.
Style and approach
This book takes a pragmatic approach and shows you techniques to write better functional constructs in Golang. We’ll also show you how use these concepts to build robust and testable apps.
Table of contents
- Title Page
- Copyright
- Credits
- About the Author
- Acknowledgments
- About the Reviewer
- www.PacktPub.com
- Customer Feedback
- Preface
-
Pure Functional Programming in Go
- Motivation for using FP
- Getting the source code
- Imperative versus declarative programming
- Pure functions
- Fibonacci sequence - a simple recursion and two performance improvements
- The difference between an anonymous function and a closure
- Testing FP using test-driven development
- A journey from imperative programming to pure FP and enlightenment
- Summary
-
Manipulating Collections
- Iterating through a collection
- Piping Bash commands
- Functors
- Predicates
- Map and filter
- Contains
- If Go had generics
- Itertools
- Functional  packages
- Another time of reflection
- The cure
- Summary
- Using High-Order Functions
- SOLID Design in Go
-
Adding Functionality with Decoration
- Interface composition
- Decorator pattern
- A decorator implementation
- Summary
-
Applying FP at the Architectural Level
- Application architectures
- The role of systems engineering
- Managing Complexity
- FP influenced architectures
- Domain Driven Design
- Domain Driven Design
- A cloud bucket application
- FP and Micyoservices
- Summary
-
Functional Parameters
-
Refactoring long parameter lists
- What's wrong with a function signature with more than seven parameters?
- Refactoring - the book
- Edsger W. Dijkstra says OOP is a bad idea
-
A closer look at refactoring
- Passing every parameter a function requires to do its job is not a good idea
- Methods can query other objects' methods internally for data required to make decisions
- Methods should depend on their host class for needed data
- Pass a whole object with required attributes to reduce the number of required parameters
- Replace parameter with method technique to reduce the number of required parameters
- Use a parameter object when we have unrelated data elements to pass
- Long parameter lists will change over time and are inherently difficult to understand
- The solution 
- Functional parameters
- Contexts
- Summary
-
Refactoring long parameter lists
- Increasing Performance Using Pipelining
-
Functors, Monoids, and Generics
- Understanding functors
- Solve lack of generics with metaprogramming
- Generics code generation tool
- Generics implementation options
- The shape of a functor
- Composition operation
- Functional composition in the context of a legal obligation
- Build a 12-hour clock functor
- The car functor
- Monoids
- Monoid examples
- Summary
- Monads, Type Classes, and Generics
-
Category Theory That Applies
- Our goal
- Proof theory
- The Curry Howard isomorphism
-
Historical Events in Functional Programming
- George Boole (1815 - 1864)
- Augustus De Morgan (1806 - 1871)
- Friedrich Ludwig Gottlob Frege (1848 – 1925)
- Charles Lutwidge Dodgson (1832 –1898)
- Alfred Whitehead and Bertrand Russell (1903)
- Moses Schonfinkel (1889–1942)
- Haskell Curry - 1927
- Gerhard Gentzen (1936)
- Alonzo Church (1930, 1940)
- Alan Turing (1950)
- MacLane and Eilenberg (1945)
- John McCarthy (1950)
- Curry-Howard-Lambek Correspondence (1969)
- Roger Godement (1958)
- Moggi, Wadler, Jones (1991)
- Gibbons, Oliveira (2006)
- The history of FP in a nutshell
- Programming language categories
- The Lambda Calculus
- The importance of Type systems to FP
- Domains, codomains, and morphisms
- Set theory symbols
- Category theory
- Morphisms
- Homomorphism
- Composable concurrency
- Graph Database Example
- Using mathematics and category theory to gain understanding
- Fun with Sums, Products, Exponents and Types
- Big data, knowledge-driven development, and data visualization
- Summary
-
Miscellaneous Information and How-Tos
-
How to build and run Go projects
- TL;DR
- Development workflow
- Dot init features and benefits
- Motivation for using goenv
- Motivation for using the init script
- Ways to manage Go dependencies
- Each dot init step in detail
- Adding standard library imports
- Adding third-party imports
- Development workflow summary
- A note about Go Dependency Management
- A conversation - Java developer, idiomatic Go developer, FP developer
- How to propose changes to Go
- FP resources
- Minggatu - Catalan number
-
How to build and run Go projects
Product information
- Title: Learning Functional Programming in Go
- Author(s):
- Release date: November 2017
- Publisher(s): Packt Publishing
- ISBN: 9781787281394
You might also like
book
Learning Functional Programming
Learn how to think and write code like a functional programmer. With this practical guide, software …
book
The Go Programming Language
is the authoritative resource for any programmer who wants to learn Go. It shows how to …
book
Learn Data Structures and Algorithms with Golang
Explore Golang's data structures and algorithms to design, implement, and analyze code in the professional setting …
book
Hands-On Software Engineering with Golang
Explore software engineering methodologies, techniques, and best practices in Go programming to build easy-to-maintain software that …