简体   繁体   English

Clojure中最高键的返回值

[英]Return value of highest key in Clojure

I'm working with these two groups of key value pairs, which is being returned by another function. 我正在使用这两组键值对,它们由另一个函数返回。 I'd like to write a function that will always find the highest key and return its corresponding value. 我想编写一个始终会找到最高键并返回其对应值的函数。 In this example, I'd be returning 2 because 499 is the highest key. 在此示例中,我将返回2,因为499是最高密钥。 The data that I am working with is 我正在使用的数据是

({-99 0, 99 0} {-99 2, 499 2})

When I call 当我打电话

   (type ({-99 0, 99 0} {-99 2, 499 2}))

Within the function that is responsible for returning that data it, I get back 在负责返回数据的函数中,我返回

 (clojure.lang.PersistentTreeMap clojure.lang.PersistentTreeMap)

I hope that helps. 希望对您有所帮助。 Thanks! 谢谢!

This function will return the rightmost entry of a Clojure sorted map (the built-in implementation is called clojure.lang.PersistentTreeMap ) in logarithmic time : 此函数将在对数时间返回Clojure排序图的最右边条目(内置实现称为clojure.lang.PersistentTreeMap ):

(defn rightmost
  "Takes a Clojure sorted map sm and returns the entry at the greatest
  key (as determined by sm's comparator)."
  [sm]
  (first (rseq sm)))

Example: 例:

(rightmost (sorted-map 1 1 2 2 3 3))
;= [3 3]

You can then fish out the value using the val function. 然后,您可以使用val函数找出该值。

All the max-key / apply max -based solutions work in linear time instead. 相反,所有基于max-key apply max 在线性时间内工作。 Needless to say, it's a huge difference. 不用说,这是一个巨大的差异。

If the other function could be convinced to return data.avl maps instead, you could access the element at any index in logarithmic time using nth : 如果可以说服另一个函数返回data.avl映射,则可以使用nth在对数时间的任何索引处访问该元素:

;; works for data.avl sorted maps, not the built-ins
(nth (avl/sorted-map 1 1 2 2 3 3) 2)
;= [3 3]
(as-> (apply merge pair)
      merged
      (->> merged
           keys
           (apply max)
           merged))

Notice that when both maps have a "highest" key, the value of second one is returned. 请注意,当两个映射都具有“最高”键时,将返回第二个的值。

This is a good use case for max-key (See this other SO question for a good example of its use), which I think has kind of a misleading name -- what it actually does is it takes a function and a collection, and it returns the item in the collection that has the highest result of applying the function to that item. 这是max-key一个很好的用例(有关其用法的一个很好的例子,请参见另一个SO问题 ),我认为它的名称具有误导性-实际上,它的作用是需要一个函数和一个集合,并且它返回集合中将函数应用到该项目的结果最高的项目。 You can use the function key , which returns the key of a key-value pair. 您可以使用function key ,它返回键值对的键。

(Note that you need to concat your maps together so that you're working with a single collection of key-value pairs.) (请注意,您需要将地图合并在一起,以便使用键值对的单个集合。)

(apply max-key key (concat {-99 0, 99 0} {-99 2, 499 2}))
;=> [499 2]

(second *1)
;=> 2
(defn val-for-max-key [maps]
  (->> (map (partial apply max-key key) maps)
       (apply max-key key)
       val))

EDIT: misunderstood desired return value 编辑:误解了所需的返回值

({-99 0, 99 0} {-99 2, 499 2}) is a lookup operation, where {-99 0, 99 0} is a dictionary and {-99 2, 499 2} is a key. ({-99 0, 99 0} {-99 2, 499 2})是一个查找操作,其中{-99 0, 99 0}是一个字典, {-99 2, 499 2}是一个键。 Since the latter is not a key in the former, the expression will return nil. 由于后者不是前者的键,因此该表达式将返回nil。

When I evaluate (type ({-99 0, 99 0} {-99 2, 499 2})) , I get nil , because the type of nil is also nil . 当我评价(type ({-99 0, 99 0} {-99 2, 499 2}))我得到nil ,因为类型nil也是nil

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

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