Skip to content

Instantly share code, notes, and snippets.

An h1 header

Some further changes yet.

Paragraphs are separated by a blank line.

2nd paragraph. Italic, bold, and monospace. Itemized lists look like:

(defn map- [f coll]
(lazy-seq (cons (f (first coll)) (map- f (rest coll)))))
unreadable message: (:emacs-rex (swank:listener-eval "(println \"→\")
") "user" :repl-thread 6
exception in read loop
java.lang.RuntimeException: java.lang.Exception: EOF while reading
at clojure.lang.RT.readString(RT.java:1263)
at clojure.core$read_string.invoke(core.clj:2897)
at swank.core.protocol$read_swank_message$fn__240.invoke(protocol.clj:44)
at swank.core.protocol$read_swank_message.invoke(protocol.clj:43)
at swank.core.connection$read_from_connection.invoke(connection.clj:59)
at swank.core$read_loop.invoke(core.clj:337)
@Netpilgrim
Netpilgrim / gist:1195103
Created September 5, 2011 14:23
Generating prefix functions
(defn gen-prefn
"Generates a function that for input q returns the lengths of the longest
prefix of pattern that is also a suffix of pattern[1..q]."
[pattern]
(let [pattern (vec pattern)
add-entry (fn [[pfn l] q]
(if (= (pattern l) (pattern q))
[(conj pfn [(inc q) (inc l)]) (inc l)]
(if (zero? l)
[(conj pfn [(inc q) 0]) 0]
@Netpilgrim
Netpilgrim / gist:1191537
Created September 3, 2011 18:00
Pre condition ignored
(defrecord DirectedGraph [vertices edges weights]
;; A directed graph: 'vertices' is a sorted set of vertices, 'edges' is a map
;; from vertices to a sorted set of adjacent vertices, 'weights' is a map from
;; edges given as vertex tuple [from to] to weights.
Graph
(set-vertex [_ v]
(DirectedGraph. (conj vertices v)
(conj edges [v (sorted-set)])
weights))
(set-edge [this edge]
@Netpilgrim
Netpilgrim / gist:1191098
Created September 3, 2011 12:16
Problem with -> (solved)
(defprotocol Graph
(set-vertex [this v])
(set-edge [this from to]
[this from to weight]))
(defrecord DirectedGraph [vs em wm]
;; A directed graph: vs is a sorted set of vertices, em is a map from vertices
;; to a sorted set of adjacent vertices, wm is a map from edges given as
;; vertex tuple [from to] to weights.
Graph
@Netpilgrim
Netpilgrim / gist:1181919
Created August 30, 2011 20:23
4clojure problem 43
(defn deinterleave
[coll n]
(->>
(loop [out [(take n coll)]
tail (drop n coll)]
(if (seq tail)
(recur (conj out (take n tail)) (drop n tail))
out))
(apply map vector)))
@Netpilgrim
Netpilgrim / gist:1176716
Created August 28, 2011 14:18
DFS path finder
(defn find-path
[graph from to]
((fn this [graph from to path]
(if (= from to)
(println "Found it:" path)
(let [children (for [v (graph from) :when (not (some #{v} path))] v)]
(if (empty? children)
(println "Dead end:" path)
(doseq [v children]
(this graph v to (conj path v)))))))
@Netpilgrim
Netpilgrim / gist:1176126
Created August 28, 2011 01:40
Lazy seq of primes
(defn mult?
"Returns true iff n is a multiple of one of the numbers in coll."
[coll n]
(some #(zero? (rem n %)) coll))
(def primes
(letfn [(rest-primes
[known-primes]
(lazy-seq
(let [highest-known-prime (peek known-primes),
@Netpilgrim
Netpilgrim / gist:1165353
Created August 23, 2011 15:00
Analyzing Android Pattern Lock
(def pattern-graph
[[0 1 0 1 1 1 0 1 0]
[1 0 1 1 1 1 1 0 1]
[0 1 0 1 1 1 0 1 0]
[1 1 1 0 1 0 1 1 1]
[1 1 1 1 0 1 1 1 1]
[1 1 1 0 1 0 1 1 1]
[0 1 0 1 1 1 0 1 0]
[1 0 1 1 1 1 1 0 1]
[0 1 0 1 1 1 0 1 0]])