Skip to content

Instantly share code, notes, and snippets.

View Hendekagon's full-sized avatar

Matthew Chadwick (work) Hendekagon

View GitHub Profile
@Hendekagon
Hendekagon / deps.edn
Last active March 16, 2023 08:45
profile all functions in a namespace
{:paths ["."] :deps {robert/hooke {:mvn/version "1.3.0"}
com.taoensso/tufte {:mvn/version "2.4.5"}}}
@Hendekagon
Hendekagon / copy2clip
Created May 19, 2017 15:18
copy2clip
(defn copy2clip [x]
(let [w (StringWriter.)]
(pprint x w)
(.. Toolkit getDefaultToolkit getSystemClipboard
(setContents (StringSelection. (.toString w)) nil))))
@Hendekagon
Hendekagon / QR.clj
Created September 12, 2021 09:09
QR decomposition
(require '[clojure.core.matrix :as m])
(defn mup [mat rs cs f]
(m/set-selection mat rs cs
(f (m/select mat rs cs))))
(defn qr
"
QR decomposition by Householder reflection
@Hendekagon
Hendekagon / dispatch.clj
Created September 9, 2021 20:37
Comparison of function dispatch methods performance in Clojure
(require '[criterium.core :as c])
(defprotocol W (w [this x y]))
(def w1 (reify W (w [t x y] (* x y))))
(c/quick-bench (w w1 4 4))
; protocol dispatch
; Execution time mean : 6.625560 ns
@Hendekagon
Hendekagon / right?.clj
Created July 22, 2019 19:19
Clojure eval right?
(let [eval-right? clojure.core/eval] (intern 'clojure.core 'eval (fn [form] (if (= 'right? (last form)) (eval-right? (butlast form)) 'right?))))
@Hendekagon
Hendekagon / economy-dw.clj
Created August 16, 2020 11:45
economy-dw
; https://www.scientificamerican.com/article/is-inequality-inevitable/
; https://threadreaderapp.com/thread/1210332075787608065.html
(defn economy-dw
"
Simulate one round of
random transfers of wealth
between the given agents' accounts
(a vector of initial amounts)
@Hendekagon
Hendekagon / transducer-vs-unrolled.clj
Last active December 8, 2020 18:09
transducer-vs-unrolled
; I was curious as to whether unrolling
; this use of transducers would be faster
;
; in fact it's slower
; we're given an array which we
; process using fns f, g and h
(defn test1-xf [^bytes data]
(comp
@Hendekagon
Hendekagon / destructuring-precedence.cljc
Last active September 27, 2020 11:08
destructuring precedence
(let [{z :x x 'y y "z" :keys [x y z] :syms [x y z] :strs [x y z]} {:x 1 'y 2 "z" 3}] [x y z])
=> [nil nil 3]
(let [{z :x x 'y y "z" :keys [x y z] :syms [x y z]} {:x 1 'y 2 "z" 3}] [x y z])
=> [nil 2 nil]
(let [{z :x x 'y y "z" :keys [x y z]} {:x 1 'y 2 "z" 3}] [x y z])
=> [1 nil nil]
(let [{z :x x 'y y "z"} {:x 1 'y 2 "z" 3}] [x y z])
@Hendekagon
Hendekagon / arity-vs-map-args.clj
Last active August 15, 2020 10:02
Testing arity vs map function call speed in clojure
(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
@Hendekagon
Hendekagon / multimethod sets
Last active July 2, 2020 16:00
I needed to dispatch on sets with multimethods, here's how I did it
(ns mms
(:require
[clojure.math.combinatorics :as x]
[clojure.set :as set]))
(defn derive-set
"
A copy of derive for sets
"
([h a-set parent-set]