Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code
Book information
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 FeaturesLearn functional programming techniques at the architectural level and use them to solve real-world problemsUnderstand how to think about code functionallyLearn about the trade-offs of functional programming and object-oriented programming (OOP) in GoBook 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 learnGain a deeper understanding of functional programming through practical examplesBuild a solid foundation in core FP concepts and see how they apply to Go codeDiscover how FP can improve the testability of your code baseApply functional design patterns for problem solvingUnderstand when to choose and not choose FP conceptsDiscover the benefits of functional programming when dealing with concurrent codeWho 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 ContentsIntroducing Functional ProgrammingTreating Functions as First-Class CitizensHigher -Order FunctionsWrite Testable Codes with Pure FunctionsImmutabilityThree Common Categories of FunctionsRecursionReadable Function Composition with Fluent ProgrammingFunctional Design PatternsThinking functionally to solve problemsFunctional Programming Libraries 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 books
Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code
MOBI
Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code
EPUB
Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code
2023 · PDF
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36
2010 · PDF
THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.
1858 · PDF