Paul Nathan
Paul Nathan

Reputation: 40319

STL name for the "map" functional programming function

I would like to be able to write something like

char f(char);
vector<char> bar;
vector<char> foo = map(f, bar);

The transform function appears to be similar, but it will not autogenerate the size of the resultant collection.

Upvotes: 54

Views: 41578

Answers (5)

Dany Gagnon
Dany Gagnon

Reputation: 112

The std::transform function does the job, but isn't performant in some cases. I would suggest using a while loop and reserving the size before hand. This function can easily be changed to be used with strings or any thing mappable for that matter.

template<typename T, typename C>
std::vector<T> map(const std::vector<C> &array, auto iteratee) {
  int index = -1;
  int length = array.size();
  std::vector<T> v(length);
  while(++index < length) {
    v[index] = iteratee(array[index], index);
  }
  return v;
}

Calling the function where array is the std::vector you want to map.

auto result = map<int, int>(array, [](int elem, int index) {
  return elem + 10;
});

Running map on 100 000 000 with std::transform took ~6.15s

the while loop version took ~3.90s

Upvotes: 1

Don F
Don F

Reputation: 159

You can mimic the map syntax above with something like

template<typename T, typename A>
T map(A(*f)(A), T & container) {
    T output;
    std::transform(container.begin(), container.end(), std::back_inserter(output), f);
    return output;
}

Upvotes: 3

einpoklum
einpoklum

Reputation: 131970

This question was asked before the C++11 standard went into effect... nowadays we have std::transform() as the (ugly) equivalent of a functional programming 'map'. Here's how to use it:

auto f(char) -> char; // or if you like: char f(char)
vector<char> bar;
vector<char> foo;
// ... initialize bar somehow ...
std::transform(bar.begin(), bar.end(), std::back_inserter(foo), f);

Upvotes: 31

MSalters
MSalters

Reputation: 179981

To make this work, you'll need the following observations:

  1. To make the assignment efficient, the map function should not do the work. Instead, it should save its arguments in a temporary object (in your case, that would be an instance of class map::result<char(*)(char), vector<char> >)
  2. This map::result temporary should have an template <typename T> operator T conversion.
  3. When the map::result is assigned to a std::vector<char>, this conversion is the only viable.
  4. In the conversion operator class map::result<char(*)(char), vector<char> >::operator vector<char> you have the input and return type, and the mapping function. At this point you can effectively transform the inputs.

<edit>

Code

template<typename CONT, typename FUNC>
class mapresult {
    CONT const& in;
    FUNC f;
public:
    template<typename RESULT> RESULT to() const
    {
        RESULT out;
        for (auto const& e : in) { out.push_back(f(e)); }
        return out;
    }
    template<typename RESULT> operator RESULT() const
    {
        return this->to<RESULT>();
    }
    mapresult(CONT const& in, FUNC f) : in(in), f(std::move(f)) { }
};

template<typename CONT, typename FUNC>
auto map(CONT const& in, FUNC f) -> mapresult<CONT, FUNC>
{
    return mapresult<CONT, FUNC>(in, f);
}

Use like this:

using namespace std;
char foo(char c) { return c | ('A' ^ 'a'); }
std::string in = "Test";

int main(int argc, char* argv[])
{
    string out = map(in, &foo);
    cout << out << endl;

    char replace = 'e';
    cout << map(in, [replace](char c){return c == replace ? '?' : c; }).to<string>();
}

Upvotes: 4

Khaled Alshaya
Khaled Alshaya

Reputation: 96879

You can use std::back_inserter in <iterator>, although providing the size in front is more efficient. For example:

string str = "hello world!", result;
transform(str.begin(), str.end(), back_inserter(result), ::toupper);
// result == "HELLO WORLD!"

Upvotes: 54

Related Questions