

Generic Function Objects - AndreyKarpov
http://yapb-soc.blogspot.ru/2012/11/generic-function-objects-or.html

======
cabalamat
If I understand him correctly (and I'm not sure if I do) he's trying to do in
C++ what in Python would be:

    
    
        def addAll(*xs):
            return reduce(operator.add, xs)
    

and in Haskell:

    
    
        addAll = foldl (+) 0
    

Is this right?

~~~
CJefferson
The Haskell comparision is I think wrong (from my limited knowledge of
Haskell, rather than C++). He is folding over a tuple of different types,
rather than an array, allowing the return type of each tuple operation to be
different. Not sure where this would be useful of course.

Doing foldl (+) 0 in C++ is done traditionally with something like (OK, still
not that nice)

    
    
        std::accumulate(v.begin(), v.end(), std::plus<int>(), 0)

~~~
Evbn
To make it work in Haskell you would need to wrap the elements in a
FoldElement type and the functions in a FoldFunction type, and then allow not
total functions or some Either/Maybe style defaulting, and use case analysis
to unpack each FoldElement.

It could be useful if you are building a general framework (like a calculator,
or a game AI decision engine) for user-defined functions.

------
dexen
A fine example of <http://en.wikipedia.org/wiki/Greenspuns_tenth_rule> at
work.

~~~
CJefferson
Really? Which bit of this looks anything like a lisp interpreter?

~~~
gsg
Common Lisp has a bunch of these kind of operators defined "variadically":
equality, relational and arithmetic ops, min, max, gcd, lcm, etc.

------
nnq
seeing these concepts expressed in C++ syntax makes my brain wanna puke...

