Skip to content

Instantly share code, notes, and snippets.

@rauhs
Created August 22, 2015 22:06
Show Gist options
  • Save rauhs/526ba615214e692ab719 to your computer and use it in GitHub Desktop.
Save rauhs/526ba615214e692ab719 to your computer and use it in GitHub Desktop.
Returns a function that quantizes data
(defn quantizer
"Returns a function that quantizes input data which when called with 'x' returns:
o <1st val> if -Inf < x <= <1st bound>
o <2st val> if <1st bound> < x <= <2st bound>
o ...
o <last val> if <last-1 bound> < x <= <last bound>
o >max if x > <last bound>
where m is a vector of vectors where the first element specifies the boundary and
the second element the value which to return.
Example:
(def points->grade (quantizer [[40 :F] [60 :D] [80 :C] [90 :B]] :A))
(map points->grade [10 80 93])
; (:F :C :A)"
[m >max]
(fn [x]
(cond
(<= x (first (first m))) (second (first m))
(> x (first (last m))) >max
:else (some
(fn [[[l _] [h hv]]]
(and (< l x) (<= x h) hv))
(partition 2 1 m)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment