18
$\begingroup$

I find the following behaviour deeply counter-intuitive:

<|1 -> 123.456 |> /. {x_?NumericQ -> Round[x]}

(* <|1 -> Round[123.456]|> *)

That is, if I make a rule-based replacement of things that sit as values inside an Association object, and I require even a minimal amount of processing on the replacement, then it is just returned unevaluated.

Maybe I'm just completely getting associations wrong, but this is not at all the behaviour I expected. Instead, I would expect this to behave exactly like lists would do in this situation:

{1 -> 123.456} /. {x_?NumericQ -> Round[x]}

(* {1 -> 123} *)

Is this a bug? If not, what causes this behaviour? Is there some clean way to avoid it? I should note that if you copy <|1 -> Round[123.456]|> into a blank cell and run that, it simplifies to <|1 -> 123|>, but passing the result through Evaluate doesn't do the same.

<|1 -> 123.456 |> /. {x_?NumericQ -> Round[x]}
Evaluate[%]

(* <|1 -> Round[123.456]|>
   <|1 -> Round[123.456]|> *)

In case this is a bug, I'm running version 11.0.0 for Linux x86 (64-bit) (July 28, 2016), and I observe the same behaviour in version 10.4.1 for Linux x86 (64-bit) (April 11, 2016).

$\endgroup$
2

1 Answer 1

17
$\begingroup$

Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated. Use RuleCondition (or other options considered in this question) to force the r.h.s. of the rule to evaluate in-place:

<|1 -> 123.456|> /. {x_?NumericQ :> RuleCondition @ Round[x]}

(* <|1 -> 123|> *)

Note that when you enter

<|1 -> Round[123.456]|>

(* <|1 -> 123|> *)

the Round function gets evaluated inside all right, because the evaluation here is inside a constructor, and it evaluates its arguments before constructing an association. But once it is constructed, no further evaluation can normally happen inside it (unless forced in some way, like e.g. the one I suggested above).

$\endgroup$
6
  • $\begingroup$ Thanks for this. I don't understand, though, why running <|1 -> Round[123.456]|> on an empty cell will skip this protection. $\endgroup$ Oct 11, 2016 at 15:36
  • $\begingroup$ @EmilioPisanty Because in this case, you are using a constructor for an Association, and the constructor does evaluate the arguments, before creating it. This is a subtle point, I will add this into the answer. $\endgroup$ Oct 11, 2016 at 15:37
  • $\begingroup$ Huh. Yeah, I don't understand that. What do you mean by constructor? $\endgroup$ Oct 11, 2016 at 15:39
  • $\begingroup$ @EmilioPisanty When you construct Association from pieces, an internal representation of it is created. So, whether you type Association[1->Round[123.456]] or the syntax with brackets, this object is constructed internally, and a reference to it is returned to you. The syntax is a bit deceiving - for assocs, the action of a constructor is non-trivial. $\endgroup$ Oct 11, 2016 at 15:40
  • $\begingroup$ I've no memory of this thread, but it looks interesting. It's been five years since it was posted. Do you know if the are any new methods for solving it which are not undocumented? $\endgroup$ Jun 27, 2022 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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