This file contains 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
(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) |
This file contains 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
(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)) |
This file contains 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
(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 [] |
This file contains 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
(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) |
This file contains 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
(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?") |
This file contains 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
(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)))) |
This file contains 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
(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)))) |
This file contains 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
(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) |
This file contains 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 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)))) |
This file contains 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 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 |