My purpose is to write function for replacing multiple substrings in a
string. Here's my approach:
(defn replace-map
"Replaces substrings in s from (keys m) by (vals m). "
[s m]
(loop [cur-str s, rps m]
(if (empty? rps)
cur-str
(recur (.replace s (first (first rps)) (second (first rps)))
(rest rps)))))
Very simple. And it seems to work with ordinal maps when I test it in
REPL. But I also have some data structure which I make in such way:
(defn make-replacements []
(loop [rps (transient {})] ;; transient map for replacements
(if (empty? skills)
(persistent! rps)
(recur (conj! rps (get-more-replacements-from-db))))))
(There's much more text in real code, but I cut it up to on function
get-more-replacements-from-db).
So, the datastructure I get must be simple persistent map.
(type (make-replacements)) gives PersistentTreeMap.
In REPL it has exactly same syntax as any other map, and
((make-replacements) substring) gives right replacements.
In other words, there's everything working... except my procedure. I
mean, for some substrings it works ok, while others may be not
replaced properly.
I'm very confused with this behavior. What is the reason? And what can
I do to avoid it?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en