Basically what I'm trying to do is print a hashmap that contains keys that are characters in a string, and these keys have value 1. For example, string "aabbce" should give a dictionary {:a 1 :b 1 :c 1}. The following is my attempt but it just prints the empty hashmap
(defn isValid [s]
(def dict {})
(map (fn [x] ((assoc dict :x 1))) (seq s))
(println dict)
)
> (into {} (for [c "aabbce"] [(keyword (str c)) 1]))
{:a 1, :b 1, :c 1, :e 1}
into {} ... sequence of pairs ...
is often a convenient way to create hashmaps. For example
> (into {} [[:x 1] [:y "foo"]])
{:x 1, :y "foo"}
and for [item collection] [(key-from item) (value-from item)]
can be a nice way to iterate over a collection to create that list of key-value pairs.
> (for [color ["red" "blue" "green"]] [(clojure.string/upper-case color) (count color)])
(["RED" 3] ["BLUE" 4] ["GREEN" 5])
I find that putting those together is often the trick for when I want to create a hashmap:
> (into {} (for [color ["red" "blue" "green"]] [(clojure.string/upper-case color) (count color)]))
{"RED" 3, "BLUE" 4, "GREEN" 5}