last day (15 days later) » 

19:14
0
A: How to organically merge nested associations?

alancalvittiTry this, we're using it on a daily basis Nest[Merge,f,n] To your starting data, slightly modified (strings vs symbols): peopleFacts = <|"alice" -> <|"age" -> 29, "shoeSize" -> 7|>, "bob" -> <|"age" -> 27, "sex" -> "male"|>|>; And new facts: newFacts = <| "steve" -> <|"hairColor" -> ...

what is f and n here?
Nest[Merge, expr, n] gives an expression with Merge applied n times to f.
You can assume I am aware of documentation :) But where do I put the initial association and the update association?
Use it as an operator, no different than using Merge by itself or iterated Merge[Merge[...]]
Could you take the example from my answer and demonstrate?
19:14
@Kuba, see edits
@alancalvitti thanks but I'm still missing something, try:
{<|bob -> <|age -> 27, sex -> male,  hobby -> <|mma -> 1, mo -> 1|>|>|>, <|bob -> <|age -> 22,  hobby -> <|mo -> 2|>|>|>} // Nest[Merge, Last, 2]
returns
<|bob -> <|age -> 22, sex -> male, hobby -> <|mo -> 2|>|>|>
{<|bob -> <|age -> 27, sex -> male,
     hobby -> <|mma -> 1, mo -> 1|>|>|>, <|bob -> <|age -> 22,
     hobby -> <|mo -> 2|>|>|>} // MergeNested
returns
<|bob -> <|age -> 22, sex -> male, hobby -> <|mma -> 1, mo -> 2|>|>|>
I don't think mma->1 should have been dropped.
so it won't work if the nested association is 'ragged'.
20:06
I pointed out ragged Associations are an issue but didn't consider that these come up naturally in the OP's setting.
Try this though: merge[f_][as : {_Association ..}] := Merge[f][as];
merge[f_][nas : {Except[_Association] ..}] := f[nas];
Probably a recursive approach would be more robust, using a similar check on the Heads of objects being merged
20:42
@alancalvitti right, clearly I wasn't paying attention. Don't know if it matters performance wise but yes, if on often works with the same structure it can be useful. Thanks for taking your time to add explanation.
20:55
So does the approach above work in all cases considered by OP? If so I'll add it to my answer.

last day (15 days later) »