This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The | |
# XML Reader is a good way to move a cursor through a (large) XML document fast, | |
# but is not as cumbersome as writing a full SAX document handler. Read about | |
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html | |
# | |
# Just pass the reader in this parser and specificy the nodes that you are interested | |
# in in a block. You can just parse every node or only look inside certain nodes. | |
# | |
# A small example: | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; lazy streams | |
(defmacro delay (expression) | |
`(lambda () ,expression)) | |
;;; ` (backquote) means construct a list just like ' (quote) | |
;;; but it allows you to evaluate parts inside it with , (comma) | |
;;; also, the result of a macro is evaluated, | |
;;; so `(lambda () ,3) is evaluated as if it were just (lambda () 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define fib | |
(lambda (n) | |
(((lambda (f) | |
((lambda (x) (x x)) | |
(lambda (y) (f (lambda (a b) | |
((y y) a b)))))) | |
(lambda (f) | |
(lambda (s n) | |
(if (= n 0) | |
((lambda (c) (c (lambda (a b) a))) s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define registers (vector 0 0 0 0 0 0 0 0)) | |
(define memory (make-vector 65536)) | |
(define pc 0) | |
(define (load-program instructions) | |
(define (recur cnt rem) | |
(if (null? rem) | |
#t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro alet (letargs &rest body) | |
`(let ((this) ,@letargs) | |
(setq this ,@(last body)) | |
,@(butlast body) | |
(lambda (&rest params) | |
(apply this params)))) | |
(defmacro alet-fsm (&rest states) | |
`(macrolet ((state (s) | |
`(progn (setq this #',s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro -> (init &rest funcs) | |
(cond | |
((null funcs) `,init) | |
((eql 'if (car funcs)) | |
(let* ((gensym (gensym))) | |
`(-> (let ((,gensym ,init)) | |
(if ,gensym (-> ,gensym ,(cadr funcs)) | |
(-> ,gensym ,(caddr funcs))) | |
,@(cdddr funcs))))) | |
((and (consp (car funcs)) (not (eql 'lambda (caar funcs)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create stack and other structures | |
stack_t* draw_stack = (stack_t*)malloc(sizeof(stack_t)); | |
stack_init(draw_stack, (wa.width*wa.height)); | |
stack_element_t random_start; | |
// push random position onto stack | |
random_start.x = rand()%wa.width; | |
random_start.y = rand()%wa.height; | |
stack_push(draw_stack, random_start); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; recur is like clojure's loop/recur | |
;;; | |
;;; declare a tail-recursive loop with | |
;;; (recur ( (var-name (&optional type) init-val) ... more vars)) | |
;;; | |
;;; recur back to the beginning with | |
;;; (tail-recur (var-name new-value) ... more vars) | |
;;; (exit &optional val) | |
;;; leaves the recursion returning either nil, or the given value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Class | |
def attr_accessor_with_default(attr_name, val) | |
attr_name = attr_name.to_s | |
self.class_eval("def #{attr_name}; @#{attr_name} = #{val};end") | |
self.class_eval("@#{attr_name} = #{val}") | |
self.class_eval("def #{attr_name}=(val) @#{attr_name} = val end") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro flip-flop (left right) | |
"Evaluates to false until the left test is satisfied. Then evaluates to true until the right test is satisfied, etc." | |
;; create a collision-safe name for our state variable | |
(let ((state-var (gensym))) | |
;; create a toplevel variable to keep state of flip-flop | |
`(progn | |
(defvar ,state-var nil)) |