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 / hsv-to-rgb.rkt
Created May 6, 2022 04:31
Convert HSV to RGB
(define (hsv-to-rgb hue saturation value)
(let* ([C (* saturation value)]
[A (abs (- (modulo (/ hue 60) 2)
1))]
[X (* (- 1 A))]
[m (- value C)]
[rgb-prime
(cond
[(<= 0 hue 59) `(,C ,X 0)]
[(<= 60 hue 119) `(,X ,C 0)]
(defn ram [start length & {:keys [paged-in bank tag]
:or {paged-in false
bank 0}}]
;; I want to check here if :tag was added or not. But also i don't want to because style
(make-memory start length paged-in bank :text (vec (replicate length 0)) :writable true))
(defn- make-memory [start length paged-in bank & {:keys [text writable tag]}]
;; TODO: Maybe check that length and end - start are the same; fill with zeros if not.
{:start start
:end (+ start length)
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]:
Disassembly of section .text.x86_io__io_wait:
c0100798 <x86_io__io_wait>:
c0100798: e6 80 out %al,$0x80
c010079a: c3 ret
@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))
(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 / 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 "/")
@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 / 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 / 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))))