Skip to content

Instantly share code, notes, and snippets.

View Hendekagon's full-sized avatar

Matthew Chadwick (work) Hendekagon

View GitHub Profile
dev:cljs.user=> (-> (a/coll-zip [3 {:x 5} 7 [4 {:y 17}] 9]) z/down z/right z/down z/down z/right (z/edit inc) z/next z/prev (z/edit inc) a/fip)
[3 ([:x 7]) 7 [4 {:y 17}] 9]
dev:cljs.user=> (-> (a/coll-zip [3 {:x 5} 7 [4 {:y 17}] 9]) z/down z/right z/down z/down z/right (z/edit inc) (z/edit inc) a/fip)
[3 {:x 7} 7 [4 {:y 17}] 9]
@Hendekagon
Hendekagon / gist:833c6d5605c8ccca50dd15cee996a370
Created May 24, 2017 07:46
Comparing performance of Clojure's protocols, records and maps for fn dispatch
(deftype Qp [] Q (q [this x] x))
=> user.Qp
(def Qm {:q (fn [x] x)})
=> #'user/Qm
(def Qp1 (Qp.))
=> #'user/Qp1
(c/quick-bench (q Qp1 7))
Evaluation count : 150426264 in 6 samples of 25071044 calls.
Execution time mean : 2.073561 ns
Execution time std-deviation : 0.274594 ns
@Hendekagon
Hendekagon / F.st
Last active May 1, 2017 17:01
bit pattern symmetry layout for Roassal
'From Pharo3.0 of 18 March 2013 [Latest update: #30846] on 1 May 2017 at 5:58:40.759228 pm'!
Object subclass: #F
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'fredkin'!
!F commentStamp: 'mjc 3/26/2015 10:19' prior: 0!
bits
!
@Hendekagon
Hendekagon / gta
Last active April 16, 2017 11:31
Cyclic tag system in Clojure
; t a list of productions, a an initial sequence
; note this halts with a NPE - it should really
; check for (empty? a) and reduced
(defn g [t a]
(reductions
(fn [[g & t] a]
(if (== 1 g) (concat t a) t))
a (cycle t)))
# https://query.wikidata.org/#PREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20ps%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0APREFIX%20pq%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20bd%3A%20%3Chttp%3A%2F%2Fwww.bigdata.com%2Frdf%23%3E%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fcountry2Label%20%3FlinkTo%0AWHERE%0A%7B%0A%09%3Fitem%20wdt%3AP47%20%3Fcountry2.%0A%20%20%09%3Fcountry2%20wdt%3AP31%20wd%3AQ6256.%0A%20%20%09%3Fitem%20wdt%3AP47%20%3FlinkTo.%0A%20%20%09SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D%0A%7D
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFI
(use 'criterium.core)
=> nil
(def v1 (vec (range 1000000)))
=> #'user/v1
(def m1 (into {} (map vector (range 1000000) (range 1000000))))
=> #'user/m1
(quick-bench (get m1 71))
Evaluation count : 9861456 in 6 samples of 1643576 calls.
Execution time mean : 60.482667 ns
Execution time std-deviation : 1.681717 ns
@Hendekagon
Hendekagon / gist:bcbdbd2edc841b4c326d7060683897ac
Last active August 6, 2016 09:55
Clojure arity vs map destructure cost
(require '[criterium.core :as crit])
=> nil
(defn Q ([x y z] [x y z]) ([{:keys [x y z]}] [x y z]))
=> #'repl/Q
(crit/quick-bench (Q 4 5 6))
Evaluation count : 43141050 in 6 samples of 7190175 calls.
Execution time mean : 12.030810 ns
Execution time std-deviation : 0.313706 ns
Execution time lower quantile : 11.631011 ns ( 2.5%)
Execution time upper quantile : 12.336856 ns (97.5%)
(defn lca
"Last common ancestor"
[db name1 name2]
(d/q '[
:find [(pull ?anc [:db/id :name]) ...]
:in $ % ?name1 ?name2
:where
(?node1 :name ?name1)
(?node2 :name ?name2)
(anc ?anc1 ?node1)
@Hendekagon
Hendekagon / cheese-hat
Last active May 23, 2016 17:06
Add a jaunty cheese-hat to any div
div::before {
width: 3em;
top: -2.6em;
height: 3em;
transform-origin: 50% 50%;
transform: rotate(-29deg);
position: relative;
z-index: 10000000;
margin-bottom: -3em;
content: url("data:image/svg+xml;utf8,<svg height='100%' viewBox='-8 -8 16 16' width='100%' xmlns='http://www.w3.org/2000/svg'><g><rect fill='white' height='5' stroke-width='0.25' stroke='black' transform='rotate(45) translate(8,8)' width='5' x='-10.5' y='-10.5'></rect><rect fill='white' height='5' stroke-width='0.25' stroke='black' width='15' x='-7.5' y='0'></rect><text font-family='monospace' font-size='2.5' x='-5' y='3.25'>CHEESE</text></g></svg>");
(defmacro explanatory-cond->
"Adapted from Clojure's cond-> macro to keep a list of conditions and results"
[expr & clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep
(fn [[test step]]
`(if ~test
[(-> (if (vector? (first ~g)) (ffirst ~g) (first ~g)) ~step)
(cons [(quote ~test) (quote ~step) (first ~g)] (second ~g))]