The following input:
Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]]
Returns {}. Why does it not return {"key" -> Association[]}? I was expecting it to match.
The following input:
Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]]
Returns {}. Why does it not return {"key" -> Association[]}? I was expecting it to match.
To answer the question of why there is no match let's look at the output of Trace:
Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] // Trace
(* {{{{Association[], <||>}, "key" -> <||>, "key" -> <||>},
{"key" -> <||>}}, Cases[{"key" -> <||>}, HoldPattern["key" -> Association[]]], {}} *)
We see that Association[] gets evaluated to <||>, which as Leonid states is not a normal expression and so will not match Association[].
As I suggested in the comments, I think the right approach in this and similar situation where an evaluation might occur with Association[], is to use Verbatim:
Cases[{"key" -> Association[]}, Verbatim["key" -> Association[]]]
(* {"key" -> <||> } *)
If you look at the Trace of the expression, you'll see why it matched.
Cases[{"key" -> Association[]}, Verbatim["key" -> Association[]]] // Trace
Verbatim allows its input to also evaluate, hence both expressions now look the same: "key" -> <||>, hence the match.
Finally, if for some reason you really want to use HoldPattern, then I suggest wrapping your expression in Unevaluated:
Cases[Unevaluated[{"key" -> Association[]}], HoldPattern["key" -> Association[]]]
(* {"key" -> <||> } *)
_ -> Association[] (using Unevaluated is not an option)?
– Ramiro Magno
Apr 19 '16 at 17:33
Unevaluated works for me. Cases[Unevaluated[{_ -> Association[]}], HoldPattern[_ -> Association[]]]
– RunnyKine
Apr 19 '16 at 17:58
Verbatimfor that.HoldPatternis for patterns – RunnyKine Apr 19 '16 at 15:43HoldPatternto treat the rule itself as a pattern, otherwise the rule is interpreted byCases(as discussed here: http://mathematica.stackexchange.com/questions/112333/pattern-matching-for-rules/112335) – Ramiro Magno Apr 19 '16 at 15:53Association[]evaluates to<||>, and even though theFullForms of the two are the same,SameQon them givesFalse, because the constructorAssociation[]does a non-trivial job when evaluated, and the result of it is not a normal expressionAssociation[], but a new atomic object. So, we haveUnevaluated[Association[]] === Association[]producingFalse. Therefore,MatchQ[Association[], HoldPattern[Association[]]]givesFalse, while of courseMatchQ[Association[], HoldPattern[Evaluate@Association[]]]givesTrue. – Leonid Shifrin Apr 19 '16 at 15:58Associationis an atomic expression. But how would you suggest I do the matching withCases? Inspired by yourMatchQexample I can see thatCases[{Association[]}, HoldPattern[Evaluate@Association[]]]matches, but it won't match if the expression containingAssociationis aRule. For instance:Cases[{"key" -> Association[]}, HoldPattern["key" -> Evaluate@Association[]]]won't match. – Ramiro Magno Apr 19 '16 at 16:31Verbatim, as @RunnyKine suggested. Since it does not prevent evaluation of parts of the pattern, you will then compare evaluated / constructedAssociation[]in your expression with a similarly evaluatedAssociation[]inside the pattern. Or, much simpler, if you don't care whether or not association is empty, you could use_Associationpattern, which is matched in both cases. – Leonid Shifrin Apr 19 '16 at 16:39