> Looks like an if then else version of the map lookup??
> ie: (if (%1 %2) (%1 %2) %3)
> Is this a special feature of maps in general, such that you can look
> up a key but return something else if it doesn't exist?
> I hadn't come across it yet, but it sounds useful :)

This is exactly right (I just confirmed it for myself).  I looked up
the java code for Keyword.java and Symbol.java both of which implement
IFn  (those things that can be put in the function position).  The
single and double arity invoke() merely delegate to 'get'.  And voila:

user> (doc get)
-------------------------
clojure.core/get
([map key] [map key not-found])
  Returns the value mapped to key, not-found or nil if key not
present.

The last parameter is definitely for those situations where 'if not
found, use this'

I think I _finally_ understand the statement, "keywords and symbols
are functions of maps".

So, the original poster's observation about ('+ '1 '2) returning '2
makes more sense when you consider that it turns into this:

(get '1 '+ '2)

or "get from the hashmap ( '1 ) the value stored at key ( '+ ) and if
nil is returned, return ( '2 ) instead"

(get '1 '+) returns nil as '1 is not even a map.


--~--~---------~--~----~------------~-------~--~----~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to