fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <algorithm>
  5. using namespace std;
  6. template <typename Container, typename F>
  7. ContainerOut Map( const F& f, const ContainerIn& xs )
  8. {
  9. ContainerOut ys;
  10. auto it = std::inserter( ys, end( ys ) );
  11. std::transform( begin( xs ), end( xs ), it, f );
  12. return ys;
  13. }
  14. template <typename Ret, typename Arg1, typename... Args>
  15. auto Curry(Ret (*f)(Arg1, Args...), Arg1 arg ) -> std::function<Ret(Args...)>
  16. {
  17. return [=]( Args... args ) { return f( arg, args... ); };
  18. }
  19. int main()
  20. {
  21. auto square = [](int x){ return x*x; };
  22.  
  23. typedef std::vector<int> Row;
  24. Row row = {1, 2 , 3};
  25. Row squaredRow = Map(square, row);
  26.  
  27. typedef std::vector<Row> Mat;
  28. Mat mat = {{1,2},{2,3,4}};
  29. auto squareElements = Curry(Map<Row, decltype(square)>, square);
  30. Mat squaredMat = Map(squareElements, mat);
  31.  
  32. return 0;
  33. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:1: error: 'ContainerOut' does not name a type
 ContainerOut Map( const F& f, const ContainerIn& xs )
 ^
prog.cpp: In function 'int main()':
prog.cpp:25:35: error: 'Map' was not declared in this scope
   Row squaredRow = Map(square, row);
                                   ^
prog.cpp:29:38: error: expected primary-expression before ',' token
   auto squareElements = Curry(Map<Row, decltype(square)>, square);
                                      ^
prog.cpp:29:40: error: expected primary-expression before 'decltype'
   auto squareElements = Curry(Map<Row, decltype(square)>, square);
                                        ^
stdout
Standard output is empty