On Mon, Dec 22, 2008 at 1:30 AM, Michael Wood <esiot...@gmail.com> wrote: > On Sun, Dec 21, 2008 at 7:03 PM, Adam Harrison (Clojure) > <adam-cloj...@antispin.org> wrote: > [...] >> (defmacro where [& triples] >> `(let [encode# (fn [x#] (cond (and (symbol? x#) (= (first (name x#)) >> \?)) (name x#) >> (integer? x#) (str "\"" x# "\"^^xsd:integer") >> (float? x#) (str "\"" x# "\"^^xsd:decimal") >> (string? x#) (str "\"" x# "\"")))] >> (apply str >> (interpose " .\n" >> (for [triple# '~triples] >> (apply str >> (interpose " " >> (map encode# triple#)))))))) >> >> As you can see, so far it correctly encodes SPARQL capture variables, >> and literal strings, integers and floats: >> >> user=> (print (where (?a ?b 1) (?a ?b 2.0) (?a ?b "string"))) >> ?a ?b "1"^^xsd:integer . >> ?a ?b "2.0"^^xsd:decimal . >> ?a ?b "string" >> >> I tried adding '(list? x#) (eval x#)' to the encode cond to make it cope >> with expressions like this: >> >> (where (?a ?b (+ 1 2))) >> >> Unfortunately that results in an unencoded literal '3' in the query >> string instead of the '"3"^^xsd:integer' I was looking for. I tried
This seems to be a lot simpler if you use keywords instead of ?something: user=> (defmacro encode [x] `(let [x# ~x] (cond (keyword? x#) (str \? (name x#)) (integer? x#) (str \" x# \" "^^xsd:integer") (float? x#) (str \" x# \" "^^:decimal") (string? x#) (str \" x# \") :else (throw (new Exception "Invalid SPARQL atom"))))) nil user=> (println (encode :a)) ?a nil user=> (println (encode 1)) "1"^^xsd:integer nil user=> (println (encode 2.0)) "2.0"^^:decimal nil user=> (println (encode "string")) "string" nil user=> (println (encode (+ 1 2))) "3"^^xsd:integer nil user=> (println (encode (keyword "b"))) ?b nil user=> (println (encode 'invalid)) java.lang.Exception: Invalid SPARQL atom (NO_SOURCE_FILE:0) user=> (println (encode 1/2)) java.lang.Exception: Invalid SPARQL atom (NO_SOURCE_FILE:0) user=> -- Michael Wood <esiot...@gmail.com> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---