Destructuring/Unpacking 先在 Clojure 休息
Destructuring/Unpacking first and rest in Clojure
在 Python 3 中,如果你想解压列表(或元组)的第一个和其余部分,你可以
x, *y = [1, 2, 3]
#x = 1, y = [2, 3]
如何在 Clojure 的 let 块中执行此操作?我试过 :as parts
和
(defn destructurer [vec]
(let [[beginning the-rest :as parts] vec]
[beginning the-rest]
)
)
;; (destructer [1 2 3])
;; [1 2] <- missing the 3
您需要添加一个 &
以进行下一次绑定捕获 rest
:
(defn destructurer [vec]
(let [[beginning & the-rest :as parts] vec]
[beginning the-rest]
)
)
有一个很好的 github
关于 clojure 解构功能的要点 here。
在 Python 3 中,如果你想解压列表(或元组)的第一个和其余部分,你可以
x, *y = [1, 2, 3]
#x = 1, y = [2, 3]
如何在 Clojure 的 let 块中执行此操作?我试过 :as parts
和
(defn destructurer [vec]
(let [[beginning the-rest :as parts] vec]
[beginning the-rest]
)
)
;; (destructer [1 2 3])
;; [1 2] <- missing the 3
您需要添加一个 &
以进行下一次绑定捕获 rest
:
(defn destructurer [vec]
(let [[beginning & the-rest :as parts] vec]
[beginning the-rest]
)
)
有一个很好的 github
关于 clojure 解构功能的要点 here。