I’m new to Clojure and functional programming in general. I’m at a loss in how to handle this in a functional way.
I have the following matrix:
(def matrix [[\a \b \c]
[\d \e \f]
[\g \h \i]])
I want to transform it into something like this (rotate counterclockwise):
((\a \d \g)
(\b \e \h)
(\c \f \i ))
I’ve hacked up this bit that gives me the elements in the correct order. If I could collect the data in a string this way I could then split it up with partition. However I’m pretty sure doseq is the wrong path:
(doseq [i [0 1 2]]
(doseq [row matrix]
(println (get (vec row) i))))
I’ve dabbled with nested map calls but keep getting stuck with that. What’s the correct way to build up a string in Clojure or handle this in an even better way?
What you’re trying to achieve sounds like transpose. I’d suggest
What does it do?
is equivalent to
which takes first elements of each of the three lists, calls list on them, then takes second elements, calls list on them… An returns a sequence of all lists which were generated this way.
A couple more examples of both apply and map can be found on ClojureDocs.