Skip to content

Instantly share code, notes, and snippets.

View fffej's full-sized avatar

Jeff Foster fffej

View GitHub Profile
(defstruct game :current-score :throws)
(def darts
(concat
(range 1 21)
(map (partial * 2) (range 1 21))
(map (partial * 3) (range 1 21))
'(25 50)))
(def finishes
(defn solve-darts-depth-first
[n]
(depth-first-search
(struct game n [])
finished?
next-dart))
(defn solve-darts-breadth-first
[n]
(breadth-first-search (struct game n []) finished? next-dart))
(defn solve-darts-beam-search
[n]
(beam-search
(struct game n [])
finished?
next-dart
(fn [d] (/ (- (:current-score d) n) (count (:throws d))))
3))
(defn new-states
"Generate successor states that have not been seen before."
[states successors state-eq old-states]
(remove
(fn [state]
(or (some (partial state-eq state) old-states)
(some (partial state-eq state) states)))
(successors (first states))))
(defn graph-search
(defstruct path :state :previous :cost-so-far :total-cost)
(defn path-to-string
[path]
(format "Path to %s, cost %s" (:state path) (:total-cost path)))
(defn make-path
"Create a new path object"
[state previous cost-so-far total-cost]
(struct path state previous cost-so-far total-cost))
@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)
(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))
@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)))
(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."