新的clojure所以这可能是一个简单的。
我有一个字符串向量,让我们调用它们names
并假装它包含:
["Jake" "Phil" "Bob"]
我想将这些字符串附加到另一个名为intro
的字符串的末尾,该字符串包含:
"My name is "
我希望将结果放入一个矢量中,以便它是:
["My name is Jake" "My name is Phil" "My name is Bob"]
我已经使用map
函数尝试了几种组合,但它们似乎都将intro
字符串分开。任何人都可以帮助我吗?
答案 0 :(得分:6)
要么不映射介绍字符串
(mapv #(str "My name is " %) ["Jake" "Phil" "Bob"])
;=> ["My name is Jake" "My name is Phil" "My name is Bob"]
(vec (for [name ["Jake" "Phil" "Bob"]] (str "My name is " name)))
;=> ["My name is Jake" "My name is Phil" "My name is Bob"]
或者,重复介绍字符串
(mapv str (repeat "My name is ") ["Jake" "Phil" "Bob"])
;=> ["My name is Jake" "My name is Phil" "My name is Bob"]