我正在寻找nth
函数的概括。
Julia有一个很好的功能,我在Clojure中找不到相应的东西:
getindex(A,inds ...)返回由inds指定的数组A的子集, 其中每个ind可以是Int,Range或Vector。
这与此问题有关: Clojure Remove item from Vector at a Specified Location
答案 0 :(得分:9)
map
已经做了你想要的。 (map v indices)
按预期工作,因为矢量可以作为其索引的函数来处理。
答案 1 :(得分:2)
这是否符合您的要求:
(defn get-nths [xs ns]
(for [n ns]
(nth xs n)))
?
矢量,范围和仅一个例子:
(defn x []
(vector
(get-nths [:a :b :c :d :e] [2 4])
(get-nths [:a :b :c :d :e] (range 3))
(get-nths [:a :b :c :d :e] [0])))
(x)
;; => [(:c :e) (:a :b :c) (:a)]
答案 2 :(得分:2)
使用矢量,您也可以使用select-keys
。在某些情况下,它可能非常有用:
user> (select-keys [:a :b :c :d] [0 1])
{0 :a, 1 :b}