7
$\begingroup$

FilterRules is a handy function that works on associations, however it does not respect delayed association-values. RuleDelayed is allowed in associations, and they are not evaluated prematurely. Neither are delayed rules when a rule-list is supplied. I wonder what FilterRules does to an Association instead of simply applying Normal to it in the first place (which it can freely do, as the result is always a list and not an association). Do you think this is a bug? Or is there a deeper reason I failed to discover?

ClearAll[a, b];
FilterRules[{a :> Print["Evaluated"], b -> 1}, a]
(* {a :> Print["Evaluated"]} *)

FilterRules[Normal[<|a :> Print["Evaluated"], b -> 1|>], a]
(* {a :> Print["Evaluated"]} *)

FilterRules[<|a :> Print["Evaluated"], b -> 1|>, a]
(* During evaluation of In[63]:= Evaluated *)
(* {a -> Null} *)
$\endgroup$
  • $\begingroup$ Fortunately something designed for associations, works well: KeyTake[<|a :> Print["Evaluated"]|>, a] $\endgroup$ – Kuba Mar 1 '16 at 7:38
  • $\begingroup$ @Kuba, that's true, but unfortunately KeyTake can not handle a pattern as a second argument correctly, e.g. a|c. By "correctly" I mean "how I would expect". Which also goes for the behaviour of the association iside FilterRules: "unexpected by me". $\endgroup$ – István Zachar Mar 1 '16 at 8:02
  • 1
    $\begingroup$ Submitted to TechSupport. $\endgroup$ – István Zachar Mar 8 '16 at 15:31

Your Answer

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

Browse other questions tagged or ask your own question.