{"id":5863,"date":"2020-03-11T21:34:39","date_gmt":"2020-03-11T21:34:39","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-pythons-map-function\/"},"modified":"2023-09-27T17:01:41","modified_gmt":"2023-09-27T17:01:41","slug":"c-20-pythons-map-function","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-pythons-map-function\/","title":{"rendered":"C++20: Python&#8217;s map Function"},"content":{"rendered":"<p>Today, I finish my experiment writing beloved Python functions in C++. So far, I have implemented the Python functions <span style=\"font-family: 'courier new', courier;\">filter<\/span>, <span style=\"font-family: 'courier new', courier;\">range<\/span>, and <span style=\"font-family: 'courier new', courier;\">xrange<\/span>. Today, I have a closer look at the <span style=\"font-family: 'courier new', courier;\">map<\/span> function and combine the functions <span style=\"font-family: 'courier new', courier;\">map<\/span> and <span style=\"font-family: 'courier new', courier;\">filter<\/span> into one function.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8380 size-full\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges.png\" alt=\"\" width=\"979\" height=\"373\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges.png 979w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges-300x114.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges-768x293.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges-705x269.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/TimelineCpp20Ranges-845x321.png 845w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><\/p>\n<p>It was implemented in my last post, &#8220;<a href=\"http:\/\/bit.ly\/2wI2z3G\">C++20: Pythons range Function, the Second<\/a>&#8221; a lazy variant of <span style=\"font-family: 'courier new', courier;\">range<\/span>: <span style=\"font-family: 'courier new', courier;\">xrange.<\/span>\u00a0A few of my German readers complain that <span style=\"font-family: 'courier new', courier;\">xrange<\/span> does not behave, such as Python 2 <span style=\"font-family: 'courier new', courier;\">xrange<\/span> function. My xrange function requires constant expressions for the created numbers&#8217; beginning and end.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> xrange<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span>();                    \n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1 2 3 4 5 6 7 8 9<\/span>\n<\/pre>\n<\/div>\n<p>In the example, 1 and 10 are constant expressions. This means an expression such as the following one would not compile.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">int<\/span> begin <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> end <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\n\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> xrange<span style=\"color: #555555;\">&lt;<\/span>begin, end<span style=\"color: #555555;\">&gt;<\/span>();                    \n<\/pre>\n<\/div>\n<p>I assume you know what that means?<\/p>\n<h2>Pythons <span style=\"font-family: 'courier new', courier;\">range<\/span> Function, the Third<\/h2>\n<p>Thanks to my German reader Clocktown, I can present the final version of <span style=\"font-family: 'courier new', courier;\">xrange<\/span>. The function <span style=\"font-family: 'courier new', courier;\">xrange<\/span> is lazy and can also accept arguments for the boundaries, which are not constant expressions.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ xrange2.hpp<\/span>\n\n#include &lt;stdio.h&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;range\/v3\/all.hpp&gt;<\/span>\n \n<span style=\"color: #006699; font-weight: bold;\">namespace<\/span> view <span style=\"color: #555555;\">=<\/span> ranges<span style=\"color: #555555;\">::<\/span>views;\n\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">xrange<\/span>(<span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> begin, <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> end, <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> step <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>) {\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span>(step <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span>) {\n        <span style=\"color: #006699; font-weight: bold;\">throw<\/span> std<span style=\"color: #555555;\">::<\/span>invalid_argument(<span style=\"color: #cc3300;\">\"Step cannot be 0\"<\/span>);\n    }\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> Step <span style=\"color: #555555;\">=<\/span> begin <span style=\"color: #555555;\">&lt;<\/span> end <span style=\"color: #555555;\">?<\/span> step <span style=\"color: #555555;\">:<\/span> <span style=\"color: #555555;\">-<\/span>step;\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> Begin <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>min(begin, end);\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> End <span style=\"color: #555555;\">=<\/span> Step <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">0<\/span> <span style=\"color: #555555;\">?<\/span> Begin <span style=\"color: #555555;\">:<\/span> std<span style=\"color: #555555;\">::<\/span>max(begin, end);\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> view<span style=\"color: #555555;\">::<\/span>iota(Begin, End)\n    <span style=\"color: #555555;\">|<\/span> view<span style=\"color: #555555;\">::<\/span>stride(std<span style=\"color: #555555;\">::<\/span>abs(Step))\n    <span style=\"color: #555555;\">|<\/span> view<span style=\"color: #555555;\">::<\/span>transform([begin, end](std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> i){\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> begin <span style=\"color: #555555;\">&lt;<\/span> end <span style=\"color: #555555;\">?<\/span> i <span style=\"color: #555555;\">:<\/span> end <span style=\"color: #555555;\">-<\/span> (i <span style=\"color: #555555;\">-<\/span> begin);\n    });\n}\n\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">xrange<\/span>(<span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> end) {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> xrange(<span style=\"color: #ff6600;\">0<\/span>, end, <span style=\"color: #ff6600;\">1<\/span>);\n}                   \n<\/pre>\n<\/div>\n<p>The key idea of his implementation is that <span style=\"font-family: 'courier new', courier;\">view::transform<\/span>\u00a0eventually transforms the calculation into a reverse variant. <span style=\"font-family: 'courier new', courier;\">xrange<\/span> can be invoked with one, two, or three arguments. The default for the first argument is 0, and for the third argument is 1. Let&#8217;s try it out. I replaced the <span style=\"font-family: 'courier new', courier;\">xrange<\/span> implementation of my last post with this new implementation.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ range2.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"xrange2.hpp\"<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;range\/v3\/all.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\n        \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> xrange(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n    res <span style=\"color: #555555;\">=<\/span> xrange(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">50<\/span>, <span style=\"color: #ff6600;\">5<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res2 <span style=\"color: #555555;\">=<\/span> xrange(<span style=\"color: #ff6600;\">20<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res2) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n    res2 <span style=\"color: #555555;\">=<\/span> xrange(<span style=\"color: #ff6600;\">50<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">5<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res2) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n    res <span style=\"color: #555555;\">=<\/span> xrange(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">1'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000<\/span>);\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ for (auto i: res) std::cout &lt;&lt; i &lt;&lt; \" \";<\/span>\n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>take(<span style=\"color: #ff6600;\">10<\/span>)) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i<span style=\"color: #555555;\">:<\/span> res <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>drop_while([](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">1'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">000<\/span>; })\n                     <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>take_while([](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">1'<\/span><span style=\"color: #ff6600;\">000'<\/span><span style=\"color: #ff6600;\">010<\/span>; })){\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    }\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n}\n<\/pre>\n<\/div>\n<p>As expected, I got the same result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5859\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/xrange.png\" alt=\"xrange\" width=\"650\" height=\"316\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/xrange.png 893w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/xrange-300x146.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/xrange-768x373.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>So far, nothing new. But here are the new use cases. <span style=\"font-family: 'courier new', courier;\">begin<\/span>\u00a0and <span style=\"font-family: 'courier new', courier;\">end<\/span> are not constant expressions, and <span style=\"font-family: 'courier new', courier;\">xrange<\/span>\u00a0supports one argument.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> begin <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>;\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> end <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\n\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span>(<span style=\"color: #006699; font-weight: bold;\">auto<\/span> x<span style=\"color: #555555;\">:<\/span> xrange(end)) {\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 0 1 2 3 4 5 6<\/span>\n  }\n\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span>(<span style=\"color: #006699; font-weight: bold;\">auto<\/span> x<span style=\"color: #555555;\">:<\/span> xrange(begin, end)) {\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3 4 5 6<\/span>\n\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span>(<span style=\"color: #006699; font-weight: bold;\">auto<\/span> x<span style=\"color: #555555;\">:<\/span> xrange(end, begin, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span>)) {\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 7 5<\/span>\n  }\n  \n}\n<\/pre>\n<\/div>\n<p>Now, I&#8217;m done with the function <span style=\"font-family: 'courier new', courier;\">range<\/span> and <span style=\"font-family: 'courier new', courier;\">xrange<\/span>. Let me continue with the function <span style=\"font-family: 'courier new', courier;\">map<\/span>.<\/p>\n<h2><span style=\"font-family: 'courier new', courier;\">map<\/span><\/h2>\n<p>First, here is my simplified definition of Pythons 2 <span style=\"font-family: 'courier new', courier;\">map<\/span> function. I restrict <span style=\"font-family: 'courier new', courier;\">map<\/span> to one sequence<\/p>\n<ul>\n<li><span style=\"font-family: 'courier new', courier;\">map(function, sequence<\/span>): Returns a list by applying the function to each input sequence element.<\/li>\n<\/ul>\n<p>If you think about it, there is one challenge to overcome. In contrast to Pythons function filter (<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-pythonic-with-the-ranges-library\">C++20: Pythonic with the Ranges Library<\/a>), <span style=\"font-family: 'courier new', courier;\">map<\/span> can change the type of the input sequence.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ map.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"range.hpp\"<\/span>\n\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;fstream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;range\/v3\/all.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\n\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Func, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Seq<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> map(Func func, Seq seq) {\n    \n    <span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Seq<span style=\"color: #555555;\">::<\/span>value_type value_type;\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> return_type <span style=\"color: #555555;\">=<\/span> decltype(func(std<span style=\"color: #555555;\">::<\/span>declval<span style=\"color: #555555;\">&lt;<\/span>value_type<span style=\"color: #555555;\">&gt;<\/span>()));     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>return_type<span style=\"color: #555555;\">&gt;<\/span> result{};\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">:<\/span>seq <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>transform(func)) result.push_back(i);\n    \n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> result;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ map(lambda i: i * i, range(1, 10))                               \/\/ (1)<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> map([](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">*<\/span> i; }, range(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span>) );          \n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> res) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n                                                                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ map(lambda word: (len(word), word), [\"Only\", \"for\", \"testing\", \"purpose.\"])<\/span>\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myStrings{<span style=\"color: #cc3300;\">\"Only\"<\/span>, <span style=\"color: #cc3300;\">\"for\"<\/span>, <span style=\"color: #cc3300;\">\"testing\"<\/span>, <span style=\"color: #cc3300;\">\"purpose\"<\/span>};\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res2 <span style=\"color: #555555;\">=<\/span> map([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_pair(s.size(), s); }, myStrings);\n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> p<span style=\"color: #555555;\">:<\/span> res2) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  p.first <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.second <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") \"<\/span> ;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n                                                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord = map(lambda word: (len(word), word), open(\"\/etc\/services\").read().split(\"\\n\"))<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord.sort(reverse = True)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord[:3]    <\/span>\n    std<span style=\"color: #555555;\">::<\/span>ifstream file(<span style=\"color: #cc3300;\">\"\/etc\/services\"<\/span>, std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>in);\n    std<span style=\"color: #555555;\">::<\/span>stringstream buffer;\n    buffer <span style=\"color: #555555;\">&lt;&lt;<\/span> file.rdbuf();\n    std<span style=\"color: #555555;\">::<\/span>string text <span style=\"color: #555555;\">=<\/span> buffer.str();\n\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> words <span style=\"color: #555555;\">=<\/span> text <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>split(<span style=\"color: #cc3300;\">'\\n'<\/span>);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> lengthWords <span style=\"color: #555555;\">=<\/span> map([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_pair(s.size(), s); }, words);\n    std<span style=\"color: #555555;\">::<\/span>sort(std<span style=\"color: #555555;\">::<\/span>begin(lengthWords), std<span style=\"color: #555555;\">::<\/span>end(lengthWords), std<span style=\"color: #555555;\">::<\/span>greater);\n    std<span style=\"color: #555555;\">::<\/span>for_each(std<span style=\"color: #555555;\">::<\/span>begin(lengthWords), std<span style=\"color: #555555;\">::<\/span>begin(lengthWords) <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">3<\/span>, [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;<\/span> p) {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> p.first <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.second <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    });\n       \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n}\n<\/pre>\n<\/div>\n<p>Line (4) deduces the <span style=\"font-family: 'courier new', courier;\">return_type. <\/span>The<span style=\"font-family: 'courier new', courier;\"> return_type <\/span>is the type to which all input sequence elements are transformed if the function <span style=\"font-family: 'courier new', courier;\">func<\/span> is applied to them. <span style=\"font-family: 'courier new', courier;\">std::declval&lt;value_type&gt;() <\/span>returns a rvalue reference which can be used by <span style=\"font-family: 'courier new', courier;\">decltype<\/span> to deduce the type.<\/p>\n<p>The commented-out lines are the corresponding Python code.<\/p>\n<ol>\n<li>maps each element to its square<\/li>\n<li>maps each word to a pair length of the word, and the word<\/li>\n<li>Reads each line from the file &#8220;<span style=\"font-family: 'courier new', courier;\">\/etc\/services<\/span>&#8220;, maps each line to the pair length of the line and the line, sorts the resulting sequence in reverse order, and displays the 3 longest lines.<\/li>\n<\/ol>\n<p>The screenshot shows the output of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5861\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/map.png\" alt=\"map\" width=\"650\" height=\"251\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/map.png 1139w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/map-300x116.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/map-1024x396.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/map-768x297.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I almost forgot to mention another issue I had to implement the <span style=\"font-family: 'courier new', courier;\">map<\/span> function. The call<span style=\"font-family: 'courier new', courier;\">\u00a0std::vector words = text | ranges::views::split(&#8216;\\n&#8217;);<\/span> (line 4) is deprecated. Instead, I should use the conversion operator<span style=\"font-family: 'courier new', courier;\"> ranges::to. ranges::to <\/span>is not part of C++20, so I asked the author of the ranges library, Eric Niebler, what I should do. He proposed a quite wordy solution that triggered a GCC bug.\u00a0 Here is the bug report\u00a0<a href=\"https:\/\/gcc.gnu.org\/bugzilla\/show_bug.cgi?id=93936\">93936<\/a> from Eric. Finally, I stick to the deprecated version.<\/p>\n<p>The function map is not the end of my experiments. I said to myself. Let&#8217;s combine <span style=\"font-family: 'courier new', courier;\">map<\/span> and <span style=\"font-family: 'courier new', courier;\">filter<\/span> into one function and create something similar to list comprehension in C++. Honestly, I&#8217;m not 100% satisfied with the result.<\/p>\n<h2>A flavor of list comprehension<\/h2>\n<p>My function <span style=\"font-family: 'courier new', courier;\">mapFilter<\/span> can only handle one sequence in contrast to list comprehension in Python.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ mapFilter.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"range.hpp\"<\/span>\n\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cctype&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;fstream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;range\/v3\/all.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> AlwaysTrue {                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n    constexpr <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span>()(<span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">true<\/span>;\n    }\n};\n                                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Map, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Seq, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Filt <span style=\"color: #555555;\">=<\/span> AlwaysTrue<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Seq<span style=\"color: #555555;\">::<\/span>value_type<span style=\"color: #555555;\">&gt;&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> mapFilter(Map map, Seq seq, Filt filt <span style=\"color: #555555;\">=<\/span> Filt()) {\n    \n    <span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Seq<span style=\"color: #555555;\">::<\/span>value_type value_type;\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> return_type <span style=\"color: #555555;\">=<\/span> decltype(map(std<span style=\"color: #555555;\">::<\/span>declval<span style=\"color: #555555;\">&lt;<\/span>value_type<span style=\"color: #555555;\">&gt;<\/span>()));  \n\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>return_type<span style=\"color: #555555;\">&gt;<\/span> result{};\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">:<\/span>seq <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>filter(filt) \n                     <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>transform(map)) result.push_back(i);\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> result;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \n                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ [ i * i for i in range(1, 10) ] <\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">*<\/span> i; }, range(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span>) );\n    \n                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ [ i * i for i in range(1, 10) if i % 2 == 1 ]<\/span>\n    res <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">*<\/span> i; }, range(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">10<\/span>) , \n                    [](<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">2<\/span> <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">1<\/span>; });\n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> res) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n                                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)  <\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ [(len(word), word) for word in [\"Only\", \"for\", \"testing\", \"purpose.\"]]<\/span>\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myStrings{<span style=\"color: #cc3300;\">\"Only\"<\/span>, <span style=\"color: #cc3300;\">\"for\"<\/span>, <span style=\"color: #cc3300;\">\"testing\"<\/span>, <span style=\"color: #cc3300;\">\"purpose\"<\/span>};\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res2 <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_pair(s.size(), s); }, myStrings);\n    \n                                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ [(len(word), word) for word in [\"Only\", \"for\", \"testing\", \"purpose.\"] if word[0].isupper()]<\/span>\n    myStrings <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #cc3300;\">\"Only\"<\/span>, <span style=\"color: #cc3300;\">\"for\"<\/span>, <span style=\"color: #cc3300;\">\"testing\"<\/span>, <span style=\"color: #cc3300;\">\"purpose\"<\/span>};\n    res2 <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_pair(s.size(), s); }, myStrings, \n                     [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> word){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>isupper(word[<span style=\"color: #ff6600;\">0<\/span>]); });\n    \n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> p<span style=\"color: #555555;\">:<\/span> res2) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  p.first <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.second <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") \"<\/span> ;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n    \n                                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3) <\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord = [(len(line), line) for line in open(\"\/etc\/services\").read().split(\"\\n\")]<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord = map(lambda word: (len(word), word), open(\"\/etc\/services\").read().split(\"\\n\"))<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord.sort(reverse = True)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ freqWord[:3]    <\/span>\n    std<span style=\"color: #555555;\">::<\/span>ifstream file(<span style=\"color: #cc3300;\">\"\/etc\/services\"<\/span>, std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>in);\n    std<span style=\"color: #555555;\">::<\/span>stringstream buffer;\n    buffer <span style=\"color: #555555;\">&lt;&lt;<\/span> file.rdbuf();\n    std<span style=\"color: #555555;\">::<\/span>string text <span style=\"color: #555555;\">=<\/span> buffer.str();\n\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> words <span style=\"color: #555555;\">=<\/span> text <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>split(<span style=\"color: #cc3300;\">'\\n'<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> lengthWords <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_pair(s.size(), s); }, words);\n    std<span style=\"color: #555555;\">::<\/span>sort(std<span style=\"color: #555555;\">::<\/span>begin(lengthWords), std<span style=\"color: #555555;\">::<\/span>end(lengthWords), std<span style=\"color: #555555;\">::<\/span>greater());\n    \n                                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ len([line for line in open(\"\/etc\/services\").read().split(\"\\n\") if 100 &lt; len(line) &lt; 150])<\/span>\n    words <span style=\"color: #555555;\">=<\/span> text <span style=\"color: #555555;\">|<\/span> ranges<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>split(<span style=\"color: #cc3300;\">'\\n'<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> allLines <span style=\"color: #555555;\">=<\/span> mapFilter([](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> line){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> line; }, words, \n                              [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> line){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">100<\/span> <span style=\"color: #555555;\">&lt;<\/span> line.size() <span style=\"color: #555555;\">&amp;&amp;<\/span> line.size() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">150<\/span>; });\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Number of lines: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> allLines.size();\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n}\n<\/pre>\n<\/div>\n<p>The default predicate that the filter function applies (line 2) consistently returns <span style=\"font-family: 'courier new', courier;\">true<\/span> (line 1). Always <span style=\"font-family: courier new, courier;\">true<\/span> means that the function <span style=\"font-family: 'courier new', courier;\">mapFilter<\/span> behaves per default, such as the\u00a0<span style=\"font-family: 'courier new', courier;\">map<\/span>\u00a0function. When you study all lines numbered (3), you see no difference to the previous program <span style=\"font-family: 'courier new', courier;\">map.cpp<\/span>. But now, the difference begins. The corresponding list comprehensions in Python are commented out.<\/p>\n<ul>\n<li>Line (4) calculates the square of the odd numbers.<\/li>\n<li>Line (5) returns pairs (length of the word, word) if the word starts with a capital character.<\/li>\n<li>Line (6) returns a vector of all lines of the file &#8220;<span style=\"font-family: 'courier new', courier;\">\/etc\/services<\/span>&#8220;, which have between 100 and 150 characters.<\/li>\n<\/ul>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5862\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/mapFilter.png\" alt=\"mapFilter\" width=\"300\" height=\"250\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/mapFilter.png 377w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/03\/mapFilter-300x250.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/h2>\n<h2>What&#8217;s next?<\/h2>\n<p>This post was a little longer than usual. My <a href=\"https:\/\/www.modernescpp.com\/index.php\/personal-words-about-corona\">next post<\/a> is about generalized functions that can be paused and resumed. To make it short: my next post is about coroutines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I finish my experiment writing beloved Python functions in C++. So far, I have implemented the Python functions filter, range, and xrange. Today, I have a closer look at the map function and combine the functions map and filter into one function.<\/p>\n","protected":false},"author":21,"featured_media":8380,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[412,413],"class_list":["post-5863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-python","tag-ranges"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5863"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5863\/revisions"}],"predecessor-version":[{"id":8386,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5863\/revisions\/8386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8380"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}