This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
OlderNewer