Skip to content

Instantly share code, notes, and snippets.

@odyssomay
odyssomay / gist:953966
Created May 3, 2011 18:54
Recursive implementation of Shunting-yard algorithm
(defprotocol parser_p
(next-type [this token])
(next-destination [this token last_op])
(update-out [this dest token])
(update-op [this dest token])
(update [this])
(sort-tokens [this]))
(defrecord Parser [tokens out_stack op_stack operators]
parser_p
@odyssomay
odyssomay / gist:957028
Created May 5, 2011 13:34
Recreate hierarchy
;; This one is inadequate, kept for history, use the one in the second file.
(defn sort-hierarchy [coll]
(if (every? #(= (count %) 1) coll)
[:files (map first coll)]
(map (fn [x]
[(ffirst x) (sort-hierarchy (map rest x))]) (partition-by first coll))))
(defn hash-hierarchy [coll]
(if (= (first coll) :files)
@odyssomay
odyssomay / gist:958001
Created May 5, 2011 21:27
Comparable function type
(deftype CFn [f form]
clojure.lang.IFn
(invoke [this x1] ((.f this) x1))
(invoke [this x1 x2] ((.f this) x1 x2))
(invoke [this x1 x2 x3] ((.f this) x1 x2 x3))
(invoke [this x1 x2 x3 x4] ((.f this) x1 x2 x3 x4))
(invoke [this x1 x2 x3 x4 x5] ((.f this) x1 x2 x3 x4 x5))
(invoke [this x1 x2 x3 x4 x5 x6] ((.f this) x1 x2 x3 x4 x5 x6))
(invoke [this x1 x2 x3 x4 x5 x6 x7] ((.f this) x1 x2 x3 x4 x5 x6 x7))
; and so on....
@odyssomay
odyssomay / gist:972423
Created May 14, 2011 17:42
Subvector at value
(defn subvec-at-val [val v]
(let [limit (count v)]
(loop [i 0]
(if (== i limit)
[]
(if (= x (v i))
(subvec v i)
(recur (inc i)))))))
@odyssomay
odyssomay / Div.java
Created May 18, 2011 14:12
Clojure unchecked double division
public class Div {
public static double divide_double(double x, double y) {
return x / y;
}
}
@odyssomay
odyssomay / gist:999430
Created May 30, 2011 20:41
Find strings of length 3 which differ in one position
(defn diff-by-1 [strings]
(let [f (partition-by first (sort-by first strings))
s (partition-by second (sort-by second strings))
l (partition-by last (sort-by last strings))
fs (map #(partition-by second (sort-by second %)) f)
fl (map #(partition-by last (sort-by last %)) f)
sl (map #(partition-by last (sort-by last %)) s)
all (concat fs fl sl)]
(remove #(= (count %) 1) (apply concat all))))
@odyssomay
odyssomay / lein.sh
Created June 15, 2011 22:43
Persistent leiningen
#!/bin/sh
LEIN_VERSION="1.6.0-SNAPSHOT"
export LEIN_VERSION
case $LEIN_VERSION in
*SNAPSHOT) SNAPSHOT="YES" ;;
*) SNAPSHOT="NO" ;;
esac
@odyssomay
odyssomay / gist:1035590
Created June 20, 2011 13:19
Macro to create a callable record with any arity
(defmacro definvokerecord [invoke_fn & body]
`(defrecord
~@body
clojure.lang.IFn
~@(map (fn [n]
(let [args (for [i (range n)] (symbol (str "arg" i)))]
(if (empty? args)
`(~'invoke [this#]
(~invoke_fn this#))
`(~'invoke [this# ~@args]
@odyssomay
odyssomay / gist:1073506
Created July 9, 2011 10:47
java iterator
(defn extract-points [iterator]
(fn []
(let [points (make-array Double/TYPE 6)]
(.currentSegment iterator points)
(.next iterator))))
(defn extract-all-points [path_iterator]
(doall
(take-while (fn [_] (not (.isDone path_iterator)))
(repeatedly (extract-points path_iterator))))
@odyssomay
odyssomay / gist:1209498
Created September 11, 2011 12:07
trace forms
(ns trace
(:use clojure.pprint))
(declare trace-form)
(def *ignore*
'#{def quote var try monitor-enter monitor-exit})
(defmulti trace-special-form (fn [form] (first form)))