Obtaining max and min values in Clojure -
i trying pull maximum , minimum results sql query clojure can perform mathematical analysis on them unsure why getting error.
i have used max , min functions in code try , determine these results although keep getting 2 errors seem relate way using :counter keyword.
the data returned in map sql query looks this:
{:date1 "20131007", :data "object1", :counter 1000} {:date1 "20131007", :data "object2", :counter 50} {:date1 "20131007", :data "object3", :counter 230}
when use code:
minvalue(min(map(keyword :counter)data2)) maxvalue(max(map(keyword :counter)data2)) valrange(- maxvalue minvalue) valpc(* (/ valrange 100) 10) x(- maxvalue valpc)
i want minvalue set 50 , maxvalue set 1000 although getting error:
java.lang.classcastexception: clojure.lang.lazyseq cannot cast java.lang.number
if remove map function code , run again, error:
java.lang.classcastexception: clojure.lang.keyword cannot cast java.lang.number
any appreciated on i'm stuck (and pretty ovbious i'm novice @ clojure)! thanks
a few changes code:
- min , max take variable number of parameters, rather collection. use apply to, apply contents of collection parameters instead.
- clojure naming conventions use - between words
- valid calls have in form (basically list first item callable)
- no need use "keyword", keyword when have string , need associated keyword. dragging data out of maps keywords act functions e.g (:a {:a 1 :b 2}) returns 1.
factored out commonality in first 2 lines
(def data [{:date1 "20131007", :data "object1", :counter 1000} {:date1 "20131007", :data "object2", :counter 50} {:date1 "20131007", :data "object3", :counter 230}]) (def counters (map :counter data)) ; => (100 50 230) (def min-value (apply min counters)) ; => 50 (def max-value (apply max counters)) ; => 1000 (def val-range (- max-value min-value)) ; => 950 (def val-pc (* (/ val-range 100) 10)) ; => 95 (def x (- max-value val-pc)) ; => 905
Comments
Post a Comment