Skip to content

Add tryInsert for sets and maps #1205

Description

@meooow25
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:

go s (x:xs)
| fx `Set.member` s = go s xs
| otherwise = x : go (Set.insert fx s) xs

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions