10 ms·
Generic Function Objects
- dexen 14y agoA fine example of http://en.wikipedia.org/wiki/Greenspuns_tenth_rule http://en.wikipedia.org/wiki/Greenspuns_tenth_rule at work.
- DanielBMarkham 14y agoHe's not there yet. Give him time.
- CJefferson 14y agoReally? Which bit of this looks anything like a lisp interpreter?
- gsg 14y agoCommon Lisp has a bunch of these kind of operators defined "variadically": equality, relational and arithmetic ops, min, max, gcd, lcm, etc.
- nnq 14y agoseeing these concepts expressed in C++ syntax makes my brain wanna puke...
- cabalamat 14y agoIf 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.