【问题标题】:Destructuring/Unpacking first and rest in Clojure首先解构/解包,然后在 Clojure 中休息
【发布时间】:2015-10-11 02:52:54
【问题描述】:

在 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

【问题讨论】:

    标签: python-3.x clojure iterable-unpacking


    【解决方案1】:

    您需要添加一个&amp; 来进行下一次绑定捕获rest

    (defn destructurer [vec]
      (let [[beginning & the-rest :as parts] vec]
        [beginning the-rest]
        )
      )
    

    关于 clojure 解构功能 here 有一个很好的 github 要点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多