Reputation: 187
I have a single vector of vectors and I want to separate them into individual vectors.
[["1" "2" "3"] ["a" "b" "c"]]
;;=> ["1" "2" "3"] ["a" "b" "c"]
[["1" "2" "3"] ["a" "b" "c"] ["10" "20" "30"]]
;=> ["1" "2" "3"] ["a" "b" "c"] ["10" "20" "30"]
Upvotes: 0
Views: 275
Reputation: 9266
You already have individual vectors inside of your vector. A variety of ways exist to access them, most notably nth
.
Separation can happen in many ways. Here are some examples that you can try out at a REPL.
A common pattern is to use let with to bind them individually in a local context:
(let [first-elem (nth my-vec 0)
third-elem (nth my-vec 2)]
(str "First: " first-elem "\Third: " third-elem))
This is often done via destructoring, too:
(let [[first-elem _ third-elem] my-vec] ;; _ is idiomatic for ignored binding
(str "First: " first-elem "\Third: " third-elem))
Another common scenario is building a lazy sequence from the individual elements, like:
(map-indexed (fn [i v] (str "Elem " i ": " v)) my-vec)
Or just iteration for side-effects
(doseq [v my-vec]
(println v))
;; in Clojure 1.7 prefer
(run! println my-vec)
Or storing one as mutable state
(def current-choice (atom nil))
(swap! current-choice (nth my-vec 2))
@current-choice
You will discover many more as you learn about Clojure collections. Continue experimentation at will.
Upvotes: 2