[Cs254f11] Example of filter usage

Lee Spector lspector at hampshire.edu
Wed Oct 5 15:10:12 EDT 2011


Very nice. The new version is easier to write, understand, debug, etc. And the style of thinking that it implements will bring these benefits to lots of other tasks as well...

 -Lee



On Oct 5, 2011, at 2:50 PM, Wm. Josiah Erikson wrote:

> 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
> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list