Skip to content

Instantly share code, notes, and snippets.

View emdeesee's full-sized avatar
🤔
hmm

Michael Cornelius emdeesee

🤔
hmm
View GitHub Profile
@emdeesee
emdeesee / slurp.lisp
Created February 5, 2016 15:27
Clojure slurp in Common Lisp
;; This is not a complete implementation of slurp, since it doesn't handle URLs, just paths.
(defun slurp (path)
(with-open-file (stream path)
(let ((data (make-string (file-length stream))))
(read-sequence data stream)
data)))
@emdeesee
emdeesee / codec.clj
Created October 3, 2012 14:52
Encode and decode "binary" strings with Clojure
(use '[clojure.string :only [trim replace]]
'[clojure.pprint :only [cl-format] :rename {cl-format format}])
(defn- unspace [s]
(trim (replace s " " "")))
(defn decode [bitstring]
"\"01100110 01101111 01101111\" -> \"foo\"
Whitespace in the input string is ignored."
@emdeesee
emdeesee / weighted-random.el
Created April 17, 2020 18:15
Weighted random selection in elisp
;; In this population, a should be selected with frequency of 10%, while
;; b and c are each chosen with frequency 45%.
(defvar weights '(10 45 45))
(defvar population '(a b c))
(defun cumulative-weights (weights)
"Return the total of `weights' and the cumulative distribution
table."
(loop for w in weights
sum w into total
@emdeesee
emdeesee / random-float.el
Created April 17, 2020 16:46
Generate a random value in [0.0, 1.0)
(defun random-0-to-1 (&optional range)
"Generate a random value in [0.0, 1.0)."
(let ((range (or range most-positive-fixnum)))
(/ (random range) (float range))))
@emdeesee
emdeesee / name-tools.lisp
Last active March 21, 2020 22:54
utilities for scraping/parsing
(ql:quickload :str)
(ql:quickload :alexandria)
(import 'alexandria:compose)
(ql:quickload :arrows)
(use-package :arrows)
(defun split-names (s)
(mapcar (compose #'str:downcase #'str:trim) (str:split #\, s)))
(let (names)
@emdeesee
emdeesee / qdrink.lisp
Created November 1, 2019 19:24
Splitting the universe to choose a drink
(ql:quickload :jsown)
(ql:quickload :dexador)
(ql:quickload :quri)
(if (evenp
(sxhash
(car (jsown:val
(jsown:parse
(dex:get
(quri:make-uri
@emdeesee
emdeesee / debounce.tcl
Last active April 12, 2019 20:52
Why is it so hard to get a debounced key sequence in TCL/Tk?
set pending_release {}
set pressed false
proc press_action {} { puts "pressed" }
proc release_action {} { puts "released" }
proc is_pressed {} {
global pressed
return $pressed
}
@emdeesee
emdeesee / chainmap.lisp
Last active January 5, 2019 00:15
Functionality like Python's ChainMap in Common Lisp
(defun make-chain-table (&rest tables)
(lambda (key)
(labels ((aux (tables)
(if (null tables)
(values nil nil)
(multiple-value-bind (value found) (gethash key (car tables))
(if found
(values value t)
(aux (cdr tables)))))))
(aux tables))))
@emdeesee
emdeesee / resolve.lisp
Last active January 3, 2019 22:55
Beginning of an action resolver for FUDGE
(defparameter *outcomes* '(terrible poor mediocre fair good great superb))
(defun build-outcome-alist ()
(loop for n from 0 for o in *outcomes* collect (cons n o)))
(defparameter *outcome-table* (build-outcome-alist))
(defparameter *superb-value* (car (rassoc 'superb *outcome-table*)))
(defparameter *terrible-value* (car (rassoc 'terrible *outcome-table*)))
@emdeesee
emdeesee / forecast.lisp
Created January 3, 2019 21:51
Get weather forecast from NWS
(ql:quickload :dexador)
(ql:quickload :jsown)
(ql:quickload :arrows)
(use-package :arrows)
(defparameter url "https://api.weather.gov/gridpoints/FWD/95,141/forecast")
(defun json-val-in (o &rest keys)
(reduce (lambda (o k) (jsown:val o k)) keys :initial-value o))