I don't have enough experiences of using Association or Dataset, and when operating with nested associations I meet the following choices.
Consider a two-level association to store members with several propertys, and Length@member is much larger than Length@property. The member keys will be added/deleted by other functions, while the property keys are fixed.
Question1: Should I use long assoc of short assoc, or short of long? Which one is recommended? (I guess long of short is better.)
To be concrete,
max=100;
level1=4;
level2=10;
data[1]=Association@Table[
"property"<>ToString@j->
Association@Table[
"member"<>ToString@i->Table[RandomInteger[max],RandomInteger[i]],
{i,1,level2}
],
{j,1,level1}
]
generates a $4\times 10$ association like
<|member1-><|property1->{},property2->{66},property3->{},property4->{58,26,10,49}|>,member2-><|property1->{5},property2->{17},property3->{12,47},property4->{44,82}|>,member3-><|property1->{},property2->{5},property3->{35},property4->{65,67}|>,member4-><|property1->{18},property2->{95},property3->{38},property4->{57,47,64,14}|>,member5-><|property1->{43},property2->{71,43},property3->{},property4->{85,6,75}|>,member6-><|property1->{},property2->{53,21},property3->{},property4->{12,65,83}|>,member7-><|property1->{},property2->{51},property3->{7},property4->{}|>,member8-><|property1->{},property2->{},property3->{41,90},property4->{}|>,member9-><|property1->{},property2->{82,32},property3->{60,94},property4->{73,18,48}|>,member10-><|property1->{},property2->{3},property3->{80,83,78},property4->{37,1,56,66}|>|>
assoc//Dataset
Question1`: Is there a standard/recommended function to Transpose nested associations?
This can be easily done like assoc//Dataset//Transpose//Normal and is discussed before, e.g. Association of Associations : how to permute level 1 and level 2 keys?. But since it's a bit old, what is the current status of this question? Which way is more efficient?
Question2: how to protect Values against modification?
Now the Values are actually list, set or some other data structure. Suppose there are some functions to modify them like add/delete/reset, like
add//Attributes={HoldFirst};
add[assoc_,member_,property_][list_]:=
Module[{},
assoc[member,property]=
Join[assoc[member,property],list];
];
I want some Values in assoc to be protected against add, and add should be like
add//Attributes={HoldFirst};
add[assoc_,member_,property_][list_]:=
Module[{},
checkProtection[assoc,member,property];
assoc[member,property]=
Join[assoc[member,property],list];
];
So I need some place to store the protected triplets assoc,member,property and there are at least two choices:
- save them into
Valueslike
assoc["member1","property1"]=<|"data"->mylist,"protected"->True|>
In this way assoc is a three-level association.
- save them into a new symbol like
protected//Attributes={HoldFirst};
protected[assoc]^={{"member1","property1"},...}
Which one is better?

Is there a standard/recommended function to Transpose nested (ragged) associations?could be a question on its own if presented with a minimal example. Or you may choose to wait for an answer. Regards. – Syed Jan 05 '23 at 06:58