Skip to content

Instantly share code, notes, and snippets.

View thomcc's full-sized avatar
🦀

Thom Chiovoloni thomcc

🦀
View GitHub Profile
(statements
(compile
'((define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
(factorial 1000))
'val
'return))
; =>
@thomcc
thomcc / quine.rb
Created March 13, 2012 14:44
quine in ruby
x = [
%q{puts "x = ["},
%q{x.each_with_index {|s,i| puts "%q{" << s << "}" << (i == x.length-1 ? "" : ",") }},
%q{puts "]"},
%q{puts "x.map {|s| eval s}"}
]
x.map {|s| eval s}
@thomcc
thomcc / gist:2200308
Created March 25, 2012 22:09
slow rendering code
(defn draw [{{[px py :as pos] :pos h :health s :score sees :sees, lv :level :as play} :player,
{:keys [points width height monsters seen]} :level, msgs :messages}]
(set! (.-font ctx) "12px monospace")
(doto ctx (set-fill "black") (.fillRect 0 0 canv-width canv-height))
(let [mpts (set (map :pos monsters))]
(loop [yy (dec height)]
(when-not (neg? yy)
(loop [xx (dec width)]
(when-not (neg? xx)
(let [p [xx yy],
@thomcc
thomcc / gist:2362787
Created April 11, 2012 21:30
small set of ad-hoc canvas functions i was using for a short while. don't use this, but its an example of drawing i guess?
;; macros.clj
(ns stuff.macros)
(defmacro with-path
[c & forms]
`(doto ~c
(.beginPath)
~@forms
(.closePath)))
;; core.cljs
(ns stuff.core
@thomcc
thomcc / gist:2410170
Created April 18, 2012 00:46
Typical closure compiler(?) stacktrace.
Compiling "pub/main.js" failed:
java.lang.OutOfMemoryError: Java heap space
JsDocTokenStream.java:239 com.google.javascript.jscomp.parsing.JsDocTokenStream.getStringFromBuffer
JsDocTokenStream.java:86 com.google.javascript.jscomp.parsing.JsDocTokenStream.getJsDocToken
JsDocInfoParser.java:2258 com.google.javascript.jscomp.parsing.JsDocInfoParser.next
JsDocInfoParser.java:928 com.google.javascript.jscomp.parsing.JsDocInfoParser.parse
IRFactory.java:352 com.google.javascript.jscomp.parsing.IRFactory.createJsDocInfoParser
IRFactory.java:260 com.google.javascript.jscomp.parsing.IRFactory.handleJsDoc
IRFactory.java:271 com.google.javascript.jscomp.parsing.IRFactory.transform
IRFactory.java:79 com.google.javascript.jscomp.parsing.IRFactory.access$300
@thomcc
thomcc / gist:2727844
Created May 18, 2012 22:10
syntax-case practice
(define-syntax (getters+setters stx)
(syntax-case stx ()
[(_) #'(begin)]
[(_ name . more)
(with-syntax ([(val . __) (generate-temporaries #'(name))])
#`(begin
(define/public (#,(format-id #'name #:source #'name "get-~a" (syntax-e #'name)))
name)
(define/public (#,(format-id #'name #:source #'name "set-~a" (syntax-e #'name)) val)
(set! name val))
@thomcc
thomcc / bfcompiler.rkt
Created June 4, 2012 21:10
Optimizing BrainFuck Compiler (targets C)
#lang racket
(define ptr "ptr")
(define tape "tape")
(define tape-size 64000)
(define (bf-compile-optimized bf [pp? #t])
(define level 0)
(define (indent lvl)
(for ([i lvl])
(display " ")))
@thomcc
thomcc / gist:2992763
Created June 26, 2012 02:10
tiny lisp in racket. most similar to clojure
#lang racket
(struct closure (args body env) #:constructor-name make-closure)
(struct env (parent data) #:constructor-name make-env)
(define-values (nothing nothing?)
(let ()
(struct <nothing> ())
(values (<nothing>) <nothing>?)))
#lang racket
(require racket/unsafe/ops racket/mpair)
(define sz 5000000)
(struct t (a b c d e f g h i j) #:transparent)
(struct mt (a b c d e f g h i j) #:mutable #:transparent)
(define lists (make-vector sz (list 1 2 3 4 5 6 7 8 9 10)))
(define mlists (make-vector sz (mlist 1 2 3 4 5 6 7 8 9 10)))
(struct vnode (edit arr) #:transparent)
(define (pvector-fresh-node edit)
(vnode edit (make-vector 32 #f)))
(define empty-node (pvector-fresh-node #f))
(define empty-pvector (pvector 0 5 empty-node (vector)))
(struct pvector (cnt shift root tail))