Skip to content

Instantly share code, notes, and snippets.

View frenchy64's full-sized avatar

Ambrose Bonnaire-Sergeant frenchy64

  • Madison, Wisconsin
View GitHub Profile
@ato
ato / debug.clj
Created December 9, 2009 11:42
Simpler debug-repl that works with unmodified Clojure
;; Inspired by George Jahad's version: http://georgejahad.com/clojure/debug-repl.html
(defmacro local-bindings
"Produces a map of the names of local bindings to their values."
[]
(let [symbols (map key @clojure.lang.Compiler/LOCAL_ENV)]
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))
(declare *locals*)
(defn eval-with-locals
@weissjeffm
weissjeffm / loop-with-timeout.clj
Created November 15, 2010 22:11
Clojure macro to insert timeouts into loops
;;Code rewriting macro
(defmacro loop-with-timeout [timeout bindings & forms]
`(let [starttime# (System/currentTimeMillis)]
(loop ~bindings
(if (> (- (System/currentTimeMillis) starttime#) ~timeout)
(throw (RuntimeException. (str "Hit timeout of " ~timeout "ms.")))
(do ~@forms)))))
;;example use of macro
(loop-with-timeout 60000 []
@eschulte
eschulte / spreadsheet.clj
Created January 24, 2011 08:20
simple propagator-backed spreadsheet -- see http://gitweb.adaptive.cs.unm.edu/propagator.git
(ns propagator.spreadsheet ; see http://gitweb.adaptive.cs.unm.edu/propagator.git
(use propagator))
(def xrange 10) (def yrange 10)
(def spreadsheet
(map (fn [_] (map (fn [_]
(let [c (gensym)] (eval `(defcell ~c nil)) c))
(range xrange)))
(range yrange)))
(use ' clojure.contrib.reflect)
(defn- gf [obj field]
[(keyword (.getName field)) (get-field (class obj) (.getName field) obj)])
(defn get-all-fields [obj]
(into (sorted-map) (map (partial gf obj) (.getDeclaredFields (class obj)))))
;; user=> (def a (memoize (partial (comp inc last concat) [2])))
;; #'user/a
@ghoseb
ghoseb / with-retries.clj
Created March 18, 2011 14:18
Macro to retry executing some code in case of an exception
(ns test)
(defn try-times
"Try executing a thunk `retries` times."
[retries thunk]
(if-let [res (try
[(thunk)]
(catch Exception e ; can be any exception
(when (zero? retries)
(throw e))))]
@swannodette
swannodette / dcg.clj
Created May 18, 2011 21:28 — forked from raek/dcg.clj
Definite Clause Grammars with core.logic (aka Logos) in Clojure http://en.wikipedia.org/wiki/Definite_clause_grammar
(ns dcg
(:refer-clojure :exclude [reify == inc])
(:use (clojure.core.logic minikanren prelude)))
(declare sentence noun-phrase verb-phrase det noun verb)
(defn sentence [s1 s3]
(exist [s2]
(noun-phrase s1 s2)
(verb-phrase s2 s3)))
@swannodette
swannodette / gist:997140
Created May 28, 2011 19:26
type-inf.clj
(ns logic.y
(:refer-clojure :exclude [== reify inc])
(:use [clojure.core.logic minikanren prelude
nonrel match]))
(defna findo [x l o]
([_ [[?y :- o] . _] _]
(project [x ?y] (== (= x ?y) true)))
([_ [_ . ?c] _] (findo x ?c o)))
(defne geto [k m v]
([_ [[k :- v] . _] _])
([_ [_ . ?r] _] (geto k ?r v)))
(defn typedo [c x t]
(conde
((geto x c t))
((matche [c x t]
([_ [:apply ?a ?b] _]
(exist [s nc]
(def-->e verb [v]
([[:v 'eats]] '[eats]))
(def-->e noun [n]
([[:n 'bat]] '[bat])
([[:n 'cat]] '[cat]))
(def-->e det [d]
([[:d 'the]] '[the])
([[:d 'a]] '[a]))
(defne assoco [m k v o]
([[] _ _ [[k v]]])
([[[k v] . _] _ _ m])
([[[k ?v] . ?r] _ _ [[k v] . ?r]]
(!= m o))
([[[?j v]] _ _ [[?j v] [k v]]]
(!= ?j k))
([[[?j ?u] . ?r] _ _ [[?j ?u] . ?o]]
(!= ?j k)
(!= m o)