Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
zehnpaard / quicksort.clj
Last active May 7, 2016 11:27
Clojure Quick Sort with random pivot
(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)
@zehnpaard
zehnpaard / mergesort.clj
Last active May 8, 2016 11:06
Clojure Merge Sort using lazy-seq and partition-all
(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)
@zehnpaard
zehnpaard / markov-text.clj
Last active May 13, 2016 23:16
Random Text Generator Using Markov Chains
;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
@zehnpaard
zehnpaard / ant-sim1.clj
Last active May 21, 2016 22:47
Ant Sim 1 - Static World (Re-write of Rich Hickey's Ant Simulation as a Single-Threaded Application for my own studies)
(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])
@zehnpaard
zehnpaard / n-queen.clj
Last active May 24, 2016 12:52
N-Queen Problem Solver in Clojure
(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]
@zehnpaard
zehnpaard / game-of-life.clj
Last active June 27, 2016 09:30
Game of Life with Clojure and Quil
(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]
@zehnpaard
zehnpaard / treductions.clj
Last active September 20, 2016 10:10
Transducing Reductions?
(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)))
@zehnpaard
zehnpaard / css-reload.cljs
Created September 22, 2016 09:09
Function for dynamic reloading of css from clojurescript
;; 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)))
@zehnpaard
zehnpaard / core.clj
Created October 25, 2016 08:38
Minimal Ring App with Request/Response Log
(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")))
@zehnpaard
zehnpaard / ring-with-handlers.core.clj
Created October 27, 2016 14:12
Simple Ring Demo with Handlers using ring.util.response functions
(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")))