Skip to content

Instantly share code, notes, and snippets.

@djtango
djtango / capture.clj
Created August 15, 2020 21:27
capture function inputs and arguments for REPL debugging
(ns capture
"This is useful if you are debugging a dependency, for example")
(defmacro capture [fname atm]
`(alter-var-root
(var ~fname)
(fn [f#]
(fn [& args#]
(let [result# (apply f# args#)]
(swap! ~atm conj {:args args#
@djtango
djtango / README.md
Created October 22, 2019 08:53
three-mens-morris

three-mens-morris

Kata: Three Men's Morris History

The game of Three Men's Morris, similar to Noughts and Crosses or Tic-Tac-Toe is known alternately as:

"Terni Lapilli" or "Three Stones" in Roman times
"Tapatan" in the Philippines

"Luk Tsut K'i" ('six man chess') in China

@djtango
djtango / core.clj
Created August 27, 2019 17:11
Clojure Socket Repl + clojure.test exception
(ns socket-test.core)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
@djtango
djtango / httpkit_ssl.clj
Created July 24, 2019 10:32
recipe for SSL configuration with httpkit to solve javax.net.ssl.SSLException
(ns httpkit-ssl
"recipe for SSL configuration with httpkit if you get javax.net.ssl.SSLException on Java 8
more info: https://github.com/http-kit/http-kit/issues/334"
(:require [org.httpkit.client :as http])
(:import (java.net URI)
(javax.net.ssl SSLEngine
SSLParameters
SNIHostName)))
(defn sni-configure
@djtango
djtango / heroku_to_jdbc.clj
Created July 19, 2019 12:19
Convert Heroku DB URL into a JDBC url
(ns heroku-to-jdbc
"untested and brittle parser - use at your own peril")
(defn colon?
[_ c]
(= c \:))
(defn label-result [result label]
(update result 0 (fn [x] [label x])))
@djtango
djtango / all_db_object_sizes.sql
Last active June 6, 2019 13:47
Postgres Disk Usage
-- adapted from https://wiki.postgresql.org/wiki/Disk_Usage
SELECT *
, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
@djtango
djtango / clean_slate.clj
Created April 3, 2019 09:03
Clean up all vars in namespace for Clojure
(ns user.repl)
(defn all-ns-fns
"Returns a map of the public intern mappings for the namespace."
[ns]
(let [ns (the-ns ns)]
(->> (ns-map ns)
(filter (comp (fn [^clojure.lang.Var v] (and (instance? clojure.lang.Var v)
(= ns (.ns v))))
val))
(ns fn-name
(:require [clojure.main]))
;; https://stackoverflow.com/questions/22116257/how-to-get-functions-name-as-string-in-clojure
(defn fn-name
[f]
(as-> (str f) $
(clojure.main/demunge $)
(or (re-find #"(.+)--\d+@" $)
@djtango
djtango / bloat.sql
Created March 25, 2019 12:26
SQL for psql bloat
/*
SOURCE: https://www.citusdata.com/blog/2017/10/20/monitoring-your-bloat-in-postgres/
I take no credit this was a straight copy paste
*/
WITH constants AS (
-- define some constants for sizes of things
-- for reference down the query and easy maintenance
SELECT current_setting('block_size')::numeric AS bs, 23 AS hdr, 8 AS ma
),
no_stats AS (
@djtango
djtango / fojc.sql
Created January 21, 2019 11:25
Full Outer Join Coalesce
-- ARBITRARY COLUMN SELECTION
create table a (x int, y int, z int);
create table b (x int, y int, z int);
create table c (x int, y int, z int);
insert into a values (1, 2, 3);
insert into b values (1, null, 30);
insert into c values (1, 200, null);
select * from a;
/*
x | y | z