Skip to content

Instantly share code, notes, and snippets.

@mkremins
mkremins / core_defs.cljs
Created November 27, 2014 22:21
Inspect cljs.core defs in CLJS
(def replacements
[["_PLUS_" \+] ["_STAR_" \*] ["_SLASH_" \/] ["_QMARK_" \?] ["_BANG_" \!]
["_LT_" \<] ["_GT_" \>] ["_EQ_" \=] ["_DOTDOT_" ".."] ["_" \-]])
(defn unmunge [s]
(reduce (fn [s [before after]]
(clojure.string/replace s before after))
s replacements))
(defn alphabetical? [c]
@mkremins
mkremins / parse_arglist.clj
Created September 4, 2015 03:14
Parse arglists for fn-like forms
(def valid-name?
"Returns truthy if argument is a non-namespaced symbol."
(every-pred symbol? (complement namespace)))
(defn parse-arglist
"Given an `arglist` (a vector of parameter names for a function method),
returns a map of information about the method. May throw an exception if
`arglist` cannot be parsed."
[arglist]
(assert (every? valid-name? arglist)
@mkremins
mkremins / let_log.clj
Created September 9, 2015 16:27
Like clojure.core/let, but logs the value of every bound symbol
(defn indent [n s]
(let [spaces (clojure.string/join (repeat n " "))]
(->> (clojure.string/split-lines s)
(map (partial str spaces))
(clojure.string/join "\n"))))
(defn bound-syms [pat]
(condp #(%1 %2) pat
(some-fn map? vector?) (distinct (mapcat bound-syms pat))
symbol? [pat]
@mkremins
mkremins / complete.elm
Created September 29, 2015 17:19
Text completions with match highlighting
import Debug
import List
import String
type alias Match = {full: String, before: String, match: String, after: String}
score : Match -> Int
score {before} = String.length before
match : String -> String -> Maybe Match
@mkremins
mkremins / smearfle.cljc
Created June 21, 2019 19:28
Smearing shuffle
(defn smearfle
"\"Smearing shuffle\": shuffle a list while semi-preserving its original order.
Slide a window of size `window-size` (default 3) across the list from left to right
in increments of `step-size` (default 1), shuffling the contents of the window on each step.
The end result: some items are carried a random distance left or right of their
original starting point, but disruption of the list's order is largely local,
so it's unlikely that any given item is (for instance) carried all the way
from the beginning to the end of the list.
You can use this to add some variation to the order in which a sorted list is traversed,
@mkremins
mkremins / fairmath.js
Created June 25, 2026 21:19
Fairmath: softly saturating arithmetic for storygames
// Generalized implementation of ChoiceScript's "fairmath":
// a variant of additive arithmetic that bounds the outputs to a specific range
// and reduces the impact of additions proportional to the starting value's closeness
// to the edge of the range you're moving toward. ChoiceScript's fairmath implementation
// uses 0-100 as the hardcoded range for values; this implementation allows you to
// specify your own range, but will default to 0-100 if none is provided.
//
// Example usage:
// `fairmath({to: 10, add: 20, min: 0, max: 100}) // => 28`
//