简体   繁体   English

Clojure哈希图查询键和值

[英]Clojure hash-map query key and value

I am trying to use a hash-map within closure as i have a for loop that passes in a sorted string and an un-sorted string to my function, what i want to do is two if statements on the hash-map 我正在尝试在闭包中使用哈希映射,因为我有一个for循环,该循环将排序后的字符串和未排序的字符串传递给我的函数,我想做的是哈希表上的两个if语句

1st if statement is 第一个if语句是

if hash-map doesnt contain string "x"
put x in a new hash-set

2nd if statement is 如果语句是第二

if hash-map key "x" value is not equal to "y"
remove "x" from hash-set

This is what i have so far 这就是我到目前为止

(defn transform3 [y]
 (let [x (sort-string (str/lower-case y))
    my-hash-map (hash-map)
    hashmap2 (hash-map)]
(if-not (contains? my-hash-map x)
  (my-hash-map x, y)
  (hashmap2 y))

(if-not (get hash-map x) y)
(dissoc hashmap2 x)

;;remove from hash-set to do...
))

How would i do if statement that would allow me to use things like "get" "contains" "put" etc...?? 如果允许我使用“获取”,“包含”,“放入”等内容的语句,我该怎么办??

hash-map is a function. hash-map是一个函数。 When you call it (using the parens) it returns an actual hash-map. 当您调用它(使用括号)时,它会返回一个实际的哈希图。 I have altered your code to get past that particular error message. 我已经更改了您的代码,以通过该特定错误消息。

(defn add-to-map [y]
  (let [x (sort-string (str/lower-case y))
        my-hash-map (hash-map)]
    (if-not (contains? my-hash-map x)
     (hash-map x, y)
      (hash-set y))
    (if-not (get hash-map x) y)
      ;;remove from hash-set to do...
      ))

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

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