4 ms·
The problem with the latter solution is that apply isn't lazy. The more correct solution would be a simple reduce: (reduce #(update-in %1 [%2] (fnil inc 0))
by ataggart 16y ago
The problem with the latter solution is that apply isn't lazy. The more correct solution would be a simple reduce:
(reduce #(update-in %1 [%2] (fnil inc 0)) {} "abcdaabccc")
- runevault 16y agoI wonder if update-in or merge-with would be more efficient. Another thing to consider is if you want to get fancy (persistent! (reduce #(update-in %1 [%2] (fnil inc 0)) (transient {}) "abcdaabccc")) Still no REPL but this should work and take advantage of transients if you're doing it against a bigger dataset (which I assume you care about since you also worried about laziness)
- deleted 16y ago[deleted]
- praptak 16y agoApply itself is lazy. This: (apply f infinite-lazy-list) does not force evaluation of the infinite-lazy-list, if the f itself does not force it. My favorite example is (apply concat ...), which can be used to flatten an infinite sequence of sequences (concat is lazy wrt to its argument list, so this works.)