On 16 April 2010 16:25, Asim Jalis <asimja...@gmail.com> wrote:
> Could you explain the rationale for this swapping? Intuitively it
> seems to me that (let [{ :body x } { :body 42 }] x) should bind x
> to 42 -- it seems intuitive because it is binding :body to :body
> and 42 to x.

My personal take on this is that maps can be thought of as relations
-- in the mathematical sense of sets of tuples, where the tuples here
are the key/value pairs -- and destructuring a map is analogous to
composing two relations. Actually the relations involved here are
functions, which is important, but functional composition *is*
relational composition in the end (under the usual set-theoretical
definition of functions). Perhaps an example will make this clearer:


1. Destructuring pattern:

{foo :foo bar :bar}
; => corresponds to a set of tuples:
; #{ [foo, :foo], [bar, :bar] }
; (using Clojure notation rather than the one usual in maths)

2. Map to be destructured:

{:foo "foo" :bar "bar"} ; may include other stuff
; => corresponds to a set of tuples:
; #{ [:foo, "foo"], [:bar, "bar"] }

3. Composition of the above:

For each element of the domain of the first function -- meaning each
key of the first map, the destructuring pattern -- take its
corresponding value, feed that to the second function and output the
pair [key-from-first-map, value-from-second-map].

With the example data:

a. combine [foo :foo] with [:foo "foo"] into [foo "foo"];
b. combine [bar :bar] with [:bar "bar"] into [bar "bar"];
c. dump the above into a map: {foo "foo" bar "bar"}.


With this picture in mind, I find Clojure's destructuring patterns
perfectly natural.

Sincerely,
Michał

-- 
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