I am having a keyword list that I want to merge. let’s say we have the following keyword list:
list_1 = [c: [t: [c: ["id_1"]]]]
list_2 = [
c: [
t: [
c: ["id_1"],
c: [
{:b, "id_2", 1},
{:b, "id_1", 1}
]
]
]
When i merge them using Keyword.merge(list_1, list_2)
then i get the right result and that is
[
c: [
t: [
c: ["id_1"],
c: [
{:b, "id_2", 1},
{:b, "id_1", 1}
]
]
]
but let’s say if we have two lists
list_1 = [c: [t: [c: ["id_1"]]]]
list_2 = [c: [t: [c: ["id_2"]]]]
Then after Keyword.merge(list_1, list_2)
then we get this result
[c: [t: [c: ["id_2"]]]]
which should be
c: [t: [c: ["id_1", "id_2"]]]]
which I have achieved through
Keyword.merge(c, p, fn _k, v1, v2 -> Enum.sort(v1 ++ v2) end)
I really don’t know how to get these two features (when a similar key value is found then replace it with a unique value while appending when they are different) in a single block of code.
Any help will be really appreciated.