Skip to content

Instantly share code, notes, and snippets.

@dominem
Last active March 19, 2020 22:28
Show Gist options
  • Save dominem/3f7182b049644f83c8f8344e86a7ceca to your computer and use it in GitHub Desktop.
Save dominem/3f7182b049644f83c8f8344e86a7ceca to your computer and use it in GitHub Desktop.
Clojure's custom map implementation using reduce
(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