[ create a new paste ] login | about

Link: http://codepad.org/j8SdDxUo    [ raw code | output | fork ]

C++, pasted on Oct 12:
#include <tr1/functional>

using std::list;
using std::tr1::function;

template<typename a, typename b>
list<b> mymap(function<b(a)> f, list<a> x)
{
  list<b> y;
  std::transform(x.begin(), x.end(), std::back_inserter(y), f);
  return y;
}

double the_f(int i) { return i / 2.; }

int main()
{
  list<int> x; x.push_back(3); x.push_back(9);
  function<double(int)> f = the_f;
  list<double> y = mymap(f, x);
  std::cout << y;
}


Output:
1
[1.5, 4.5]


Create a new paste based on this one


Comments: