2
$\begingroup$

Consider this example:

a = <|"x" -> 1, "y" -> 2|>;

a // Query[Association["q" -> (# &)]]
(* <|"q" -> <|"x" -> 1, "y" -> 2|>|> *)

It returns what I expect according to the documentation (see Dataset, under Details):

enter image description here

Now consider the following:

a // Query[Association[("q" -> # &)]]
(* <|"q" -> <|"x" -> 1, "y" -> 2|>|> *)

It returns the same thing. But what does it actually do? Is this documented syntax?

To phrase it another way: Should I treat this as valid usage or as undefined behaviour?

I typed something similar to this by accident. If I saw this code without the possibility to run it, I would say that it is nonsense and it should ideally trigger an error.


Here are the first two things that seemed reasonable to try:

Clear[fun1]
a // Query[Association[fun1]]
(* Association[(GeneralUtilities`SafeConstruct[#1, <|"x" -> 1, "y" -> 2|>] &)[Dataset`CompileSubquery[fun1]]] *)

fun2[val_] := "key" -> val
a // Query[Association[fun2]]
(* <|"key" -> <|"x" -> 1, "y" -> 2|>|> *)

One triggers a bug (??) the other works just like my first example.

$\endgroup$
7
  • $\begingroup$ I probably fail to grasp the full the full extend of the problem, but I would interpret a\\Query[Association[("q" -> # &)]] as Query[Association[Function[{x}, "q" -> x]]][a] (this makes it easier for me to read). That form (as far as I can see) is documented as Query[operator1,...][expr] in the help section under details of Query. I would interpret Function as the operator here. Which would be indentical to your last definition (fun2). $\endgroup$
    – Max1
    Apr 30 '20 at 17:01
  • $\begingroup$ Mhm sorry seems like I was mixing Association[Function[...]] with Function[Association[...]]. Trace output is also insanely long. $\endgroup$
    – Max1
    Apr 30 '20 at 17:07
  • $\begingroup$ @Max1 Yes, what's written there is not a function, f, but a function wrapped in Association, Association[f]. Having Association here is important. Other heads don't behave this way. Also, while this thing has head Association, it is not an association (AssociationQ). It may become one after the contents are evaluated. If it does, it behaves just just the <|"key" -> operator|> form. If not, it returns some internal ugliness. $\endgroup$
    – Szabolcs
    Apr 30 '20 at 17:17
  • $\begingroup$ Yes I absolutely overlooked that. One thing which looked interesting to me is the very last part of the Trace of Query[Association[Function[{x}, "q" -> x]]][a]. One of the very last expressions is (GeneralUtilities`SafeConstruct[#1, <|"x" -> 1, "y" ->2|>] &) /@ Association[Dataset`CompileSubquery[Function[{x}, "q" -> x]]]. Since I'm not a mathematica wizard that looks like #[<|"x" -> 1, "y" -> 2|>] & /@ Association[Function[{x}, "q" -> x]] to me with some internal mumbo jumbo. I realize tho that your main point is if this is documented or some kind of undefined behaviour. $\endgroup$
    – Max1
    Apr 30 '20 at 17:48
  • 1
    $\begingroup$ I think this is something that works accidentally. $\endgroup$
    – jose
    May 9 '20 at 1:52

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.