Skip to content

Instantly share code, notes, and snippets.

View cls's full-sized avatar

Connor Lane Smith cls

View GitHub Profile
@cls
cls / derivative.sml
Created May 12, 2018 16:02
Derivative of a function in Standard ML, using Real.nextAfter
fun derivative (f : real -> real) (x : real) : real =
let val pos = Real.nextAfter (x, Real.posInf)
val neg = Real.nextAfter (x, Real.negInf)
in
(f pos - f neg) / (pos - neg)
end
@cls
cls / sample-list.lisp
Last active May 25, 2018 14:52
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)))
@cls
cls / codons.c
Last active May 9, 2020 15:02
Counting DNA codons. Mostly an exploration of multidimensional array pointers. (No input checking.)
#include <stdio.h>
#define CHAR_TO_BASE(C) (((C) >> 1) & 3)
enum base {
A = CHAR_TO_BASE('A'),
C = CHAR_TO_BASE('C'),
G = CHAR_TO_BASE('G'),
T = CHAR_TO_BASE('T'),
};