2 Mar 2011 redi   » (Master)

In C++ foldr is pronounced std::accumulate and map is pronounced std::transform.

I don't think we have a word for unfold, but I guess I'd say it like this:


template<class OutIt, class P, class F, class G, class T>
size_t
unfold(OutIt result, P stop, F f, G g, T x)
{
    if (stop(x))
        return 0;
    *result = f(x);
    return 1 + unfold(++result, stop, f, g, g(x));
}

Edit: hmm, no I wouldn't, it's unspecified whether g or g(x) gets evaluated first, which matters if G is stateful (which it shouldn't be, but have ever tried telling a C++ programmer they shouldn't use a sharp object?) A workaround would be to pass stateful functors in by reference, e.g. using a reference wrapper such as boost::ref or std::ref but it'd be nice if that wasn't needed. I'll have to think about that further ...

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!