5
$\begingroup$

<|a -> 2, b -> 3|> == <|b -> 3, a -> 2|> returns False; can anybody explain why? If I am not mistaken here these two associations would be identical from the practical point of view.

$\endgroup$
  • 4
    $\begingroup$ The developers of Mathematica do not agree with you, or they would have given Association the Orderless property. With this property those two associations would indeed be equal. You can use Attributes[Association] to confirm that Association does not have that property. Maybe someone knows why this is, let's see. $\endgroup$ – C. E. Jun 1 '15 at 15:59
  • $\begingroup$ Thanks for the comment. Indeed I was tried to mimic what Options do, which seems to be not caring of the order they are given. $\endgroup$ – Rho Phi Jun 1 '15 at 16:11
  • 3
    $\begingroup$ @Roberto The order of options do matter in the sense that earlier options will override later options. This is actually the reverse of associations where later entries override earlier ones. I am not sure what you are attempting but be careful. $\endgroup$ – Mr.Wizard Jun 1 '15 at 16:14
  • $\begingroup$ It's true that if you write a->something many times you might not realizing that later you are overwriting your assignment, but isn't it the same for variable assignment with =? I mean a=2; b=3; a=something is the same as <| a->2, b->3, a->something |> ... right? $\endgroup$ – Rho Phi Jun 2 '15 at 9:08
  • 1
    $\begingroup$ This is indeed a deliberate design decision. There is an undocumented version of Association that does not care about order: Data`UnorderedAssociation. $\endgroup$ – Stefan R Jun 3 '15 at 19:26
7
$\begingroup$

Perhaps as Pickett describes the developers do not consider these associations Equal since that function performs other conversions like:

Quantity[5, "Percent"] == 0.05
True

But perhaps Equal has not been (properly?) extended to associations yet as 2.0 and 2 are Equal but these are not:

<|a -> 2|> == <|a -> 2.0|>
False

Depending on what you are doing this might be useful, but beware it does not have early exit (short-circuit) behavior:

eq[{x_}] = False;
eq[{x__}] := Equal[x]

one = <|a -> 2, b -> 3|>;
two = <|b -> 3, a -> 2.0|>;

And @@ Merge[{one, two}, eq]
True
$\endgroup$
  • $\begingroup$ Thanks a lot for this detailed investigation of the fact. I think that <|a -> 2|> == <|a -> 2.0|> being False points towards a bug, rather than a feature. $\endgroup$ – Rho Phi Jun 2 '15 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.