OR
Size: a a a
OR
ST
KC
AC
KC
AC
AC
AC
ST
AC
ST
(ns lib.clojure.vector
(:require [clojure.test :as test]))
#?(:clj (set! *warn-on-reflection* true)
:cljs (set! *warn-on-infer* true))
;•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(defn vec-insert
"Inserts element `x` in the vector `v` at position `pos`."
{:test (fn [] (let [x 'x]
(test/are [expected actual] (= expected actual)
[1 2 x 3 4 5] (vec-insert [1 2 3 4 5] 2 x)
[x 1 2 3 4 5] (vec-insert [1 2 3 4 5] 0 x)
[1 2 3 4 5 x] (vec-insert [1 2 3 4 5] 5 x)
[x],,,,,,,,,, (vec-insert [] 0 x)
true,,,,,,,,, (vector? (vec-insert [1 2 3 4 5] 2 x)))))}
[v pos x]
(reduce conj (subvec v 0 pos) (cons x (subvec v pos))))
;•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(defn vec-remove
"Removes element from the vector `v` at position `pos`."
{:test (fn [] (test/are [expected actual] (= expected actual)
[2 3 4 5] (vec-remove [1 2 3 4 5] 0)
[1 2 3 4] (vec-remove [1 2 3 4 5] 4)
[1 2 4 5] (vec-remove [1 2 3 4 5] 3)
[],,,,,,, (vec-remove [1] 0)
true,,,,, (vector? (vec-remove [1 2 3 4 5] 0))))}
[v pos]
(reduce conj (subvec v 0 pos) (subvec v (inc pos))))
;•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(defn vec-swap
"Swaps two elements in the vector `v` at positions `pos1` and `pos2`."
{:test (fn [] (test/are [expected actual] (= expected actual)
[2 1 3 4 5] (vec-swap [1 2 3 4 5] 0 1)
true,,,,,,, (vector? (vec-swap [1 2 3 4 5] 0 1))))}
[v pos1 pos2]
(-> v
(assoc pos2 (v pos1))
(assoc pos1 (v pos2))))
;•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
ST
AC
IS
IS
OR