Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Last active August 29, 2015 14:14
Show Gist options
  • Save jirkapenzes/4144f6539cea7089281e to your computer and use it in GitHub Desktop.
Save jirkapenzes/4144f6539cea7089281e to your computer and use it in GitHub Desktop.
Clojure swap function
(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)
@rarous
Copy link

rarous commented Feb 4, 2015

 (defn swap [v i1 i2] 
    (asso­c v i2 (v i1) i1 (v i2)))­

@jirkapenzes
Copy link
Author

Dik, lepsi!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment