Clojure:将字符串向量附加到字符串,从而生成向量

时间:2014-06-24 16:04:27

标签: clojure

新的clojure所以这可能是一个简单的。

我有一个字符串向量,让我们调用它们names并假装它包含:
 ["Jake" "Phil" "Bob"]

我想将这些字符串附加到另一个名为intro的字符串的末尾,该字符串包含:
"My name is "

我希望将结果放入一个矢量中,以便它是:

["My name is Jake" "My name is Phil" "My name is Bob"]

我已经使用map函数尝试了几种组合,但它们似乎都将intro字符串分开。任何人都可以帮助我吗?

1 个答案:

答案 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"]