Skip to content

Instantly share code, notes, and snippets.

@glts
glts / clojure
Last active July 23, 2022 23:40
Shell script for running stand-alone Clojure scripts
#!/usr/bin/env bash
# Runner for stand-alone Clojure scripts.
set -o errexit
set -o nounset
: "${JAVA_CMD:=java}"
: "${CLOJURE_VERSION:=1.8.0}"
: "${CLOJURE_JAR:=${HOME}/.m2/repository/org/clojure/clojure/${CLOJURE_VERSION}/clojure-${CLOJURE_VERSION}.jar}"
@glts
glts / skeleton
Created April 27, 2016 19:34
Indispensable Bash script skeleton: never forget
#!/usr/bin/env bash
# Shell script skeleton. Try each of these:
# ./skeleton
# ./skeleton one
# ENVIRONMENTAL=x ./skeleton one
# ENVIRONMENTAL=x ./skeleton one two
set -o errexit
set -o nounset
set -o pipefail
@glts
glts / README
Last active November 6, 2016 09:31
A basic Maven POM for reproducible builds
Useful commands for Maven POM maintenance.
- mvn dependency:analyze
- mvn dependency:tree
- mvn dependency:tree -Dverbose
- mvn versions:display-dependency-updates
- mvn versions:display-plugin-updates
@glts
glts / philosophers.clj
Created August 22, 2015 21:41
My Clojure solution for Dijkstra's 'dining philosophers' problem
;; Philosophers sitting around a table, one chopstick per person.
;; Dijkstra's "dining philosophers" problem.
(let [logger (agent nil)]
(defn log [& args]
(send-off logger #(apply println %2) args)))
(defn philosophers
"Returns a vector of philosophers with the given names, each having a
chopstick on their left and right. A chopstick contains a reference to the
@glts
glts / Dijkstra1965.java
Created August 22, 2015 09:27
Dijkstra's 1965 mutual exclusion algorithm in Java
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Mutual exclusion without locks, as first discovered by Dijkstra in 1965.
*
* <p>The code is so strange because the point of this class is to not use any
* of the synchronisation primitives nor any of the concurrency utilities in the
* library.
@glts
glts / jlexer.clj
Last active August 29, 2015 14:07
Trampolining lexer for J sentences
;; Trampolining lexer for J sentences
;; Predicates
(def digits (set "0123456789_"))
(def alphabet (set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
(def space? (partial contains? #{\space \tab}))
(def alpha-n? (partial contains? (disj alphabet \N)))
(def n? (partial = \N))
@glts
glts / vbench
Created February 8, 2014 18:15
Wrapper around vspec to support simple benchmarks
#!/usr/bin/env perl
# Usage: vbench [non_standard_runtimepaths ...] input_script [reference_output]
#
# 'vbench' is a wrapper around vspec that adds benchmarking capabilities.
#
# vbench runs a test script, just like vspec. When the test script follows the
# convention of outputting a time measurement alongside a successful test
# result, in the following format,
#
# ok 1 - Test case outputs time measurement
@glts
glts / numerals.vim
Last active January 1, 2016 03:29
Vim autoload library for base-k number systems without zero
" Tiny library for conversion of numbers of bijective base-k number systems
" (number systems without zero) to decimal integers and vice-versa. Examples:
"
" base 3: 1 2 3 11 12 13 21 22 23 31 32 33 111 112 113 ...
" base 5 (digits {a b c d e}): a b c d e aa ab ac ad ae ba ...
"
" The public interface is numerals#CreateConverter(digitstring) which creates
" a {converter} for the number system defined by the digitstring.
"
" {converter}.ValueOf(int) - converts an integer to a base-k number string
@glts
glts / schedule.clj
Created December 20, 2013 18:36
Tool for exploring a time-tracking system
;; Tool for exploring a time-tracking system
;;
;; Some time-tracking systems track time differently from what
;; an employee expects. Suppose you work 80%, Monday to Thursday. Some
;; time-tracking systems sum your hours up and distribute them over
;; the week: on average you'll work overtime Monday to Thursday, and
;; work too little on Friday. On average this is fair, but depending
;; on when the holidays are it can cost the employee a few hours.
(defn select
@glts
glts / vgrep.sh
Last active December 17, 2015 23:59
A grep with Vim regular expressions
#!/bin/bash
vgrep() {
local re=${1:-^}
local files=()
for arg in "${@:2}"; do
[ -f "${arg}" ] && files+=( "${arg}" )
done
if [ -z "${files[*]:1}" ]; then
vim -Ni NONE -nes <<<"argdo g/${re////\\/}/p" -- "${files[@]}"
else