Reputation: 698
Can anyone explain me this function definition line of clojure
(defn insert [{:keys [el left right] :as tree} value]
(**something**))
Upvotes: 1
Views: 126
Reputation: 841
This is argument destructuring in Clojure. You can read more about it here: https://gist.github.com/john2x/e1dca953548bfdfb9844
{:keys [el left right]}
assumes the first argument (we'll call it arg
) is a map and binds (:el arg)
to el
, (:left arg)
to left
and (:right arg)
to right
for the scope of the function.
{:keys [.. .. ..]} :as tree}
binds arg
to tree
.
Then value
is handled normally, without any desctructuring.
Upvotes: 2
Reputation: 14187
The insert function is using destructuring for maps, retrieving values from keys. I think the below would make this clearer:
(defn insert [{:keys [el left right] :as tree} value]
(println (str el " " left " " right))
(println "-")
(println tree)
(println "-")
(println value) )
(def mytree {:el "el" :left "left" :right "right"})
(insert mytree 3)
Upvotes: 3