Last active
August 29, 2015 14:14
-
-
Save jirkapenzes/4144f6539cea7089281e to your computer and use it in GitHub Desktop.
Clojure swap function
This file contains 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 jpenzes.swap (:require [clojure.test :refer :all])) | |
(defn swap [v i1 i2] | |
(let [t (get v i1)] | |
(assoc (assoc v i1 (get v i2)) i2 t))) | |
(deftest swap-test | |
(testing "Should swap two items by index in a vector" | |
(let [test-vector [1 2 3 4 5 6 7 8 9] | |
expected [1 8 3 4 5 6 7 2 9]] | |
(is (= expected (swap test-vector 1 7)))))) | |
(run-tests) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment