Skip to content

Instantly share code, notes, and snippets.

@cls cls/sample-list.lisp
Last active May 25, 2018

Embed
What would you like to do?
Reservoir sampling over a list in Common Lisp
(defun sample-list (items &optional (size 1))
(let ((sample (make-array size :fill-pointer 0)))
(loop for item in items
for enum from 1
if (<= enum size)
do (vector-push item sample)
else
do (let ((rand (random enum)))
(when (< rand size)
(setf (aref sample rand) item)))
finally (return sample))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.