Created
March 23, 2015 23:21
-
-
Save LispyAriaro/467146959f6451d2d7de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;How to count all occurrences of 42? | |
(count (filter #{42} coll)) | |
;How to express count using reduce? | |
(defn my-count [coll] (reduce (fn [n _] (inc n)) 0 coll)) | |
;How to count all occurrences of 42 using reduce? | |
(reduce (fn [n _] (inc n)) 0 (filter #{42} coll)) | |
;Can you get rid of the filter? | |
(reduce (fn [n x] (if (= 42 x) (inc n) n)) 0 coll) | |
;I'd like the result to be {42 occurences-count}. | |
(reduce (fn [m x] (if (= 42 x) (assoc m 42 (inc (m 42))) m)) {42 0} coll) | |
;42 is hardcoded in four places, it's bad! | |
(reduce (fn [m x] (if (= 42 x) (assoc m x (inc (m x))) m)) {42 0} coll) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment