𝔖 Scriptorium
✦   LIBER   ✦

πŸ“

Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code

✍ Scribed by Dylan Meeus


Publisher
Packt Publishing
Year
2023
Tongue
English
Leaves
248
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


Leverage core functional programming (FP) concepts to write more maintainable and testable code in Go

Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Learn functional programming techniques at the architectural level and use them to solve real-world problems
  • Understand how to think about code functionally
  • Learn about the trade-offs of functional programming and object-oriented programming (OOP) in Go

Book Description

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.

What you will learn

  • Gain a deeper understanding of functional programming through practical examples
  • Build a solid foundation in core FP concepts and see how they apply to Go code
  • Discover how FP can improve the testability of your code base
  • Apply functional design patterns for problem solving
  • Understand when to choose and not choose FP concepts
  • Discover the benefits of functional programming when dealing with concurrent code

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 want to broaden your knowledge of functional programming, this book is for you.

Table of Contents

  1. Introducing Functional Programming
  2. Treating Functions as First-Class Citizens
  3. Higher -Order Functions
  4. Write Testable Codes with Pure Functions
  5. Immutability
  6. Three Common Categories of Functions
  7. Recursion
  8. Readable Function Composition with Fluent Programming
  9. Functional Design Patterns
  10. Thinking functionally to solve problems
  11. Functional Programming Libraries

✦ Table of Contents


Cover
Preface
Title Page
Copyright
Dedication
Contributors
Table of Contents
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
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
About Packt
Other Books You May Enjoy


πŸ“œ SIMILAR VOLUMES


Functional Programming in Go: Apply func
✍ Dylan Meeus πŸ“‚ Library πŸ“… 2023 πŸ› Packt Publishing 🌐 English

<p><span>Leverage core functional programming (FP) concepts to write more maintainable and testable code in Go</span></p><p><span>Purchase of the print or Kindle book includes a free PDF eBook</span></p><h4><span>Key Features</span></h4><ul><li><span><span>Learn functional programming techniques at

Functional Programming in Go: Apply func
✍ Dylan Meeus πŸ“‚ Library πŸ› Packt Publishing 🌐 English

<p><span>Leverage core functional programming (FP) concepts to write more maintainable and testable code in Go</span></p><p><span>Purchase of the print or Kindle book includes a free PDF eBook</span></p><h4><span>Key Features</span></h4><ul><li><span><span>Learn functional programming techniques at

Functional Programming in Go: Apply func
✍ Dylan Meeus πŸ“‚ Library πŸ› Packt Publishing 🌐 English

<p><span>Leverage core functional programming (FP) concepts to write more maintainable and testable code in Go</span></p><p><span>Purchase of the print or Kindle book includes a free PDF eBook</span></p><h4><span>Key Features</span></h4><ul><li><span><span>Learn functional programming techniques at

Functional Programming in javascript Ho
✍ Luis Atencio πŸ“‚ Library πŸ“… 2016 πŸ› Manning 🌐 English

Functional Programming in javascript teaches javascript developers functional techniques that will improve extensibility, modularity, reusability, testability, and performance. Through concrete examples and jargon-free explanations, this book teaches you how to apply functional programming to real-l

Functional Programming in javascript Ho
✍ Luis Atencio πŸ“‚ Library πŸ“… 2016 πŸ› Manning Publications 🌐 English

Functional Programming in javascript teaches you techniques to improve your web applications - their extensibility, modularity, reusability, and testability, as well as their performance. This easy-to-read book uses concrete examples and clear explanations to show you how to use functional programmi