Skip to content

Merging mappings together #1079

Answered by HT154
rohan-datar asked this question in Q&A
May 27, 2025 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Give this a shot:

function mergeMaps(l: Listing<Mapping<Any, Listing>>): Mapping<Any, Listing> =
  l.fold(new Mapping<Any, Listing> {}, (acc, elem) ->
    elem.fold(acc, (acc, k, v) ->
      (acc) { [k] { ...v } }
    )
  )

Shadowing acc here is a little naughty, but it does work. You also need a little more typing on your input, see https://pkl-playground.vercel.app/?share=these-hearing-bee for everything together.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rohan-datar
Comment options

Answer selected by rohan-datar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants