Clojure: how to get multiple items by index from a vector

I am using the following code to retrieve data by index [1 2], is there any shorter solution?

(vec (map #(nth ["a" "b" "c"] % ) [1 2]))
+4
source share
2 answers

If you want ONLY the first and second index of a vector, there are many ways ...

A simple optional vector can be used to store the first index to the third index.

(subvec ["a" "b" "c"] 1 3)

You can match the vector and apply your vector to the first and second index to return the last two indexes as a vector.

(mapv ["a" "b" "c"] [1 2])

Using the thread-last macro , you can take 3 indexes and discard the first.

(->> ["a" "b" "c"] (take 3) (drop 1))

, n , , , n , 0, n.

(drop 1 ["a" "b" "c"])
+5

mapv ,

(mapv ["a" "b" "c"] [1 2])
+7

Source: https://habr.com/ru/post/1569874/


All Articles