fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. namespace mtl {
  7.  
  8. template <typename ForwardIterator, typename U>
  9. std::vector<U> map(std::function<U(typename ForwardIterator::value_type)> fn,
  10. const ForwardIterator &input) {
  11. std::vector<U> output;
  12. std::transform(input.cbegin(), input.cend(), std::back_inserter(output), fn);
  13. return std::move(output);
  14. }
  15.  
  16. template <typename ForwardIterator, typename U>
  17. std::vector<U> map(U (*fn)(typename ForwardIterator::value_type),
  18. const ForwardIterator &input) {
  19. std::function<U(typename ForwardIterator::value_type)> std_fn = fn;
  20. return map(std_fn, input);
  21. }
  22.  
  23. } // namespace mtl
  24.  
  25. float example_fn(int x) { return x * x; }
  26.  
  27. std::vector<int> input() { return std::vector<int>({1, 2, 3, 4}); }
  28.  
  29. std::vector<float> try_free_fn() { return mtl::map(example_fn, input()); }
  30.  
  31. std::vector<float> try_std_fn() {
  32. return mtl::map(std::function<float(int)>(example_fn), input());
  33. }
  34.  
  35. std::vector<float> try_lambda() {
  36. return mtl::map(+[](int x) -> float { return x * x; }, input());
  37. }
  38.  
  39. template <typename U>
  40. U call(U(*fn)()) {
  41. return fn();
  42. }
  43.  
  44. int one() {
  45. return 1;
  46. }
  47.  
  48. int try_call() {
  49. return call(+[]() -> int { return 0; });
  50. }
  51.  
  52. int main(int argc, char** argv) {
  53.  
  54. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty