Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Functional Programming in Go

You're reading from   Learning Functional Programming in Go Change the way you approach your applications using functional programming in Go

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781787281394
Length 670 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Lex Sheehan Lex Sheehan
Author Profile Icon Lex Sheehan
Lex Sheehan
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Pure Functional Programming in Go 2. Manipulating Collections FREE CHAPTER 3. Using High-Order Functions 4. SOLID Design in Go 5. Adding Functionality with Decoration 6. Applying FP at the Architectural Level 7. Functional Parameters 8. Increasing Performance Using Pipelining 9. Functors, Monoids, and Generics 10. Monads, Type Classes, and Generics 11. Category Theory That Applies 12. Miscellaneous Information and How-Tos

What this book covers

Chapter 1, Pure Functional Programming in Go, introduces the declarative style of programming and demonstrates recursion, memorization, and Go's concurrency constructs using the Fibonacci Sequence. We will learn how to benchmark/performance test your recursive code and we will get some bad news.

Chapter 2, Manipulating Collections, shows us how to use intermediate (Map, Filter, and Sort) and terminal (Reduce, GroupBy, and Join) functions to perform data transformations. We use a Mocha-like BDD Go framework to test predicate functions. Itertools helps us grasp the breadth of FP collection manipulating function. Gleam is a fast, scalable distributed map/reduce system with DAG execution.

Chapter 3, Using High-Order Functions, covers a list of 27 FP characteristics: Anonymous function, closures, currying, Either data type, first-class functions, functions, functional composition, Hindley-Milner type system, Idempotence, immutable state, immutable variables, Lambda expressions, List Monad, Maybe data type, Maybe Monad, Monadic error, handling, No side-effects, operator overloading, option type, parametric polymorphism, partial function application, recursion, referential transparency, sum or union types, Tail Call Optimization, typeclasses, and Unit type. It also covers an example of Generics, and illustrates its value to FP programmers. We implement the Map, Filter, and Reduce functions, as well as lazy evaluation using Goroutines and a Go channel.

Chapter 4, SOLID Design in Go, talks about why Gophers eschew Java, principles of good software design, how to apply the Single Responsibility principle, function composition, the open/closed Principle, FP contracts, and duck typing. It also covers how to model behavior using interfaces, compose software using the Interface Segregation principle and embedded interfaces. We will learn about the law of Associativity with a purple Monoid chain and get the big reveal—Monads chain continuations.

Chapter 5, Adding Functionality with Decoration, illustrates interface composition using Go's complimentary Reader and Writer interfaces. Next, we will learn how procedural design compares to functional Inversion of Control. We will implement the following decorators: authorization, logging, and load balancing. Also, we will add easy-metrics to our app to see our decorator pattern in action.

Chapter 6, Applying FP at the Architectural Level, builds an application framework using a layered architecture, which solves cyclical dependency errors. We will learn how to apply the Hollywood principle and the difference between the observer pattern and dependency injection. We will use Inversion of Control (IoC) to control the flow of logic and build a layered application. Also, we will build an effective table-driven framework to test our application's API.

Chapter 7, Functional Parameters, enlightens us as to why a lot of  we've learned from Java and object-oriented programming does not apply to Go, teaches us a better way to refactor long parameter lists using functional options, and helps us understand the difference between currying and partial application. We will learn how to apply partial application to create another function with a smaller arity. We will use a context to gracefully shut down our server and see  how to cancel and roll back a long-running database transaction using a context.

Chapter 8, Increase Performance Using Pipelining, covers data flow types (Read, Split, Transform, Merge, and Write) and teaches us when and how to build a data transformation pipeline. We use buffering to increase throughput, goroutines and channels to process data faster, improve API readability using interfaces, and implement some useful filters. We also implement and compare imperative and functional pipeline designs for processing credit card charges.

Chapter 9, Functors, Monoids, and Generics, gives us an appreciation for the lack of support for Generics in Go. We will see how to use a code generation tool to solve the repetitive boilerplate code problem. We will dive deep into function composition, implement a few functors, and learn how to map between worlds. We will also learn how to write a Reduce function to implement an invoice processing monoid.

Chapter 10, Monads, Type Classes, and Generics, shows us how a Monad works and teaches us how to compose functions using the Bind operation. It shows us how Monads process errors and deal with Input/Output (I/O). This chapter works through a monadic workflow implementation in Go. We cover what The Lambda Calculus is and what it has to do with Monads, see how The Lambda Calculus implements Recursion, and learn how the Y-Combinator works in Go. Next, we use the Y-Combinator to control a workflow and learn how to handle all errors at the end of the pipe. We will learn how type classes work and implement a few in Go. Finally, we review the pros and cons of generics in Go.

Chapter 11, Category Theory That Applies, gives us a working understanding of category theory. We will learn to appreciate the deep connection between category theory, logic, and type theory. We will increase our understanding with a journey through the history of FP. This chapter uses a Venn diagram to help explain various categories of programming languages. We come to understanding of what binding, currying, and application mean in the context of a lambda expression. This chapter shows us that the Lambda Calculus is like chocolate milk. This chapter covers the type system implications of FP, shows us different categories of homomorphisms and when to use them, and uses mathematics and the flight of a soccer ball to increase our understanding of morphisms. We will cover function composition with linear and quadratic functions, and we will learn about interface-driven development. We'll explore the value in knowledge-driven systems, and we will learn how to apply our understanding of category theory to build better applications.

Appendix, Miscellaneous Information and How-Tos, shows us how the author suggests that we build and run the Go projects in this book. It shows us how to propose changes to Go, introduces the Lexical Workflow Solution: a Go-compatible way to handle errors in one place, provides a place to go to provide feedback and an FP resources page, discusses the Minggatu-Catalan Number, and offers a solution for world peace.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime