Search code examples
haskellfunctional-programmingclojure

How to implement in Clojure unzip like in Haskell?


unzip function takes m elements with n elements each and returns n elements with m elements each, eg

(def pairs [[1 2] [3 4] [5 6]])

(unzip pairs) => [[1 3 5] [2 4 6]]

Ideally looking for something that works for arbitrary number of elements


Solution

  • The for macro is quite useful for these sorts of things:

    (defn unzip [input]
      (for [iter (iterate (partial map rest) input)
            :while (every? seq iter)]
        (map first iter)))