假设我有以下向量
(def x [[1 2 3] [4 5 6] [7 8]])
Run Code Online (Sandbox Code Playgroud)
我想将数字9附加到最后一个向量(我不知道向量的索引)
(conj (vec (butlast x)) (conj (last x) 9))
#=> [[1 2 3] [4 5 6] [7 8 9]]
Run Code Online (Sandbox Code Playgroud)
有没有更好/更清晰的方法来做到这一点?
A. *_*ebb 10
使用有效的尾部访问功能
(conj (pop x) (conj (peek x) 9))
Run Code Online (Sandbox Code Playgroud)
但你也可以
(update-in x [(dec (count x))] conj 9)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
488 次 |
最近记录: |