Functional Programming in Go

Book description

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. Functional Programming in Go
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
    9. Download a free PDF copy of this book
  6. Part 1: Functional Programming Paradigm Essentials
  7. Chapter 1: Introducing Functional Programming
    1. What is functional programming?
      1. Introducing first-class functions
      2. What are pure functions?
      3. Say what you want, not how you want it    
    2. A brief history of functional programming
    3. Modern functional programming
    4. The Go programming paradigm
    5. Why functional programming?
    6. Why not functional programming in Go?
    7. Comparing FP and OOP
    8. Summary
  8. Chapter 2: Treating Functions as First-Class Citizens
    1. Technical requirements
    2. Benefits of first-class functions
    3. Defining types for functions
      1. Type aliases for primitives
      2. Type aliases for functions
    4. Using functions as objects
      1. Passing functions to functions
      2. In-line function definitions
      3. Anonymous functions
      4. Returning functions from functions
      5. Functions in var
      6. Functions inside data structures
      7. Functions inside structs
    5. Example 1 – map dispatcher
      1. Creating a simple calculator
    6. Example 2 – mocking functions for testing
    7. Summary
  9. Chapter 3: Higher-Order Functions
    1. Technical requirements
    2. An introduction to higher-order functions
    3. Closures and variable scoping
      1. Variable scoping in Go
      2. Capturing variable context in functions (closures)
    4. Partial application
    5. Function currying, or how to reduce n-ary functions to unary functions
      1. Example: function currying
    6. Example: server constructor
    7. Summary
  10. Chapter 4: Writing Testable Code with Pure Functions
    1. Technical requirements
    2. What is purity?
      1. Demonstrating pure versus impure function calls
      2. Referential transparency
      3. Idempotence
      4. Statelessness
      5. Side effects
    3. Why does purity improve our code?
      1. Increases the testability of our code
      2. Increases the confidence in our code
      3. Improved confidence in function names and signatures
      4. Safer concurrency
    4. When not to write pure functions
      1. Input/output operations
      2. Non-determinism can be desired
      3. When we really have to panic!
    5. How do we create pure functions?
      1. Avoid global state
      2. Separate pure and impure functionality
    6. Example 1 – hotdog shop
      1. Bad hotdog shop
      2. Better hotdog shop
    7. Summary
  11. Chapter 5: Immutability
    1. Technical requirements
    2. What is immutability?
      1. Immutability at the data layer
    3. How to write immutable code in Go
      1. Writing immutable code for collection data types
    4. Measuring performance in mutable and immutable code
      1. Benchmarking functions
      2. Understanding stacks, heaps, and garbage collection
    5. When to write mutable functions
    6. What are functors and monads?
      1. What’s a functor?
      2. From functor to monad
    7. Summary
  12. Part 2: Using Functional Programming Techniques
  13. Chapter 6: Three Common Categories of Functions
    1. Technical requirements
    2. Predicate-based functions
      1. Implementing a Filter function
      2. Any or all
      3. Implementing DropWhile and TakeWhile
    3. Map/transformation functions
      1. Transformations while maintaining the data type
    4. Data reducing functions
    5. Example – working with airport data
    6. Summary
  14. Chapter 7: Recursion
    1. Technical requirements
    2. What is recursion?
    3. Why do functional languages favor recursion?
    4. When to use recursive functions
      1. Iterating over trees
    5. Recursion and functions as first-class citizens
    6. Limits of recursive functions
      1. Measuring the performance of recursive versus iterative solutions
      2. Space limitation of recursive functions
      3. Tail recursion as a solution to stack limitations
    7. Summary
  15. Chapter 8: Readable Function Composition with Fluent Programming
    1. Technical requirements
    2. Chaining functions through dot notation
      1. Chaining methods for object creation (builder pattern)
      2. Dot notation to chain functions on slices
      3. Infinite data structures and lazy evaluation
      4. Continuation-passing style programming
      5. CPS and goroutines
      6. When to use CPS?
    3. Summary
  16. Part 3: Design Patterns and Functional Programming Libraries
  17. Chapter 9: Functional Design Patterns
    1. Technical requirements
    2. Classical design patterns in a functional paradigm
      1. The strategy pattern
      2. The decorator pattern
      3. The Hollywood principle
    3. Functional design patterns
    4. Summary
  18. Chapter 10: Concurrency and Functional Programming
    1. Technical requirements
    2. Functional programming and concurrency
      1. Concurrency, parallelism, and distributed computing
      2. Functional programming and concurrency
    3. Creating concurrent functions
      1. Concurrent filter implementation
      2. Concurrent Map and FMap implementation
    4. The pipeline pattern
    5. Summary
  19. Chapter 11: Functional Programming Libraries
    1. Technical requirements
      1. Is the library alive – and do the examples still match it?
      2. Legal requirements
    2. Pre-generics libraries for creating common FP functions
      1. Code generation libraries for pre-generics Go
    3. Post-generics functional programming libraries
      1. Pie with generics
      2. Lodash, for Go
      3. Mo, for go
    4. Summary
  20. Index
    1. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Functional Programming in Go
  • Author(s): Dylan Meeus
  • Release date: March 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781801811163