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
(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] |
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
(defn mkframe [] | |
(doto (java.awt.Frame.) | |
(.setVisible true) | |
(.setSize (java.awt.Dimension. 512 512)) | |
(-> .getGraphics (.drawString (.toString (java.util.Date.)) | |
160 256)))) |
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
(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] |
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
(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)])] |
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
(defn p [path & {:keys [params headers] | |
:or {headers {}}}] | |
[params headers]) | |
;; was hoping to see :headers {} here | |
(p "path" :params {:id 1}) |
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 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)) |
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
;; 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))] |
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
(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)))) |
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
;; 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] |
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
(let [x 10 | |
m {:id 1}] | |
(-> m | |
(given (comp #{1} :id) assoc :id 2) | |
(given (< x 10) assoc :y true))) |