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 / 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 / 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))
@emdeesee
emdeesee / fb.lisp
Created September 25, 2018 17:47
Inside out FizzBuzz in Common Lisp
(flet ((cycle (list) (setf (cdr (last list)) list)))
(let ((fcyc (cycle (list "" "" "Fizz")))
(bcyc (cycle (list "" "" "" "" "Buzz"))))
(mapcar (lambda (f b n &aux (fb (concatenate 'string f b)))
(if (= 0 (length fb)) n fb))
fcyc bcyc (loop for n from 1 to 100 collect n))))
@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 / while.lisp
Last active June 21, 2018 19:44
Return the sequence created by calling fn on each element of seq, until the result is NIL
(defun while (fn seq)
"Evaluates to the sequence that results from calling fn on each element of seq, until the result is NIL"
(labels ((aux (seq acc)
(if (null seq) acc
(let ((val (funcall fn (first seq))))
(if val
(aux (rest seq) (cons val acc))
acc)))))
(aux seq nil)))
@emdeesee
emdeesee / mutate-integer-at-point.el
Last active November 30, 2017 22:09
Mutate integer at point
(require 'thingatpt)
(defun beginning-of-integer-at-point ()
(let ((inhibit-changing-match-data t))
(skip-chars-backward "[[:digit:]]")
(unless (looking-at "[[:digit:]]")
(error "No integer here"))
(when (looking-back "[+-]")
(backward-char 1))))