what is the best way to update / calculate a variable during doseq?
Obviously I could just do the following:
(doseq [x xs]
(println (string/join " " x)))
(println "total:" (reduce + 0 (map count xs)))
But I want to avoid having to map over the entire list at the end to calculate a value... It makes more sense to just update a count as we iterate.
I found that this works, but it seems like a bit of a cludge.
(defn display-xs [xs]
;; all I want to do is update a count while I print,
;; and have that value available afterwards!
(let [n (ref 0)]
(do
(doseq [x xs]
(dosync
(ref-set n (+ @n (count x)))
(println (string/join " " x))))
(println "total:" @n))))
I know that doseq allows for :let but I need the value after doseq is finished.
Or
(println "total:" (reduce (fn [m x] (do (println x) (+ m (count x)))) 0 xs))
reduceseems perfectly good to me. - amalloy