Today, I start an experiment. I want to implement beloved functions in Python in C++ using the ranges library. I’m curious about how it goes.
You maybe know it. I’m also a Python trainer since 2004. Python has excellent functions, and often Python is, for me, the threshold of how comfortable a programming language could be. Today, I want to implement the Python functions range and filter.
- range creates a list “containing an arithmetic progression of integers” (Python built-in help).
- filter applies a predicate to a sequence and returns those elements for which the predicate returns true.
A sequence is a term in Python that stands for something iterable such as a list ([1, 2, 3]), a tuple ((1, 2, 3)), or a string (“123”). Instead of a list, I use a std::vector in C++. The functions filter stands for the functional style in Python.
Before I start with the range function, I have to make a few remarks.
- I use in my examples the range-v3 library from Eric Niebler, which is the basis for the C++20 ranges. I showed in my previous post, C++20: The Ranges Library, how to translate the ranges-v3 to the C++20 syntax.
- Python code is often shorter than C++ Code for two reasons. First, I don’t store the Python lists in a variable, and second, I don’t display the result.
- I don’t like religious wars about programming languages. The middle-aged are long gone. I will not react to these comments.
Let’s start with the range function. The range function is a kind of building block for creating integers.
In the following examples, I first show the Python expression commented out and then the corresponding C++ call.
The calls in lines (1) – (4) should be quite easy to read when you look at the output.
The first two arguments of the range call stand for the beginning and end of the created integers. The beginning is included but not the end. Step size as the third parameter is per default 1. When the interval [begin, end[ is decreasing, the step size should be negative. If not, you get an empty list or std::vector<int>.
I cheat a little in my range implementation. I use the function ranges::views::stride which is not part of C++20. stride(n) returns the n-th element of the given range. If you know an elegant implementation based on C++20, please let me know.
The if condition (begin < end) of the range function in line (1) should be quite easy to read. Create all numbers starting with begin (ranges::views::iota(begin)), take each n-th element (ranges::views::stride(stepsize), and do it as long as the boundary condition holds (ranges::views::take_while(boundary). Finally, push the integers on the std::vector<int>.
I use a little trick in the other case (line 2). I create the numbers [end++, begin++[, take them until the boundary condition is met, reverse them (ranges::views::reverse), and take each n-th element.
I implement the eager version for filter and map (next post) in my examples. With Python 3 filter and map are lazy. filter and map return, in this case, generators. To get the eager behavior of Python 2, put a list around the filter and map calls in Python 3.
Both calls produce the same list: [1, 3, 5, 7, 9].
I continue with the function filter because it is easier to implement, such as the map function.
Before I explain the program, let me show you the output.
This time, I include the range implementation from before. The filter function (line 2) should be easy to read. I apply the callable func to each sequence element and materialize the elements in the std::vector. Line (3) creates all numbers i from 20 to 50 for which hold (i % 3) == 0. Only the strings that start with an uppercase letter can pass the filter inline (4). Line (5) counts how many lines in the file “/etc/services” are comments. Comments are lines that start with the ‘#’ character.
If you ignore the different ways to implement lambdas in Python and C++, the filter calls are quite similar.
map was way more complicated to implement than filter. First, map may change the type of the input sequence. Second, my implementation of map triggered a GCC bug report. Afterward, I combine the functions map and filter in a function and I get … . Read the details in my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,