haskell / haskell/containers

Add tryInsert for sets and maps

Open
#1,205 7 comments 0 reactions 0 assignees View on GitHub
feature-request IntMap IntSet Map Set
Dominant language
Haskell
Stars
355
Forks
194
Avg merge
3d 4h
Merged PRs (30d)
4

Description

```hs
tryInsert :: Ord k => k -> a -> Map k a -> Maybe (Map k a)
tryInsert :: Ord a => a -> Set a -> Maybe (Set a)
tryInsert :: Int -> a -> IntMap a -> Maybe (IntMap a)
tryInsert :: Int -> IntSet -> Maybe IntSet
```

Inserts the value if the key is not in the map/set, otherwise returns `Nothing`.

This will be useful in `nubOrdOn` to avoid `member`+`insert`:

https://github.com/haskell/containers/blob/d82a404141537c97df421d589a946a09272cc6d6/containers/src/Data/Containers/ListUtils.hs#L86-L88

Alternate name suggestions are welcome.

If we go with `tryInsert` I think we should rename `pop` from #1134 to `tryDelete` for consistency. I didn't think of this name at that time. Luckily we haven't released it yet, so we can still change the name.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.