Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
An operator that performs a Join of its argument with the sequence of lists it is provided
| ResourceFunction["JoinRest"][list] gives an operator that will join list with the sequence of lists it is given. | 
JoinRest uses Join to join its argument {3,4} with whatever else it encounters, which you may think of as the "Rest" of the Join:
| In[1]:= | ![ResourceFunction["JoinRest"][{3, 4}][{5, 6, 7}, {8, 9, 10, 11}]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/4fbf523d3d03854c.png) | 
| Out[1]= |  | 
JoinRest applied to an Association creates an operator that will add key-value pairs to the Join of the argument it is passed, but only if those joined associations fail to contain a key present in the association to which JoinRest was applied:
| In[2]:= | ![ResourceFunction["JoinRest"][<|"a" -> 0, "b" -> 0, "c" -> 0|>][
 Association["a" -> 7, "b" -> 9]]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/606d889f87682055.png) | 
| Out[2]= |  | 
The operator created by JoinRest using an Association can accept a sequence of associations:
| In[3]:= | ![ResourceFunction["JoinRest"][<|"a" -> 0, "b" -> 0, "c" -> 0|>][
 Association["a" -> 7, "b" -> 9], Association["a" -> -8, "c" -> 3]]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/556a566d0917fabe.png) | 
| Out[3]= |  | 
If JoinRest has no argument, it operates as an identity on the data it is given:
| In[4]:= | ![ResourceFunction["JoinRest"][][Association["a" -> 7, "b" -> 9]]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/0e881c9cadc9afc7.png) | 
| Out[4]= |  | 
If JoinRest has a sequence of associations as its argument, it will use the last value for any key it encounters in that sequence as a basis for adding key-value pairs:
| In[5]:= | ![ResourceFunction["JoinRest"][<|"a" -> 0|>, <|"a" -> 4, "c" -> -2|>][
 Association["b" -> 9], Association["b" -> -8, "c" -> 3]]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/3e73b4117d03290f.png) | 
| Out[5]= |  | 
JoinRest can be right-composed with Counts to create an Association that obtains the number of every anticipated value in an expression:
| In[6]:= | ![Query[Counts/*
   ResourceFunction[
    "JoinRest"][<|"a" -> 0, "b" -> 0, "c" -> 0|>]][{"a", "b", "a", "b", "a", "a", "a", "b"}]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/19ace54777024406.png) | 
| Out[6]= |  | 
JoinRest works similarly to Merge with a First combiner function:
| In[7]:= | ![With[{data = {"a", "b", "a", "b", "a", "a", "a", "b"}, default = <|"a" -> 0, "b" -> 0, "c" -> 0|>}, SameQ[Query[Counts/*ResourceFunction["JoinRest"][default]][data], Merge[{Query[Counts][data], default}, First]
  ]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/3e70e6d1bcf80a09.png) | 
| Out[7]= |  | 
JoinRest is very similar to the resource function JoinMost. In JoinRest its argument is placed first in the Join; in JoinMost its argument is placed last in the Join.
JoinRest, like Join, will fail to evaluate if the heads of the expressions to be joined differ from one another:
| In[8]:= | ![ResourceFunction["JoinRest"][{1, 2, 3}][Association["a" -> 2]]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/5732a60e58d40020.png) | 
| Out[8]= |  | 
Insert JoinRest to a pipeline of operators created by Query, such that the count of various categories that survived and died on the Titanic defaults to 0:
| In[9]:= | ![formatting = Row[{Labeled[#1, "ugly"], Labeled[#2, "nicer"]}, Spacer[6]] &;](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/616cbd114382bfcf.png) | 
| In[10]:= | ![Module[{noMaleSurvivors = Query[Select[Not[#sex === "male" && #survived] &]][
    ExampleData[{"Dataset", "Titanic"}]], uglyQuery, prettyQuery, survivedString = (If[#survived, "survived", "died"] &), f1 = GroupBy[#class &], f2 = GroupBy[#sex &]/*KeySort,
  f3 = Counts/*KeySort,
  joiner = ResourceFunction["JoinRest"][
    Association["survived" -> 0, "died" -> 0]]},
 uglyQuery = Query[f1, f2, f3, survivedString];
 prettyQuery = Query[f1, f2, Insert[f3, joiner, 2], survivedString]; formatting[uglyQuery[noMaleSurvivors], prettyQuery[noMaleSurvivors]]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/337/337050c8-e053-40f9-9a76-8c2224da0720/1f8bf8107c739b8f.png) | 
| Out[10]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License