Skip to content

Instantly share code, notes, and snippets.

@mtnygard
mtnygard / main.clj
Created April 3, 2018 14:13
Use macros to make nested try/catch with values less of an eyesore.
(defn -main
[& args]
(let [file (try-> args
parse-args
(:! clojure.lang.ExceptionInfo ei (fe/print-other-exception ei))
:filename)]
(when (and file (not= ::try/exit file))
(try-> file
fern/load-from-file
(:! java.io.FileNotFoundException fnfe (fe/print-error-message (str "File not found: " (pr-str (.getMessage fnfe)))))
@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 / deps.edn
Last active April 9, 2024 11:51 — forked from athos/deps.edn
Friendly REPL. Run with `clojure -Sdeps '{:deps {hello-clojure {:git/url "https://gist.github.com/mtnygard/9b2dd3c88b3309d82210b84f33ee954d" :sha "774314af2d28261af4a52ac270136d5ba21ff046"}}}' -m frenpl`
{:paths ["." "src" "test"]
:deps {expound {:mvn/version "0.8.4"}
clansi {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.24.0"}
refactor-nrepl {:mvn/version "2.5.0"}
com.bhauman/rebel-readline {:mvn/version "0.1.4"}}}
(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 / api.clj
Created May 18, 2017 13:06
Mixing Vase routes with hand-written routes in a single service
;; This is a Component that starts up a Vase API from a descriptor file.
(ns control-server.api
(:require [com.stuartsierra.component :as component]
[com.cognitect.vase :as vase]))
(defrecord VaseAPI [api-root specs routes]
component/Lifecycle
(start [this]
(vase/ensure-schema specs)
@mtnygard
mtnygard / kein.sh
Last active April 28, 2017 20:06 — forked from cgrand/kein.sh
Launch a plain clojure repl according to project.clj without leiningen (most of the time). This version supports CIDER and clj-refactor
#! /bin/bash
# launch a clojure plain repl but with options and classpath matching project.clj
# Except when project.clj changes (and on first launch), lein is not called.
CODE='
(let [p (leiningen.core.project/read)
args (@(var leiningen.core.eval/get-jvm-args) p)
cp (with-out-str (leiningen.classpath/classpath p))]
(print "ARGS=\"")
(apply print args)
@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")
@mtnygard
mtnygard / generator.clj
Created March 2, 2017 17:29
Simulating foreign exchange rates as Brownian motion processes
;; Caution: do not attempt to print this out. It contains many
;; lazy infinite sequences.
(def rate-table
(atom
(into {}
(map (fn [[ccypair rate]]
[ccypair (rand/brownian rate 0.0005)]))
rates/rate-table)))
(defn exchange-rate
@mtnygard
mtnygard / ptable.clj
Created February 12, 2017 04:14
Randomly generate pairs from a table of pairwise weights.
(require [clojure.test.check.generators :as gen])
(defn ptable
[label cols & rows]
(let [ncols (count cols)]
(assert (= 0 (mod (count rows) (inc ncols))))
(mapcat
(fn [[r & vs]]
(map (fn [c v] [v [c r]]) cols vs))
(partition (inc ncols) rows))))