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):
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.
a\\Query[Association[("q" -> # &)]]
asQuery[Association[Function[{x}, "q" -> x]]][a]
(this makes it easier for me to read). That form (as far as I can see) is documented asQuery[operator1,...][expr]
in the help section under details ofQuery
. I would interpretFunction
as the operator here. Which would be indentical to your last definition (fun2
). $\endgroup$Association[Function[...]]
withFunction[Association[...]]
.Trace
output is also insanely long. $\endgroup$f
, but a function wrapped in Association,Association[f]
. HavingAssociation
here is important. Other heads don't behave this way. Also, while this thing has headAssociation
, 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$Trace
ofQuery[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$