For FAQ style plain text I like having the RHS comments moved below
the S-Expression as I can C-n down the file and do C-x C-e evaluation
to REPL as I go.
Once the RHS ;;;Comments are below the S-Expressions I find i like
having a symbol to indicate the eval =>
I took the liberty of re-formatting :)
;;; =======================
Here's a summary of what I think I know about quoting in Clojure. I'd
appreciate some feedback if I said anything wrong below or maybe
didn't describe something well.
; One use of quoting is to prevent a list from being evaluated
; where the first item is treated as the name of a function
; and the remaining items are treated as arguments to the function.
(println '(+ 1 2))
; => (+ 1 2) - instead of 3 due to the quote
(println (quote (+ 1 2)))
; => (+ 1 2) - same
; Quoting a list is *not* the same as quoting everyting inside it.
(println ('+ '1 '2))
; => 2 - outputs 2 which is the value of the last item evaluated
; Using a "syntax quote" produces a similar result,
; but the items are resolved to their fully-qualified names.
(println `(+ 1 2))
; => (clojure.core/+ 1 2)
; In all these examples, the result can be bound to a variable
; and evaluated later.
(def demo1 '(+ 1 2))
(println (eval demo1))
; => 3
; Particularly in macros it can be useful to
; evaluate a subset of the items inside a syntax quoted list.
; ~ (unquote) and ~@ (unquote-splicing) are used for this.
; ~@ splices in all the items in a collection.
; This doesn't work in quoted lists, only in syntax quoted lists.
; Note that while "quote" is the name of a real function,
; "unquote" and "unquote-splicing" are not.
(let [a 1, b [2 3]]
(println `(+ ~a ~...@b c)))
; =>(clojure.core/+ 1 2 3 user/c)
On Dec 16, 12:12 am, Daniel Eklund <[email protected]> wrote:
> > How did you know that it delegates to 'get'?
>
> sorry, I rushed that part.
>
> the keyword and symbol are instances of clojure.lang.Keyword and
> clojure.lang.Symbol
> which are _java_ classes found in Keyword.java and Symbol.java (I
> found these in the src/jvm directory)
>
> These are what the invoke( ) methods looks like for the single and
> double arity of both these classes (this is _java_ code):
>
> /**
> * Indexer implements IFn for attr access
> *
> * @param obj - must be IPersistentMap
> * @return the value at the key or nil if not found
> * @throws Exception
> */
> public Object invoke(Object obj) throws Exception{
> return RT.get(obj, this);
>
> }
>
> public Object invoke(Object obj, Object notFound) throws Exception{
> return RT.get(obj, this, notFound);
>
> }
>
> -----------------
> which in turn goes to RT.java
>
> static public Object get(Object coll, Object key){
> if(coll == null)
> return null;
> else if(coll instanceof Associative)
> return ((Associative) coll).valAt(key);
> else if(coll instanceof Map)
> {
> Map m = (Map) coll;
> return m.get(key);
> }
> else if(coll instanceof IPersistentSet)
> {
> IPersistentSet set = (IPersistentSet) coll;
> return set.get(key);
> }
> else if(key instanceof Number && (coll instanceof String ||
> coll.getClass().isArray()))
> {
> int n = ((Number) key).intValue();
> if(n >= 0 && n < count(coll))
> return nth(coll, n);
> return null;
> }
>
> return null;}
>
> static public Object get(Object coll, Object key, Object notFound){
> if(coll == null)
> return notFound;
> else if(coll instanceof Associative)
> return ((Associative) coll).valAt(key, notFound);
> else if(coll instanceof Map)
> {
> Map m = (Map) coll;
> if(m.containsKey(key))
> return m.get(key);
> return notFound;
> }
> else if(coll instanceof IPersistentSet)
> {
> IPersistentSet set = (IPersistentSet) coll;
> if(set.contains(key))
> return set.get(key);
> return notFound;
> }
> else if(key instanceof Number && (coll instanceof String ||
> coll.getClass().isArray()))
> {
> int n = ((Number) key).intValue();
> return n >= 0 && n < count(coll) ? nth(coll, n) : notFound;
> }
> return notFound;
>
> }
>
> ---------
>
> all really cool....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---