Skip to content

Instantly share code, notes, and snippets.

Tim McCormack timmc

Block or report user

Report or block timmc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:cbf503895f08dc39f3bc471aaa5e068a
Have the following been addressed in the branch, if appropriate?
- Tests (unit, API, integration)
- Docs (both in source and in docs directory, and in public docs if separate)
- Changelog
- Compatibility with previous versions (calls, shared files or DBs, data formats -- backward and forward compatibility)
- Rollback friendly?
- Feature switches?

Port-forwarding JMX

Proof of concept:

  • Terminal 1:
    • SSH to remote host
    • Start a Java process with JMX registry port 50004, RMI callback port 50005, and RMI hostname pinned to localhost: java -Djava.rmi.server.hostname=localhost -cp /some/jar/file main.class
  • Terminal 2:
timmc / weighted-shuffle-sampling.clj
Created Mar 25, 2019
A sampling-based version of weighted-shuffle (better to use the exponential random solution)
View weighted-shuffle-sampling.clj
;; This is asymptotically slower (n^2) than the exponential random sort
;; one (n log n) shown in
;; but it is preserved here for possible later interest
(defn weighted-random-sample
"Given a coll of weights, pick one according to a weighted-random
selection, and return its index. Weights must be non-negative."
(when (empty? weights)
(throw (IllegalArgumentException. "Cannot sample from empty list")))
View weighted-shuffle-fancy.clj
(defn weighted-shuffle
"Perform a weighted shuffle on a collection. weight-fn is called at
most once for every element in the collection."
[weight-fn coll]
(->> coll
(shuffle) ;; tie-break any zero weights
(map (fn [el]
;; Bound the weight to positive values
(let [weight (Math/max Double/MIN_VALUE (double (weight-fn el)))
;; Weighted Random Sampling (2005; Efraimidis, Spirakis)
timmc /
Last active Mar 19, 2019
Success-prioritized, concurrency-limited stochastic fallback cascade


  • State
    • For each node, store:
      • Rolling window of 6 historical stat buckets
      • One additional "sticky" bucket
    • Write stats to newest bucket
    • Age out the oldest bucket every 5 seconds, and add a new one
      • If the oldest bucket had data, copy it over the "sticky" bucket
    • Recorded stats: Number of finished requests, number that
# Create and sign a JWT token with ES256 given the path to an ECDSA
# private key and a JSON payload.
# $0 path/to/keypair.der '{"JSON": "payload"}'
# Example keypair creation:
# openssl ecparam -name prime256v1 -genkey -noout -outform DER > example-keypair.der
# A few tips for generating the payload:
# - Pipe raw strings through `jq --raw-input .` to encode them as
while read line; do
url_encoded="${line//+/ }"
printf '%b\n' "${url_encoded//%/\\x}"
timmc /
Created Jan 22, 2019
Custom query term splitter, as state machine (demonstration only; a regex with possessive quantifiers slightly out-performs this)
public static List<String> splitTermsStateMachine(String query) {
// Horizontal or vertical whitespace
Pattern isWhitespace = Pattern.compile("[\\h\\v]");
//== Manually managed state bits ==//
// If non-null, we're in a term, and this contains the term so far
StringBuilder currentTerm = null;
// True iff inside a quoted run
boolean inQuotes = false;
View MapEntry.kt
data class MapEntry<K: Any?, V: Any?>(
override val key: K,
override val value: V
): Map.Entry<K, V>
timmc / layout.kt
Created Nov 18, 2018
Wanted: Fluent interface for specifying filesystem layout
View layout.kt
class Repo : Layout() {
val config by leaf("config.json")
val myKeyring by leaf("self.keyring")
val posts by dir("posts", ::PostsDir)
class PostsDir : Layout() {
val post by multiple().dir(name = """[0-9]+""".toRegex(), ::OnePost)
You can’t perform that action at this time.