Wednesday, September 30, 2009

Simple Clojure Tricks

Just some simple Clojure tricks I have learned while playing around with the language recently.

To operate on a collection of collections, use map and apply together:

Get a seq with the max of each vector given

user=> (map #(apply max %) [[3 6 2][1 3 2][8 5][4]])
(6 3 8 4)

Get a seq with the values in the vectors summed.

user=> (map #(apply + %) [[3 6 2][1 3 2][8 5][4]])
(11 6 13 4)

Probably not really useful for anything, but maybe this will impress your geekiest friends.

No comments: