Python list comprehension-like syntax in C++
In Pyhon: y = [foo(_1) for _1 in x] In C++: list<int> x; x.push_back(...); .. list<int> y; for (list<int>::iterator it = x.begin(); x.end() != it; ++ it) y.push_back( foo(y) ); With boost, can we write the Python-like statement: copy( x.begin(), x.end(), magic_back_inserter(foo(_1), y) ); // pseudo-code -- Tzu-Chien Chiu, 3D Graphics Hardware Architect URL:http://www.csie.nctu.edu.tw/~jwchiu
Tzu-Chien Chiu wrote:
In Pyhon:
y = [foo(_1) for _1 in x]
In C++:
list<int> x; x.push_back(...); ..
list<int> y; for (list<int>::iterator it = x.begin(); x.end() != it; ++ it) y.push_back( foo(y) );
With boost, can we write the Python-like statement:
copy( x.begin(), x.end(), magic_back_inserter(foo(_1), y) ); // pseudo-code
With the range_ex library in boost-sandbox, this would look like: boost::copy( x | boost::adaptor::transform(&foo) , std::back_inserter(y) ); range_ex.zip lives at: http://www.boost-consulting.com/vault/index.php?directory=Algorithms HTH -- Eric Niebler Boost Consulting www.boost-consulting.com
In Pyhon:
y = [foo(_1) for _1 in x]
In C++:
list<int> x; x.push_back(...); ..
list<int> y; for (list<int>::iterator it = x.begin(); x.end() != it; ++ it) y.push_back( foo(y) );
With boost, can we write the Python-like statement:
copy( x.begin(), x.end(), magic_back_inserter(foo(_1), y) ); // pseudo- code
With the range_ex library in boost-sandbox, this would look like:
boost::copy( x | boost::adaptor::transform(&foo) , std::back_inserter(y) );
What about: std::transform( x.begin(), x.end(), std::back_inserter(y), boost::bind( &foo, _1 ) ); Sebastien Martel
Seb Martel wrote:
std::transform( x.begin(), x.end(), std::back_inserter(y), boost::bind( &foo, _1 ) );
Or even
std::transform( x.begin(), x.end(), std::back_inserter(y), foo );
Could it be possible that bind detects identity usage like this and notify of the inefficiency?
bind( foo, _1 ) is not identity. It converts foo into a function object that is capable of taking N arguments, N >= 1, and ignoring all but the first. (Similarly, bind( foo, _2 ) ignores all arguments but the second.) In a template parameterized by the type of f, bind( f, _1 ) also works as an "universal adaptor" of sorts, turning member pointers and reference_wrappers into function objects (and defining a ::result_type as an extra bonus.)
Tzu-Chien Chiu wrote:
In Pyhon:
y = [foo(_1) for _1 in x]
In C++:
list<int> x; x.push_back(...); ..
list<int> y; for (list<int>::iterator it = x.begin(); x.end() != it; ++ it) y.push_back( foo(y) );
With boost, can we write the Python-like statement:
copy( x.begin(), x.end(), magic_back_inserter(foo(_1), y) ); // pseudo-code
-- Tzu-Chien Chiu, 3D Graphics Hardware Architect URL:http://www.csie.nctu.edu.tw/~jwchiu But what would an expression-template version look like? y = for_( _1 >> in >> x)[foo] That is what the composition operator would be for: operator _, say, so we could do : foo for_ _1 in x
participants (5)
-
Eric Niebler
-
Peter Dimov
-
Seb Martel
-
Simon Buchan
-
Tzu-Chien Chiu