Skip to content

Instantly share code, notes, and snippets.

Jeff Foster fffej

Block or report user

Report or block fffej

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:132843
(defn contains-value?
[coll val]
(not (nil? (some (partial = val) coll))))
(defn executing?
"Is x of the form: (executing ...)?"
(and (seq? x) (= 'executing (first x))))
(defn convert-op
View gist:134503
(deftest test-pattern-matching
(is (= fail (pat-match '(i need a ?X) '(i really need a vacation))))
(is (= no-bindings (pat-match '(this is easy) '(this is easy))))
(is (= fail (pat-match '(?X is ?X) '((2 + 2 is 4)))))
(is (= '{?X (2 + 2)} (pat-match '(?X is ?X) '((2 + 2) is (2 + 2)))))
(is (= '{?P (Mr Hulot and I) ?X (a vacation)} (pat-match '((?* ?P) need (?* ?X))
'(Mr Hulot and I need a vacation))))
(is (= '{?X (1 2 a b)} (pat-match '((?* ?X) a b (?* ?X)) '(1 2 a b a b 1 2 a b)))))
View gist:134781
(defn read-symbol
"Read a single symbol from a string returning nil on failure"
(read s false nil))
(defn get-response
"Get a response from the given string"
(with-open [r (PushbackReader. (StringReader. s))]
View gist:138036
(defn run-machine
"Run the virtual machine with the decoded instructions.
Reset the program counter when complete"
[vm ops update-input]
(update-input vm)
(doseq [[op args] ops]
(apply op (list vm args)) ;; dodgy side effect
(swap! (:counter vm) inc))
(swap! (:counter vm) (constantly 0))
(swap! (:firstrun vm) (constantly false))
View gist:138049
(defstruct virtualmachine :mem :counter :inport :outport :status :firstrun :user)
(defn increment-counter
(assoc vm :counter (inc (:counter vm))))
(defn memory-put
"Update the memory specified by the key with the address to the given value"
[vm key addr val]
(assoc vm key (assoc (key vm) addr val)))
View gist:138052
(defn run-machine
"Run the virtual machine with the decoded instructions."
[vm ops update-input]
(fn [v [op args]]
(increment-counter (op v args)))
(update-input vm)
View gist:140303
(defn tree-search
"Find a state that satisfies goal? Start with states, and search
according to successors and combiner"
[states goal? successors combiner]
(dbg :search "Search %s" states)
(empty? states) nil
(goal? (first states)) (first states)
:else (recur
(combiner (successors (first states)) (rest states))
View gist:140551
(defn depth-first-search
"Search new states first until goal is reached."
[start goal? successors]
(tree-search (list start) goal? successors concat))
(defn reverse-concat
"Prepend y to start of x"
[x y]
(concat y x))
View gist:140555
(defn sorter
"Return a combiner function that sorts according to cost-fn"
(fn [new old]
(sort (fn [n o] (< (cost-fn n) (cost-fn o))) (concat new old))))
(defn best-first-search
"Search lowest cost states first until goal is reached"
[start goal? successors cost-fn]
(tree-search (list start) goal? successors (sorter cost-fn)))
View gist:140556
(defn beam-search
"Search highest scoring states first until goal is reached"
[start goal? successors cost-fn beam-width]
(tree-search (list start) goal? successors
(fn [old new]
(let [sorted ((sorter cost-fn) old new)]
(if (> beam-width (count sorted))
(take beam-width sorted))))))
You can’t perform that action at this time.