On Nov 17, 5:16 pm, "Mark Volkmann" <[EMAIL PROTECTED]> wrote:
> On Mon, Nov 17, 2008 at 3:48 PM, Rich Hickey <[EMAIL PROTECTED]> wrote:
>
> > On Nov 17, 4:22 pm, samppi <[EMAIL PROTECTED]> wrote:
> >> Inhttp://groups.google.com/group/clojure/browse_thread/thread/62140a28b...,
> >> the following example was given:
>
> >> (defn test1 [{x :x, y :y, :or {:y 3}}]
> >>         [x y])
> >> (test1 {:x 2}) => [2 3]
>
> >> But this doesn't work on the latest Clojure from Subversion I just
> >> got:
>
> >> (test1 {:x 2}) => [2 nil]
>
> >> Was this a feature that was removed? Or should it work?
>
> > That's a typo in the original, should be:
>
> > (defn test1 [{x :x, y :y, :or {y 3}}]
> >  [x y])
>
> Can someone explain what this does?

Yes!

> It looks to me like the function named test1 returns a vector
> containing the values of x and y.

Right.

> Are you suppose to pass a map to the function?

Yes.

> Does it take the value for the key :x and put it in the parameter x,
> and similarly for y?

Yes.

> Is the value 3 used for y if the key :y isn't present in the map?
>

Right again.

> I'm still at the point where reading some Clojure code is like trying
> understand a complicated regular expression.
>

You seem to be doing fine!

This is an example of Clojure's destructuring binding. The basic idea
is that you are in a context in which you'd normally be using a
symbol, intended to be bound to something, e.g. a fn arglist or let
binding. But instead of using a symbol, you instead use a data
structure. In it, there are symbols. Those symbols will be bound to
corresponding parts of a composite data structure. There is general
destructuring for sequences and associative things, like maps.

Destructuring is described here:

http://clojure.org/special_forms#let

While it may look like a regular expression to you right now, it
actually still is code-as-data. The vector and map binding forms are
actual vectors and maps, there are just some rules for interpretation
of things like :or, :keys, :as etc., all described above.

Rich

--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to