Merging two nested Keyword lists

I have two keyword lists

list 1:

[ j: [ 
 u: [{:b, "a", 1}], 
 u: [ {:b, "b", 1}, {:b, "c", 1}, {:b, "d", 2} ] ] 
]

list 2:

[ j: [ 
 u: [{:b, "e", 1}], 
 u: [ {:b, "f", 1}, {:b, "g", 1}, {:b, "d", 2} ] ] 
]

The final result should be the merge of these two lists. If any value in {:b, identifier, value} is greater than the corresponding value then it should be replaced. The final output should look like

[ j: [ 
 u: [{:b, "a", 1}, {:b, "e", 1}], 
 u: [ {:b, "b", 1}, {:b, "c", 1}, {:b, "f", 1}, {:b, "g", 1}, {:b, "d", 2} ] ] 
]

As you can see in list 1 and 2 {:b, “d”, value} identifier is “d” hence only the larger number will be merged

I have tried Keyword.merge but didn’t helped much,

Thanks

I don’t think you can do such custom logic in the standard functions. Especially not with both keywords being u. I find that rather odd to have two apparently separate things having the same key.

My first impression is to merge each ‘u’ separately and then filter out any duplicate identifiers based on you logic.

ok so i tried the same query on my original question and it works too.

Keyword.merge(list1, list2, fn _k, v1, v2 -> v1 ++ v2 end)

Thanks

1 Like