Brendon Cheung
Brendon Cheung

Reputation: 1035

Function argument with maps in Clojure

I'm getting my feet wet with Clojure the past couple of day and came across this piece of code:

(defn reduce-example
  [new-map [key val]]
  (assoc new-map key (inc val)))

(reduce reduce-example
        {}
        {:max 30 :min 10})
; => {:max 31, :min 11}

I am confused about the function argument in reduce-example, more specifically this: new-map [key value]

From what I studied so far and after the function name, you can declare the number of arguments (arities). For example [x y z], but what does [new-map [key value]] mean? of course it can extract the key and value but how? how should I interpret this function argument?

Thank you

Upvotes: 1

Views: 201

Answers (1)

Shawn Zhang
Shawn Zhang

Reputation: 1884

[new-map [key value]] mean the function expect two argument:

  • first one will bind to name new-map

  • second one (should be a sequential type with 2 elements inside) . The elements inside will bind to key and value

Clojure has an ability of destructuring

Upvotes: 6

Related Questions