Vector 如何在Clojure中展平嵌套向量?
我有一个原子叫做历史:Vector 如何在Clojure中展平嵌套向量?,vector,clojure,flatten,Vector,Clojure,Flatten,我有一个原子叫做历史: (def history (atom [])) 我想让它保持一个向量的向量。每个向量都是一个“命令”,历史向量按顺序保存所有命令的历史: (swap! history conj current-command) 我希望它看起来像: [["move" 20] ["turn" 90]] 但目前的形式是: atom[[["move" 20] ["turn" 90] ["turn" 120]]] 我能告诉你一些技巧,如何将它展平成[[“移动”20][“旋转”90]]你不需
(def history (atom []))
我想让它保持一个向量的向量。每个向量都是一个“命令”,历史向量按顺序保存所有命令的历史:
(swap! history conj current-command)
我希望它看起来像:
[["move" 20] ["turn" 90]]
但目前的形式是:
atom[[["move" 20] ["turn" 90] ["turn" 120]]]
我能告诉你一些技巧,如何将它展平成
[[“移动”20][“旋转”90]]
你不需要展平它。有一个向量,其中包含要作为其第一个也是唯一一个元素的向量。首先使用:
user> (first [[["move" 20] ["turn" 90] ["turn" 120]]])
[["move" 20] ["turn" 90] ["turn" 120]]
问题是为什么你一开始就得到了嵌套向量。你能分享一段生成带有额外嵌套的向量的代码吗?可能历史原子的初始值不合适
(def history (atom []))
(swap! history conj ["move" 20])
(swap! history conj ["move" 30])
(swap! history conj ["turn" 50])
; [[move 20] [move 30] [turn 50]]
(first @history)
; ["move" 20]
编辑:
(println history)
; atom[[["move" 20] ["turn" 90] ["turn" 120]]]
; but an atom's value should be accessed using the @ mark
(println @history)
; [["move" 20] ["turn" 90] ["turn" 120]]
嗨,迭戈,现在我的历史评估为atom[[[“移动”20][“旋转”90][“旋转”120]]。如果我这样做(第一个历史记录),它会抛出错误“不知道如何从:clojure.lang.Atom创建ISeq”@truffle您必须对一个Atom进行解除绑定,即
(第一个@history)
。