Last active
March 19, 2020 22:28
-
-
Save dominem/3f7182b049644f83c8f8344e86a7ceca to your computer and use it in GitHub Desktop.
Clojure's custom map implementation using reduce
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
(defn my-map | |
([f coll] | |
(reverse | |
(reduce (fn [result x] | |
(cons (f x) result)) | |
() | |
coll))) | |
([f coll & colls] | |
(let [colls (cons coll colls) | |
n (apply min (my-map count colls))] | |
(loop [i n | |
result ()] | |
(if (zero? i) | |
result | |
(recur (dec i) | |
(cons (apply f (my-map #(nth % (dec i)) colls)) result))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment