INTERVIEW

When Functional Programming Meets C++

Image for post
Image for post

Six Questions for Ivan Čukić, author of Functional Programming in C++

By Frances Lefkowitz

Ivan Čukić has been coding since 1998, and is now a core developer in C++ at KDE. He teaches modern C++ and functional programming at the Faculty of Mathematics at the University of Belgrade.

__________________________________________________________________

Take 39% off Functional Programming in C++. Just enter intcukic into the discount code box at checkout at manning.com.
__________________________________________________________________

It is true that C++ was created a long time ago, but it has been evolving in recent years so much that it “feels like a new language,” to quote Bjarne Stroustrup, the creator of C++. It appears in most places where people care about efficiency. From embedded systems, through normal software like games, photo manipulation software, office suites, web browsers, to web services in big companies like Google, Facebook or Yandex. One of my small pleasures is to see software I worked on show up in photographs from historical events, like the discovery of the Higgs boson at CERN; and more recently, NASA’s Mars InSight landing. So, C++ was there as well.

While languages like C++ are not traditionally considered to be FP, most of them do provide us with features which allow us to express functional idioms. C++ possibly even more so, because it stopped being just OOP-focused quite early on. When C++ first became an ISO standard, it included a standard library that was everything but object-oriented. It used a novel paradigm created by Alexander Stepanov that he dubbed ‘generic programming. ’ Incidentally, this is what made FP get its foot into the world of C++. The generic programming idioms Alex created used many concepts derived from FP, tailored to fit the programming model of C++.

A long time ago, all software was written in some assembly language. Then people realized that thinking in terms of jumps (gotos and alike) leads to errors. Nowadays most software is written in higher-level languages that raise the level of abstraction from jumps into loops, branches and function calls. These abstractions model common patterns that people had in their assembly code. And this is good. But it is not as good as it can be. All these are still overly general and low-level, and it is easy to again recognize patterns in how people use them.

Common usage patterns can again be used to design higher-level abstractions, like all the things in the <algorithm> header of the standard library of C++. Just as with abandoning explicit jumps, using these higher-level abstractions removes a lot of places where we can make mistakes. The abstractions like those from <algorithm> are now tailored at achieving a specific task, and they communicate that clearly with their names. If we see something named find_if, it takes less than a second to figure out what it does from its name alone. Abstractions like these are used to design programs in the terms of “what it should do” They allow us to write more expressive code, more terse code, and thus more correct code.

FP is about thinking in transformations that need to be performed on the data, using the previously mentioned abstractions instead of thinking about the algorithm steps that need to be performed to achieve the result.

FP can help any programmer (not only C++) to write safer code. Being able to express ideas on a higher level of abstraction removes many places in which we might make errors if we were to manually translate those ideas to a low-level programming language.

I have to say I didn’t expect this question. Although I like making art (drawing and music mainly), I have never studied it. I have an unorthodox (and I guess completely wrong) view on art: I don’t want to be taught ‘the right way’ to do it; I just want to do what I like. Most of my artwork nowadays is digital. Almost everything I do is done in programs for vector graphics like Inkscape.

Originally published at freecontent.manning.com.

Written by

Follow Manning Publications on Medium for free content and exclusive discounts.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store