Lately I have been thinking of macros. How else, I thought, could I introduce a variable number of columns (vector indexes) to a function, so that I could extract a number of columns from a .csv file parsed by clojure-csv? Well, I almost got to my goal after re-discovering partial.
So here is a solution for pulling a random number of columns from a sequence, thanks to StackOverflow.com.
(def s1 [[:000-00-0000 "TYPE 1" "JACKSON" "FRED"] [:000-00-0001 "TYPE 2" "SIMPSON" "HOMER"] [:000-00-0002 "TYPE 4" "SMITH" "SUSAN"]]) (def cols [0 2 3]) (defn f1 [s1 col] (map #(get-in s1 [% col] nil) (range (count s1)))) (apply interleave (map (partial f1 s1) cols)) (:000-00-0000 "JACKSON" "FRED" :000-00-0001 "SIMPSON" "HOMER" :000-00-0002 "SMITH" "SUSAN")
All this brings me to my final conclusion. After nearly a year of working with Clojure, I have a fair idea on how to manipulate data. I even saw the range .. count solution, but for the wrong sequence. I envisioned looping through the columns, not the sequence returned by clojure-csv.
For Clojure texts, we have a lot of good books. By the way, Chas Emeric’s Brian Carper’s and Christopher Grande’s Clojure Programming is finally published, and the book covers the kind of sophistication involved in the “get-in” form.
Still, I believe what is now needed for learning Clojure is a “cookbook”. That is how to take higher order functions (HOFs) and constructs to build up to sophisticated solutions.