fork(1) download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iterator>
  4. #include <utility>
  5. #include <vector>
  6.  
  7. template <typename Container, typename F>
  8. Container Map( const F& f, const Container& xs )
  9. {
  10. Container ys;
  11. // For performance reasons one would use
  12. // ys.reserve( xs.size() )
  13. // and std::back_inserter instead of std::inserter
  14. // if ys is a std::vector.
  15. auto it = std::inserter( ys, end( ys ) );
  16. std::transform( begin( xs ), end( xs ), it, f );
  17. return ys;
  18. }
  19.  
  20. template <typename F, typename T>
  21. auto Curry(F&& f, T&& t)
  22. {
  23. return [f = std::forward<F>(f), t = std::forward<T>(t)]
  24. (auto&&... args)
  25. { return f(t, std::forward<decltype(args)>(args)...); };
  26. }
  27.  
  28. int main()
  29. {
  30. auto square = [](int x){ return x*x; };
  31.  
  32. typedef std::vector<int> Row;
  33. Row row;
  34. Row squaredRow = Map(square, row);
  35.  
  36. typedef std::vector<Row> Mat;
  37. Mat mat;
  38. auto squareRow = Map<Row, decltype(square)>;
  39. auto squareRowElems = Curry((Map<Row, decltype(square)>), square);
  40. Mat squaredMat = Map(squareRowElems, mat);
  41. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Standard output is empty