On Nov 6, 8:27 am, "Michael Wood" <[EMAIL PROTECTED]> wrote:
> On Thu, Nov 6, 2008 at 8:25 AM, Chanwoo Yoo <[EMAIL PROTECTED]> wrote:
>
> > Hi all. In Stuart's book - Programming Clojure, there is a multi
> > method like following:
>
> > (defmulti blank? class)
> > (defmethod blank? String [s] (every? #{\space} s))
> > (defmethod blank? nil [_] true)
>
> > After reading the method, I was curious about type or class of native
> > data structures of Clojure. So I typed next code in slime. Hmm.. I
> > didn't understand why classes of {:a 1} and {} are not same. Like
> > Stuart's code, to make multi method branching based on clojure data
> > structure type(map, vector, list), what symbol should I use? (Like
> > String for "string", Is there a Map for {:a 1}? I tried
> > PersistentHashMap, HashMap, Map, so on.. I didn't find it.)
>
> I think what you're looking for is "clojure.lang.PersistentHashMap"
> instead of just "PersistentHashMap".
>
> user=> String
> java.lang.String
> user=> PersistentHashMap
> java.lang.Exception: Unable to resolve symbol: PersistentHashMap in this 
> context
> [...]
> user=> clojure.lang.PersistentHashMap
> clojure.lang.PersistentHashMap
> user=>
>
> I don't know much about Clojure, though, so I suspect there is better
> advice than the above :)
>
> > user> (= (class "abc") String)
> > true
> > user> (class {:a 1 :b 2})
> > #=clojure.lang.PersistentHashMap
> > user> (class {})
> > #=clojure.lang.PersistentHashMap
> > user> (= (class {:a 1}) (class {}))
> > false
> > user> (= (class {:a 1}) (class {:b 2}))
> > true
>
> That seems rather strange to me too.
>
> --
> Michael Wood <[EMAIL PROTECTED]>



On Nov 6, 8:27 am, "Michael Wood" <[EMAIL PROTECTED]> wrote:
> On Thu, Nov 6, 2008 at 8:25 AM, Chanwoo Yoo <[EMAIL PROTECTED]> wrote:
>
> > Hi all. In Stuart's book - Programming Clojure, there is a multi
> > method like following:
>
> > (defmulti blank? class)
> > (defmethod blank? String [s] (every? #{\space} s))
> > (defmethod blank? nil [_] true)
>
> > After reading the method, I was curious about type or class of native
> > data structures of Clojure. So I typed next code in slime. Hmm.. I
> > didn't understand why classes of {:a 1} and {} are not same. Like
> > Stuart's code, to make multi method branching based on clojure data
> > structure type(map, vector, list), what symbol should I use? (Like
> > String for "string", Is there a Map for {:a 1}? I tried
> > PersistentHashMap, HashMap, Map, so on.. I didn't find it.)
>
> I think what you're looking for is "clojure.lang.PersistentHashMap"
> instead of just "PersistentHashMap".
>
> user=> String
> java.lang.String
> user=> PersistentHashMap
> java.lang.Exception: Unable to resolve symbol: PersistentHashMap in this 
> context
> [...]
> user=> clojure.lang.PersistentHashMap
> clojure.lang.PersistentHashMap
> user=>
>
> I don't know much about Clojure, though, so I suspect there is better
> advice than the above :)

If you are just playing or if the class is only used once in your file/
namespace then this is absolutely okay (clojure does this in
boot.clj) :).
Or you type (import '(clojure.lang PersistentHasMap)),
or (ns my-namespace (:import (clojure.lang PersistentHashMap))) on top
of your file.

> > user> (= (class "abc") String)
> > true
> > user> (class {:a 1 :b 2})
> > #=clojure.lang.PersistentHashMap
> > user> (class {})
> > #=clojure.lang.PersistentHashMap
> > user> (= (class {:a 1}) (class {}))
> > false
> > user> (= (class {:a 1}) (class {:b 2}))
> > true
>
> That seems rather strange to me too.

This is a reader optimization. For very short hashmaps (currently 1
element), the clojure reader uses a clojure.lang.PersistentArrayMap
instead of a full fledged HashMap. The reader does this because he
knows that this map ({:a 1}) will only be constructed with one
MapEntry.

user> (= (class (into {} '([:a 1]))) (class {}))
true
Here the reader doesn't see any literal maps and (into {} '([:a 1]))
evals to a PersistentHashMap with one MapEntry.

Multimethods use the isa? function for dispatch, so to dispatch on a
Map type in general, one would use a java.util.Map or a
clojure.lang.IPersistentMap as the dispatch-value.

user> (and (isa? (class {}) java.util.Map) (isa? (class {:a 1})
java.util.Map))
true

There is also a similar optimization for clojure vectors. They are
constructed as c.l.LazilyPersistentVector which is basically a wrapper
arround a java array. Once you conj onto them, they turn into a
c.l.PersistentVector.

user> (let [v [1 2]] [(class v) (class (conj v 3))])
[#=clojure.lang.LazilyPersistentVector
#=clojure.lang.PersistentVector]

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