I am using the following code to extract data by index of [1 2], is there any shorter solution?
(vec (map #(nth ["a" "b" "c"] % ) [1 2]))
mapv
maps into a vector, and vectors when applied as functions do index lookup
(mapv ["a" "b" "c"] [1 2])
If you want ONLY the first and second index of a vector, there are many ways...
A simple sub vector can be used to persist the first index until the third index.
(subvec ["a" "b" "c"] 1 3)
You can map the vector and apply your vector to the first and second index to return the last two indices as a vector.
(mapv ["a" "b" "c"] [1 2])
Using the thread-last macro, you can take 3 indices and drop the first.
(->> ["a" "b" "c"] (take 3) (drop 1))
If you have a vector defined with n indices, and all you need is the last n indices, drop base 0 to return the last n.
(drop 1 ["a" "b" "c"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With