
Functional Programming in Golang: Apply functional techniques in Go to improve the testability, readability, and security of your code
- Length: 274 pages
- Edition: 1
- Language: English
- Publisher: Packt Publishing
- Publication Date: 2023-04-11
- ISBN-10: 1801811164
- ISBN-13: 9781801811163
- Sales Rank: #1168997 (See Top 100 Books)
Learn how to leverage core functional programming (FP) concepts to write more maintainable and testable code in Go.
Key Features
- Learn functional programming techniques at the architectural level, and use the lessons learned to solve real-world problems.
- Get a deeper understanding of how to think functionally about code.
- Go is a multi-paradigm language, learn about the tradeoffs of FP and OOP.
Book Description
In “Functional Programming with Go”, you will learn the essential concepts of functional programming. Go is a multi-paradigm language that gives us the option to choose whichever paradigm works best for the particular problem we aim to solve. In this book, you will learn concepts central to the functional programming paradigm and how to leverage them to improve your Go codebases.
The book starts by explaining common concepts of functional programming and how to apply these to your codebase, even if you don’t fully buy into the functional mindset. The subsequent chapters of the book will build out a more comprehensive view of techniques and methods used in functional languages, such as Function Currying and Partial Application. We will develop this further into functional design patterns. Examples will be given throughout the book to show each concept “in action”.
By the end of this book, not only will you know how to apply these techniques in Go, but you will also know when to apply them.
What you will learn
- Learn how to think functionally about your code
- Learn about core FP concepts and how they apply to Go code
- Understand how FP can improve the testability of your codebase
- Learn functional design patterns
- Understand when to choose and not choose FP concepts
- Get a deeper understanding of functional programming
Who This Book Is For
If you are a Go engineer with a background in traditionally object-oriented languages such as Java or C++ and you want to broaden your knowledge of functional programming, this book is for you.
Functional Programming in Go Contributors About the author About the reviewer Preface Who this book is for What this book covers To get the most out of this book Download the example code files Download the color images Conventions used Get in touch Share your thoughts Download a free PDF copy of this book Part 1: Functional Programming Paradigm Essentials Chapter 1: Introducing Functional Programming What is functional programming? Introducing first-class functions What are pure functions? Say what you want, not how you want it A brief history of functional programming Modern functional programming The Go programming paradigm Why functional programming? Why not functional programming in Go? Comparing FP and OOP Summary Chapter 2: Treating Functions as First-Class Citizens Technical requirements Benefits of first-class functions Defining types for functions Type aliases for primitives Type aliases for functions Using functions as objects Passing functions to functions In-line function definitions Anonymous functions Returning functions from functions Functions in var Functions inside data structures Functions inside structs Example 1 – map dispatcher Creating a simple calculator Example 2 – mocking functions for testing Summary Chapter 3: Higher-Order Functions Technical requirements An introduction to higher-order functions Closures and variable scoping Variable scoping in Go Capturing variable context in functions (closures) Partial application Function currying, or how to reduce n-ary functions to unary functions Example: function currying Example: server constructor Summary Chapter 4: Writing Testable Code with Pure Functions Technical requirements What is purity? Demonstrating pure versus impure function calls Referential transparency Idempotence Statelessness Side effects Why does purity improve our code? Increases the testability of our code Increases the confidence in our code Improved confidence in function names and signatures Safer concurrency When not to write pure functions Input/output operations Non-determinism can be desired When we really have to panic! How do we create pure functions? Avoid global state Separate pure and impure functionality Example 1 – hotdog shop Bad hotdog shop Better hotdog shop Summary Chapter 5: Immutability Technical requirements What is immutability? Immutability at the data layer How to write immutable code in Go Writing immutable code for collection data types Measuring performance in mutable and immutable code Benchmarking functions Understanding stacks, heaps, and garbage collection When to write mutable functions What are functors and monads? What’s a functor? From functor to monad Summary Part 2: Using Functional Programming Techniques Chapter 6: Three Common Categories of Functions Technical requirements Predicate-based functions Implementing a Filter function Any or all Implementing DropWhile and TakeWhile Map/transformation functions Transformations while maintaining the data type Data reducing functions Example – working with airport data Summary Chapter 7: Recursion Technical requirements What is recursion? Why do functional languages favor recursion? When to use recursive functions Iterating over trees Recursion and functions as first-class citizens Limits of recursive functions Measuring the performance of recursive versus iterative solutions Space limitation of recursive functions Tail recursion as a solution to stack limitations Summary Chapter 8: Readable Function Composition with Fluent Programming Technical requirements Chaining functions through dot notation Chaining methods for object creation (builder pattern) Dot notation to chain functions on slices Infinite data structures and lazy evaluation Continuation-passing style programming CPS and goroutines When to use CPS? Summary Part 3: Design Patterns and Functional Programming Libraries Chapter 9: Functional Design Patterns Technical requirements Classical design patterns in a functional paradigm The strategy pattern The decorator pattern The Hollywood principle Functional design patterns Summary Chapter 10: Concurrency and Functional Programming Technical requirements Functional programming and concurrency Concurrency, parallelism, and distributed computing Functional programming and concurrency Creating concurrent functions Concurrent filter implementation Concurrent Map and FMap implementation The pipeline pattern Summary Chapter 11: Functional Programming Libraries Technical requirements Is the library alive – and do the examples still match it? Legal requirements Pre-generics libraries for creating common FP functions Code generation libraries for pre-generics Go Post-generics functional programming libraries Pie with generics Lodash, for Go Mo, for go Summary Index Why subscribe? Other Books You May Enjoy Packt is searching for authors like you Share your thoughts Download a free PDF copy of this book
How to download source code?
1. Go to: https://github.com/PacktPublishing
2. In the Find a repository… box, search the book title: Functional Programming in Golang: Apply functional techniques in Go to improve the testability, readability, and security of your code
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.