C++ Logo

std-proposals

Advanced search

Python-style comprehensive containers

From: Phil Bouchard <boost_at_[hidden]>
Date: Wed, 16 Jun 2021 07:54:23 -0400
Greetings,


1) I admit code is quite clean using Python-style comprehensive
containers. So by looking around I know there is the following already
available:

https://www.codeproject.com/Tips/5276120/Multi-Container-List-Comprehension-in-Cplusplus


2) But I'm sure there is a way to mix that with the new pipe operator to
make it even cleaner:

set nums = {1,2,3,4};

/* insert all elements multiplied by themselves */
auto result = nums | each(x, x*x);

/* insert all elements but only multiply those that fulfill the condition */
auto result = nums | each(x, (x>2)?x*x:x);

/* insert only the elements that fulfill the condition */
auto result = nums | only(x, x<2, x*x);

/* insert and print only the elements that fulfill the condition */
auto result = nums | only(x, x<4 && cout<<x, x*x); /* prints: 123 */


3) The standards should make room to parallelizing these algorithms as well.


Regards,

-- 
*Phil Bouchard*
Founder & CTO
C.: (819) 328-4743
Fornux Logo <http://www.fornux.com>

Received on 2021-06-16 06:54:28