#collect:as:Dictionary

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

#collect:as:Dictionary

Peter Uhnak
Hi,

is this intentional behavior?

#(a b c) collect: [ :each | each -> each first asciiValue ] as: Dictionary.
"a Dictionary(1->#a->97 2->#b->98 3->#c->99 )"

(#(a b c) collect: [ :each | each -> each first asciiValue ]) asDictionary.
"a Dictionary(#a->97 #b->98 #c->99 )"

I would expect that collect:as: for Dictionaries would act in the same way as #asDictionary.

{eter
Reply | Threaded
Open this post in threaded view
|

Re: #collect:as:Dictionary

Marcus Denker-4

> On 23 Oct 2015, at 07:12, Peter Uhnák <[hidden email]> wrote:
>
> Hi,
>
> is this intentional behavior?
>
> #(a b c) collect: [ :each | each -> each first asciiValue ] as: Dictionary.
> "a Dictionary(1->#a->97 2->#b->98 3->#c->99 )"
>
> (#(a b c) collect: [ :each | each -> each first asciiValue ]) asDictionary.
> "a Dictionary(#a->97 #b->98 #c->99 )"
>
> I would expect that collect:as: for Dictionaries would act in the same way as #asDictionary.
>
Yes, I would expect the same… asDictionary is fairly recent, though. Before that, any kind
of conversion would have been fine.

        Marcus


Reply | Threaded
Open this post in threaded view
|

memory question

wernerk
Hi,
i'd like to test how much memory a dictionary uses. i guess i could
simply delete that dictionary and see how much memory the garbage
collector releases, but i wonder how i could measure that without
destroying that dictionary?
werner

Reply | Threaded
Open this post in threaded view
|

Re: memory question

wernerk
sorry, wrong thread, that was unintentional. werner

On 10/23/2015 01:24 PM, Werner Kassens wrote:
> Hi,
> i'd like to test how much memory a dictionary uses. i guess i could
> simply delete that dictionary and see how much memory the garbage
> collector releases, but i wonder how i could measure that without
> destroying that dictionary?
> werner
>
>

Reply | Threaded
Open this post in threaded view
|

Re: memory question

NorbertHartl

> Am 23.10.2015 um 13:29 schrieb Werner Kassens <[hidden email]>:
>
> sorry, wrong thread, that was unintentional. werner
>
> On 10/23/2015 01:24 PM, Werner Kassens wrote:
>> Hi,
>> i'd like to test how much memory a dictionary uses. i guess i could
>> simply delete that dictionary and see how much memory the garbage
>> collector releases, but i wonder how i could measure that without
>> destroying that dictionary?
>> werner

Try

Dictionary new sizeInMemory

Norbert



Reply | Threaded
Open this post in threaded view
|

Re: memory question

abergel
Well, this is not enough. This #sizeInMemory only tells you the size of the object, which is simply the number of variables * 4 + size of the header.
For collection, such as OrderedCollection and Dictionary, it is worth asking #size and #capacity:
        - #size tells you the amount of objects kept in the collection
        - #capacity tells you the maximum size the collection can hold without triggering a grow operation.

For example:
Dictionary new sizeInMemory => 16

d := Dictionary new.
d at: #foo put: 1.
d sizeInMemory
=> 16

d := Dictionary new.
d at: #foo put: 1.
d capacity
=> 5

d := Dictionary new.
1 to: 1000 do: [ :i | d at: i put: i ].
d capacity
=> 2213

d := Dictionary new.
1 to: 1000 do: [ :i | d at: i put: i ].
d size
=> 1000

Alexandre




> On Oct 23, 2015, at 9:06 AM, Norbert Hartl <[hidden email]> wrote:
>
>
>> Am 23.10.2015 um 13:29 schrieb Werner Kassens <[hidden email]>:
>>
>> sorry, wrong thread, that was unintentional. werner
>>
>> On 10/23/2015 01:24 PM, Werner Kassens wrote:
>>> Hi,
>>> i'd like to test how much memory a dictionary uses. i guess i could
>>> simply delete that dictionary and see how much memory the garbage
>>> collector releases, but i wonder how i could measure that without
>>> destroying that dictionary?
>>> werner
>
> Try
>
> Dictionary new sizeInMemory
>
> Norbert
>
>
>

--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.




Reply | Threaded
Open this post in threaded view
|

Re: memory question

wernerk
Thank you Norbert and Alexandre,
#sizeInMemory was obviously what i was looking for. and <grin> the hint
about the little complication in a collection that can grow, was helpful
indeed!
werner

Reply | Threaded
Open this post in threaded view
|

Re: #collect:as:Dictionary

Nicolai Hess-3-2
In reply to this post by Marcus Denker-4


2015-10-23 8:33 GMT+02:00 Marcus Denker <[hidden email]>:

> On 23 Oct 2015, at 07:12, Peter Uhnák <[hidden email]> wrote:
>
> Hi,
>
> is this intentional behavior?
>
> #(a b c) collect: [ :each | each -> each first asciiValue ] as: Dictionary.
> "a Dictionary(1->#a->97 2->#b->98 3->#c->99 )"
>
> (#(a b c) collect: [ :each | each -> each first asciiValue ]) asDictionary.
> "a Dictionary(#a->97 #b->98 #c->99 )"
>
> I would expect that collect:as: for Dictionaries would act in the same way as #asDictionary.
>
Yes, I would expect the same… asDictionary is fairly recent, though. Before that, any kind
of conversion would have been fine.

        Marcus



I would not change this.
The first just collects each entry and builds an association with the index,
and the second expects a list of assocations.
They have different behaviors if the list does not contains (or does not only contains) associations:

{  $a -> 1 . $b -> 2. 'not_an_association' } collect:#yourself as:Dictionary. "works - I would not expect an error"
 
{  $a -> 1 . $b -> 2. 'not_an_association' } asDictionary "throws an error - as I would expect it "
 

Reply | Threaded
Open this post in threaded view
|

Re: #collect:as:Dictionary

Peter Uhnak
What is the practical value of creating a dictionary from non-association objects? That seems to me like the programmer made a mistake and forgot something. But maybe you do it regularly, I can only speak for myself.

However consider also this:

 #(a b c) collect: [ :each | each -> each first asciiValue ]  as: Dictionary. "a Dictionary(1->#a->97 2->#b->98 3->#c->99 )"
(#(a b c) collect: [ :each | each -> each first asciiValue ]) as: Dictionary. "a Dictionary(#a->97 #b->98 #c->99 )"


Peter

On Sat, Oct 24, 2015 at 12:13 PM, Nicolai Hess <[hidden email]> wrote:


2015-10-23 8:33 GMT+02:00 Marcus Denker <[hidden email]>:

> On 23 Oct 2015, at 07:12, Peter Uhnák <[hidden email]> wrote:
>
> Hi,
>
> is this intentional behavior?
>
> #(a b c) collect: [ :each | each -> each first asciiValue ] as: Dictionary.
> "a Dictionary(1->#a->97 2->#b->98 3->#c->99 )"
>
> (#(a b c) collect: [ :each | each -> each first asciiValue ]) asDictionary.
> "a Dictionary(#a->97 #b->98 #c->99 )"
>
> I would expect that collect:as: for Dictionaries would act in the same way as #asDictionary.
>
Yes, I would expect the same… asDictionary is fairly recent, though. Before that, any kind
of conversion would have been fine.

        Marcus



I would not change this.
The first just collects each entry and builds an association with the index,
and the second expects a list of assocations.
They have different behaviors if the list does not contains (or does not only contains) associations:

{  $a -> 1 . $b -> 2. 'not_an_association' } collect:#yourself as:Dictionary. "works - I would not expect an error"
 
{  $a -> 1 . $b -> 2. 'not_an_association' } asDictionary "throws an error - as I would expect it "