Hello!
I'm new to clojure and new to lisp, so I'm still getting accustomed to
working functionally and I'm trying to take advantage of clojure's
sequences when appropriate.
I'm working on a project to display a variety of measurements and
comparing values across various intervals. I'm pulling the measurement
data from an SQL database and its most efficient to pull all of the
attributes for a given interval at once. When I'm comparing the same
measurements from different intervals, I need to split the attributes
into individual series.
Here's an example of the results. The first vector is the data from
today: the second is from yesterday.
(def stats ([{:timestamp 1.238226902583523E12,
:attr-1 221, :attr-2 1220553964, :attr-3 1899985 }
{:timestamp 1.238227202350991E12,
:attr-1 221, :attr-2 1220553964, :attr-3 1899985 }
{:timestamp 1.2382275025300952E12,
:attr-1 221, :attr-2 1220553964, :attr-3 1899985 }]
[{:timestamp 1.2382281021213591E12,
:attr-1 221, :attr-2 1220553964, :attr-3 1899985 }
{:timestamp 1.238227202230129E12,
:attr-1 220, :attr-2 1212431385, :attr-3 1899985 }
{:sampled_at_js_timestamp 1.238227501935876E12,
:attr-1 220, :attr-2 1212431385, :attr-3 1899985 }])
And here's the results transformed:
{"attr-1":{"today":[[1.238226902583523E12,221],
[1.238227202350991E12,221],
[1.2382275025300952E12,221]],
"yesterday":[[1.2382269016771082E12,220],
[1.238227202230129E12,220],
[1.238227501935876E12,220]]},
"attr-2":{"today":[[1.238226902583523E12,1220553964],
[1.238227202350991E12,1220553964],
[1.2382275025300952E12,1220553964]],
"yesterday":[[1.2382269016771082E12,1220553964],
[1.238227202230129E12,1212431385],
[1.238227501935876E12,1212431385]]},
"attr-3":{"today":[[1.238226902583523E12,1899985],
[1.238227202350991E12,1899985],
[1.2382275025300952E12,1899985]],
"yesterday":[[1.2382269016771082E12,1899985],
[1.238227202230129E12,1899985],
[1.238227501935876E12,1899985]]}}
Here's what I'm currently using:
(let [get-2ple (fn [row col-name] (vector (row :timestamp) (row col-
name)))
map-stats (fn [col-name stats] (map #(get-2ple % col-name)
stats))
collect-stats (fn [col-name stats]
(hash-map "today" (map-stats col-name (first
stats))
"yesterday" (map-stats col-name
(first (next stats)))))
chart-data (hash-map "attr-1" (collect-stats :attr-1 stats)
"attr-2" (collect-stats :attr-2 stats)
"attr-3" (collect-stats :attr-3 stats))]
chart-data)
However, this requires multiple passes over the result data. I should
think it'd be possible to do it in one, but I can't seem to wrap my
head around it.
Any pointers would be much appreciated.
Cheers!
Michael Glaesemann
grzm seespotcode net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---