Avram
unread,Mar 30, 2011, 3:04:12 PM3/30/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Clojure
hi,
I have 2 report files in JSON format like below:
# file 1: a.json
{ "abcdef" : { "value" : "10" }
# file 2: b.json
{ "abcdef" : { "value" : "20" }
I am using clojure.contrib.json to read each file, and I want a
function that takes in the 2 structures and a metric name (e.g.
abcdef). The function will then look up abcdef in each structure,
find the value of each, and compute the difference. I am running into
an issue, however, that the function needs to take in a string (e.g.
"abcdef") but convert this to a keyword (e.g. :abcdef ).
Thus I am looking for the opposite of as-str in
clojure.contrib.string, i.e. something that takes a string and creates
a keyword.
Or perhaps there is a better way?
This is what I have so far:
(use 'clojure.contrib.json)
(import 'java.io.FileReader)
(def report1 (clojure.contrib.json/read-json (FileReader.
"a.json" )) )
(def report2 (clojure.contrib.json/read-json (FileReader.
"b.json" )) )
;; Seek out the values from report1 and report2 for the metric desired
( e.g. :abcdef), and
;; compute the difference
;;-----------------------------------------------------------------------------
(defn compute-metric-diff [ json1 json2 :metric ]
(- (:value (json1 :metric )) (:value (json2 :metric))))
user=> (defn compute-metric-diff [ json1 json2 :metric ]
(- (:value (json1 :metric )) (:value (json2 :metric))))
java.lang.Exception: Unsupported binding form: :metric (NO_SOURCE_FILE:
58)
;; I want to be able to call it like so
(compute-metric-diff report1 report2 "abcdef" )
Any help appreciated.
Many thanks,
Avram