Skip to content

Instantly share code, notes, and snippets.

View Hendekagon's full-sized avatar

Matthew Chadwick (work) Hendekagon

View GitHub Profile
@Hendekagon
Hendekagon / docker.clj
Created July 23, 2024 15:30
Docker admin
(ns hendekagon.docker
"
Docker admin
adapted from
https://github.com/lispyclouds/contajners/blob/main/doc/002-general-guidelines.md
"
(:require
[clojure.string :as string]
[clojure.java.io :as io]
@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 / 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 / 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 / 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]
@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 / a.cljc
Last active November 20, 2019 18:53
Clojure namespace declaration by functions experiment
(fn [params]
{
:namespace-name 'a
:requires []
:defs
'{
x (fn [y] (str "a's " y))
}})
@Hendekagon
Hendekagon / structure.cljc
Created October 18, 2019 14:52
reverse destructuring
(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 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?))))