Skip to content

Instantly share code, notes, and snippets.

@tggreene
tggreene / Backtick_Test
Last active December 31, 2015 09:39 — forked from JoelBesada/README.md
Script
@tggreene
tggreene / partial-nth.clj
Created October 2, 2018 13:19
partial-nth #clojure
(defn partial-nth
"Takes a function f and position pos with one fewer than required args and
returns a fn that takes one argument which will be applied at pos (where pos
is 0 indexed ordinal position)"
[f pos & args]
(fn [x]
(let [[before after] (split-at pos args)]
(apply f (concat before [x] after)))))
@tggreene
tggreene / randw-nth.clj
Created October 2, 2018 13:21
randw-nth #clojure
(defn randw-nth
"As rand-nth but takes weight-fn applied to col to weight selection e.g.
(randw-nth [{:type :a :weight 3}
{:type :b :weight 1}]
:weight)
; => {:type :a :weight 3} is chosen 3/4 times"
[col weight-fn]
(let [weights (reductions + (map weight-fn col))
choices (map vector col weights)
@tggreene
tggreene / Authors.md
Last active October 19, 2018 14:00
Clojure Library Authors
@tggreene
tggreene / basic-sounds.json
Last active December 8, 2018 17:59
Airhorn #1
{"name": "Test Board!",
"sounds": {"drip": "//tQZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWGluZwAAAA8AAAAfAAAmFAAFBQUSEhIdHR0oKCgyMjIyPDw8R0dHUVFRXV1dXWdnZ3FxcXp6eoODg4yMjIyVlZWenp6mpqaurq6utra2vr6+xcXFzMzM19fX193d3eTk5Onp6ezs7Oz39/f5+fn5+fn5+fkAAAAsTEFNRTMuODIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/7EGQAD/AAAGkHAAAAAAANIOAAAAAAAaQUAAAAAAA0goAAAPGYC9fgM4NgjwucE4eKTCyP8TmBgDbT/GgFzAsgiH/i5w+MUuJ3LH/5oSZFDRAj//8cYsgnDQg5fL///4zBORzDSxmb//uQZCIAA3yGRIZCAABucQiAyMAADXTtOljFgAGwpWbXFmAA///+iTZuhkXJ83L5f/////K5FC4aLN3dM3NDM3NP/////////MC4aF83c38AsBkTwxgGW/EpjJ+IAEf+RMnhWn+MgT5Nlf/yqOeVz5Mf/mhfJs3lD//IgOA64yBDyJ///h8goAcAzZFyfUQAiH///m6zc0MCcIOXzdMz/////NycRTdBAvv5FCc/////////zQn1Msvm5cabpgoAAQgltQkH4X3MIiyWTHm9UWhxWS0CQiUforaOkuZqST1DhKHcPzV7PxNRHTmuXYfhnuf6zfo9Nsi2u2bDj84anikilhnsq+732/39LnUkIhpsrLhEy6cKFDqT2DJRZ0fb/DAUVvYpkF5WkAAVkCgbbSVBnRuJc1+YPYyXOfM2tWt3ucvMiHyEYfxGdzpjHet+fx4bH9w+s7W+PZ+YpGwQaFTSH7ayDRn+1hpxPXaA6naoxvnh8xv/8efbZ/r9TE4ofUX/LCNxoyGEmlOoUkIO//q//ygXQgAAmH+DGdFgfElv//uQZAW
@tggreene
tggreene / hugsql-create-table
Last active November 3, 2019 19:25
yas-snippets for hugsql
# -*- mode: snippet -*-
# name: hugsql-create-table
# key: create-table
# --
-- :name create-$1-table
-- :command :execute
-- :return :raw
create table if not exists $1 (
$2
);
@tggreene
tggreene / shadow-cljs-refactor-nrepl.md
Last active April 2, 2020 09:13
Setting up refactor-nrepl with shadow-cljs

shadow-cljs plays nicely with cider-nrepl courtesy of this little fella, however, not so much love for cider-nrepl's little brother refactor-nrepl.

Unfortunately there's no project agnostic way of loading nrepl middleware with the nrepl launched by shadow-cljs using user config alone (as stated here), but you can ask nrepl to configure middleware for itself with ~/.nrepl/nrepl.edn.

I have a working setup with the following (with shadow-cljs > 0.2.39 and nrepl 0.6.0):

@tggreene
tggreene / .tmux.conf
Created April 3, 2020 11:36
Tmux for perverts
# Bind C-a to leader key
unbind C-a
set -g prefix C-a
bind C-a send-prefix
#set -g prefix C-Space
#bind C-Space send-prefix
# 0 is too far from ` ;)
set -g base-index 1
@tggreene
tggreene / thread_by_name.clj
Created May 4, 2020 11:16
Get a thread by name in clojure
(defn get-thread-by-name
[thread-name]
(let [threads (keys (Thread/getAllStackTraces))]
(->> threads
(filter #(= thread-name (.getName %)))
(some identity))))
;; You can then use the value to stop the thread:
(.stop (get-thread-by-name "server-loop"))
@tggreene
tggreene / urandom.clj
Created May 4, 2020 18:57
Get random bytes in clojure
(defn urandom
[n]
(with-open [in (io/input-stream (io/file "/dev/urandom"))]
(let [buf (byte-array n)
_ (.read in buf)]
buf)))