0
votes

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

1st if statement is

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...??

1
If you incorporate my answer into the question then my answer no longer makes any sense. I'll probably just end up removing it. I'm pretty sure this is not the way SO is supposed to work. - Chris Murphy
You should probably accept my answer and then compose another fresh question. You could also read up on assoc and dissoc. Also conj. And then difference between sets and maps. Quite possibly you should be using a set in your question. - Chris Murphy

1 Answers

1
votes

hash-map is a function. 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...
      ))