Skip to content

Instantly share code, notes, and snippets.

@kannangce
Last active September 20, 2022 17:45
Show Gist options
  • Save kannangce/932851633902c7f5b398540a7649a294 to your computer and use it in GitHub Desktop.
Save kannangce/932851633902c7f5b398540a7649a294 to your computer and use it in GitHub Desktop.
Handy Clojure utils functions.
(defn create-matrix
"Creates a multi-dimensional vector with dimensions given in dims
with the given default value"
([default-value & dims]
(loop [val default-value remaining dims]
(if (empty? remaining)
val
(recur (vec (repeat (first remaining) val))
(rest remaining)))
))
([] nil))
(defn apply-fn-times
"Applies given function f for the given times t on the result of
each application, starting with p.
For ex (apply-fn-times inc 1 3) is equivalent to (inc(inc(inc 1)))"
[f p t]
((apply comp (repeat t f)) p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment