;; persistent maps only
(instance? clojure.lang.IPersistentMap %)

;; all Java maps
(instance? java.util.Map %)

On 24 December 2013 04:28, larry google groups
<lawrencecloj...@gmail.com> wrote:
> I find this surprising. I do this:
>
> (supers %)
>
> inside of my :post condition, and I get:
>
> java.lang.ClassCastException: clojure.lang.PersistentHashMap cannot be cast
> to java.lang.Class
> at clojure.core$bases.invoke(core.clj:4985)
> at clojure.core$supers.invoke(core.clj:4994)
> at admin.secretary$fetch.invoke(secretary.clj:327)
>
> How else do I find what interfaces the return value might be implementing?
>
>
>
>
>
> On Monday, December 23, 2013 10:04:54 PM UTC-5, larry google groups wrote:
>>
>> > enforce that the return is a subtype of java.util.Map rather than
>> > checking for a specific concrete class of map.
>>
>>
>> Thank you. I'll do that. All the same, can anyone tell me why this
>> function changes the type of the value? All it does is call "fetch". The
>> fetch function has this post condition:
>>
>>    :post [(= (type %) clojure.lang.PersistentHashMap)]}
>>
>> And "get-distinct" has the same restriction, but in get-distinct I get
>> this error:
>>
>> java.lang.AssertionError: Assert failed: (= (type %)
>> clojure.lang.PersistentHashMap)
>> at admin.controller$get_distinct.invoke(controller.clj:40)
>>
>> The :post condition of "fetch" does not throw an error, so I know that
>> "fetch" is returning clojure.lang.PersistentHashMap.  However, when I print
>> the return type of get-distinct, I am amazed to see that it is now
>> clojure.lang.PersistentArrayMap. This function does almost nothing, so I am
>> surprised it changes the concrete implementation type of the return value.
>>
>>
>>
>>
>>
>>
>> On Monday, December 23, 2013 3:43:09 AM UTC-5, Cedric Greevey wrote:
>>>
>>> The two classes have essentially the same semantics, but performance
>>> differences, which is why Clojure sometimes uses one and sometimes the
>>> other. If you want to enforce that a map is returned, enforce that the
>>> return is a subtype of java.util.Map rather than checking for a specific
>>> concrete class of map.
>>>
>>>
>>> On Sun, Dec 22, 2013 at 3:07 PM, larry google groups
>>> <lawrenc...@gmail.com> wrote:
>>>>
>>>> Hmm, I see. get-distinct was returning an empty lazyseq, which
>>>> apparently made the difference.
>>>>
>>>>
>>>> On Sunday, December 22, 2013 2:56:01 PM UTC-5, larry google groups
>>>> wrote:
>>>>>
>>>>> Hmm, the different return types seem tied to the 2 different functions
>>>>> being called, but both functions have the same return type, which is a
>>>>> lazyseq. I am using Monger to get data from MongoDb. These functions are
>>>>> private:
>>>>>
>>>>> (defn- get-distinct [request]
>>>>>   {:pre [(= (type request) clojure.lang.PersistentHashMap)]
>>>>>    :post [(= (type %) clojure.lang.LazySeq)]}
>>>>>   (monger/get-distinct (:item-type request)))
>>>>>
>>>>> (defn- paginate-results [request]
>>>>>   {:pre [ (= (type request) clojure.lang.PersistentHashMap)]
>>>>>    :post [(= (type %) clojure.lang.LazySeq)]}
>>>>>   (monger/paginate-results (:item-type request) (if (:which-page
>>>>> request)
>>>>>                                                   (:which-page request)
>>>>>                                                   0)))
>>>>>
>>>>> Both of these functions return lazyseqs, as expected. The results from
>>>>> both get run through a (reduce) function that does some minor filtering. 
>>>>> Yet
>>>>> in one case the return type (from the reduce function) is
>>>>> clojure.lang.PersistentArrayMap and in the other it is
>>>>> clojure.lang.PersistentHashMap. I'd like to be able to write a :post
>>>>> condition that enforces strictness, but that seems impossible because I 
>>>>> can
>>>>> not figure out what the rule is that handles the conversion. I don't care 
>>>>> if
>>>>> the return type is clojure.lang.PersistentArrayMap or
>>>>> clojure.lang.PersistentHashMap, all I want is it for it be consistently 
>>>>> one
>>>>> or the other.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Sunday, December 22, 2013 2:31:45 PM UTC-5, larry google groups
>>>>> wrote:
>>>>>>
>>>>>>
>>>>>> I am surprised that a map literal is clojure.lang.PersistentArrayMap
>>>>>> but as soon as I assign it to a var, it becomes
>>>>>> clojure.lang.PersistentHashMap. Are there any rules for being able to
>>>>>> predict when these conversions occur?
>>>>>>
>>>>>> user> (type {})
>>>>>> clojure.lang.PersistentArrayMap
>>>>>>
>>>>>> user> (type {:what "why?"})
>>>>>> clojure.lang.PersistentArrayMap
>>>>>>
>>>>>> user> (def curious {:what "why?"})
>>>>>> #'user/curious
>>>>>>
>>>>>> user> (type curious)
>>>>>> clojure.lang.PersistentHashMap
>>>>>>
>>>>>> user> (def sug (assoc curious :whodoneit "mikey"))
>>>>>> #'user/sug
>>>>>>
>>>>>> user> (type sug)
>>>>>> clojure.lang.PersistentHashMap
>>>>>>
>>>>>> I am curious because I wrote a (reduce) function which mostly just
>>>>>> builds a map:
>>>>>>
>>>>>>      (assoc map-of-data (:item-name next-item) next-item))
>>>>>>
>>>>>> Since I was using assoc I was certain I would get
>>>>>> clojure.lang.PersistentHashMap back, but instead I got
>>>>>> clojure.lang.PersistentArrayMap.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>> --
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To post to this group, send email to clo...@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+u...@googlegroups.com
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/clojure?hl=en
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to clojure+u...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to