Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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))))