Skip to content

Instantly share code, notes, and snippets.

View xiongtx's full-sized avatar

Tianxiang Xiong xiongtx

  • San Francisco, CA
View GitHub Profile
@xiongtx
xiongtx / min_window.clj
Created January 4, 2025 01:56
Minimum window containing all terms
(defn valid-window?
[terms m]
(every? #(>= (get m % 0) 1) terms))
(defn full-position
[coll idx]
(first (nth coll idx)))
(defn min-window
"Given a set of terms and sequence of tokens, find the minimum window (in
@xiongtx
xiongtx / ring.erl
Last active February 2, 2018 06:50
Erlang ring benchmark
-module(ring).
-export([start/2]).
%% Write a ring benchmark. Create N processes in a ring. Send a message round
%% the ring M times so that a total of N * M messages get sent. Time how long
%% this takes for different values of N and M.
start(M, N) ->
[P|_] = create_ring(N),
T1 = erlang:monotonic_time(microsecond),
@xiongtx
xiongtx / ini-sort.el
Last active May 12, 2025 18:33
Sort INI file
(require 'cl-lib)
(require 'subr-x)
(defun *-ini-sort ()
"Sort an INI buffer by sections and properties.
Combines repeated sections. E.g.
[b]
b1=1
b3=3
@xiongtx
xiongtx / carpenter.el
Last active September 7, 2017 03:34
Elisp snippet to tell you to become a carpenter if any Clojure test fails too many times in a row
(require 'cl-lib)
(require 'cider-test)
(defcustom carpenter-threshold 3
"Number of times a CIDER test can fail consecutively before
you're told to become a carpenter."
:type 'integer)
(defvar carpenter-fail-counts (make-hash-table :test #'equal))
@xiongtx
xiongtx / docker-tools.sh
Created August 30, 2017 17:58
Docker-related stuff
function rm-old-images() {
IMAGESINFO=$(sudo docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}} {{.CreatedSince}}' |grep -E " (weeks|months|years)")
TAGS=$(echo "$IMAGESINFO" | awk '{ print $2 ":" $3 }' )
IDS=$(echo "$IMAGESINFO" | awk '{ print $1 }' )
for t in $TAGS; do sudo docker rmi $t; done
for i in $IDS; do sudo docker rmi $i; done
}
@xiongtx
xiongtx / with-redefs-laziness.clj
Created April 7, 2017 21:49
`with-redefs` problems with laziness
(defn two-deep [_]
"two-deep")
(defn other-two-deep [_]
"other-two-deep")
(defn one-deep [& args]
(map two-deep args))
(defn foo []
@xiongtx
xiongtx / markdown-table-align.el
Last active October 12, 2016 06:56
Align markdown table
(defun *-markdown-table-align (beg end)
(interactive (list (org-table-begin) (org-table-end)))
(save-excursion
(align-regexp beg end "\\(\\)|" 1 0 t)
(goto-char beg)
(re-search-forward "|---" end t)
(subst-char-in-region (line-beginning-position)
(line-end-position)
?\s ?- t)))
;; Debugger == interactive restarter
;; If we try to open a non-existent file, we are put into the debugger
(with-open-file (in "nonexistent-file.txt")
(format t "~A~%" (read-line in nil)))
;; The debugger offer a restart that retries the evaluation
(with-open-file (str "nonexistent-file.txt"
:direction :output)
(format str "This file now exists."))