Clojure将字符串替换应用于数组中的每个元素

时间:2015-09-01 11:18:27

标签: arrays clojure

给定一个数组包含如下元素:

[":test" ":do_this" "dont" ":_another_one"]

我需要移除:并将其替换为empty string,可以使用以下方式填写:

(clojure.string/replace element #":" "")

但是,如何将其应用于每个元素?我已经看过使用apply函数,但它似乎没有修改元素,这是因为它们实际上是immutable

(apply function collection)

到目前为止,我一直在做什么,但没有运气。

1 个答案:

答案 0 :(得分:6)

使用map

(let [input [":test" ":do_this" "dont" ":_another_one"]
      updated (map #(clojure.string/replace % #":" "") input)]
  ...)

这将返回一个序列而不是一个向量,因此如果要创建一个新向量,请使用mapv