[Cs254f11] Example of filter usage

Wm. Josiah Erikson wjerikson at hampshire.edu
Wed Oct 5 14:50:26 EDT 2011


So a light went on for me when Alec asked (probably for my benefit, 
thanks Alec) about filter. I was able to reduce the following code:

(loop [i 0 result 0]
     (if (= i ( - (count (get scales scale)) 1))
     result
     (recur (inc i) (+ result(modified_truth (= (mod note 12) (nth (get 
scales scale) i) ) 1 ))))))

to:

(count(filter #(= (mod %1 12) note) (get scales scale)))

Which is pretty neat.

-- 
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002
(413) 559-6091



More information about the Cs254f11 mailing list