4 ms·
I dunno. This reminds me of that post about "Say what you mean in javascript" http://news.ycombinator.com/item?id=1358753 http://news.ycombinator.com/item?id=1
by iamwil 16y ago
I dunno. This reminds me of that post about "Say what you mean in javascript"
http://news.ycombinator.com/item?id=1358753 http://news.ycombinator.com/item?id=1358753
Sometimes, things are confusing or are seemingly super weird depending on your background. While I haven't done C++ in years, I recognized that std::accumulate() was probably very much like Ruby's inject().
I suspect those that have seen map, inject, and their ilk wouldn't be so confused.
While you can argue that the 'average corporate programmer' wouldn't know what the hell it is, and you might be right. But if we stuck with that attitude, we'd still be using goto's liberally because the 'average corporate programmer' would find for loops weird and confusing.
- loup-vaillant 16y ago> I suspect those that have seen map, inject, and their ilk wouldn't be so confused. No doubt. However, C++ stays a major contributor to that confusion. In ML languages, let total = foldl (+) 1 (map face_value dice) is nearly idiomatic. Compare that to the C++ code. Compare that to C++ code that would use accumulate and an equivalent of map.
- lsb 16y agoAnd in Haskell, foldl (+) 1 (map face_value dice) would use fusion to avoid causing map to create another array!
- nominolo 16y agoIncidentally fusion won't work for this example (at least not as currently implemented.) Only `foldr` is fusable. Fortunately, regular inlining does just fine. There is of course an even simpler version: sum (map face_value dice)
- ramchip 16y agoWhy does the sum start with 1? Why not eg. foldl1' (+) $ map face_value dice
- loup-vaillant 16y agoIt should have started with 0. Sorry. And I avoided `foldl1` because I wanted to stay close to C++'s `accumulate`.
- jvoorhis 16y agoShouldn't that be: let total = foldl (+) 0 (map face_value dice)
- loup-vaillant 16y agoWhoops. Yes it should.
- iamwil 16y agoI don't disagree. But it's a weakness of C++, not of the language concept itself. Perhaps if OP was saying you shouldn't use it for C++, then maybe I'd say he'd have a case.
- jedbrown 16y ago> Perhaps if OP was saying you shouldn't use it for C++ I think that's exactly what he was saying.
- loup-vaillant 16y agoIt is.
- iamwil 16y agoAh. I didn't get that from reading it.