Skip to content

Instantly share code, notes, and snippets.

View joelittlejohn's full-sized avatar

Joe Littlejohn joelittlejohn

View GitHub Profile
@joelittlejohn
joelittlejohn / find-unused-clj.sh
Last active November 6, 2020 16:56
Very quick and dirty command to find unused functions and vars in a Clojure project
#!/bin/bash
for f in $(egrep -o -R "defn?-? [^ ]*" * --include '*.clj' | cut -d \ -f 2 | sort | uniq); do
echo $f $(grep -R --include '*.clj' -- "$f" * | wc -l);
done | grep " 1$"
@joelittlejohn
joelittlejohn / brisfunctional-zipper.clj
Created February 26, 2013 21:58
BrisFunctional zipper
(ns brisfunctional-zipper.core)
(def sample
[:+ [:* 5 6] [:* 7 4] [:+ 1 2]])
(defn loc [l r c p]
{:left l
:right r
:cur c
:path p})
@joelittlejohn
joelittlejohn / brisfunctional-sokoban.clj
Last active December 15, 2015 10:49
BrisFunctional sokoban
(ns sokoban.core)
(comment #{:n :s :e :w})
(def start-world
{:person [3 1]
:targets #{[1 3]}
:crates #{[1 2]}
:blanks #{[1 3] [2 3] [3 3]
[1 2] [3 2]
@joelittlejohn
joelittlejohn / trie.clj
Last active May 25, 2020 14:59
Trie for auto-complete (or: how to implement auto-complete in 4 lines of Clojure)
(def t
"trie containing the 100,000 most common english words"
(with-open [r (clojure.java.io/reader "/tmp/words-100000")]
(reduce #(assoc-in %1 %2 (sorted-map \0 nil)) (sorted-map) (line-seq r))))
(defn search [p m]
"return a sorted sequence of all words in the trie m that start with the given prefix p"
(let [n (get-in m p)
next (mapcat #(search (str p (key %)) m) (dissoc n \0))]
(if (contains? n \0) (cons p next) next)))
@joelittlejohn
joelittlejohn / TtlHashMap.java
Last active October 4, 2023 10:50
A passive TTL hash map, that expires and removes values if they are older than some time-to-live. I have only proved it correct, not tried it ;) [As the author of this code, I dedicate it to the public domain https://creativecommons.org/publicdomain/zero/1.0]
package com.example;
import static java.util.Collections.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@joelittlejohn
joelittlejohn / generate-contributors.sh
Created May 22, 2013 22:08
Instant CONTRIBUTORS.md from your git log
echo "# Contributors" > CONTRIBUTORS.md && git log --pretty=tformat:"* %an <%ae>" | tac | awk ' !x[$0]++' >> CONTRIBUTORS.md
@joelittlejohn
joelittlejohn / bots.clj
Last active December 19, 2015 00:19 — forked from cgrand/bots.clj
Add joebot
;; joebot
(ns tron.bots
(:require [tron.core :as tron]))
(defn empty-look
"A mock look function which just checks for the arena
boundaries."
[pos]
(when-not (tron/valid-pos? pos) :wall))
@joelittlejohn
joelittlejohn / generate-changelog.sh
Last active March 4, 2021 18:50
Instant CHANGELOG.md from your GitHub issues
#!/usr/bin/env bash
# Generates this kind of thing:
# https://github.com/joelittlejohn/jsonschema2pojo/blob/master/CHANGELOG.md
#
# If your issue has 'breaking' label, the issue will be shown in the changelog with bold text
#
# All versions of this script are dedicated to the Public Domain, no rights reserved,
# as per https://creativecommons.org/publicdomain/zero/1.0/
#
if [ "$#" -ne 2 ]; then
@joelittlejohn
joelittlejohn / merge_ascii.clj
Created July 31, 2013 19:02
Merge two ascii (art) files like image layers, treating spaces as transparent.
(ns merge-ascii
(:require [clojure.java.io :refer [reader writer]]))
(defn choose-char [a b]
(cond (nil? a) b
(nil? b) a
(not= b \space) b
:else a))
(defn merge [a b out]
@joelittlejohn
joelittlejohn / base62-id.clj
Last active December 26, 2015 21:39
Create base62 ids with 64-bit entropy in Clojure
(let [digits (into [] (map char (concat (range 48 58) (range 65 91) (range 97 123)))) ;; 0-9,A-Z,a-z
base (biginteger (count digits))
entropy 64]
(defn id []
(loop [id10 (BigInteger. entropy (java.security.SecureRandom.))
id62 ""]
(if (and (<= id10 (BigInteger/ZERO)) (seq id62))
id62
(let [[d r] (.divideAndRemainder id10 base)]
(recur d (str id62 (digits (.intValue r)))))))))