Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Created April 27, 2026 07:34
Show Gist options
  • Select an option

  • Save vvgomes/be85606571bf70cb7c12dce8ab7f938b to your computer and use it in GitHub Desktop.

Select an option

Save vvgomes/be85606571bf70cb7c12dce8ab7f938b to your computer and use it in GitHub Desktop.
(ns transpose
(:require [clojure.test :refer :all]))
;; Matrix Transpose
;;
;; If you Google the solution, you'll find this:
;;
;; ```clojure
;; (def transpose
;; (partial apply map vector))
;; ```
;;
;; I struggle to understand that.
;; So I implemented transpose in a way I understand
(defn transpose [m]
(when (seq (flatten m))
(cons (map first m)
(transpose (map rest m)))))
(def input
[[1 2 3]
[4 5 6]
[7 8 9]])
(def expected-output
[[1 4 7]
[2 5 8]
[3 6 9]])
(deftest transpose-test
(testing "it matches expected output"
(is (= (transpose input) expected-output)))
(testing "it works both ways"
(is (= (transpose expected-output) input))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment