Hacker News new | comments | show | ask | jobs | submit login

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?



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)


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.


In 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.




Applications are open for YC Summer 2018

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: