;; What a lot of ways there are to write frequencies! (defn freq-merge [sq] (apply merge-with + (map (fn [c] {c 1}) sq))) (defn freq-reduce-assoc [sq] (reduce #(assoc %1 %2 (inc (get %1 %2 0))) {} sq)) (defn freq-reduce-update [sq] (reduce #(update-in %1 [%2] (fnil inc 0)) {} sq)) (defn freq-loop [sq] (loop [acc {} sq sq] (if-let [sq (seq sq)] (recur (assoc acc (first sq) (inc (get acc (first sq) 0))) (rest sq)) acc))) ;; Here's the source definition (defn frequencies "Returns a map from distinct items in coll to the number of times they appear." {:added "1.2"} [coll] (persistent! (reduce (fn [counts x] (assoc! counts x (inc (get counts x 0)))) (transient {}) coll))) (def sq "abcdaabccc") (time (frequencies sq)) "Elapsed time: 0.13 msecs" (time (freq-loop sq)) "Elapsed time: 0.12 msecs" (time (freq-reduce-assoc sq)) "Elapsed time: 0.13 msecs" (time (freq-merge sq)) "Elapsed time: 0.20 msecs" (time (freq-reduce-update sq)) "Elapsed time: 0.27 msecs" (def sq (apply str (repeat 1000 "abcdaabccc"))) (time (frequencies sq)) "Elapsed time: 8 msecs" (time (freq-loop sq)) "Elapsed time: 13 msecs" (time (freq-reduce-assoc sq)) "Elapsed time: 13 msecs" (time (freq-merge sq)) "Elapsed time: 42 msecs" (time (freq-reduce-update sq)) "Elapsed time: 45 msecs" (def sq (apply str (repeat 100000 "abcdaabccc"))) (time (frequencies sq)) "Elapsed time: 900 msecs" (time (freq-loop sq)) "Elapsed time: 1500 msecs" (time (freq-reduce-assoc sq)) "Elapsed time: 1500 msecs" (time (freq-merge sq)) "Elapsed time: 5000 msecs" (time (freq-reduce-update sq)) "Elapsed time: 5400 msecs" ;; Mumon's commentary: Learn and use the standard library ;; I don't know how many people read my ramblings, but if anyone notices places ;; where I could have used a library function, but have done it the hard way, ;; I'd consider it a great favour if you'd let me know.
Blog Archive
-
▼
2010
(44)
-
▼
September
(15)
- Clojure 1.3 : First Impression : Clojure vs Scala ...
- Clojure 1.3 alpha release : A pom.xml to try it ou...
- Macros and Type Hints: Metadata and the Medusa.
- Clojure Faster than Machine Code?
- Graphics like it's 1979: How to get clojure to act...
- An Astonishing Macro of Narayan Singhal
- Clojure Swank Server is Insecure by Default : Use ...
- Clojure is Fast
- Clojure Macro Tutorial (Part III: Syntax Quote)
- Clojure Macro Tutorial (Part II: The Compiler Stri...
- Clojure Macro Tutorial (Part I, Getting the Compil...
- defn vs defmacro: What is a macro? Why is it diffe...
- reduce-seq: An Exercise in Laziness and Futility
- Learn and Use the Standard Library
- Understanding RSA Public Key Encryption with Clojure
-
▼
September
(15)
Search This Blog
Sunday, September 5, 2010
Learn and Use the Standard Library
Subscribe to:
Post Comments (Atom)
great point. i had written a crazy (for) to piece together a string only to later find out that i had forgotten about str-join.
ReplyDelete