Skip to content

Instantly share code, notes, and snippets.

@mtnygard
mtnygard / multimethod_selector.clj
Created August 23, 2018 16:00
A clojure.spec.gen.alpha generator that picks a multimethod implementation from the known set.
(defn multimethod-selector
"Returns a generator that picks one dispatch value from the known
dispatch values of a multimethod. Defers the lookup of dispatch
values until sampling time, so any defmethods evaluated after the
generator is created may still be selected."
[s]
#(sgen/bind
(sgen/int)
(fn [i]
(let [ms (keys (methods s))]
@mtnygard
mtnygard / jenkins.json
Last active October 14, 2019 09:35
Packer.io template for a Jenkins server
{
"builders": [{
"type": "amazon-ebs",
"access_key": "",
"secret_key": "",
"region": "us-east-1",
"source_ami": "ami-de0d9eb7",
"instance_type": "m1.large",
"ssh_username": "ubuntu",
"ami_name": "packer-jenkins {{.CreateTime}}"
@mtnygard
mtnygard / responses.clj
Last active July 16, 2019 20:16
Build Pedestal response maps for every standard HTTP status code.
(defn response
([status body]
{:status status
:headers {}
:body body}))
(defmacro http-status [code sym]
`(def ~sym (partial response ~code)))
(defmacro http-statuses [& pairs]
@mtnygard
mtnygard / README.md
Last active May 9, 2018 04:38
Create a dependency matrix of Clojure namespaces.

The dependency matrix shows the strength of coupling at a var-by-var level between namespaces.

If you pull the CSV output into a spreadsheet, the numbers in the cells show how many var->var dependencies there are between the namespaces.

If you want to just look at namespace->namespace dependencies (e.g., to create a visualization) then just consider any cell > 0 as a dependency.

@mtnygard
mtnygard / cljsjs.clj
Created March 9, 2017 14:46
Access CLJSJS assets via a Pedestal Interceptor
(ns overt.cljsjs
(:require [clojure.java.io :as io]
[io.pedestal.interceptor :as i]
[io.pedestal.log :as log])
(:import java.net.URL
java.util.Date
java.util.jar.JarFile))
(def ^:private default-base "cljsjs")
(require '[clojure.string :as str])
(def icao
(->> (str/split
"Alfa, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, X-ray, Yankee, Zulu"
#", ")
(map #(hash-map (first %) %))
(apply merge)))
(defn radiocode [s]
@mtnygard
mtnygard / ConvertGraffle.scpt
Created December 7, 2017 21:13
Omnigraffle automation script
-- Convert a single document
-- on process_cli_item(source_file)
set source_file to "/Users/mtnygard/tmp/adjacency-diagram.graffle"
try
do shell script "open -a 'OmniGraffle' " & source_file
delay 2
tell application id "com.omnigroup.OmniGraffle7"
set area type of current export settings to entire document
@mtnygard
mtnygard / service.clj
Created November 23, 2016 13:17
Using the not-found-interceptor as a catchall
(ns support.service
(:require [io.pedestal.http :as http]
[io.pedestal.http.route :as route]))
(defn about-page
[request]
{:status 200 :body (format "Clojure %s - served from %s"
(clojure-version)
(route/url-for ::about-page))})
@mtnygard
mtnygard / gist:5615250
Created May 20, 2013 20:29
Query for all data types supported in Datomic.
user> (require '[datomic.api :as d])
nil
user> (def conn (d/connect (doto "datomic:mem://temp" d/create-database)))
#'user/conn
user> (d/q '[:find ?type ?doc :where
[_ :db.install/valueType ?t]
[?t :db/ident ?type]
[?t :db/doc ?doc]]
(d/db conn))
#{[:db.type/long "Fixed integer value type. Same semantics as a Java long: 64 bits wide, two's complement binary representation."]
@mtnygard
mtnygard / gist:7555463
Last active May 22, 2017 10:59
The limitations of test

From Out of the Tar Pit:

The key problem with testing is that a test (of any kind) that uses one particular set of inputs tells you nothing at all about the behaviour of the system or component when it is given a different set of inputs. The huge number of different possible inputs usually rules out the possibility of testing them all, hence the unavoidable concern with testing will always be — have you performed the right tests?. The only certain answer you will ever get to this question is an answer in the negative — when the system breaks.

This is a strong argument for some form of generative, simulation, or property-based testing.