This code discussing named arguments in Clojure from "The Joy of Clojure":
(defn slope [& {:keys [p1 p2] :or {p1 [0 0] p2 [1 1]}}]
(float (/ (- (p2 1) (p1 1))
(- (p2 0) (p1 0)))))
(slope :p1 [4 15] :p2 [3 21])
The function itself, I understand it -no problem with destructuring- but I don't understand the calling.
Are we passing four arguments to slope? how vectors are getting assigned to :p1 and :p2?
You are passing four arguments to slope, yes. The [] part of slope specifies the parameters, in which & means "slurp all additional parameters into this form", which then specifies that it is looking for arguments that form a map with keys p1 and p2 (and gives default values if either doesn't exist).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With