Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive (doall) in clojure

I have some structures with nested lazy sequences which read from files. When I'm testing I would like to be able to wrap them in a recursive version of doall to make sure all the data is pulled from the files before the files get closed.

like image 542
Arthur Ulfeldt Avatar asked Aug 01 '09 18:08

Arthur Ulfeldt


1 Answers

(defn doall-recur [s]
  (if (seq? s)
    (doall (map doall-recur
                s))
    s))

(use 'clojure.contrib.duck-streams)
(with-open [r1 (reader "test1.txt")
            r2 (reader "test2.txt")]
  (doall-recur (list (line-seq r2) (line-seq r1))))

Output:

(("This is test2.txt" "") ("This is test1.txt" ""))
like image 196
alanlcode Avatar answered Sep 30 '22 16:09

alanlcode