4 ms·
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.
by cabalamat 14y ago
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?
- scott_s 14y agoIn functionality, I believe that is correct. The key difference, though, is that his techniques are closer to using macros in Lisp to create new functions, rather than using function composition.
- CJefferson 14y agoThe 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 14y agoTo 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.