2 ms·
map and fold are available in C++, though they're called std::transform (in <algorithm>) and std::accumulate (in <numeric>). Totally agree with the author's poi
by hythloday 15y ago
map and fold are available in C++, though they're called std::transform (in <algorithm>) and std::accumulate (in <numeric>). Totally agree with the author's point, the C++ I write nowadays is heavily influenced by my Scala/Haskell/Lisp exposure and is much better for it.
- evincarofautumn 15y agoYep. <algorithm>, <numeric>, <functional>, and <iterator> are all incredibly useful, yet so underused.
- Drbble 15y agoDoes '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.