Table Of ContentLearning C++ Functional
Programming
Metaprogramming, concurrency, lazy evaluation, and more
Wisnu Anggoro
BIRMINGHAM - MUMBAI
Learning C++ Functional Programming
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: August 2017
Production reference: 1080817
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78728-197-4
www.packtpub.com
Credits
Author Copy Editor
Wisnu Anggoro Zainab Bootwala
Reviewer Project Coordinator
Aivars Kalvāns Prajakta Naik
Commissioning Editor Proofreader
Aaron Lazar Safis Editing
Acquisition Editor Indexer
Chaitanya Nair Rekha Nair
Content Development Editor Graphics
Lawrence Veigas Abhinash Sahu
Technical Editor Production Coordinator
Supriya Thabe Nilesh Mohite
About the Author
Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an
experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network
Programming - Second Edition and Functional C# by Packt. He has been programming since
he was in junior high school, which was about 20 years ago, and started developing
computer applications using the BASIC programming language in the MS-DOS
environment. He has solid experience in smart card programming, as well as desktop and
web application programming, including designing, developing, and supporting the use of
applications for SIM Card Operating System Porting, personalization, PC/SC
communication, and other smart card applications that require the use of C# and C/C++. He
is currently a senior smart card software engineer at CIPTA, an Indonesian company that
specializes in innovation and technology for smart cards. He can be reached through his
email at [email protected].
First and foremost, I would like to thank God, the almighty, for providing me with this
opportunity and granting me the capability to proceed successfully. To my wife and best
motivator, Vivin, who never stopped supporting me in finishing what I started. To my
beloved sons, Olav and Oliver, who are the source of my joy; you both never fail to make
me happy every day. And to my parents and family for their inspiration.
The best team at PACKT, especially Lawrence Veigas, my content development editor, for
his effort to help me supply the best content for this book, and Denim Pinto, who invited
me to author this book.
Also, I would like to show my gratitude to Benediktus Dwi Desiyanto, my best mentor,
teacher, and superior at CIPTA, who always supports me to leverage my skill set, for both
hard and soft skills. And to all my friends at CIPTA, for the insight of C++ and functional
programming I have learned from all of you.
About the Reviewer
Aivars Kalvāns holds the position of Lead Software Architect at Tieto Latvia. He has been
working on the Card Suite payment card system for more than 15 years and maintains
many core C++ libraries and programs. He is also responsible for the C++ programming
guidelines, secure coding training, and code reviews, and he organizes and speaks at
internal C++ developer meetups as well.
I would like to thank my lovely wife, Anete, and sons, Kārlis, Gustavs, and Leo, for making life
much more interesting.
www.PacktPub.com
For support files and downloads related to your book, please visit www.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 [email protected] 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.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt
books and video courses, as well as industry-leading tools to help you plan your personal
development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial
process. To help us improve, please leave us an honest review on this book's Amazon page
at https://www.amazon.com/dp/1787281973.
If you'd like to join our team of regular reviewers, you can e-mail us at
[email protected]. We award our regular reviewers with free eBooks and
videos in exchange for their valuable feedback. Help us be relentless in improving our
products!
Table of Contents
Preface
1
Chapter 1: Diving into Modern C++
7
Getting closer with several new features in modern C++ 8
Defining the data type automatically using the auto keyword 8
Querying the type of an expression using the decltype keyword 11
Pointing to a null pointer 13
Returning an iterator using non-member begin() and end() function 14
Iterating over collections using range-based for loops 15
Leveraging the use of C++ language with the C++ Standard Libraries 17
Placing any objects in the container 17
Using algorithms 20
Simplifying the function notation using a Lambda expression 26
Using the Lambda expression for a tiny function 27
Using the Lambda expression for multiline functions 28
Returning a value from the Lambda expression 30
Capturing a value to the Lambda expression 32
Preparing the value using initialization captures 38
Writing a generic Lambda expression to be used many times with many
different data types 39
Avoiding manual memory management with smart pointers 41
Replacing a raw pointer using unique_ptr 41
Sharing objects using shared_ptr 47
Tracking the objects using a weak_ptr pointer 50
Storing many different data types using tuples 52
Unpacking tuples values 53
Returning a tuple value type 55
Summary 56
Chapter 2: Manipulating Functions in Functional Programming
57
Applying the first-class function in all functions 58
Passing a function as another function's parameter 58
Assigning a function to a variable 61
Storing a function in the container 64
Creating a new function from the existing functions at runtime 67
Getting acquainted with three functional techniques in the higher-order
function 71
Executing each element list using map 71
Extracting data using filter 73
Combining all elements of a list using fold 76
Avoiding the side effect with pure function 80
Reducing a multiple arguments function with currying 83
Summary 87
Chapter 3: Applying Immutable State to the Function
88
Understanding the essential part from immutable object 88
Modifying a local variable 89
Modifying a variable passed into a function 91
Preventing the modification of a value 94
Applying the first-class function and the pure function to the immutable
object 97
Developing the immutable object 99
Starting with a mutable object 100
Refactoring a mutable object into an immutable one 103
Enumerating the benefits of being immutable 108
Summary 109
Chapter 4: Repeating Method Invocation Using Recursive Algorithm
110
Repeating the function invocation recursively 111
Performing the iteration procedure to repeat the process 111
Performing the recursion procedure to repeat the process 114
Recurring the immutable function 116
Getting closer to tail recursion 118
Getting acquainted with functional, procedural, and backtracking
recursion 122
Expecting results from functional recursion 122
Running a task recursively in procedural recursion 124
Backtracking recursion 126
Summary 137
Chapter 5: Procrastinating the Execution Process Using Lazy
Evaluation
138
Evaluating the expression 138
Running the expression immediately with strict evaluation 139
Delaying the expression with non-strict evaluation 140
The basic concept of lazy evaluation 142
[ ii ]