Skip to content

Instantly share code, notes, and snippets.

@noisesmith
noisesmith / repl session
Created October 7, 2016 21:41
parallel binding expansion
=> ,(time (apply (fn [a b c] (+ a b c)) (map deref [(future (Thread/sleep 1000) 1) (future (Thread/sleep 1000) 2) (future (Thread/sleep 1000) 3)])))
"Elapsed time: 1000.790535 msecs"
6
@noisesmith
noisesmith / shell.html
Last active September 27, 2016 19:19
starting a cljs function via script tag in a shell.html
<html>
...
<body>
<script src="cljs-js/app.js"></script>
<script>
Stripe.setPublishableKey('{{stripekey}}');
window.kingfisher_environment = {{{environment}}};
kingfisher.core.start();
</script>
</body>
@noisesmith
noisesmith / gist:bf55a69b223a6762df4f30cf4a675daf
Created September 13, 2016 23:54
default method on LazyDelay protocol
kingfisher.core=> (defprotocol LazyDelay (default [this x]))
LazyDelay
kingfisher.core=> (extend-protocol LazyDelay clojure.lang.Delay (default [this x] (if (realized? this) @this x)))
nil
kingfisher.core=> (def d (delay 42))
#'kingfisher.core/d
kingfisher.core=> (default d 12)
12
kingfisher.core=> @d
42
@noisesmith
noisesmith / repl session
Created September 10, 2016 18:41
using add-watch to track past states of an atom
Clojure 1.8.0
+user=> (def a (atom nil))
#'user/a
+user=> (def s (atom []))
#'user/s
+user=> (add-watch a :state-watcher (fn [_ _ _ n] (swap! s conj n)))
#object[clojure.lang.Atom 0x928763c {:status :ready, :val nil}]
+user=> (swap! a conj :initial)
(:initial)
+user=> @s
@noisesmith
noisesmith / repl session
Created August 10, 2016 17:49
methods of the same name from different protocols work, if the arg counts differ
user=> (ns foo.bar)
nil
foo.bar=> (defprotocol Baz (quux [this]))
Baz
foo.bar=> (ns foo.baz)
nil
foo.baz=> (defprotocol Bar (quux [this]))
Bar
foo.baz=> (in-ns 'user)
#object[clojure.lang.Namespace 0x2847f343 "user"]
peregrine.circle=> (binding [*warn-on-reflection* true *unchecked-math* :warn-on-boxed] (eval '(fn [] (rand-int))))
#object[peregrine.circle$eval33277$fn__33278 0x1da5063e "peregrine.circle$eval33277$fn__33278@1da5063e"]
peregrine.circle=> (def a 1)
#'peregrine.circle/a
peregrine.circle=> (binding [*warn-on-reflection* true *unchecked-math* :warn-on-boxed] (eval '(fn [] (rand-int))))
#object[peregrine.circle$eval33283$fn__33284 0x3e49d47f "peregrine.circle$eval33283$fn__33284@3e49d47f"]
peregrine.circle=> (binding [*warn-on-reflection* true *unchecked-math* :warn-on-boxed] (eval '(fn [] (+ 1 (rand-int)))))
Boxed math warning, /private/var/folders/_m/9py48m892rq93bgbc91br8mr0000gr/T/form-init366749289092648620.clj:1:84 - call: public static java.lang.Number clojure.lang.Numbers.unchecked_add(long,java.lang.Object).
#object[peregrine.circle$eval33289$fn__33290 0x32cc6003 "peregrine.circle$eval33289$fn__33290@32cc6003"]
@noisesmith
noisesmith / range_transducer.clj
Last active July 30, 2016 18:30
range transducer perf test
(ns org.noisesmith.range-transducer)
(defn trange
([transform]
(trange transform 0 nil 1 (constantly nil)))
([transform final]
(trange transform 0 final 1 >=))
([transform init final]
(trange transform init final 1 >=))
([transform init final step]
(defn anagrams?
[& args]
(let [alpha (map char (range (int \a) (inc (int \z))))
metrics (map (comp #(select-keys % alpha)
frequencies
clojure.string/lower-case)
args)]
(or (apply = metrics)
(let [result-keys (reduce into #{} (map keys metrics))
empty-result (zipmap result-keys (repeat 0))
@noisesmith
noisesmith / debug-file.clj
Created July 21, 2016 18:33
create an edn file to load in a unit test
(defn debug-file
([file-name data]
(debug-file file-name data {}))
([file-name data replacements]
(assert (or (string? file-name)
(symbol? file-name))
"file name?")
(assert (map? replacements)
"map of data type replacements please")
(let [out-to (str file-name '.edn)
@noisesmith
noisesmith / example 1
Created July 13, 2016 13:35
test the same code in many clojure versions
$ ./test-clojure-versions '(+ 1 1)'
clojure 1.8.0
2
clojure 1.7.0
2
clojure 1.6.0
2
clojure 1.5.1
2
clojure 1.3.0