Skip to content

Instantly share code, notes, and snippets.

View KingMob's full-sized avatar

Matthew Davidson KingMob

View GitHub Profile
@KingMob
KingMob / gaussian-logpdf-improvement.clj
Created April 15, 2024 12:55
Incremental math optimization of a Clojure Gaussian logpdf fn
;; basic version
(set! *unchecked-math* false)
(defn gaussian-logpdf
"Returns log probability of `x` under a gaussian distribution parameterized
by shape parameter `mu`, with optional scale parameter `sigma`."
[x {:keys [mu sigma]}]
(let [z-inv (* -0.5 (+ (math/log sigma)
(math/log 2.0)
(math/log math/PI)))
px (* -0.5 (math/pow (/ (- x mu)
@KingMob
KingMob / primitive-math-comparison.clj
Last active April 15, 2024 12:07
Speed of primitive-math vs basic clj math
;; primitive-math is a low-level Clojure math library that
;; warns you if reflection is being used in your math pathway.
;; This demonstrates how it improves over the default Clojure
;; math fns, by preventing accidental reflection.
;; It's possible to get part of this behavior from core Clojure
;; by setting *unchecked-math* to :warn-on-boxed, but this only
;; applies to a fraction of the fns that primitive-math covers.
;; In particular, just +, -, *, inc, dec, and the casts.
;; primitive-math will also cover divison, bool operations, bit
@KingMob
KingMob / manifold-transducer-ex-handling.clj
Created November 8, 2023 07:56
Example of exception-handling in Manifold with transducers
(in-ns 'user)
(require '[manifold.stream :as s])
(defn flaky
[x]
(if (< (rand-int 10) 2)
(throw (Exception. "flaky"))
x))
(defn reliable-add-sentinel
@KingMob
KingMob / lein-to-deps-pre-commit.sh
Created June 25, 2023 10:56
git pre-commit hook that runs a script when project.clj file has been modified
#!/usr/bin/env bash
# Move this to .git/hooks/pre-commit
# echo "Running pre-commit hook..."
# Check if project.clj file has been modified
if git diff --cached --name-only | grep -q "^project.clj$"; then
echo "project.clj modified, running lein-to-deps"
@KingMob
KingMob / brew-outdated-leaves.sh
Created March 30, 2023 05:04
A script to show you outdated brew formulae, but only if they're leaves
#!/usr/bin/env bash
# A script to show you outdated brew formulae, but only if they're leaves.
# Who wants to scan through intermediate libs?
set -euo pipefail
# set -x # echo commands to terminal; for debugging
# with comm - slower for me, but may work better for you?
# FORMULAE=$(comm -12 <(brew outdated --quiet | sort) <(brew leaves))
@KingMob
KingMob / defmulti-enhanced.clj
Created April 22, 2022 09:12
defmulti+, an enhanced version of defmulti that unmaps itself from the ns when present. Eases the pain of redefining the dispatch method at the REPL.
(defmacro defmulti+
"An enhanced version of defmulti that unmaps itself from the ns when present.
Eases the pain of redefining the dispatch method at the REPL."
[name & args]
`(do
(when (resolve '~name)
(ns-unmap *ns* '~name))
(defmulti ~name ~@args)))
(require '[manifold.stream :as s])
(defn coerce-seq
"if the page should happen to be an Exception, wrap
it in a vector so that it can be concatenated to the
value stream"
[v]
(if (sequential? v) v [v]))
@KingMob
KingMob / submission_publisher.clj
Created November 13, 2020 23:39
Adaptation of java.util.concurrent.SubmissionPublisher to a Manifold sink
(utils/when-class java.util.concurrent.SubmissionPublisher
(import '(java.util.concurrent SubmissionPublisher))
(s/def-sink SubmissionPublisherSink
[^SubmissionPublisher publisher
^AtomicReference last-put]
(isSynchronous [_]
false)
@KingMob
KingMob / search-classpath.sh
Created September 28, 2018 18:26
Bash script to search a Java classpath for files with a regex
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Use like:
# LCP=$(lein classpath) # in case you need to rerun it and don't want to wait on Leiningen
# search-classpath.sh $LCP tools.reader
# search-classpath.sh $LCP Reader.class
# ...etc
@KingMob
KingMob / gist:3561511
Created September 1, 2012 00:06
Brew error configuring Leiningen 2.0.0-preview6 on OS X 10.7.4
King-Mob:~ $ HOMEBREW_MAKE_JOBS=1 VERBOSE=1 brew install leiningen2
==> Downloading https://github.com/technomancy/leiningen/zipball/master
Already downloaded: /Library/Caches/Homebrew/leiningen2-2.0.0-preview6.zip
/usr/bin/unzip /Library/Caches/Homebrew/leiningen2-2.0.0-preview6.zip
Archive: /Library/Caches/Homebrew/leiningen2-2.0.0-preview6.zip
db69ddedff3945cfdaec7a9a53d25e6236a0e78f
creating: technomancy-leiningen-db69dde/
inflating: technomancy-leiningen-db69dde/.gitattributes
inflating: technomancy-leiningen-db69dde/.gitignore
inflating: technomancy-leiningen-db69dde/.travis.yml