Skip to content

Instantly share code, notes, and snippets.

@Hendekagon
Hendekagon / collatz.clj
Created Feb 8, 2018
Comparison of checked-vs-unchecked-vs-if-vs-ifless
View collatz.clj
(defn collatz [i] (if (even? i) (/ i 2) (+ 1 (* i 3))))(defn unchecked-collatz [i] (if (even? i) (unchecked-divide-int i 2) (unchecked-add-int 1 (unchecked-multiply-int i 3))))(defn ifless-collatz ([i] (ifless-collatz i (rem i 2))) ([i p] (+ (* p (+ 1 (* 3 i))) (* (- 1 p) (/ i 2)))))(defn unchecked-ifless-collatz ([i] (unchecked-ifless-collatz i (unchecked-remainder-int i 2))) ([i p] (unchecked-add-int (unchecked-multiply-int p (unchecked-add-int 1 (unchecked-multiply-int 3 i))) (unchecked-multiply-int (unchecked-subtract-int 1 p) (unchecked-divide-int i 2)))))
(quick-bench (ifless-collatz 7))Evaluation count : 2353194 in 6 samples of 392199 calls. Execution time mean : 257.547841 ns Execution time std-deviation : 8.540479 ns Execution time lower quantile : 249.726040 ns ( 2.5%) Execution time upper quantile : 269.887290 ns (97.5%) Overhead used : 1.806094 ns=> nil(quick-bench (unchecked-ifless-collatz 7))Evaluation count :
@Hendekagon
Hendekagon / gist:4671b105774bf31f1240a2f0c25febb2
Last active Sep 24, 2018
A note on Clojurescript React interfaces
View gist:4671b105774bf31f1240a2f0c25febb2
@Hendekagon
Hendekagon / 206
Created Sep 28, 2017
add 206 partial content in Aleph
View 206
; ---- the following fns copied from ring.util.response because they were
; useful but private ----
(defn canonical-path [^File file]
(str (.getCanonicalPath file)
(if (.isDirectory file) File/separatorChar)))
(defn safe-path? [^String root ^String path]
(.startsWith (canonical-path (File. root path))
(canonical-path (File. root))))
View gist:d3877ae82f9418d687c7ecfec09fe881
(use 'criterium.core)
=> nil
(defprotocol p1 (pt [this]))
=> p1
(extend-type Number p1 (pt [this] (list this)))
=> nil
(extend-type clojure.lang.PersistentList p1 (pt [this] this))
=> nil
(defn pf [x] (if (list? x) x (list x)))
=> #'user/pf
View gist:ce7b654a92485847cbcce5884cf55b28
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 / programmer_jargon
Created Jun 9, 2017
A list of programmer jargon
View programmer_jargon
modulo - 'except' as in "modulo means an equivalence relation modulo its use by programmers"
orthogonal - 'unrelated' as in "the use of 'orthogonal' by programmers is orthogonal to its use by mathematicians"
land, landed - when a new feature is added to a library, it isn't mearly 'added', it 'lands' -- as if from the heavens, like a magical bird or an expensive jet
cut - when a new version of library is released it isn't mearly 'released' it is 'cut' -- like a sparkling diamond
curated - if you release a list that wasn't created by an algorithm, then say it's 'curated' as in "A curated list of programmer jargon"
@Hendekagon
Hendekagon / gist:833c6d5605c8ccca50dd15cee996a370
Created May 24, 2017
Comparing performance of Clojure's protocols, records and maps for fn dispatch
View gist:833c6d5605c8ccca50dd15cee996a370
(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
View copy2clip
(defn copy2clip [x]
(let [w (StringWriter.)]
(pprint x w)
(.. Toolkit getDefaultToolkit getSystemClipboard
(setContents (StringSelection. (.toString w)) nil))))
@Hendekagon
Hendekagon / F.st
Last active May 1, 2017
bit pattern symmetry layout for Roassal
View F.st
'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 Apr 16, 2017
Cyclic tag system in Clojure
View gta
; 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)))
You can’t perform that action at this time.