Another one using for

(defn col-widths [arr] (for [i (range (count arr))] (apply max (map #(nth %
i) arr))))

On Thu, Apr 8, 2010 at 1:55 PM, John Sanda <john.sa...@gmail.com> wrote:

> Thanks for the explanation. I did see in the docs that the map function can
> take multiple collections, but I guess I did not quite understand it. Your
> explanation really helps illustrate how it works. My Java version of this is
> probably around 30 lines with several branches/execution paths and variables
> as opposed to a 1-line clojure version that is free of branching and
> variables. I'm hooked ;-)
> On Thu, Apr 8, 2010 at 1:38 PM, James Reeves 
> <weavejes...@googlemail.com>wrote:
>
>> On Apr 8, 1:13 pm, John Sanda <john.sa...@gmail.com> wrote:
>> > [
>> >   [1 2 3]
>> >   [2 5 1]
>> >   [4 2 6]
>> > ]
>> >
>> > I am comparing the values in each of the columns, so the result should
>> be [4
>> > 5 6] where the first element represents the largest value in the first
>> > column, the second element represents the largest value in the second
>> > column, etc.
>>
>> The `map` function allows you to specify more than one collection.
>> With multiple collections, the mapping function is passed an argument
>> for each collection. So:
>>
>> (map + [1 2 3] [4 5 6])
>> => ((+ 1 4) (+ 2 5) (+ 3 6))
>> => (5, 7, 9)
>>
>> By combining this with `apply` and `max`, you can find the maximum
>> value of each column:
>>
>> (defn max-columns [coll]
>>  (apply map max coll))
>>
>> If we pass your vector to max-columns:
>>
>> (max-columns [[1 2 3] [2 5 1] [4 2 6]])
>> => (apply map max [[1 2 3] [2 5 1] [4 2 6]])
>> => (map max [1 2 3] [2 5 1] [4 2 6])
>> => ((max 1 2 4) (max 2 5 2) (max 3 1 6))
>> => (4 5 6)
>>
>> - James
>>
>> --
>> 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<clojure%2bunsubscr...@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.
>>
>
>
>
> --
>
> - 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<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
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

Reply via email to