简体   繁体   English

clojure检查向量中向量中的值是否等于另一个

[英]clojure check if value in vector in vector in vector equals another

Basically I have vectors within vectors, within vectors, and those deepest vectors contain two coordinate points. 基本上,我在矢量内,矢量内都有矢量,并且最深的矢量包含两个坐标点。 for example [[[3 4] [4 5]] [[5 6] [6 7]]] I want to check to see if a certain x and y value match any of the coordinate points inside, and if they do, return -1. 例如[[[3 4] [4 5]] [[5 6] [6 7]]]我想检查某个x和y值是否与内部的任何坐标点匹配,如果匹配,返回-1。 I tried doing this but the function isn't working. 我尝试这样做,但是该功能无法正常工作。

    (defn check-for-stuff
      [vect x y]
      (if (not (empty? vect)) 
        (dotimes [n (count vect)]
          (if (not (empty? (vect n)))
            (dotimes [p (count (vect n))]
              (if (and (= (((vect n) p) 0) x)
                       (= (((vect n) p) 1) y))
                -1
                ))))))

For a literal answer to the question, this is my first top-of-head attempt: 为了从字面上回答这个问题,这是我的首次尝试:

(defn check-for-stuff [vect x y]
  (when (some (fn [v] (some #(= % [x y]) v)) vect)
    -1))

Observed values in testing: 测试中的观察值:

(check-for-stuff [[[1 2] [3 4]]] 1 2)
-1

(check-for-stuff [[[1 2] [3 4]]] 1 3)
nil

That said -- the above is bad practice, since it needs to iterate through the vectors provided (and thus having O(n) performance), rather than being able to do a constant-time membership check as a hash-map or set can. 话虽如此-上面的做法是不好的做法,因为它需要遍历提供的向量(从而具有O(n)性能),而不是像哈希映射或set那样可以进行固定时间的成员资格检查。

Also, it would be more conventional to return a more meaningful value for a truthy result, or true if no such meaningful value exists. 同样,为真实结果返回更有意义的值将更为常规,如果不存在这样的有意义的值,则返回true

@CharlesDuffy is right on the money about this not being the ideal structure for something like this. @CharlesDuffy是对的,因为它不是这样的理想结构。 Based on your description of what you're doing, I would recommend a set of sets of xy vectors (representing points). 根据您对所做工作的描述,我建议您使用一组xy向量集(代表点)。 For example: 例如:

#{#{[3 4] [4 5]} #{[5 6] [6 7]}}

This is a set that represents 2 planes, each represented as a set of xy vectors. 这是代表2个平面的集合,每个平面表示为一组xy向量。 To determine whether any of these sets contains a given xy point, you can use some and contains? 要确定这些集合中是否有一个集合包含给定的xy点,可以使用并contains? some contains? , like this: , 像这样:

(defn check-for-stuff [planes x y]
  (some #(contains? % [x y]) planes))

As @CharlesDuffy also pointed out, -1 might not be the best value to use as a return value. 正如@CharlesDuffy也指出的那样, -1可能不是用作返回值的最佳值。 The idiomatic thing to do in Clojure when you're writing a function that checks inside a collection for a specific value and tells you whether it's in there or not, is to take advantage of the way that "truthiness" works in Clojure. 在编写用于检查集合内部是否有特定值并告诉您是否在其中的函数时,在Clojure中要做的惯用做法是利用“真实性”在Clojure中的工作方式。 Every value is considered "truthy" except for false and nil . 除了falsenil之外,每个值都被认为是“真实的”。 In the function above, some returns either the plane containing [xy] if it exists in planes , or nil . 在上面的函数中,如果包含[xy]的平面存在于planes ,则其中some返回nil You can use the return value of this function just like it were returning true or false , like this: 您可以使用此函数的返回值,就像它返回truefalse一样,如下所示:

(if (check-for-stuff #{#{[3 4] [4 5]} #{[5 6] [6 7]}} 4 5)
  (println "point found")
  (println "point not found"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM