【发布时间】:2021-04-09 23:09:22
【问题描述】:
我一直在尝试惯用地循环遍历嵌套向量,如下所示:
[[:a 1 :b 1 :c 1] [:a 1 :b 1 :c 3] [:a 1 :b 1 :c 1]]
我还需要在找到值后返回坐标。
例如调用(find-key-value 3)应该返回[1 2]
这是我目前所拥有的,但它没有给我我需要的输出它会返回([] [] [] [] [] [1 2] [] [] []),而我只需要[1 2]
(defn find-key-value
[array value]
(for [x (range 0 (count array))]
(loop [y 0
ret []]
(cond
(= y (count (nth array x))) [x y]
:else (if (= value (get-in array [x y]))
(recur (+ 1 y) (conj ret [x y]))
(recur (+ 1 y) ret))))))
任何人都对如何修复我的代码以获得我想要的解决方案或想到更好的方法有任何想法!
【问题讨论】:
-
看来
(find-key-value 3)应该返回坐标[1 5],而不是[1 2] -
@AlanThompson - 在我看来
[1 2]的意思是second sub-collection (index 1) at third element of sub-collection (index 2)。
标签: clojure clojurescript