Gemeralised biggest-to-the-middle-search

...and renamed it to mountain-search.
This commit is contained in:
Simon Brooke 2017-07-12 23:43:09 +01:00
parent 985c91a72e
commit 5bed2beca2

View file

@ -129,14 +129,25 @@
:y (- cy min-radius)} (as-label label)]]) :y (- cy min-radius)} (as-label label)]])
(defn biggest-to-the-middle-sort (defn- in-mountain-sort
"Sort this list of `maps` representing parties so that those with the most votes are in "Internal function to mountain-sort; not public. Divide this
the middle." `sorted-sequence` into even and odd sub-sequences, and return
[maps] these concatenated back to back."
(let [first-sort (sort-by :votes maps) [sorted-sequence]
evens (take-nth 2 first-sort) (concat (take-nth 2 sorted-sequence)
odds (take-nth 2 (rest first-sort))] (reverse (take-nth 2 (rest sorted-sequence)))))
(concat evens (reverse odds))))
(defn mountain-sort
"Sort this `sequence` so that the largest elements are in the middle.
If `accessor-fn` argument is supplied, it will be used to access a
value on the element to compare."
([sequence]
(let [first-sort (sort sequence)]
(in-mountain-sort first-sort)))
([sequence accessor-fn]
(let [first-sort (sort-by accessor-fn sequence)]
(in-mountain-sort first-sort))))
(defn recursively-draw-segments (defn recursively-draw-segments
@ -220,7 +231,7 @@
(- 0 mid-point-deflection) (- 0 mid-point-deflection)
mid-point-deflection)}] mid-point-deflection)}]
(apply vector (apply vector
(cons :g (recursively-draw-segments (biggest-to-the-middle-sort (vals model)) nil total-votes cx cy scale-radius))) (cons :g (recursively-draw-segments (mountain-sort (vals model) :votes) nil total-votes cx cy scale-radius)))
[:rect {:class frame-class [:rect {:class frame-class
:id (str id "-frame") :id (str id "-frame")
:x (* width 0.05) :y (* height .05) :height cy :width (* width 0.9)}]]]])) :x (* width 0.05) :y (* height .05) :height cy :width (* width 0.9)}]]]]))