4 ms·
It always amazes me how many people either never noticed merge-with or simply don't use it. Having done something similar with a list of words (created via reg
by runevault 16y ago
It always amazes me how many people either never noticed merge-with or simply don't use it. Having done something similar with a list of words (created via regex parsed over a file) I adore what you can do with it. Especially if you use transients.
Unless of course they just didn't think of the fact strings are seqs in clojure by default.
edit: I could be crazy, but one problem with using apply is you force the realization of your object, so while in THIS case it's fine, using larger amount of data would cause a problem. IIRC more space sane way to do this is use
(reduce {} (merge-with + %) list)
and man I MUST be rusty, I can't remember if the default value comes before or after the function, and no repl handy. Hopefully the point is gotten across :)
- runevault 16y agoReplying to myself. Just googled it and I had the code wrong shame (defn count-words [coll] (reduce #(merge-with + %1 {%2 1}) {} coll))
- jared314 16y agoLooking from the outside in, clojure has too many useful/amazing functions to learn in an afternoon, weekend, or a month. So, not using something you don't understand is perfectly valid in my opinion.