Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 918
Ilia Choly Avatar asked Sep 25 '12 01:09

Ilia Choly


1 Answers

Seems like :as works for functions too:

with 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]} 

with 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}} 

See this terrific blog post: http://blog.jayfields.com/2010/07/clojure-destructuring.html

like image 143
noahlz Avatar answered Sep 19 '22 15:09

noahlz