Skip to content

Instantly share code, notes, and snippets.

View fffej's full-sized avatar

Jeff Foster fffej

View GitHub Profile
@fffej
fffej / A* Search Algorithm Visualization.clj
Created July 7, 2009 22:25
A* Search Algorithm Visualization
(def grid-size 30)
;; A collection of walls
(def walls (atom #{}))
;; The route to the finish
(def route (atom #{}))
(defstruct point :x :y)
@fffej
fffej / gist:145985
Created July 13, 2009 07:30
RSS Merge
(defn rss-merge
"Join together the two feeds specified, using min-date as the selector which grabs the minimum
date from the published field"
[feed1 feed2]
(join-all
min-date
(get-entries-from-rss feed1)
(get-entries-from-rss feed2)))
(defn join-all
"Join the lists provided using f to select an element each time"
[f & lists]
(let [l (remove empty? lists)]
(when-not (empty? l)
(let [n (reduce f (map first l))
c (count (filter (partial = true) (map (fn [x] (= (first x) n)) l)))
r (map (fn [x] (if (= (first x) n) (rest x) x)) l)]
(lazy-seq
(concat
@fffej
fffej / gist:145975
Created July 13, 2009 06:57
Finding RSS Entries
(defn get-entries-from-rss
"Get a list of entries from an RSS feed"
[src]
(xm/xml-> (zip/xml-zip (parse-trim src)) :entry))
(defn get-date
"Given an RSS entry, get the date"
[entry]
(xm/xml-> entry :published xm/text))
(defvar
dispatch-table
{:single-match
{'?is match-is '?or match-or '?and match-and '?not match-not}
:segment-match
{'?* segment-match '?+ segment-match+ '?? segment-match? '?if match-if}}
"Dispatch table")
(defn single-match-fn
"Get the single-match function for x, if it is a symbol that has one."
(defn match-if
"Test an arbitrary expression involving variables."
[pattern input bindings]
(dbg :patmatch (format "match-if %s %s %s" pattern input bindings))
(let [f (postwalk-replace bindings (second (first pattern)))]
(when (eval f)
(pat-match (rest pattern) input bindings))))
;; From http://norvig.com/paip/patmatch.lisp
(defun match-if (pattern input bindings)
"Test an arbitrary expression involving variables.
The pattern looks like ((?if code) . rest)."
;; *** fix, rjf 10/1/92 (used to eval binding values)
(and (progv (mapcar #'car bindings)
(mapcar #'cdr bindings)
(eval (second (first pattern))))
(pat-match (rest pattern) input bindings)))
(defn histogram
[filename]
(reduce
(fn [accum x]
(let [h (Math/floor (/ x 10))]
(assoc accum h (inc (get accum h 0)))))
{}
(take-nth 2 (drop 1 (read-lines filename)))))
(ns uk.co.fatvat.wave.parrot
(:import [com.google.wave.api RobotMessageBundle EventType])
(:gen-class :extends com.google.wave.api.AbstractRobotServlet))
(defn- add-blip
[wavelet message]
(.append (.getDocument (.appendBlip wavelet)) message))
(defn -processEvents
[_ bundle]
(defn solve
"Solve a system of equations by constraint propagation"
[equations known]
(dbg :student (format "SOLVE %s %s" equations known))
(or
(some (fn [equation]
(let [x (one-unknown equation)]
(when x
(let [answer (solve-arithmetic (isolate equation x))]
(solve (postwalk-replace {(:lhs answer) (:rhs answer)}