将向量的向量映射到向量的哈希在Clojure [英] Map vector of vectors to vector of hashes in Clojure
本文介绍了将向量的向量映射到向量的哈希在Clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在2D空间中有一个点的向量,例如:
I have a vector of points in 2D space such as:
[[0 0] [1 1] [2 2]]
并且需要计算某个点和向量中每个点之间的距离,向量中的点最接近我查询的点。我需要知道距离和最近点,所以我想要一个散列的向量,如下:
And need to compute the distance between some point and each point in the vector to determine which point in the vector is closest to the point I'm querying. I need to know both the distance and the closest point, so I want a vector of hashes like this:
[{:point [0 0] :dist 2.7} {:point [1 1] :dist 3.5} {:point [2 2] :dist 4.3}]
如何将前者转换为后者?我试过这样的:
How can I transform the former into the latter? I tried something like this:
(defn closest-point [point all-points]
(map #({:point % :dist (distance point %)}) all-points))
错误:
(user=> ArityException Wrong number of args (0) passed to: PersistentArrayMap clojure.lang.AFn.throwArity (AFn.java:437)
推荐答案
如果你使用来代替
的解析而不是
的映射
,实际上选择最近的点,你可以使用 min-key
:
This is more readable if you use a for
comprehension instead of a map
. Also, to actually select the nearest point, you can just use min-key
:
(defn closest-point [point all-points]
(apply min-key :dist
(for [p all-points]
{:point p :dist (distance point p)})))
这篇关于将向量的向量映射到向量的哈希在Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文