9
$\begingroup$

Values of a list can be added to an association's values like so,

<|"a" -> 10, "b" -> 10, "c" -> 10|> + {1, 2}

(*  {<|"a" -> 11, "b" -> 11, "c" -> 11|>, <|"a" -> 12, "b" -> 12, "c" -> 12|>} *)

The order does not matter:

{1, 2} + <|"a" -> 10, "b" -> 10, "c" -> 10|>

(*  {<|"a" -> 11, "b" -> 11, "c" -> 11|>, <|"a" -> 12, "b" -> 12, "c" -> 12|>} *)

Except when {1, 2} is replaced by Range[2]:

<|"a" -> 10, "b" -> 10, "c" -> 10|> + Range[2]

(*  {<|"a" -> 11, "b" -> 11, "c" -> 11|>, <|"a" -> 12, "b" -> 12, "c" -> 12|>} *)

Range[2] + <|"a" -> 10, "b" -> 10, "c" -> 10|>

(*  <|"a" -> {11, 12} "b" -> {11, 12} "c" -> {11, 12}|> *)

Here, argument order matters. Trace shows that Range[2] is evaluated first in both cases, so why the different behaviour?

$\endgroup$

1 Answer 1

6
$\begingroup$

It's probably a bug related to packed arrays:

<|"a" -> 10, "b" -> 10, "c" -> 10|> + Developer`ToPackedArray[{1, 2}]
(*  {<|"a" -> 11, "b" -> 11, "c" -> 11|>, <|"a" -> 12, "b" -> 12, "c" -> 12|>}  *)


Developer`ToPackedArray[{1, 2}] + <|"a" -> 10, "b" -> 10, "c" -> 10|>
(*  <|"a" -> {11, 12}, "b" -> {11, 12}, "c" -> {11, 12}|>  *)

It should be reported to WRI.

$\endgroup$
1
  • 1
    $\begingroup$ I reported it to WRI. $\endgroup$ Commented Jun 18 at 19:34

Your Answer

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

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