Created
December 16, 2015 06:34
-
-
Save gugat/210f5be93b162f1f0ebc to your computer and use it in GitHub Desktop.
Find ocurrences of items
This file contains hidden or 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
| (let [my_list '(a b b c a a c) my_set (set my_list)] | |
| (zipmap | |
| my_set | |
| (map | |
| (fn [y] | |
| (count | |
| (filter | |
| (fn [x] (= x y)) my_list | |
| ) | |
| ) | |
| ) my_set | |
| ) | |
| ) | |
| ) |
jplaza
commented
Dec 21, 2015
Una versión más idiomática de tu solución
(let [my-list '(a b b c a a c)
ks (set my-list)]
(into {} (map (fn [y] [y (count (filter #{y} my-list))]) ks)))
;; O también
(let [my-list '(a b b c a a c)
ks (set my-list)]
(zipmap ks (map (fn [y] (count (filter #{y} my-list))) ks)))
;; Hecha función
(def my-list '(a b b c a a c))
(defn count-ocurrences [my-list]
(let [ks (set my-list)]
(into {} (map (fn [y] [y (count (filter #{y} my-list))]) ks))))
(clojure.pprint/pprint (count-ocurrences my-list))
(defn freq [the-list] (reduce #(assoc %1 %2 (inc (%1 %2 0))) {} the-list))Similar a la mía pero igual válida 😄
Que viva la programación funcional 🎉
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment