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 / 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))))
@emdeesee
emdeesee / updatef.lisp
Created October 25, 2017 14:49
Universal read/modify/write macro for Common Lisp. Apply a function to the value at a place, storing the result in the place.
(define-modify-macro updatef (f &rest args)
(lambda (r f &rest args) (apply f (cons r args)))
"'Update' the value at a place by applying function F to the old value and any supplied arguments.")
@emdeesee
emdeesee / ltffu.lisp
Last active October 25, 2017 14:06
A less-terrible, non-recursive approach to finding the first unique character in a string
(defun find-first-unique (s)
"return the first (leftmost) unique character in string s, or nil if
none found"
(let ((freqs (make-hash-table)))
(loop for c across s do (incf (gethash c freqs 0)))
(find-if (lambda (x) (= 1 (gethash x freqs))) s)))
@emdeesee
emdeesee / ffu.lisp
Last active October 23, 2017 23:08
Return the first (leftmost) unrepeated character in string s or nil if none found
(defun find-first-unique (s)
"return the first (leftmost) unrepeated character in string s or nil if none found"
(labels ((aux (s uniq rep)
(if (null s) (car (last uniq))
(destructuring-bind (f &rest r) s
(cond
((member f rep)
(aux r uniq rep))
((member f uniq)
(aux r (remove f uniq) (cons f rep)))
@emdeesee
emdeesee / pbev.lisp
Last active August 23, 2017 14:53
Computing the expected value of a Powerball ticket
(defvar ticket-price 2)
(defvar fixed-payouts (list 1000000 50000 100 100 7 7 4 4 0))
(defun 1-in (x)
(/ 1 x))
(defvar probabilities
(map 'list #'1-in
(list 282201388.0
11688053.52
@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 / codec.lisp
Last active April 25, 2017 21:12
Encode and decode "binary" strings with Common Lisp
(require :split-sequence)
(use-package :split-sequence)
(defun bits->text (s)
"\"01100110 01101111 01101111\" -> \"foo\""
(coerce
(mapcar (lambda (x) (code-char (parse-integer x :radix 2)))
(split-sequence #\space s))
'string))
@emdeesee
emdeesee / hashy.lisp
Created March 31, 2017 14:11
Before I learned of the alexandria library, I rolled my own hash table deconstruction. Preserved here because of my inordinate pride.
(defun hash-table-unzip (table)
(with-hash-table-iterator (iter table)
(labels ((aux (keys values)
(multiple-value-bind (more? k v) (iter)
(if more?
(aux (cons k keys) (cons v values))
(values keys values)))))
(aux nil nil))))
(defun hash-table-keys (table)
@emdeesee
emdeesee / atti.lisp
Last active March 17, 2017 19:18
Acing the Technical Interview. Inspired by https://aphyr.com/posts/340-acing-the-technical-interview, an exercise in Common Lisp
(defun pros (head tail)
(lambda (m) (funcall m head tail)))
(defun kar (l)
(funcall l (lambda (head _) (declare (ignore _)) head)))
(defun kdr (l)
(funcall l (lambda (_ tail) (declare (ignore _)) tail)))
(defun mth (l m)