Skip to content

Instantly share code, notes, and snippets.

@arichiardi
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arichiardi/bb1015227d593d2c6307 to your computer and use it in GitHub Desktop.
Save arichiardi/bb1015227d593d2c6307 to your computer and use it in GitHub Desktop.
Some snippet from my Clojure hacks.
(ns com.andrearichiardi.utils
(:require [clojure.java.io :as io]))
(defn make-file-from-resource [name] (io/file (io/resource name)))
(defn to-bit-representation [^java.lang.Double d]
(-> (Double/doubleToRawLongBits d) (java.lang.Long/toBinaryString)))
(defmacro with-private-fns [[ns fns] & tests]
"Refers private fns from ns and runs tests in context."
`(let ~(reduce #(conj %1 %2 `(ns-resolve '~ns '~%2)) [] fns)
~@tests))
(defn remove-and-cons
"Removes the items in coll for which (pred item) is true and then conses new-item.
Returns a lazy seq."
[pred new-item coll]
(lazy-seq (cons new-item (remove pred coll))))
(defmacro do-if-not-value
"This macro executes (do side-effect not-found) if a value
corresponding to accessor cannot be found in coll"
[coll accessor not-found side-effect]
`(let [~'v# (get ~coll ~accessor)]
(if-not ~'v#
(do ~side-effect ~not-found)
~'v#)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment