Mastering Functional Programming
Functional techniques for sequential and parallel programming with Scala
Anatolii Kmetiuk
BIRMINGHAM - MUMBAI
Mastering Functional Programming
Copyright 2018 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Aaron Lazar
Acquisition Editor: Sandeep Mishra
Content Development Editor: Manjusha Mantri
Technical Editor: Abhishek Sharma
Copy Editor: Safis Editing
Project Coordinator: Prajakta Naik
Proofreader: Safis Editing
Indexer: Mariammal Chettiyar
Graphics: Jisha Chirayil
Production Coordinator: Shantanu Zagade
First published: August 2018
Production reference: 1300818
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-079-6
www.packtpub.com
To my father and mother, Alexander Kmetiuk and Aurelia Kmetiuk, for shaping me into the person that I've become.
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Anatolii Kmetiuk is a Functional Programming and Data Science Freelance Developer. During his programming career, he has worked on Scala projects involving parallel computing, web APIs, SaaS, and data engineering. His areas of expertise include using applications of pure functional programming to build fault-tolerant, reactive systems, as well as parallel computing. Another area of his focus is machine learning and natural language processing.
I want to express gratitude to Marco Borst and Andr van Delft for the time and effort they put into reviewing this book thoroughly.
About the reviewers
Marco Borst is an independent programmer with more then 20 years of experience programming systems that yield useful applications. He teaches development teams alternative points of view regarding the applicability and limits of our functional abstractions. Stoked to be the most pragmatic person in the room, Marco shares his appreciation that, out of a multitude of daily choices to do either A or B, the least useless choice is often to do neither A nor B. It is a difficult choice to act upon. Being an engineer by education, a programmer by experience, and a communicator because nature forced him to be, Marco is inclined to share his craft with others and is positively thrilled when able to learn from others as well.
Andr van Delft is an independent computer science researcher in the Netherlands. Having studied mathematics and business administration, he has made a living as a consultant and scientist in various organizations. His main project is SubScript, which extends Scala with the so called Algebra of Communicating Processes, a concurrency theory from the 1980s, which he has presented at various conferences, such as Lambda Days, LambdaConf, and FLOPS. His product, SlideMight, may be the first off-the-shelf software that is partially powered with the Algebra of Communicating Processes.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
Functional programming languages, such as Java, Scala, and Clojure, are attracting attention as an efficient way to handle the new requirements for programming multi-processor and high-availability applications. This book will teach you functional programming with the help of Scala. The book takes a thought-leadership approach, gently introducing you to functional programming and taking you all the way to becoming a master at the paradigm. Beginning with an introduction to functional programming, the book gradually moves forward teaching you how to write declarative code, making use of functional types and values. After covering the basics, we will discuss the more advanced concepts in functional programming.
We will cover the concepts of pure functions and type classes, problems they aim to solve, and how to use them in practice. We will see how libraries can be used for purely functional programming. We will look at the broad family of libraries for functional programming. Finally, we will discuss some of the more advanced patterns in the functional programming world, such as Monad Transformers and Tagless Final. After covering the purely functional approach to programming, we will look into the subject of parallel programming. We will introduce the Actor model and how it is implemented in the modern functional languages. By the end of this book, you will have mastered the concepts entailing functional programming alongside OOP to build robust applications.
Next page