Today's exercise has two parts. First, replace multiple occurrences of a character in a string with just the first occurrence. So,
The second part is to replace multiple consecutive spaces with one space. In this case
a b stays unchanged while
a b becomes
These kinds of problems are rather trivial in Clojure due to the rich set of functions you get out of the box.
;;Remove dups & mult spaces (def s1 "aaabbb") (def s2 "abcbd") (def s3 "abcd") (def s4 "a b"); (def s5 "a bb x b"); (defn remove-dup-chars [s] (apply str (distinct s))) (defn remove-consecutive-spaces [s] (apply str (mapcat (fn [l] (if (= (first l) \space) (distinct l) l)) (partition-by (partial = \space) s))))
In the first case, we use
distinct to remove the duplicates and the
apply str to turn it back into a string.
In the second case, we partition the string breaking on strings. Then we
mapcat using our function that looks at the sublists and removes duplicate spaces. Since
mapcat flattens our sublists into 1 list we can just use
apply str on the result to turn it back into a string.