Skip to content

Add Map.merge to FSharp.Core#19994

Open
bbatsov wants to merge 1 commit into
dotnet:mainfrom
bbatsov:feature/map-merge
Open

Add Map.merge to FSharp.Core#19994
bbatsov wants to merge 1 commit into
dotnet:mainfrom
bbatsov:feature/map-merge

Conversation

@bbatsov

@bbatsov bbatsov commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Now that #19265 (print/printn) is in good shape, I went looking for other small, self-contained additions to FSharp.Core I could help with, and landed on suggestion #560: a Map.merge function. It's approved-in-principle and gets asked for fairly regularly, so it felt worth a shot.

Map.merge combines two maps into one. When a key is present in both, a caller-supplied function resolves the two values:

(sample1, sample2) ||> Map.merge (fun _key v1 v2 -> v1 + v2)

I went with the combiner form rather than the plain "last one wins" from the original 2017 proposal, since right-biased merge is just a trivial special case of it (Map.merge (fun _ _ v2 -> v2)).

One caveat: unlike the print/printn ticket, this suggestion never got a detailed RFC, so the exact shape (name, argument order, combiner vs. right-biased) is really my best guess and probably wants a maintainer's eye before it goes any further.

Suggestion: fsharp/fslang-suggestions#560

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/FSharp.Core docs/release-notes/.FSharp.Core/11.0.100.md

@github-actions github-actions Bot added the AI-Tooling-Check-Scanned-Clean Tooling check: diff analyzed, no interesting infrastructure files label Jun 25, 2026
Combines two maps into one, using a caller-supplied function to resolve
the values of keys present in both maps. The right-biased "last one wins"
merge from the original proposal is just a special case of the combiner
form (Map.merge (fun _ _ v2 -> v2)).

Implements fsharp/fslang-suggestions#560.
@bbatsov bbatsov force-pushed the feature/map-merge branch from f0b5cdd to 84c93dd Compare June 25, 2026 12:37
Comment thread src/FSharp.Core/map.fs
Comment on lines +1223 to +1229
let merge resolve (table1: Map<_, _>) (table2: Map<_, _>) =
(table1, table2)
||> fold (fun acc key value2 ->
acc
|> change key (function
| Some value1 -> Some(resolve key value1 value2)
| None -> Some value2))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of the signature decided on, I think we'd want the implementation to use the internal MapTree<_, _> directly to avoid creating intermediate Map<_, _> values for every entry.

@T-Gro

T-Gro commented Jul 5, 2026

Copy link
Copy Markdown
Member

Please follow-up and revive the suggestion first.

  • merge vs union
  • FSharp.Core tends to use the "with" suffix for functions taking a function argument (e.g. Map.union would be a chosen default, and Map.unionWith would use a passed function)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-Tooling-Check-Scanned-Clean Tooling check: diff analyzed, no interesting infrastructure files

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

3 participants