Merge association lists

I have two association lists with the same key

let foo = toMap { a = [1, 2] }
let bar = toMap { a = [2, 3] }

If I try to combine them using #, I get that foo # bar equals bar and the last value for the a key is chosen.

Is there a way to merge foo and bar to obtain toMap {a = [1, 2, 3] }?

I guess the answer is no due to https://docs.dhall-lang.org/discussions/Design-choices.html?highlight=association#no-dictionaries-maps-hashes

After using toMap, it’s no longer possible to compare the keys. Though you can merge all the values, for example if your association list only contains the a key, then this could work:

let Prelude = https://prelude.dhall-lang.org/package.dhall

let --| The nub function removes duplicate elements from a list.
    nub =
      let elem =
            \(x : Natural) ->
            \(xs : List Natural) ->
              Prelude.List.any Natural (Prelude.Natural.equal x) xs

      in  \(list : List Natural) ->
            Prelude.List.fold
              Natural
              list
              (List Natural)
              ( \(item : Natural) ->
                \(acc : List Natural) ->
                  if elem item acc then acc else [ item ] # acc
              )
              (Prelude.List.empty Natural)

let foo = toMap { a = [ 1, 2 ] }

let bar = toMap { a = [ 2, 3 ] }

let Entry = Prelude.Map.Entry Text (List Natural)

in  nub
      ( Prelude.List.fold
          Entry
          (foo # bar)
          (List Natural)
          (\(item : Entry) -> \(acc : List Natural) -> item.mapValue # acc)
          (Prelude.List.empty Natural)
      )

Results in [1, 2, 3].

Thanks a lot @tristanC.

For what I’m trying to do, I need to have them already as Maps. I think I’ll try to separate a bit more my data to avoid having duplicate keys, so that I can use plain #