Skip to content

Instantly share code, notes, and snippets.

@MayDaniel
Created November 13, 2010 14:41
Show Gist options
  • Save MayDaniel/675377 to your computer and use it in GitHub Desktop.
Save MayDaniel/675377 to your computer and use it in GitHub Desktop.
(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