Table of Contents
Functional Programming with C++
Introduction
Side effects
Sequences
Compiler
Performance
Algorithms
Recursive functions
all_of
any_of
none_of
for_each
find
find_if
find_if_not
find_end
find_first_of
adjacent_find
count
accumulate
count_if
mismatch
equal
advance
distance
is_permutation
search
search_n
min_element
adjacent_difference
inner_product
partial_sum
iota
Run-time Results
constexpr
Recursive Lambda
Lambdas
Memoization
Lazy Evaluation
Templates
Boost MPL
Boost Fusion
Persistent Data
List Functions
Persistent Vector
More Persistent Data Structures
Appendix
Tuple Hash
Bibliography
Books from the Author
Functional Programming with C++
An Alternative Perspective on C++
By Chris Weed
Functional C++
Copyright © 2015 by Chris Weed
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any
means, including photocopying, recording, or other electronic or mechanical methods, without the prior written
permission of the publisher, except in the case of brief quotations embodied in critical reviews and certain other
noncommercial uses permitted by copyright law. For permission requests, write to the publisher, addressed “Attention:
Permissions Coordinator,” at the address below.
Chris Weed
chrisweed@gmail.com
Trademarked names may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, I use the names in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “as is” basis, without warranty. Although
every precaution has been taken in the preparation of this work, the author shall have no
liability to any person or entity with respect to any loss or damage caused or alleged to be
caused directly or indirectly by the information contained in this work.
About the Author
Chris Weed is a software engineer who lives and works in the Boston area of
Massachusetts. Chris has been building C++ software since 1995, and endeavors to use
functional programming every day. He has found functional programming is key to
writing understandable and less buggy software. This has resulted in greater productivity
and happier software engineers. His interests include computer vision, video processing,
software patterns, and rapid development.
Additional Titles
Introduction to CMake
Introduction to Git-flow
Introduction to Hg-flow
Introduction to bjam
Introduction
Overview
Functional programming is experiencing a resurgence with languages like Python,
Haskell, and Scala. C++ and Java have also added functional features, such as lambdas
and futures. Writing C++ in a functional style using const variables, functions without side
effects, recursive functions, and function objects results in code that is often simpler and
easier to maintain and understand, especially when poorly documented. This book
explores functional techniques in C++ code and their advantages and disadvantages.
Side effects
One of the most important features of functional programming is that programs are written
almost exclusively without side effects. According to Wikipedia, a function has a side
effect if, in addition to returning a value, it also modifies some state or has an observable
interaction with calling functions or the outside world. For example, a function might
modify a global variable or static variable, modify one of its arguments, raise an
exception, write data to a display or file, read data, or call other side-effecting functions.
[1]
Functions without side effects are generally referred to as “pure” functions, and are the
norm for functional languages. These functions only produce output variables based on the
input arguments. However, C++ code commonly includes functions with mutable
arguments, references global, static, or member variables, or calls other non-pure
functions.
A useful property of pure functions is that when called with the same arguments always
results in the same output. Due to this, functional languages can cache the reproducible
result using a process called “memoization” for following calls to pure functions. A non-
pure function is based on some external state or changes an external state, which is often
difficult to reproduce, test, or understand.