NikolaDimitroff

Pythonic C++

Oct 14th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. #define forevery ,
  5. #define inside ,
  6.  
  7. #define PythonRange2(expression, variable, collection) \
  8.     [&collection]() \
  9.     { \
  10.         std::remove_reference<decltype(collection)>::type __collectionCopy; \
  11.         for (const auto& variable : collection) \
  12.         { \
  13.             __collectionCopy.push_back(expression); \
  14.         } \
  15.         return __collectionCopy; \
  16.     }()
  17.  
  18. #define LeftBrace (
  19.  
  20. #define PythonRange(expression) \
  21.     PythonRange2 LeftBrace expression )
  22.  
  23. int main() {
  24.     std::vector<int> myList = { 1, 2, 3 };
  25.     auto transformed = PythonRange(x * x forevery x inside myList);
  26.  
  27.     for (const auto& x : transformed)
  28.     {
  29.         std::cout << x << std::endl;
  30.     } // 1, 4, 9
  31. }
Add Comment
Please, Sign In to add comment