C++ for fun … ctional programmers… ??? by Harald Achitz

September 23, 2020

The presentation C++ for fun … ctional programmers… ??by Harald Achitz explores the application of functional programming concepts in C++, focusing on higher-order functions, immutability, references, pointers, resource management, and constructors/destructors. The speaker delves into the intricacies of C++ as a multi-paradigm programming language, emphasizing its versatility and the importance of understanding details like constructors, destructors, and resource management for efficient and effective coding. Additionally, the presentation touches on the use of modern features like ranges in C++20, provides recommendations for learning resources, and encourages interactive learning and knowledge exchange within the programming community.

C++ for fun … ctional programmers… ???: A comprehensive overview

Why C++? A Misunderstood Giant

Harald Achitz starts by addressing the reputation of C++, acknowledging the emotional baggage many programmers associate with it. Often considered difficult and low-level, C++ is seen by some as inaccessible, particularly when compared to more modern languages that are explicitly designed for functional programming.

However, Harald argues that C++ contains powerful functional programming tools, despite its roots in imperative and object-oriented paradigms. He highlights that C++, which is standardized by a consortium rather than driven by one company, has evolved into a multi-paradigm language, allowing programmers to apply imperative, object-oriented, generic, and even functional programming techniques.

 

Functional Programming in C++? Yes, really

One of the most compelling parts of Harald’s talk revolves around how core functional programming concepts are embedded in C++. For example, he introduces the audience to the Standard Template Library (STL), which was built on years of research into generic programming. The STL offers tools like algorithms, containers, and iterators, which can be utilized in a functional manner.

Harald provides concrete examples of functional programming techniques in C++, such as using filter, map, and reduce. He demonstrates how the copy_if algorithm can be used to filter data, transform for mapping values, and accumulate for reducing a dataset. These tools show how C++ can adopt functional concepts, even if the naming and syntax are different from languages like Haskell or Lisp.

 

Immutability and higher-order functions in C++

Harald takes a deeper dive into functional programming’s key principles, starting with higher-order functions. He shows how C++ can handle these concepts, with examples from the Lift library, which allows developers to write clean, function-based logic.

C++ also offers a form of immutability through its handling of data copying. By default, C++ passes arguments by value, meaning functions operate on copies of data rather than shared references. This behavior ensures that the original data remains unchanged — a concept that aligns well with functional programming’s principle of immutability.

 

References, lambda functions, and state management

C++ handles references and mutable state differently from pure functional languages. Harald stresses that references in C++ need to be explicitly declared, making the handling of shared mutable data intentional. To mitigate potential issues, developers can declare data as `const` and use references to ensure variables are not accidentally modified.

Lambda functions in C++ offer another link to functional programming, providing anonymous functions that can be passed around, much like in functional languages. However, Harald notes that recursion and functional programming optimizations are not as efficient in C++, which lacks the garbage collection and tail-call optimization features found in functional-first languages.

 

Pointers, constructors, and resource management

Harald explains the intricate relationship between pointers, resource management, and constructors in C++. Pointers are powerful tools in C++, and while they may seem daunting, they offer efficient resource management through scope-based mechanisms. Harald explains how constructors and destructors work in tandem to ensure that resources like memory and file handles are properly managed. C++’s use of smart pointers, like unique_ptr and shared_ptr, gives developers control over resource ownership, providing a safety net similar to garbage collection in other languages.

 

Modern C++ and functional programming tools

With C++20, a range of new tools, such as ranges, have been introduced, which simplify working with algorithms and data in a functional style. Ranges allow for lazy evaluation and composability, making it easier to build efficient, functional-style pipelines in C++.

Harald recommends several books for those interested in exploring the deeper functional aspects of C++, including works by Alexander Stepanov and Ivan, which cover the historical and mathematical underpinnings of generic and functional programming in C++.

 

The road ahead: Embracing Functional Programming in C++

Harald wraps up his presentation by encouraging functional programmers to take a closer look at C++. Despite its complexity, C++ has the flexibility and power to incorporate functional programming concepts, and modern versions of the language are pushing the boundaries further. Tools like lambda functions, higher-order functions, and smart resource management give developers the ability to write clean, efficient code in a functional style.

In closing, Harald emphasizes the importance of writing code regularly, regardless of how challenging it may feel at first. He suggests creating interactive spaces for knowledge exchange between developers of various programming languages to foster a deeper understanding of their strengths and weaknesses.

 

Final thoughts

Harald Achitz’s presentation provides a refreshing view of C++ for functional programmers, showing how this oft-maligned language can embrace functional principles. His talk highlights that while C++ can be intimidating, it is far from inaccessible, and with the right tools and understanding, functional programmers can unlock a whole new world of possibilities in this multi-paradigm powerhouse.

For functional programmers looking for new challenges, C++ offers a unique opportunity to broaden their horizons, blending functional ideas with the raw power of one of the most widely used languages in the world.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.