Skip to content

Instantly share code, notes, and snippets.

@drlivingston
drlivingston / deterministic-gentemp.clj
Created April 17, 2012 23:12
deterministic gentemp
(def ^:dynamic *gentemp-counters*)
(defn new-gentemp-counters [] {})
(defn next-name [name]
(let [c (get *gentemp-counters* name 0)]
(set! *gentemp-counters* (assoc *gentemp-counters* name (inc c)))
(str name "-" c)))
(defmacro with-reset-ids [& body]
`(binding [*gentemp-counters* (new-gentemp-counters)]
@drlivingston
drlivingston / gist:1172638
Created August 26, 2011 03:46
example AO annotation serialization in RDF using Perl
use Data::UUID;
#use RDF::Trine::Serializer::Turtle;
#use RDF::Trine::Serializer::Notation3;
use RDF::Trine::Serializer::NTriples;
use RDF::Trine::Namespace qw(rdf);
@drlivingston
drlivingston / gist:1150860
Created August 17, 2011 05:23
using a var to count a callback
;; the simple example:
(with-local-vars [foo 0]
(var-set foo (+ 1 (var-get foo)))
(var-get foo))
;; the reason why - a call back
;; (this code still needs some cleaning)
;; the TupleQueryResultHandler is called once per result of the query
(defn sesame-visit-count-sparql [kb query-string]
(with-local-vars [count 0]
@drlivingston
drlivingston / gist:1150606
Created August 17, 2011 01:32
macro for making function
(defmacro kb-test [name triples & body]
`(defn ~name [kb-creator]
(binding [*kb* (new-test-kb kb-creator ~triples)]
~@body)))
create this:
user> (macroexpand-1 '(kb-test test-kb-up test-triples
(is kb)))
@drlivingston
drlivingston / binding-fn.clj
Created April 9, 2011 03:24
macro for functions that bind special vars
(defmacro binding-non-default-fn [fn-name default-specials args & body]
(let [special-vars (vec (doall (map gensym default-specials)))]
`(defn ~fn-name
(~args ~@body)
(~(vec (concat special-vars args))
(binding ~(vec (interleave default-specials special-vars))
~@body)))))
call: