Skip to content

Instantly share code, notes, and snippets.

@ifesdjeen
Created June 13, 2013 10:08
Show Gist options
  • Save ifesdjeen/5772655 to your computer and use it in GitHub Desktop.
Save ifesdjeen/5772655 to your computer and use it in GitHub Desktop.
Fast repeatedly
(defn repeatedly*
"Like `repeatedly` but faster and returns given collection type."
[coll n f]
(if-not (instance? clojure.lang.IEditableCollection coll)
(loop [v coll idx 0]
(if (>= idx n)
v
(recur (conj v (f)) (inc idx))))
(loop [v (transient coll) idx 0]
(if (>= idx n)
(persistent! v)
(recur (conj! v (f)) (inc idx))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment