Skip to content

Add upsert and fromListUpsert#590

Draft
sjakobi wants to merge 5 commits intomasterfrom
add-upsert
Draft

Add upsert and fromListUpsert#590
sjakobi wants to merge 5 commits intomasterfrom
add-upsert

Conversation

@sjakobi
Copy link
Copy Markdown
Member

@sjakobi sjakobi commented Apr 13, 2026

Closes #549.

Add the upsert function for compatibility with containers, which added
upsert in PR #1145. The function updates the value at a key or inserts
a value if the key is not in the map:

  upsert :: Hashable k => (Maybe v -> v) -> k -> HashMap k v -> HashMap k v

This fills a gap in the family of map-modifying functions:
- adjust: (a -> a) - updates existing value only
- update: (a -> Maybe a) - update or delete
- upsert: (Maybe a -> a) - insert or update (always returns a value)
- alter: (Maybe a -> Maybe a) - insert, update, or delete

Closes #549.

Assisted-by: GLM-5 via Fireworks AI <noreply@fireworks.ai>
sjakobi added 2 commits April 14, 2026 00:59
The upsert and fromListUpsert functions are not yet in a released
containers version. Fetch containers from git to enable model-based
testing.

Assisted-by: GLM-5 via Fireworks AI <noreply@fireworks.ai>
Add fromListUpsert for compatibility with containers, which added it
in PR #1190:

  fromListUpsert :: Hashable k => (a -> Maybe v -> v) -> [(k, a)] -> HashMap k v

This builds a map from a list, using the provided function to combine
values. Equivalent to performing an upsert for each key/value pair.

Assisted-by: GLM-5 via Fireworks AI <noreply@fireworks.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add upsert?!

1 participant