fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <algorithm>
  5. using namespace std;
  6. template <typename Container, typename F>
  7. Container Map(F f, Container xs )
  8. {
  9. Container 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. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Standard output is empty