4 ms·
Does 'accumulate' work on non-'numeric' types? Part of the beauty of Haskell is the aggressive effort to define each function on the most general type it appli
by Drbble 15y ago
Does 'accumulate' work on non-'numeric' types?
Part of the beauty of Haskell is the aggressive effort to define each function on the most general type it applies to (and then specialize to concrete types during compilation, a la C++, when performance is requested via a pragma), leading to a rich hierarchy of tiny type classes (type classes are similar to C++ abstract classes)
- hythloday 15y agoYeah, it's a real fold, just misleadingly named: int main() { string s[] = { "a", "b", "c" }; // will print "abc" cout << accumulate(s, s+3, string(""), [](string l, string r) { return l+r; }); return 0; } I think a closer C++ equivalent for typeclasses are specialized template functions--for example, if I define: template<InputIterator> string accumulate<string>(InputIterator first, InputIterator last, string init); then my call to accumulate(s, s+3, string("")) will use that specialization.