Skip to content

Instantly share code, notes, and snippets.

(defn add-buildings
([world]
(add-buildings world 0.5))
([world prob]
(let [maxy (dec (count world))
maxx (dec (count (world 0)))]
(loop [world world, cury maxy, curx maxx]
(if (and (zero? curx) (zero? cury))
world
(let [new-world (update-in world [cury curx]
(defn mkframe []
(doto (java.awt.Frame.)
(.setVisible true)
(.setSize (java.awt.Dimension. 512 512))
(-> .getGraphics (.drawString (.toString (java.util.Date.))
160 256))))
(def mono-font
(atom (Font. (if (is-mac)
"Monaco"
"Courier New")
Font/PLAIN default-font-size)))
(defn change-font [myfn]
(fn [myfont]
(let [new-font (swap! myfont
(fn [old-font]
(defn group-indices [coll]
(loop [[head & tail] coll
new-ids {}
next-id 0
ret []]
(let [found-id (get new-ids head)
[new-ids next-id] (if found-id
[new-ids next-id]
[(assoc new-ids head next-id) (inc next-id)])]
(defn p [path & {:keys [params headers]
:or {headers {}}}]
[params headers])
;; was hoping to see :headers {} here
(p "path" :params {:id 1})
(defmacro fold
"sugar on reduce, similar to how 'for' is a nicer version of 'map'.
init is a symbol that evaluates to a value (local or var) or a vector of a symbol and an initial value. In the loop, init is bound to the result of the previous loop.
Binding can take any arguments supported by for, but should only bind one variable.
"
;; (fold r [i (range 10)] ;; existing symbol
;; (+ r i))
;; amalloy's solution to Number Maze
;; https://4clojure.com/problem/106
(let [paths (fn [[curr :as path]]
(for [op [* / +]
:let [next (op curr 2)]
:when (integer? next)]
(cons next path)))
bfs (fn [choices]
(mapcat paths choices))]
(defn get-prime-factors [n-arg]
(loop [factors (), p 2, n n-arg]
(cond (= n 1) factors
(zero? (mod n p)) (recur (cons p factors), p, (/ n p))
:else (recur factors, (inc p), n))))
;; amalloy's solution to Game of Life
;; https://4clojure.com/problem/94
(fn [board]
(let [board (vec (map vec board))
[h w] (map count ((juxt identity first) board))
cell (fn [y x]
(get-in board (map mod [y x] [h w])))
alive? (comp #{\#} cell)
neighbors (let [offsets [-1 0 1]
@amalloy
amalloy / gist:1150681
Created August 17, 2011 02:28 — forked from mwmitchell/gist:1150671
same-ole-same-ole.clj
(let [x 10
m {:id 1}]
(-> m
(given (comp #{1} :id) assoc :id 2)
(given (< x 10) assoc :y true)))