11 ms·
One thing I don't get is why do people prefer functions like accumulate? Why not: int sum = 0; for(auto&& wc : word_count) sum += wc.second return
by okasaki 11y ago
One thing I don't get is why do people prefer functions like accumulate? Why not:
int sum = 0;
for(auto&& wc : word_count) sum += wc.second
return sum;
It's shorter, more general, more familiar, possibly has better performance[[citation needed]], and gives better errors if you mess something up.
- lmf4o 11y agofunctional programming hype
- adrusi 11y agoIts more composable, and because its less general, it more clearly articulates intent and iis easier to scan for bugs. Or at least it would in an ecosystem where functional programming is embraced, but I don't know how practical it is in practice. Its not composable if you can't expect any other given piece of code to be written in a way conducive to composing with accumulate.
- Veedrac 11y agoIn C++? No idea - I think C++ just has an inferiority complex and wants to look cool. In other languages, though, it's pretty neat. For example, in Rust one just does data.iter().fold(0, |s, e| s + e)