I would like to use JoinAcross to combine several lists of associations. Extending an example from the documentation, I'd like to do this:
JoinAcrossList[{{<|a -> 1, b -> X|>}, {<|a -> 1, c -> Y|>}, {<|a -> 1,
e -> Z|>}}, Key[a]]
Of course, this fails. I have a way of doing this but am wondering if there is anything built-in that would do it or perhaps a more efficient way.
My solution is to use this:
JoinAcrossList[{{<|a -> 1, b -> X|>}, {<|a -> 1, c -> Y|>}, {<|a -> 1, e -> Z|>}}, Key[a]]
Where
JoinAcrossList[associations_List, joinKey_] := Fold[JoinAcross[#1, #2, joinKey] &, associations[[1]], Drop[associations, 1]];
The above example gives this:
In[]:= JoinAcrossList[{{<|a -> 1, b -> X|>}, {<|a -> 1,
c -> Y|>}, {<|a -> 1, e -> Z|>}}, Key[a]]
Out[]= {<|a -> 1, b -> X, c -> Y, e -> Z|>}
Which is the desired result. It also works for the case where the list contains only two elements such as this example:
JoinAcrossList[{{<|a -> 1, b -> X|>}, {<|a -> 1, c -> Y|>}}, Key[a]]
As I write this, I realize that a more efficient combination would probably split this into multiple pairs and then perform successive combinations of those. Would be nice to have a built-in function for this if the number of elements in the list is large.
I can see how to divide a list of elements into pairs using something like the following but not yet sure how to evaluate starting at the lowest elements and then building back up.
NestWhile[Partition[#, 2, 2, {1, 1}, {}] &, Range[11],Ceiling[Length[#]/2] != 1 &]
JoinAcrossList
? $\endgroup$Fold[f, list]
is equivalent toFold[f, First[list], Rest[list]]
. $\endgroup$