The following is the implementation of fold in C ++ syntax. Please note that the code is not very reliable and serves only to demonstrate the point. It is designed to support the more common 3-argument fold operators (range, binary operation, and neutral).
It's easy to laugh at how to abuse (did you just say "rape"?) Operator overload and one of the best ways to shoot with a 900 pound artesian projectile.
enum { fold } fold_t; template <typename Op> struct fold_intermediate_1 { Op op; fold_intermediate_1 (Op op) : op(op) {} }; template <typename Cont, typename Op, bool> struct fold_intermediate_2 { const Cont& cont; Op op; fold_intermediate_2 (const Cont& cont, Op op) : cont(cont), op(op) {} }; template <typename Op> fold_intermediate_1<Op> operator/(fold_t, Op op) { return fold_intermediate_1<Op>(op); } template <typename Cont, typename Op> fold_intermediate_2<Cont, Op, true> operator<(const Cont& cont, fold_intermediate_1<Op> f) { return fold_intermediate_2<Cont, Op, true>(cont, f.op); } template <typename Cont, typename Op, typename Init> Init operator< (fold_intermediate_2<Cont, Op, true> f, Init init) { return foldl_func(f.op, init, std::begin(f.cont), std::end(f.cont)); } template <typename Cont, typename Op> fold_intermediate_2<Cont, Op, false> operator>(const Cont& cont, fold_intermediate_1<Op> f) { return fold_intermediate_2<Cont, Op, false>(cont, f.op); } template <typename Cont, typename Op, typename Init> Init operator> (fold_intermediate_2<Cont, Op, false> f, Init init) { return foldr_func(f.op, init, std::begin(f.cont), std::end(f.cont)); }
foldr_func and foldl_func (actual left and right fold algorithms) are defined elsewhere.
Use it as follows:
foo myfunc(foo, foo); container<foo> cont; foo zero, acc; acc = cont >fold/myfunc> zero; // right fold acc = cont <fold/myfunc< zero; // left fold
The word fold used as some poor human new reserved word here. You can define several variations of this syntax, including
<<fold/myfunc<< >>fold/myfunc>> <foldl/myfunc> <foldr/myfunc> |fold<myfunc| |fold>myfunc|
The internal statement must have the same or higher priority as the external (s). This is a limitation of C ++ grammar.
map requires only one intermediate link, and the syntax can be, for example,
mapped = cont |map| myfunc;
Implementation is a simple exercise.
Oh, and please do not use this syntax in production if you do not know very well what you are doing, and perhaps even if you do it;)