The following code works, but I'd like to re-write, so that a single index is returned, rather than a sequence of indexes visited.
Here are the defs and function.
(def usage-vec-len 13) (def read-levels [9999 3000 2000 1000 100 90 80 70 60 50 40 30 10]) (defn sel-adj-idx "Finds the correct position at which to bin part of the reading." [reading] (for [idx (reverse (range 0 usage-vec-len)) :let [out-idx idx] :when (and (<= reading (nth read-levels idx)) (>= idx 0))] out-idx))
If I run this with a reading of 5, I get what I expect
wtr-usage1.core=> (sel-adj-idx 5) (12 11 10 9 8 7 6 5 4 3 2 1 0) wtr-usage1.core=> (first (reverse (sel-adj-idx 5))) 0 wtr-usage1.core=>
I keep going until the first index. The value of 5 would be binned there. I would like just to return the index, but still need to traverse read-levels to compare the reading against each of the levels.
Is there a better form to use for this purpose? I could probably write this recursively, but am trying to see if I can do it with Clojure's rich set of sequence functions.