简体   繁体   English

如何使用for循环永久更改clojure函数中的向量

[英]How to permanently change a vector in a clojure function with a for loop

I am trying to make make a function that will get a vector that contains letters and transform it into a vector with letter pairs 我正在尝试制作一个函数,该函数将获取包含字母的向量并将其转换为带有字母对的向量

["a" "b" "c"] to ["ab" "bc"] 

I found that this function does what I need, but it seems that it doesn't change the vector that I entered as a param, instead it makes a new vector for every iteration. 我发现该函数可以满足我的需要,但似乎并没有改变我作为参数输入的向量,而是为每次迭代创建了一个新向量。

(defn test [param] (for [i (range (count param))] (assoc param i 
(clojure.string/join [(get param i) (get param (inc i))]))))

Does anyone have an idea how to permanently change the elements of the vector? 有谁知道如何永久更改向量的元素?

clojure has a host of built-in fns to do this sort of manipulation. clojure有许多内置的fns可以进行这种操作。 For example: 例如:

(->> ["a" "b" "c" "d"]                                                                                                                                                                                                                                                           
     (partition 2 1)       ; generates (("a" "b") ("b" "c") ("c" "d"))                                                                                                                                                                                                                                                        
     (map clojure.string/join)); joins the pairs
;=> ("ab" "bc" "cd")

If you really want a vector, change the map to mapv 如果您确实想要向量,请将map更改为mapv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM