0

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

2
  • 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. Commented Jan 31, 2016 at 15:48
  • 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. Commented Jan 31, 2016 at 17:00

1 Answer 1

1

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...
      ))
Sign up to request clarification or add additional context in comments.

4 Comments

Ah right so i would have to declare it to some name to reference it?? How would i remove from a set though? as at the minute im just using print statements to check its going through the if statements
You usually use dissoc to remove from a hash-map. You don't have any sets in your code so I assume you mean hash-map. I recommend going through the 4clojure problems to get a handle of the language.
So i have updated original post, so what i think i have now it one hash-map taking in both X and Y. Then put Y onto hashmap 2 and then if X is not equal to Y remove Y from hashmap2?? Would be correct? Thanks for the advice, i have never heard of 4clojure problems
4clojure problems allow you to look at others code as you solve problems from easy to hard. Clojure is a great language but it is not easy to learn, especially if it is your first go at a functional language. You kind of need to hit the books before just throwing yourself in, but if you have to write code right away I suggest googling 4clojure and clojure koans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.