Learning Functional Programming in Go
Change the way you approach your applications using functional programming in Go
Lex Sheehan
BIRMINGHAM - MUMBAI
Learning Functional Programming in Go
Copyright 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2017
Production reference: 1221117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78728-139-4
www.packtpub.com
Credits
Author Lex Sheehan | Copy Editor Pranjali Chury |
Reviewer John Pradeep | Project Coordinator Vaidehi Sawant |
Commissioning Editor Merint Mathew | Proofreader Safis Editing |
Acquisition Editor Karan Sadawana | Indexer Rekha Nair |
Content Development Editor Rohit Kumar Singh | Graphics Jason Monteiro |
Technical Editor Pavan Ramchandani | Production Coordinator Shraddha Falebhai |
About the Author
Lex Sheehan has a B.S. in Computer Science from Auburn University, resides in Atlanta, GA, and works as a senior software engineer with over 20 years of experience. He has a deep understanding of functional programming; His first encounter was using high-order functions in Ruby, Scala, JavaScript, Haskell, Java, and Go.
Lex worked for IBM Software Group and IBM Global Business Services, designing and building various enterprise business systems. He is the author of eight US patents (IT security and data transformations) and he writes a blog titled Application Development with Lex Sheehan.
Lex is available to consult and meet with your CTO, or provide in-house training on the information in this book.
Acknowledgments
I would like to acknowledge the people who helped me during the writing of this book.
Thank you, Rohit, for editing my writing and providing your insight, guidance, and words of encouragement.
Thank you, John and Wayne, for reviewing this book for technical accuracy. Thank you to all other reviewers who helped improve the quality of this book's content.
Thank you, Mac, for being my wise counsel.
Thank you, Marty, for being my sounding board.
Thank you, Erik, for providing legal counsel regarding the Forgetful Functor.
Thank you all who lifted me up in your prayers during this endeavor.
Thank you, Salim, for your support. Your leadership style has earned my trust and loyalty.
Thank you, to the one who would say, "Lex Brilliance". That was and still is a great source of inspiration. Words matter.
We are all given talents. Some fear failure and suffer for it; others invest their talents.
This book is dedicated to those who invest in themselves and go on to help others.
"Do or do not. There is no try".
- Yoda
Preface
Until recently, the message has been Go and functional programmingdon't do it.
Functional programming (FP) is a perfect fit for multicore, parallel processing. Go is a concurrency baller (with Goroutines, channels, and so on) and already runs on every available CPU core. FP reduces complexity; simplicity is one of Go's biggest strengths.
So, what can FP bring to Go that will actually improve our software applications? Here's what it offers:
- Composition: FP shows us how to decompose our apps and rebuild them by reusing small building blocks.
- Monads: Using monads, we are able to safely order our workflows into pipelines of data transformations.
- Error handling: We can leverage monadic error handling and still maintain compatibility with idiomatic Go code.
- Performance: Referential transparency is where we can evaluate our function once and then subsequently refer to its pre-computed value.
- Expressive code: FP allows us to concisely express business intent in our code. We declare what our functions do, without the clutter of error checking after every function call, and without having to follow state changes (pure FP means immutable variables).
- Simpler code: No shared data means not having to deal with semaphores, locks, race conditions, or deadlocks.
Most people have difficulty grasping FP.
I did too. And when I got it, I wrote this book. Take this journey with me. We'll see hundreds of illustrations, read easy-to-understand explanations, and implement FP in Go code along the way.
I enjoyed coaching soccer. The litmus test I used to determine whether I succeeded as a coach was the answer to this simple question: Did they all register for next season and request me to be their coach? Just like planning practice, I planned each chapter, starting with simple concepts and adding to them. Read this book, then you too will be able to say, I got it.
If you want to improve your FP skills, this book is for you.
What this book covers
, 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.
, 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 and we look at a distributed MapReduce solution: Gleam = Go + LuaJIT + Unix Pipes.
, 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.
Next page