我在寻找nth函数的一个推广。在Julia中有一个很好的函数,我很难在Clojure中找到一个类似的函数:
getindex(A,inds.)返回由inds指定的数组A的子集,其中每个ind可以是Int、范围或向量。
这与这个问题有关:Clojure Remove item from Vector at a Specified Location
发布于 2017-02-10 07:03:46
map已经做了你想做的事。(map v indices)按预期工作,因为向量可以作为其索引的函数来处理。
发布于 2017-02-10 04:57:47
这能满足你的需要吗?
(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)]发布于 2017-02-10 06:36:03
对于向量,您也可以使用select-keys。在某些情况下,它可能非常有用:
user> (select-keys [:a :b :c :d] [0 1])
{0 :a, 1 :b}https://stackoverflow.com/questions/42151486
复制相似问题