(defn min-max-by-columns [s]
(reduce (fn [[smallest largest] y]
[(map min smallest y) (map max largest y)])
[(first s) (first s)]
s))
I’m trying to find out the max and min of each column of a large table (sequence of fixed length sequences)
The above code works fine for small tables, but for large tables I get a stackoverflow error
clojure.core/map/fn--3594 (core.clj:2370)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.RT.seq (RT.java:447)
clojure.core/seq (core.clj:133)
clojure.core/map/fn--3594 (core.clj:2371)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.RT.seq (RT.java:447)
clojure.core/seq (core.clj:133)
clojure.core/map/fn--3594 (core.clj:2371)
clojure.lang.LazySeq.sval (LazySeq.java:42)
clojure.lang.LazySeq.seq (LazySeq.java:60)
clojure.lang.RT.seq (RT.java:447)
clojure.core/seq (core.clj:133)
clojure.core/map/fn--3594 (core.clj:2371)
…
Am I holding on to the head because of the line [(first s) (first s)]? I need these values for the algorithm to work.
How can I fix this?
I seemed to find the solution. As amalloy explained, the (map smallest x) and (map max largest x) is being realized all at once. I am, however, trying to solve a different problem than what he is solving. Luckily, his insight helped lead me to the solution; the trick is to wrap a doall around each map so that they are realized during intermediate steps as opposed to all at the once in the end.