任何人都可以解释下面代码中的类型,我在clojure docs中看到了string / replace吗?
(clojure.string/replace "The color is red" #"red" "blue")
我特别谈到#"red" "blue"
另外,如果我有这样的数组映射:
{"red" "blue"}
如何将此数组映射转换为此未知类型?
{"red" "blue"} ;=> #"red" "blue"???
答案 0 :(得分:4)
如果您有地图{"红色" "蓝色"}并且你想用它来驱动更换,你可以这样做:
;; Generic form of your question - uses re-pattern to create a regex
(defn replace-with [s find replacement]
(clojure.string/replace s (re-pattern find) replacement))
;; Walk through every [find replace] pair in replacements map
;; and repeatedly apply it to string
(defn replace-with-all [s replacements]
(reduce (fn [s [f r]] (replace-with s f r))
s
replacements))
(replace-with-all "foo bar baz" {"foo" "blue" "baz" "red"})
;; "blue bar red"
答案 1 :(得分:3)
在Clojure中,#“.....”是正则表达式定义。因此,您要将red
替换为blue
。
(替换s匹配替换)
用s中的替换替换所有匹配实例。匹配/替换可以是:
string / string char / char pattern /(匹配的字符串或函数)。
但我不明白'将此数组映射转换为此未知类型'是什么意思。