Skip to content

Instantly share code, notes, and snippets.

View lgaff's full-sized avatar
💭
Perpetual Procrastination

Lindsay Gaff lgaff

💭
Perpetual Procrastination
  • Sydney, Australia
View GitHub Profile
@lgaff
lgaff / starstuff.lisp
Created March 20, 2012 01:53
Common lisp recursion vs. Ruby
;;; The idea here is to be able to determine velocity and distance
;;; attained as a function of time.
;; some physical constants/definitions.
(defconstant +g+ 9.81)
(defconstant +C+ 2.998e8)
;; Helper to determine if we've hit C (we'll use 99.9% as the cutoff)
(defun velocity-cap (velocity accel)
(if (<= (+ velocity accel) (* +C+ 0.999))
@lgaff
lgaff / loop expansion.lisp
Created April 30, 2012 03:47
An argument for DSL's
;; The following two functions are equivalent:
(loop repeat 10 for x = 0 then y and y = 1 then (+ x y) collect y) ; ===> (1 1 2 3 5 8 13 21 34 55)
(BLOCK NIL
(LET ((#:LOOP-REPEAT-876 (CEILING 10)) (X NIL) (Y NIL))
(DECLARE (TYPE INTEGER #:LOOP-REPEAT-876))
(SB-LOOP::WITH-LOOP-LIST-COLLECTION-HEAD (#:LOOP-LIST-HEAD-877
#:LOOP-LIST-TAIL-878)
(SB-LOOP::LOOP-BODY NIL
@lgaff
lgaff / gist:3167329
Created July 24, 2012 01:21
subdirectory creation using a double-dereference
(defmacro create-subdir (path subdir)
`(ensure-directories-exist
(merge-pathnames (make-pathname :directory `(:relative ,,subdir) :name "ignored") ,path))))
@lgaff
lgaff / gist:3897208
Created October 16, 2012 04:16
Lexical analysis of strings for the Cool programming language
<INITIAL>{QUOTE} { BEGIN STRCONST; string_buf_ptr = string_buf; }
<STRCONST>{QUOTE} { BEGIN 0;
cool_yylval.symbol = stringtable.add_string(string_buf);
reset_buffer();
return STR_CONST;
}
<STRCONST>[^\\\"\n\0]+ { add_to_buffer(yytext); }
<STRCONST>\\\n { if(!add_to_buffer("\n")) { BEGIN 0; return ERROR; } }
<STRCONST>\\n { if(!add_to_buffer("\n")) { BEGIN 0; return ERROR; } }
<STRCONST>\\\\ { if(!add_to_buffer("\\")) { BEGIN 0; return ERROR; } }
@lgaff
lgaff / boot.asm
Created November 30, 2012 03:02
FAT12 stage 1 boot loader
; incboot.s
; Incrementally build a boot block for x86
; I'm going to keep adding bits to this one step at a time from
; boot-and hang to hopefully loading a kernel
; Here we go.
; We're implementing a FAT12 file system for the boot disk. This file system is well
; documented so it shouldn't be a hard ask for a first-timer like me.
; There is a certain order required for the metadata appearing at the top of the
; boot block below the jump to start, we'll introduce each as we go.
@lgaff
lgaff / handler.clj
Last active December 12, 2015 06:18
Routing with compojure
(defroutes app-routes
(GET "/" [] (render-page :status-page))
(GET "/pages/:page" [page] (render-page (keyword page)))
(context "/create" []
(POST "/metric" {params :params} (new-metric params))
(POST "/server" {params :params} (new-server params)))
(context "/response" []
(GET "/up/:id" [id] (views/up-file id))
(GET "/down/:id" [id] (views/down-file id)))
(route/resources "/")
(defn bayes [a b-given-a b-given-c]
; p(a|b ) = p(b | a) * p(b) / p(a)
(let [c (- 1.0 a)] ; Complementary event of a
(/ (* b-given-a a)
(+ (* b-given-a a) (* b-given-c c)))))
(def plots (take 20 (iterate #(+ % 0.005) 0.005)))
(println "Accuracy of a drug test that is 99% sensitive, 99% specific")
(println "(eg. 99% true positive for users, 99% true negative for non-users)")
@lgaff
lgaff / core.clj
Created July 1, 2013 11:47
Calling libc from Java.
(ns jna-test.core
(:import (com.sun.jna "Native")))
(gen-interface
:name jna.CLibrary
:extends [com.sun.jna.Library]
:methods [[printf [String] Integer]])
(def glibc (Native/loadLibrary "c" jna.CLibrary))
Disassembly of section .text.x86_io__io_wait:
c0100798 <x86_io__io_wait>:
c0100798: e6 80 out %al,$0x80
c010079a: c3 ret
def bogosort(source):
"""Sort by shuffling the input until it's in order. relies on probability allowing that (assuming a healthy shuffling algorithm),
eventually the list will appear in order"""
def in_order(source, iter):
sorted = True
i = 0
print("(%d) in_order[%d]: %d < %d: %s :: %d <= %d: %s" %\
(iter, i, i, len(source), i<len(source), source[i], source[i+1], source[i] <= source[i+1]))
while i < len(source)-1 and source[i] <= source[i+1]: