Skip to content

Instantly share code, notes, and snippets.

@djtango
djtango / write-date.clj
Created November 15, 2017 10:47
Output clj-time objects into readable clojure
(ns djtango.write-date
"Serialising/deserialsing mixed Clojure/JodaTime data.")
;; I made use of the advice at http://proofbyexample.com/print-and-read-in-clojure.html
;; in writing this file.
;; Make it possible to print joda DateTime instances in re-readable form, for persistence.
(defmethod print-dup org.joda.time.DateTime
[dt out]
(.write out (str "#=" `(org.joda.time.DateTime. ~(.getMillis dt) ~org.joda.time.DateTimeZone/UTC))))
@djtango
djtango / rebuild_views.sql
Created February 6, 2018 11:27
saving and rebuilding nested (materialized) views in postgres
create table saved_dependencies_ddl
(
deps_id serial primary key,
deps_view_schema varchar(255),
deps_view_name varchar(255),
dependency_name varchar(255),
deps_ddl_to_run text
);
create or replace function save_and_drop_dependencies(p_view_schema varchar, p_view_name varchar) returns void as
@djtango
djtango / callback-hell.js
Last active August 8, 2018 15:12
callback hell
const genLongString = (url) => {
// doesn't actually use the url
let chars = [];
const bigNum = Math.floor(Math.random() * 1000000 + 1000000);
for (let i = 0; i <= bigNum; i += 1) { chars.push('x'); }
const longString = chars.join('');
return longString;
};
const getLargeExternalData = (url, retn) => {
@djtango
djtango / prime.clj
Created December 1, 2018 20:07
A horrible abuse of spec and laziness
(ns prime
(:require [clojure.spec.alpha :as s]))
(s/def ::prime-number
(fn [x]
(cond (<= x 1) false
(= 2 x) true
:else
(let [naturals (drop 2 (map inc (range)))
@djtango
djtango / chess-gm.pgn.clj
Last active December 5, 2018 17:39
Specs that describe Chess Portable Chess Notation
(ns chess-gm.pgn
"See https://opensource.apple.com/source/Chess/Chess-110.0.6/Documentation/PGN-Standard.txt
and https://en.wikipedia.org/wiki/Portable_Game_Notation for more details on specification"
(:require [clojure.spec.alpha :as s]))
(s/def ::column-index (set "abcdefgh"))
(s/def ::row-index (set "12345678"))
(s/def ::piece-name (set "QKWBNRP"))
@djtango
djtango / chess-gm-test.clj
Created December 5, 2018 17:37
Tests for chess-gm.clj
(ns chess-gm.pgn-test
(:require [clojure.test :refer :all]
[orchestra.spec.test :as stest]
[clojure.spec.alpha :as s]
[chess-gm.pgn :as sut]))
(deftest basic-SAN
(testing "a simple vertical move should be valid"
(let [white-king-pawn-two-steps (seq "e4")]
(is (s/valid? ::sut/basic-move white-king-pawn-two-steps))))
@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
@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 (
(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 / 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))