Created
November 13, 2010 14:41
-
-
Save MayDaniel/675377 to your computer and use it in GitHub Desktop.
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
(defn repeat | |
([x] (iterate identity x)) | |
([n x] (take n (repeat x)))) | |
(def bar (doall (repeat 100 5))) | |
(defn mapmap [f coll] | |
(zipmap coll (map f coll))) | |
;; => (time (dotimes [_ 10000] (mapmap (partial * 10) bar))) | |
;; => "Elapsed time: 759.188944 msecs" | |
(defn mapmap [f coll] | |
(persistent! | |
(reduce #(assoc! %1 %2 (f %2)) (transient {}) coll))) | |
;; => (time (dotimes [_ 10000] (mapmap (partial * 10) bar))) | |
;; => "Elapsed time: 543.236477 msecs" | |
(defn mapcachemap [f coll] | |
(reduce #(assoc %1 %2 (or (%1 %2) (f %2))) {} coll)) | |
;; => (time (dotimes [_ 10000] (mapcachemap (partial * 10) bar))) | |
;; => "Elapsed time: 246.671207 msecs" | |
(defn mapcachemap [f coll] | |
(reduce #(if (%1 %2) %1 (assoc %1 %2 (f %2))) {} coll)) | |
;; ___ | |
(defn mapmap | |
([f coll] (into {} (map (juxt identity f) coll))) | |
([f coll & more] (into {} (apply map (juxt vector f) coll more)))) | |
;; => (mapmap + [1 2 3] [4 5 6]) | |
;; => {[1 4] 5, [2 5] 7, [3 6] 9} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment