Skip to content

Instantly share code, notes, and snippets.

@noisesmith
noisesmith / gist:96baacb982d5924eeb9ddeabc1c4832d
Created July 13, 2016 13:01
trying some code in many clojure versions by hand
+justin@fuckmonkey:~/clojure/line-edit$ rlwrap java -jar ~/.m2/repository/org/clojure/clojure/1.8.0/clojure-1.8.0.jar
Clojure 1.8.0
+user=> (defrecord A [x y])
user.A
+user=> (.getBasis A)
IllegalArgumentException No matching field found: getBasis for class java.lang.Class clojure.lang.Reflector.getInstanceField (Reflector.java:271)
+user=>
+justin@fuckmonkey:~/clojure/line-edit$ rlwrap java -jar ~/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar
Clojure 1.7.0
+user=> (defrecord A [x y])
@noisesmith
noisesmith / gist:3490f2d3ed98e294e033b002bc2de178
Created July 7, 2016 17:32
capturing locals in clojure
kingfisher.core=> (defmacro locals-map [] (into {} (for [[sym val] &env] [(keyword (name sym)) sym])))
#'kingfisher.core/locals-map
kingfisher.core=> (let [a 0 b 1] (locals-map))
{:a 0, :b 1}
@noisesmith
noisesmith / gist:012c4c60d35088a6bc92233a10f1fe62
Created June 29, 2016 03:45
testing memoize thread safety?
:justin@fuckmonkey:~$ cat example-of-memoize.clj
(let [f (memoize (fn [x] [x (rand-int 1000)]))]
(->> (range 10)
(cycle)
(take 1000)
(map (fn [n]
(future (Thread/sleep (rand-int 100))
(f n))))
(map deref)
(group-by first)
@noisesmith
noisesmith / gist:2f920f1ca6edc566bae114136776ed03
Last active June 23, 2016 23:43
clojure condp = vs. simple hash-map lookup
user=> (crit/bench (condp = 1 1 nil))
WARNING: Final GC required 8.483608755785822 % of runtime
Evaluation count : 6919395060 in 60 samples of 115323251 calls.
Execution time mean : 6.292270 ns
Execution time std-deviation : 0.118501 ns
Execution time lower quantile : 6.111276 ns ( 2.5%)
Execution time upper quantile : 6.572198 ns (97.5%)
Overhead used : 2.391966 ns
Found 2 outliers in 60 samples (3.3333 %)
justinsmith@X: ~$ java -jar ~/Downloads/clojure-1.0.0.jar
Clojure 1.0.0-
user=> ()
()
@noisesmith
noisesmith / gist:85d7144ce90638b83f681d26d56c1f23
Created June 13, 2016 03:08
performance of java.util.function.Function / stream and map vs. clojure mapv
+user=> (crit/bench (-> (.stream (range 100)) (.map (reify java.util.function.Function (apply [this x] (inc x)))) (.collect (java.util.stream.Collectors/toList))))
Evaluation count : 4354320 in 60 samples of 72572 calls.
Execution time mean : 13.864355 µs
Execution time std-deviation : 226.969932 ns
Execution time lower quantile : 13.507800 µs ( 2.5%)
Execution time upper quantile : 14.308498 µs (97.5%)
Overhead used : 1.804276 ns
Found 1 outliers in 60 samples (1.6667 %)
low-severe 1 (1.6667 %)
@noisesmith
noisesmith / gist:9fa04e6c7c713b1d38af353874ad3520
Last active June 6, 2016 16:18
benchmarking equality of keys in clojure
user=> (crit/bench (= :a :a))
WARNING: Final GC required 7.231393615188381 % of runtime
WARNING: Final GC required 2.197827844386288 % of runtime
Evaluation count : 16534111500 in 60 samples of 275568525 calls.
Execution time mean : 1.394909 ns
Execution time std-deviation : 0.030680 ns
Execution time lower quantile : 1.350542 ns ( 2.5%)
Execution time upper quantile : 1.444246 ns (97.5%)
Overhead used : 2.241525 ns
@noisesmith
noisesmith / gist:2514a8244573e4863830984b4d035568
Last active June 1, 2016 20:43
using cemerick/pomegranate
kingfisher.core=> (require '[cemerick.pomegranate :as pom])
nil
kingfisher.core=> (def repos {"clojars" "http://clojars.org/repo"})
#'kingfisher.core/repos
kingfisher.core=> (pom/add-dependencies :coordinates '[[incanter "1.2.3"]] :repositories repos)
{[incanter/jtransforms "0.9.4"] nil, [net.sf.opencsv/opencsv "2.0.1"] nil, [org.clojure/clojure "1.2.0"] nil, [org.clojure/clojure-contrib "1.2.0"] nil, [swingrepl "1.0.0-20100502.112537-4"] nil, [org.apache.poi/poi "3.6"] #{[commons-logging "1.1" :scope "runtime"] [log4j "1.2.13" :scope "runtime"]}, [incanter/optimization "0.9.4"] nil, [commons-logging "1.1" :scope "runtime"] #{[javax.servlet/servlet-api "2.3" :scope "runtime"] [avalon-framework "4.1.3" :scope "runtime"] [logkit "1.0.1" :scope "runtime"]}, [javax.servlet/servlet-api "2.3" :scope "runtime"] nil, [incanter/jcommon "1.0.16"] nil, [incanter/incanter-processing "1.2.3"] #{[incanter/processing-core "1.1"]}, [incanter/arpack-combo "0.9.4"] nil, [org.clojars.somnium/clojure-db-object "0.1.1-20091229.02
@noisesmith
noisesmith / gist:23299a02ff7f84ed49dd4bab442a8855
Created May 26, 2016 14:02
clojure vars are mutated, if they exist in the ns, but otherwise a new var is created
+user=> (def x 1)
#'user/x
+user=> (def the-x #'x)
#'user/the-x
+user=> (ns-unmap 'user 'x)
nil
+user=> @the-x ; the var is captured, so we can still use it
1
+user=> (def x 2)
#'user/x
user=> (def tg (.getThreadGroup (Thread/currentThread)))
#'user/tg
user=> (def t (.getParent tg))
#'user/t
user=> (.getParent t)
nil
user=> ;; t is the top level thread group
user=> (def threads (make-array Thread 4096))
#'user/threads