This question is a follow up from my previous question.
Query
has a nice syntax that allows it to apply functions on association based on specific Keys
.
assoc=<|"a"->{1,3,2,9,4},"b"->{6,1,8},"c"->{3,2,8,9,8},"d"->{5},"e"->{5,3}|>;
Query[{"b"->f,"a"->g}]@assoc
<|"a"->g[{1,3,2,9,4}],"b"->f[{6,1,8}],"c"->{3,2,8,9,8},"d"->{5},"e"->{5,3}|>
Or even chain functions (right to left) on specific Keys
.
Query[{"b"->f,"a"->g,"b"->h}]@assoc
<|"a"->g[{1,3,2,9,4}],"b"->f[h[{6,1,8}]],"c"->{3,2,8,9,8},"d"->{5},"e"->{5,3}|>
Note to use this functionality of Query
on Numeric
Keys one has to be explicit otherwise it has the interpretation of Part
index for Integers
:
assoc=<|1->{1,3,2,9,4},"b"->{6,1,8},2.1->{3,2,8,9,8},2->{5},"e"->{5,3}|>;
Query[{Key[2.1]->f,"b"->g,Key[1]->h,2->k}]@assoc
<|1->h[{1,3,2,9,4}],"b"->g[k[{6,1,8}]],2.1`->f[{3,2,8,9,8}],2->{5},"e"->{5,3}|>
Now my question is there a way to make this work for general patterns:
Query[{"a"|"b"|"c"->f,_->g,Except["b"]->h}]@assoc