简体   繁体   English

如何在映射中检索其值包含clojure中的特定子字符串的键?

[英]how to retrieve a key in a map whose value contains a particular substring in clojure?

i need to retrieve the key whose value contains a string "TRY" 我需要检索其值包含字符串“ TRY”的键

:CAB "NAB/TRY/FIGHT.jar"

so in this case the output should be :CAB . 因此,在这种情况下,输出应为:CAB

I am new to Clojure, I tried a few things like .contains etc but I could not form the exact function for the above problem.its easier in few other languages like python but I don't know how to do it in Clojure. 我是Clojure的新手,我尝试了一些诸如.contains等的事情,但是我无法形成上述问题的确切函数。在python等其他几种语言中它更容易实现,但我不知道如何在Clojure中做到这一点。

Is there a way to retrieve the name of the key ? 有没有办法检索密钥的名称?

for can also filter with :when . for还可以过滤:when Eg 例如

(for [[k v] {:FOO "TRY" :BAR "BAZ"} 
      :when (.contains v "TRY")] 
        k)

First, using .contains is not recommended - first, you are using the internals of the underlying language (Java or JavaScript) without need, and second, it forces Clojure to do a reflection as it cannot be sure that the argument is a string. 首先,不建议使用.contains首先, .contains使用基础语言(Java或JavaScript)的内部结构,其次,由于无法确定参数是否为字符串,它会强制Clojure进行反射。

It's better to use clojure.string/includes? 最好使用clojure.string/includes? instead. 代替。

Several working solutions have been already proposed here for extracting a key depending on the value, here is one more, that uses the keep function: 这里已经提出了几种可行的解决方案,用于根据值提取密钥,这里还有一个使用keep函数的解决方案:

(require '[clojure.string :as cs])

(keep (fn [[k v]] (when (cs/includes? v "TRY") k))
      {:CAB "NAB/TRY/FIGHT.jar" :BLAH "NOWAY.jar"})  ; => (:CAB)

The easiest way is to use the contains method from java.lang.String. 最简单的方法是使用java.lang.String中的contains方法。 I'd use that to map valid keys, and then filter to remove all nil values: 我会用它来映射有效键,然后过滤以删除所有nil值:

(filter some? 
        (map (fn [[k v]] (when (.contains v "TRY") k)) 
             {:CAB "NAB/TRY/FIGHT.jar" :BLAH "NOWAY.jar"}))
=> (:CAB)

If you think there is at most one such matching k/v pair in the map, then you can just call first on that to get the relevant key. 如果您认为地图中最多有一个这样的匹配k / v对,那么您可以first调用该对以获取相关密钥。

You can also use a regular expression instead of .contains , eg 您也可以使用正则表达式代替.contains ,例如

(fn [[k v]] (when (re-find #"TRY" v) k))

You can use some on your collection, some will operate in every value in your map a given function until the function returns a non nil value. 您可以在集合上使用某些函数, some函数将对映射中的每个值操作给定的函数,直到该函数返回非nil值。

We're gonna use the function 我们将使用该功能

(fn [[key value]] (when (.contains values "TRY") key))

when returns nil unless the condition is matched so it will work perfectly for our use case. when返回nil除非条件匹配,否则它将完全适合我们的用例。 We're using destructuring in the arguments of the function to get the key and value. 我们在函数的参数中使用分解来获取键和值。 When used by some, your collection will indeed be converted to a coll which will look like 当被某些人使用时,您的收藏集确实会转换为一个看起来像

'((:BAR "NAB/TRY/FIGHT.jar"))

If your map is named coll, the following code will do the trick 如果您的地图名为coll,则以下代码可以解决问题

(some
  (fn [[key value]] (when (.contains value "TRY") key)) 
  coll)

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

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