Skip to content

Instantly share code, notes, and snippets.

@jjcomer
jjcomer / stateMachine.clj
Created July 30, 2011 11:32
Clojure State Machine
(ns stateMachine)
(defn parse-integer [str]
(try (Integer/parseInt str)
(catch NumberFormatException nfe 0)))
(defn displayMenu
"This function displays the menu and gets the user's input"
[]
(println "Displaying the menu")
@jjcomer
jjcomer / stateMachine.clj
Created July 31, 2011 00:43
Clojure State Machine with Application State
(ns StateMachine)
(defn initTodo []
"This function will return a hash representing the inital application state"
{:file "todo.txt"
:some-other-option true})
(defn parse-integer [str]
(try (Integer/parseInt str)
(catch NumberFormatException nfe 0)))
@jjcomer
jjcomer / HelperMethods.java
Created July 31, 2011 11:46
Java Unit Test Helpers
import java.lang.reflect.*;
public class HelperMethods {
@SuppressWarnings("unchecked")
public static <R, T> R invokePrivateMethod(T object, String methodName, Object... args) {
final Method methods[] = object.getClass().getDeclaredMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
try {
method.setAccessible(true);
@jjcomer
jjcomer / colour.clj
Created August 6, 2011 01:31
Colour for terminal output
(ns ^{:doc "Add some colour to your output"
:author "Josh Comer <joshjcomer@gmail.com>"}
cljcolour)
(def black {:fore "02;30" :back "02;40"})
(def red {:fore "02;31" :back "02;41"})
(def green {:fore "02;32" :back "02;42"})
(def yellow {:fore "02;33" :back "02;43"})
(def blue {:fore "02;34" :back "02;44"})
(def purple {:fore "02;35" :back "02;45"})
@jjcomer
jjcomer / trollscript.clj
Created September 11, 2011 20:25
Trollscript interpreter in Clojure
(ns trollscript.core
(:use [clojure.string :only [lower-case]])
(:gen-class))
(def MAX_CELLS 30000)
(defn split-code
"Splits the string into its trollscript commands"
[raw-code]
(->> raw-code
@jjcomer
jjcomer / ants.clj
Created December 18, 2011 18:22
Ant Simulation -- From Clojure Concurrency Presentation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ant sim ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
; which can be found in the file CPL.TXT at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
;As shown in the presentation: http://blip.tv/clojure/clojure-concurrency-819147
@jjcomer
jjcomer / median.clj
Created January 29, 2012 11:53
Median Finding Algorithms
(ns median.core
(:use [clojure.math.numeric-tower :only [floor ceil]]))
(defn rand-list
"Generates a random vector of length n with unique
values inclusive 1 to exclusive n"
[n]
(shuffle (map inc (range n))))
(defn- median-skeleton
@jjcomer
jjcomer / analysis.clj
Created January 29, 2012 12:02
Analysis of my Median Finding Algorithms
(ns median.analyze
(:use [median core]
[incanter core charts datasets stats]))
(defn test-lists
"Generate test lists applying f to p and n"
[n p f]
(map #(rand-list (f p %)) (range n)))
(defmacro time-it
@jjcomer
jjcomer / columns.clj
Created August 8, 2012 00:38
Ordering columns
(def c-order
(let [order [:id :title :author :date :price]]
(zipmap order (range (count order)))))
(defn column-sort
[k1 k2]
(compare (get c-order k1 -1)
(get c-order k2 -1)))
(def test-data
@jjcomer
jjcomer / json-middleware.clj
Created October 11, 2012 20:56
JSON Middleware
(defn- json-request?
[req]
(if-let [#^String type (:content-type req)]
(not (empty? (re-find #"^application/(vnd.+)?json" type)))))
(defn wrap-json-params [handler]
(fn [req]
(if-let [body (and (json-request? req) (:body req))]
(let [bstr (slurp body)
json-params (parse-string bstr)