Created
April 27, 2026 07:34
-
-
Save vvgomes/be85606571bf70cb7c12dce8ab7f938b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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