Vector Clojure:向量不是不变的

Vector Clojure:向量不是不变的,vector,clojure,Vector,Clojure,我遇到了一个问题,我的向量突然无法保持不变性。我想知道是否有一种方法可以创建给定集合的新的、不变的向量副本 Clojuredocs建议使用“aclone”,但这给了我一个错误,即没有这样的方法 (defn stripSame [word wordList] (def setVec (into #{} wordList)) (def wordSet word) (def wordVec (into #{} [wordSet])) (def diffSet (set/differen

我遇到了一个问题,我的向量突然无法保持不变性。我想知道是否有一种方法可以创建给定集合的新的、不变的向量副本

Clojuredocs建议使用“aclone”,但这给了我一个错误,即没有这样的方法

(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]))))))
(defn stripSame[单词列表]
(def setVec(进入#{}单词列表))
(def单词集单词)
(def wordVec(进入{}[wordSet]))
(def diffSet(设置/差异setVec wordVec))
(def diffVec(进入[]diffSet))
diffVec)
(defn findInsOuts[单词密码列表]
(def字列表(带相同字密码列表))
(println单词列表)
(def结尾字母(子单词(dec(计数单词)))
(def startLetter(子字01))
(打印字母结尾)
(def输出(过滤器(部分起始端字母)字表))
(def ins(过滤器(部分过滤器)字列表))
(打印输入输出)
(def索引[(计数入(计数出)])
索引)
(defn findAll[密码列表]
(def wordList(进入[]密码列表)))
(println词表)
(循环[n0索引列表[]]
(println“在这里”(获取单词列表n)单词列表)
(如果(
passList是这样的单词列表(lol at good),然后将其转换为向量

基本上findAll调用findInsOuts,它遍历列表中的每个单词,查看有多少其他单词以最后一个字母开头,但在执行一些防止重复的功能之前,它首先从向量中删除搜索词。问题是这个向量实际上是可变的,所以findAll中向量的副本也有这个值

当我尝试创建一个新的向量,然后对该向量执行操作时,同样的事情仍然会发生,这意味着它们的别名/共享相同的内存位置

如何创建一个实际不可变的新向量


非常感谢您的帮助

恐怕您的代码充满了误解。首先,不要在
defn
中使用
def
。使用
let
。这将把您的第一个函数变成

(defn stripSame [word wordList]
  (let [setVec (into #{} wordList)
        wordSet word
        wordVec (into #{} [wordSet])
        diffSet (clojure.set/difference setVec wordVec)
        diffVec (into [] diffSet)]
    diffVec))
比如说,

=> (stripSame 2 [1 2 :buckle-my-shoe])
[1 :buckle-my-shoe]
该函数可以简化为

(defn stripSame [word wordList]
  (vec (disj (set wordList) word)))
。。。或者,使用线程宏来

(defn stripSame [word wordList]
  (-> wordList set (disj word) vec))
我不认为函数做了你认为它做的事情,因为它并不总是保持向量中元素的顺序



如果我是你,我会通过一些社区教程来学习。这里也提到了几本好书。一旦你掌握了这种语言的习惯用法,你会发现你在这里要做的事情会更清楚、更容易

恐怕您的代码充满了误解。首先,不要在
defn
中使用
def
。使用
let
。这将把您的第一个函数变成

(defn stripSame [word wordList]
  (let [setVec (into #{} wordList)
        wordSet word
        wordVec (into #{} [wordSet])
        diffSet (clojure.set/difference setVec wordVec)
        diffVec (into [] diffSet)]
    diffVec))
比如说,

=> (stripSame 2 [1 2 :buckle-my-shoe])
[1 :buckle-my-shoe]
该函数可以简化为

(defn stripSame [word wordList]
  (vec (disj (set wordList) word)))
。。。或者,使用线程宏来

(defn stripSame [word wordList]
  (-> wordList set (disj word) vec))
我不认为函数做了你认为它做的事情,因为它并不总是保持向量中元素的顺序



如果我是你,我会通过一些社区教程来学习。这里也提到了几本好书。一旦你掌握了这种语言的习惯用法,你会发现你在这里要做的事情会更清楚、更容易

应该添加的是,
def
进行全局变量绑定,这就是函数相互碰撞数据的原因。应该添加的是,
def
进行全局变量绑定,这就是函数相互碰撞数据的原因。