【发布时间】:2018-11-17 00:28:47
【问题描述】:
我遇到了一个问题,即不变性突然不适用于我的向量。我想知道是否有一种方法可以创建给定集合的新的、不可变的向量副本。
Clojuredocs 建议“克隆”,但这给了我一个错误,指出没有这种方法。
(defn stripSame [word wordList]
(def setVec (into #{} wordList))
(def wordSet word)
(def wordVec (into #{} [wordSet]))
(def diffSet (set/difference setVec wordVec))
(def diffVec (into [] diffSet))
diffVec)
(defn findInsOuts [word passList]
(def wordList (stripSame word passList))
(println word wordList)
(def endLetter (subs word (dec (count word))))
(def startLetter (subs word 0 1))
(println startLetter endLetter)
(def outs (filter (partial starts endLetter) wordList))
(def ins (filter (partial ends startLetter) wordList))
;(println ins outs)
(def indexes [ (count ins) (count outs)])
indexes)
(defn findAll [passList]
(def wordList (into [] passList) ))
(println wordList)
(loop [n 0 indexList []]
(println "In here" (get wordList n) wordList)
(if (< n (count wordList))
(do
(def testList wordList)
(def indexes (findInsOuts (get wordList n) testList))
(println (get wordList n) indexes)
(recur (inc n) (conj indexList [(get wordList n) indexes]))))))
passList 是这样的单词列表(很好笑),然后将其转换为向量。
所以基本上 findAll 调用 findInsOuts ,它遍历列表中的每个单词并查看有多少其他单词以它的最后一个字母开头,但它首先从向量中删除搜索词,然后再执行一些功能以防止重复。问题在于,不知何故,这个向量实际上是可变的,所以 findAll also 中的向量副本永久删除了该值。
当我尝试创建一个新向量然后对该向量执行操作时,仍然会发生同样的事情,这意味着它们被别名/共享相同的内存位置。
如何创建一个实际上不可变的新向量以供使用?
感谢任何帮助
【问题讨论】: