My first crack at clojure... if you have time to provide input, I'd appreciate it. I'm as much interested in style as proper usage. I'm just getting started.
The task to start with an vector of N zeros, and then "scramble" it by: Select 2 items in the vector. If one is less than 4, and the other is greater than -4, increment one and decrement the other -- so that the sum of the vector remains the same.
am I abusing let?
this generates a sequence of vectors, and I take the last one. Is there a better way to iterate and get the last result?
what tricks that I've missed?
this is just a small sample ;) but what community standards and practices have I missed?
code
(defn level [c] (let [n+ (rand-int (count c))
n- (rand-int (count c))
v+ (inc (nth c n+))
v- (dec (nth c n-))
go (and (not= n+ n-) (< v+ 4) (> v- -4))
]
(if go (assoc (assoc c n+ v+) n- v-) c)
))
(defn rseq [n]
(let [first (vec (repeat n 0))]
(last (take (* n 2) (iterate level first)))))
(rseq 16)
(deftest rseq-sum-equals-2
(let [actual (rseq 16)]
(is (= 0 (apply + actual)))))
(run-tests 'ursound.core)
go
should be bound to(and (not= n+ n-) (<= v+ 4) (>= v- -4))
. \$\endgroup\$