Skip to content

Instantly share code, notes, and snippets.

View fffej's full-sized avatar

Jeff Foster fffej

View GitHub Profile
(defn isolate
"Isolate the lone x in e on the left-hand side of e"
[e x]
(dbg :student (format "e=%s x=%s" e x))
(cond
;; X = A ==> X = n
(= (:lhs e) x) e
;; A = f(X) ==> f(X) = A
(in-exp x (:rhs e)) (isolate (mk-exp (:rhs e) '= (:lhs e)) x)
(defn separation
"Avoid crowding neighbours (short-range repulsion)"
[boid boids]
(reduce
(fn [[x y] b]
[(- x (- (:x b) (:x boid)))
(- y (- (:y b) (:y boid)))])
[0 0]
(filter
(fn [b] (< (distance [(:x boid) (:y boid)] [(:x b) (:y b)]) separation-distance))
(defn animate
[_]
(when @running
(send-off *agent* animate)
(doseq [agent-boid boids]
(send-off agent-boid behave (remove (partial = @agent-boid) (map deref boids))))
(.repaint canvas)
(Thread/sleep animation-delay-ms)))
(defn start-animate []
(defstruct transform :coefficients :prob)
(defn mk-transform
[[a b c d e f] prob]
(struct transform [a b c d e f] prob))
(defn calculate-point
"Calculate the next point to render based on the previous"
[transform [x y]]
(let [r (rand)
(ns uk.co.fatvat.ifs
(:import [javax.swing JFrame JPanel])
(:import [java.awt Color Polygon])
(:import [java.awt.image BufferedImage])
(:import [javax.swing.event MouseInputAdapter])
(:use clojure.contrib.def))
(defvar width 512 "Width of the rendering plane")
(defvar height 512 "Height of the rendering plane")
(defvar running (atom false) "Are we going?")
(defn vector-map-slow
"A version of map that only works on vectors"
[f v]
(loop [i 0 out [] n (count v)]
(if (<= n i)
out
(recur (inc i) (conj out (f (v i))) n))))
(defn vector-map-transient
"A version of map that only works on vectors"
[f v]
(loop [i 0 out (transient []) n (count v)]
(if (<= n i)
(persistent! out)
(recur (inc i) (conj! out (f (v i))) n))))
(mac accum (accfn . body)
(w/uniq gacc
`(withs (,gacc nil ,accfn [push _ ,gacc])
,@body
(rev ,gacc))))
(def keys (h)
(accum a (each (k v) h (a k))))
(def vals (h)
(defmacro accum
[f body]
`(with-local-vars [acc# (transient [])
~f (fn [x#](conj! (var-get acc#) x#))]
~body
(persistent! (var-get acc#))))
(defn my-keys [m] (accum a (doseq [[k v] m] (a k))))
(defn my-vals [m] (accum a (doseq [[k v] m] (a k))))
(defmacro rec-seq
[binding-name & body]
`(let [s# (atom nil)]
(reset! s# (lazy-seq (let [~binding-name @s#] ~@body)))))
(defmacro rec-cat
[binding-name & exprs]
`(rec-seq ~binding-name (lazy-cat ~@exprs)))
;; Fibonacci