Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traverseMonotonic, etc. #816

Open
josephcsible opened this issue Jan 27, 2022 · 3 comments
Open

Add traverseMonotonic, etc. #816

josephcsible opened this issue Jan 27, 2022 · 3 comments

Comments

@josephcsible
Copy link
Contributor

import Data.Set.Internal

traverseMonotonic :: Applicative t => (a -> t b) -> Set a -> t (Set b)
traverseMonotonic _ Tip = pure Tip
traverseMonotonic f (Bin sz x l r) = flip (Bin sz) <$> traverseMonotonic f l <*> f x <*> traverseMonotonic f r
import Data.Map.Internal

traverseKeysMonotonic :: Applicative t => (k1 -> t k2) -> Map k1 a -> t (Map k2 a)
traverseKeysMonotonic _ Tip = pure Tip
traverseKeysMonotonic f (Bin sz k x l r) = (\l' k' -> Bin sz k' x l') <$> traverseKeysMonotonic f l <*> f k <*> traverseKeysMonotonic f r

bitraverseMonotonic :: Applicative t => (k1 -> t k2) -> (v1 -> t v2) -> Map k1 v1 -> t (Map k2 v2)
bitraverseMonotonic _ _ Tip = pure Tip
bitraverseMonotonic f g (Bin sz k x l r) = (\l' k' x' -> Bin sz k' x' l') <$> bitraverseMonotonic f g l <*> f k <*> g x <*> bitraverseMonotonic f g r

I think we should add these to Data.Set and Data.Map, respectively, as analogues of mapMonotonic, etc. Note that these are unsafe just like they are, and require the exact same care to avoid breaking invariants.

@treeowl
Copy link
Contributor

treeowl commented Nov 20, 2022

Would you like to open a PR?

@josephcsible
Copy link
Contributor Author

If these are indeed good functions to add, then sure. (I just don't want to end up doing all the work of writing tests and documentation for them, and then find out we don't want them.)

@treeowl
Copy link
Contributor

treeowl commented Nov 28, 2022

I think they make an awful lot of sense and match the library conventions perfectly. Let's do it.

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

No branches or pull requests

3 participants