In my last post, C++20: Pythonic with the Ranges Library, I started my experiment to implement the beloved Python functions range and filter in C++. Due to two very interesting comments to my last post, I revisit the function range.
Admittedly, it took me quite a time to become comfortable with the ranges library, but the effort paid off. You will see why.
I got a few fascinating remarks about my range implementation in my last post. Therefore, I have to visit it once more.
As a short reminder, the call range(begin, end, step) generates in Python 2 a list of all integers from begin to end in stepsize steps. begin is inclusive, and the end is exclusive. step is per default 1.
My last range implementation of the last was over-engineered, as one of my German readers remarked. The following code snippet shows the over-engineered and the improved version.
I removed the boundary condition (line 1) in the first implementation and changed the infinite number generator ranges::views::iota(begin) (line 2) to a finite number generator ranges::view::iota(begin, end) (line 3). Consequentially, I did the same in the else branch.
From range to xrange
The presented range function is eager. It generates a std::vector<int>. Aleksei Guzev reminded me that Python 2 also has a lazy xrange function which corresponds to the Python 3 range function. He is right. I’m sufficiently comfortable with the ranges library to apply functional concepts to C++. If you are puzzled by the terms eager and lazy, read my previous post, C++20: Functional Patterns with the Ranges Library.
The following example shows a lazy range variant, which I called, consequentially, xrange.
Implementing the lazy xrange function is way more complicated than the previous eager range function. But the added complexity pays off. The following numbers correspond to the numbers in the source code snippet.
- The xrange function returns not a std::vector<int> but a composition of views. I let the compiler deduce the return type with auto to ease my job. OK, but the return type caused the first challenge. The return types of the if and else branch diver. A function with different return types is not valid in C++.
- To overcome this issue, I used a C++17 feature: constexpr if. constexpr if allows conditional compilation. When the expression if constexpr (Begin < End)) becomes true, the if branch is compiled; if not, the else branch. To be valid, Begin and End have to be constant expressions.
- Begin and End are now non-type template parameters which makes it possible to use them in a constexpr if (line 2) expression. I used a non-type template parameter of type long long to deal with big numbers. You read in a few sentences why.
- Constant expressions such as Begin and End can not be modified. Consequentially, I introduced the variables end and begin to adapt the boundaries for the ranges::views::iota call.
Let’s try it out.
Lines (1) – (4) show that the xrange function works as the previous range function. The only difference is that the function arguments become template arguments. I have to kill the program when I want to have all numbers up to a quintillion (line 6).
Using tics for numbers (1’000’000’000’000’000’000) (line 5) is valid since C++14 and makes the big numbers easier to read. I should not be so eager but lazy. If I ask only for ten numbers (line 7) or between 1’000’000 and 1’000’010 (line 8) the program works like a charm. Only the numbers are generated that are requested.
As I already promised in my last post C++20: Pythonic with the Ranges Library, I present in my next post Python’s map function. map empowers you to apply a function to sequences. For convenience reasons, I combine the map and filter functions into one function.
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, and Marco Parri Empoli.
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|
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,