This question is a bit abstruse, so please bear with me :-)

Here is an example of a function with named arguments and  default
values from the book "The Joy of Clojure".  It uses optional arguments
(via &) and map destructuring.

(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])
;evaluates to -6.0


(slope :p2 [2 1])
;evaluates to 0.5


Here is an example of map destructuring with no optional arguments:

(defn print-value-a [{vala :a}]
  (println vala))

(print-value-a {:b 7 :a 3})
; prints 3

My understanding of the way optional function arguments work based on
the book and the documentation at clojure.org is that all "leftover"
arguments are, conceptually speaking, stored in a (presumably vector-
like) sequence which is bound to (or destructured according to) the
"entity" after the &.  If that is true, then I would expect to be able
to do explicitly what is being done in the slope function implicitly;
i.e., the following expression should print 3:

(print-value-a [:b 7 :a 3])
; actually prints nil

So this can't be how it works, but I don't know how else to interpret
the documentation.  Is this a special case meaning "if there are
optional arguments AND they are to be destructured via a map, then
insert them pairwise into a map instead of a vector"?  Or does this
behavior fall directly out of the destructuring rules without a
special case based on something I'm missing?

   Mike

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to