Comparing Dictionaries

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

Comparing Dictionaries

Barry Carr
Hello All,

Does Object>>= work on collections, specifically Dictionaries? Reading the
comment for = I suspect that it won't work. Is this correct?
Failing the above, does Set>>equals: work on dictionaries, and if not could
anyone advise me on what the best way to compare two dictionaries is?

Cheers

Barry Carr
Ixian Software Components Ltd
Blairgowrie
Perthshire


Reply | Threaded
Open this post in threaded view
|

Re: Comparing Dictionaries

Ian Bartholomew-17
Barry,

> Does Object>>= work on collections, specifically Dictionaries? Reading the
> comment for = I suspect that it won't work. Is this correct?

Yes and No. :-)

#= does work sensibly for some collections e.g. SequenceableCollection and
it's subclasses which override Object>>=.

It doesn't work as expected for Sets (Dictionaries) or Bags which use the
Object implementation.

> Failing the above, does Set>>equals: work on dictionaries,

Not really. It only compares the Dictionaries values,  and gets a bit
confused about that as it is expecting to be comparing Sets whose size will
match.
Probably not what you want :-)

a := Dictionary new
    at: $a put: 1;
    at: $b put: 1;
    at: $c put: 1;
    yourself.
b := Dictionary new
    at: $x put: 1;
    at: $y put: 2;
    at: $z put: 3;
    yourself.
a equals: b ==> true

> and if not could
> anyone advise me on what the best way to compare two dictionaries is?

Depends a bit on what you mean by compare (just keys, just values, keys and
values, matching keys and values) but the easiest "one liners" I could think
of that answers true if the keys and values in two Dictionaries match
completely is -

a := Dictionary new
    at: $c put: 3;
    at: $b put: 2;
    at: $a put: 1;
    yourself.
b := Dictionary new
    at: $a put: 1;
    at: $b put: 2;
    at: $c put: 3;
    yourself.

a associations asSet equals: b associations asSet

or

a associations asSortedCollection = b associations asSortedCollection

Neither is too efficient, and the second obviously has some restrictions,
but from my *limited* testing they appear to work?

Regards
    Ian


Reply | Threaded
Open this post in threaded view
|

Re: Comparing Dictionaries

Barry Carr-3
Hi Ian,

Thanks for your help. Thats much clearer now.

Cheers

Barry

"Ian Bartholomew" <[hidden email]> wrote in
news:w%_b9.3033$J47.244785@stones:

> Barry,
>
>> Does Object>>= work on collections, specifically Dictionaries?
>> Reading the comment for = I suspect that it won't work. Is this
>> correct?
>
> Yes and No. :-)
>
> #= does work sensibly for some collections e.g. SequenceableCollection
> and it's subclasses which override Object>>=.
>
> It doesn't work as expected for Sets (Dictionaries) or Bags which use
> the Object implementation.
>
>> Failing the above, does Set>>equals: work on dictionaries,
>
> Not really. It only compares the Dictionaries values,  and gets a bit
> confused about that as it is expecting to be comparing Sets whose size
> will match.
> Probably not what you want :-)
>
> a := Dictionary new
>     at: $a put: 1;
>     at: $b put: 1;
>     at: $c put: 1;
>     yourself.
> b := Dictionary new
>     at: $x put: 1;
>     at: $y put: 2;
>     at: $z put: 3;
>     yourself.
> a equals: b ==> true
>
>> and if not could
>> anyone advise me on what the best way to compare two dictionaries is?
>
> Depends a bit on what you mean by compare (just keys, just values,
> keys and values, matching keys and values) but the easiest "one
> liners" I could think of that answers true if the keys and values in
> two Dictionaries match completely is -
>
> a := Dictionary new
>     at: $c put: 3;
>     at: $b put: 2;
>     at: $a put: 1;
>     yourself.
> b := Dictionary new
>     at: $a put: 1;
>     at: $b put: 2;
>     at: $c put: 3;
>     yourself.
>
> a associations asSet equals: b associations asSet
>
> or
>
> a associations asSortedCollection = b associations asSortedCollection
>
> Neither is too efficient, and the second obviously has some
> restrictions, but from my *limited* testing they appear to work?
>
> Regards
>     Ian
>
>