Hi,

I am just getting started with Clojure and with functional programming for
matter. I decided that a good exercise would be for to try and port some
Java code that I recently wrote. In the Java code I had a 2-D array which
basically represents a table of values, and to keep things simple assume
that they are just integers. My Java code had to find the greatest value in
each column and return those values in an array. For example, given
something like, [[1 2 3] [2 5 1] [4 2 6]], I would expect [4 5 6] to be
returned. I came up with the following solution in clojure,

(defn num-cols [rows]
  (count (first rows)))

(defn next-col [col rows]
  (rem (inc col) (num-cols rows)))

(defn col-max [col rows]
  (apply max (map #(nth % col) rows)))

(defn col-widths [rows]
  (loop [widths [] col 0]
    (if (= (count widths) (num-cols rows))
      widths
      (recur (conj widths (col-max col rows)) (next-col col rows)))))

I am assuming that there is probably a simpler, more efficient doing this
and am hoping someone can point me in the right direction. I would like to
do something along these lines. Start with a sequence that contains the
values from the first row of my table. For each row in my table, compare the
column values in that row  against the corresponding values in the sequence.
If any of the columns in the row are greater than the corresponding values
in the sequence, then update/replace the sequence with the greater values
from that row.

Thanks

- John

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to