3 ms·
Sorry, but I think we'll have to agree that this just depends on background. I can figure out what the second is doing significantly quicker than the first. W
by mquander 16y ago
Sorry, but I think we'll have to agree that this just depends on background. I can figure out what the second is doing significantly quicker than the first.
When I think about this problem, my mind immediately thinks "well, I want to take all the characters, group them into buckets, one per letter, and then count how many are in each bucket." I don't think "I want to make an empty map, starting with each character mapping to zero, iterate over every character and increment an associated counter in the map, and then read out the counters at the end." If I have to write the second -- because my language has bad list libraries or no higher-order-functions -- I have to "translate" the first into the second in my head.
The Clojure code expresses the computation in exactly the same way it lives in my head. The imperative code expresses that like a computer does it, and to even understand it I have to "step through" it in my head a little bit to make sure the computer is doing the right thing.