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 quick-sort | |
"Sort sequence using simple recursion based on splitting by randomly chosen pivot" | |
[xs] | |
(if (empty? (rest xs)) | |
xs | |
(let [pivot-index | |
(rand-int (count xs)) | |
[heads [pivot & tails]] | |
(split-at pivot-index xs) |
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 merge-sorted | |
"Merge two sorted sequences into one sorted sequence | |
If called with one sequence as argument, return the sequence unchanged" | |
([xs] xs) | |
([[head1 & tail1 :as xs1] [head2 & tail2 :as xs2]] | |
(cond | |
(some empty? [xs1 xs2]) | |
(concat xs1 xs2) | |
(< head1 head2) |
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
;Utility Functions | |
(defn map-vals | |
"Transform a map by applying f to each value" | |
[f hmap] | |
(zipmap (keys hmap) | |
(map f (vals hmap)))) | |
(defn reductions-vals | |
"Transform a map by running (reductions f ...) onto the values |
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 ant-sim | |
(:require [quil.core :as q] | |
[quil.middleware :as m])) | |
;Core data records for simulation | |
(defrecord Cell [food pheromone ant home]) | |
(defrecord Ant [position direction food]) | |
(defrecord World [board ants time]) |
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
(declare valid-position?) | |
(defn n-queen [n] | |
(letfn [(f [positions i] | |
(filter valid-position? | |
(for [position positions | |
piece (map #(vector i %) (range n))] | |
(conj position piece))))] | |
(reduce f '(()) (range n)))) | |
(defn valid-position? [position] |
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 game-of-life | |
(:require [quil.core :as q] [quil.middleware :as m])) | |
;Core logic | |
(defn next-round | |
"Take as input a set of vectors representing coords of currently living cells | |
and return a set of vectors representing coords of living cells in the next stage" | |
[live-cells] | |
(letfn [(surrounding [[cell-x cell-y]] | |
(for [x [-1 0 1] |
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 treductions [f] | |
(fn [xf] | |
(let [last-val (volatile! ::none)] | |
(fn | |
([] (xf)) | |
([result] (xf result)) | |
([result input] | |
(if (= ::none @last-val) | |
(vreset! last-val input) | |
(vreset! last-val (f @last-val input))) |
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
;; Intended use: hot-loading changes to garden-generated css via figwheel | |
(defn load-css [css-text] | |
(let [current-style (js/document.getElementById "mystyle")] | |
(if current-style | |
(.removeChild (.-parentNode current-style) current-style))) | |
(let [new-style (js/document.createElement "style")] | |
(set! (.-id new-style) "mystyle") | |
(.appendChild new-style (js/document.createTextNode css-text)) | |
(.appendChild (.-head js/document) new-style))) |
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 ring-min-log.core | |
(require | |
[ring.util.response :as res] | |
[ring.adapter.jetty :refer [run-jetty]] | |
)) | |
(defn handler [req] | |
(-> (res/response "Hello Goodbye") | |
(res/content-type "text/plain"))) |
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 ring-with-handlers.core | |
(require | |
[ring.util.response :as res] | |
[ring.adapter.jetty :refer [run-jetty]] | |
)) | |
(defn plain-text [req] | |
(-> (res/response "Hello Goodbye") | |
(res/content-type "text/plain"))) |
OlderNewer