我很难将keep-indexed
与多个参数一起使用:
使用:
(keep-indexed #(if (= %1 3) %2) [1 2 3 4 5])
我可以使用索引3获取值,即(4)
。
如果索引应作为参数给出,例如索引3:
(keep-indexed #(if (= %1 ???) %2) 3 [1 2 3 4 5])
我应该如何修改??? - 部分?
答案 0 :(得分:2)
您需要将其考虑在内,因为keep-indexed
需要两个参数函数。
(defn my-nth [index coll] (keep-indexed #(if (= %1 index) %2) coll))
(my-nth 3 [1 2 3 4 5])
;=> (4)
如果您只是想通过索引访问,那么对于矢量
(get [1 2 3 4 5] 3) ;=> 4
或者确实只是
([1 2 3 4 5] 3) ;=> 4
如果你,想要索引1和3的值。
(map (partial get [1 2 3 4 5]) [1 3])
;=> (2 4)
或者,如果你喜欢
(map [1 2 3 4 5] [1 3])
;=> (2 4)
对于列表或序列类型,请使用nth
。