Destructure parameter of a Clojure function while keeping the original value.

Ili*_*oly 24 clojure destructuring

Can you destructure a function parameter but still have the original available for use? The way I'm doing it now is just using a let form inside the function body, but I wondering if there was a terser way of doing it.

noa*_*hlz 28

似乎也:as适用于功能:

vector

(defn test [[x y :as v]]
  {:x x :y y :v v})

(test [1 2 3 4])
=>  {:x 1 :y 2 :v [1 2 3 4]}
Run Code Online (Sandbox Code Playgroud)

hash-map

(defn test2 [{x :x y :y :as m}]
    {:x x :y y :m m})

(test2 {:x 1 :y 2 :z 3})
=> {:x 1 :y 2 :m {:x 1 :y 2 :z 3}}
Run Code Online (Sandbox Code Playgroud)

请参阅这篇了不起的博文:http://blog.jayfields.com/2010/07/clojure-destructuring.html

  • 我相信let,fn,defn等都使用相同的解构语法. (4认同)
  • 在内部使用`let`的所有内容都可以以相同的方式进行解构. (2认同)