Skip to content

Instantly share code, notes, and snippets.

Matthew Chadwick Hendekagon

Block or report user

Report or block Hendekagon

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Hendekagon
Hendekagon / a.cljc
Last active Nov 20, 2019
Clojure namespace declaration by functions experiment
View a.cljc
(fn [params]
{
:namespace-name 'a
:requires []
:defs
'{
x (fn [y] (str "a's " y))
}})
@Hendekagon
Hendekagon / structure.cljc
Created Oct 18, 2019
reverse destructuring
View structure.cljc
(defn put [f x] (let [is-str? (char? (first x)) sx (apply str x)] (cond (map? f) (into {} (map vec (partition 2 (if (even? (count x)) x (conj x nil))))) (vector? f) (if is-str? (conj f sx) (into f x)) (list? f) (if is-str? (cons sx f) (concat f x)) (set? f) (if is-str? #{sx} (into f x)))))
(defn structure [s f] (mapv (fn [b a] (if (empty? b) (put b a) (let [r (structure a b)] (if (vector? b) (vec r) r)))) f (partition-all (int (/ (count s) (count f))) s)))
(structure (range 32) [[[] #{}] [#{} [] []]])
=>
[[[0 1 2 3 4 5 6 7] #{15 13 12 11 9 14 10 8}]
@Hendekagon
Hendekagon / right?.clj
Created Jul 22, 2019
Clojure eval right?
View right?.clj
(let [eval-right? clojure.core/eval] (intern 'clojure.core 'eval (fn [form] (if (= 'right? (last form)) (eval-right? (butlast form)) 'right?))))
@Hendekagon
Hendekagon / deps.edn
Last active Jun 12, 2019
profile all functions in a namespace
View deps.edn
{:paths ["."] :deps {robert/hooke {:mvn/version "1.3.0"}
com.taoensso/tufte {:mvn/version "2.0.1"}}}
@Hendekagon
Hendekagon / defns--.clj
Created May 14, 2019
Make functions public
View defns--.clj
(defn defns--
"Makes all private fns
in the namespace with the
given name public"
[namespace-name]
(doseq [[_ v]
(filter
(fn [[_ x]]
(if (var? x) (-> x meta :ns ns-name #{namespace-name}) false))
(ns-map namespace-name))]
@Hendekagon
Hendekagon / gist:4671b105774bf31f1240a2f0c25febb2
Last active Sep 24, 2018
A note on Clojurescript React interfaces
View gist:4671b105774bf31f1240a2f0c25febb2
@Hendekagon
Hendekagon / upsert
Last active Aug 10, 2018
Unique upsert in Datomic
View upsert
; create attributes p and q, where p is unique identity
(d/transact edb1 {:tx-data [{:db/ident :p :db/cardinality :db.cardinality/one :db/valueType :db.type/keyword :db/unique :db.unique/identity}]})
=>
(d/transact edb1 {:tx-data [{:db/ident :q :db/cardinality :db.cardinality/one :db/valueType :db.type/keyword}]})
=>
; now add a datum
(d/transact edb1 {:tx-data [{:p :a1 :q :x}]})
View change-unique.clj
(defn make-attr-test [conn]
(let [tx
[
{:db/ident :y :db/cardinality :db.cardinality/many :db/valueType :db.type/ref :db/isComponent true}
{:db/ident :ya :db/cardinality :db.cardinality/one :db/valueType :db.type/keyword}
{:db/ident :yb :db/cardinality :db.cardinality/one :db/valueType :db.type/string}
]]
(d/transact conn {:tx-data tx})))
(defn add-test-data [conn]
@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 / arity-vs-map.clj
Created Mar 4, 2018
Testing arity vs map function call speed in clojure
View arity-vs-map.clj
(defn add [x y] (+ x y))=> #'user/add(defn addm [{x :x y :y}] (+ x y))=> #'user/addm(reduce (fn [r i] (addm {:x r :y i})) 0 (range 128))=> 8128(quick-bench (reduce (fn [r i] (add r i)) 0 (range 128)))Evaluation count : 270720 in 6 samples of 45120 calls. Execution time mean : 2.212768 µs Execution time std-deviation : 76.515990 ns Execution time lower quantile : 2.130557 µs ( 2.5%) Execution time upper quantile : 2.336060 µs (97.5%) Overhead used : 1.902301 nsFound 1 outliers in 6 samples (16.6667 %) low-severe 1 (16.6667 %) Variance from outliers : 13.8889 % Variance is moderately inflated by outliers=> nil(quick-bench (reduce (fn [r i] (add r i)) 0 (range 128)))Evaluation count : 265158 in 6 samples of 44193 calls. Execution time mean : 2.323233 µs Execution time std-deviation : 176.530052 ns Execution time lower quantile : 2.223932 µs ( 2.5%) Execution time upper quantile : 2.626066 µs (97.5%) Overhead used : 1.902301 nsFound 1 outl
You can’t perform that action at this time.