Hands-On Functional Programming with C++
An effective guide to writing accelerated functional code using C++17 and C++20
Alexandru Bolboaca
BIRMINGHAM - MUMBAI
Hands-On Functional Programming with C++
Copyright 2019 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: Richa Tripathi
Acquisition Editor: Shriram Shekhar
Content Development Editor: Manjusha Mantri
Senior Editor: Afshaan Khan
Technical Editor: Riddesh Dawne
Copy Editor: Safis Editing
Project Coordinator: Prajakta Naik
Proofreader: Safis Editing
Indexer: Rekha Nair
Production Designer: Nilesh Mohite
First published: June 2019
Production reference: 1270619
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78980-733-2
www.packtpub.com
To all the people in my life who pushed me forward Luc Rogge, Corey Haines, JB Rainsberger, Samir Talwar, Thomas Sundberg, Johan Martinsson, and Peter "CodeCop" Kofler,
to name a few.
To my brother, Adi, for being a conversation partner on difficult topics. To my mother, Raluca, for her support throughout my formative years.
To David Hussman, for sowing the seeds of inspiration that continued even after his premature passing. RIP dudeI miss you, but you keep inspiring me.
And finally, to my best friend, mentor, wife, and CEO/entrepreneur, Maria, for her continuous support and patience, and for building our story together, with more to come!
- Alex Bolboaca
Packt.com
Subscribe to our online digital library for full access to over 7,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
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
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.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.packt.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
With 20 years' experience in the software development industry, Alexandru Bolboaca has gone from being a junior C++ programmer to a technical lead and software architect, before becoming a technical coach and trainer. He has extensive experience in helping customers to improve the way they work, as well as their code and approach to testing. He is also the author of Usable Software Design, and the co-author of Coderetreat.
The team at Packt Publishing were a great help in writing this book. I've learned a lot in the process. Thank you for your patience and support. I hope I was easy to work with.
About the reviewers
Trevor Hickey is a software engineer at Uber. He holds a bachelor's degree in computer science from Edinboro University of Pennsylvania. He has experience in literature peer review and specializes in computational pasigraphy. His research interests include pragmatics and relevance theory.
Will Brennan is a C++/Python software engineer based in London, with experience of working on high-performance image processing and machine learning applications.
Ryan Riley has been involved in the futures and derivatives industry for almost 20 years. He received a bachelor's degree and a master's degree from DePaul University in applied statistics. Doing his course work in math meant that he had to teach himself how to program, thereby forcing him to read more technical books on programming than he would otherwise have done. Ryan has worked with numerous AI libraries in various languages, and is currently using the Caffe2 C++ library to develop and implement futures and derivatives trading strategies at PNT Financial.
Andreas Oehlke is a professional full-stack software engineer. He holds a bachelor's degree in computer science and loves to experiment with software and hardware. His trademark has always been his enthusiasm and affinity for electronics and computers. His hobbies include game development, building embedded systems, sports, and making music. He currently works full time as a senior software engineer for a German financial institution. He has also worked as a consultant and game developer in San Francisco, CA. He is also the author of the book, Learning LibGDX Game Development.
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
Welcome to a hands-on tour of functional programming in C++! This book is about an old idea, that is, functional programming, and a classic programming language, that is, C++, finally uniting forces.
Functional programming has been around since the 1950s; however, due to its mathematical underpinnings, it has been of limited interest to mainstream software development for many years. With the advent of multicore CPUs and big data leading to the need for parallelization, and with programming language designers becoming more interested in immutability and lambdas, functional programming concepts have been gradually introduced in all major programming languages, including C#, Java, PHP, JavaScript, Python, and Ruby. C++ has never been far from functional programming, with features such as function pointers, functors, and the algorithms from STL allowing many programmers to take advantage of certain constructs. However, starting with C++ 11, we see the introduction of lambdas, and of higher-order functions such as all_of, any_of, and none_of. In C++ 17, we see more progress, with the introduction of map (implemented as transform). Additionally, the features coming in C++ 20 are very exciting; for example, the ranges library, which allows composable, lightweight, and lazily evaluated transformations, is a great addition to the standard.
Next page