简体   繁体   English

Function 为 clojure 中向量的最大索引

[英]Function for the maximum index of vector in clojure

Hi I'd like to ask how to get the maximum value of index from Vector in clojure?您好我想问一下如何从 clojure 中的 Vector 中获取索引的最大值?

(def dummyvector [190 260 300 310 250]) (def dummyvector [190 260 300 310 250])

I have dummyvector here, index 0 = 190, 1 = 260... I know the maximum is index 4 = 250. Does clojure have function to get the index value of 4?我这里有 dummyvector,索引 0 = 190, 1 = 260... 我知道最大值是索引 4 = 250。clojure 是否有 function 以获得索引值 4?

Please see the list of documentation in this template project , especially Getting Clojure, Brave Clojure, and the Clojure CheatSheet.请参阅此模板项目中的文档列表,尤其是 Getting Clojure、Brave Clojure 和 Clojure CheatSheet。

As for your particular question, just type something like:至于您的特定问题,只需键入以下内容:

> (count [5 4 3 2 1])
5

You can then use the dec function (decrement by 1) to get 4 .然后,您可以使用dec function(减 1)得到4

> (dec (count [5 4 3 2 1]))
4

If you are instead trying to search for an arbitrary value in the list, you may wish to use Java interop like:如果您尝试在列表中搜索任意值,则可能希望使用 Java 互操作,例如:

(.indexOf  [0 2 4 1 3 5]  4) => 2

Indexes are zero-based in Clojure so you could use Clojure 中的索引从零开始,因此您可以使用

(dec (count dummyvector))

or或者

(->> dummyvector count dec)

or if you really want to go the long way around the barn to find this或者如果你真的想 go 在谷仓周围很长的路要找到这个

(last (keys (zipmap (range 1 (count dummyvector)) dummyvector)))

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

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