-
Notifications
You must be signed in to change notification settings - Fork 182
Set difference and union in one #944
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
Comments
This feels way too specialized. Most combinations of operations can probably be implemented more efficiently by having a combined operation, but that doesn't mean we should do it. This isn't something I'd expect any library to implement, on the contrary, I'd feel like this would be bloat. |
I've been wanting to make an equivalent of the |
@treeowl that sounds much better than what I proposed here! Which applicative would you use? I believe the straightforward |
I'd think data Pair a = Pair a a We'd have to make sure it generated good code in typical cases. |
I personally was looking for a function that is even more "specific" than the -- | Given two sets, produces:
-- (elements only in set 1, elements in both sets, elements only in set 2)
mergeSplit :: Ord a => Set a -> Set a -> (Set a, Set a, Set a)
mergeSplit s1 s2 =
go [] [] [] (S.toList s1) (S.toList s2)
where
go a b c [] [] = (S.fromDescList a, S.fromDescList b, S.fromDescList c)
go a b c [] (y:ys) = go a b (y : c) [] ys
go a b c (x:xs) [] = go (x : a) b c xs []
go a b c xss@(x:xs) yss@(y:ys) =
case compare x y of
EQ -> go a (x : b) c xs ys
LT -> go (x : a) b c xs yss
GT -> go a b (y : c) xss ys |
You can use mergeSplit :: Ord a => Set a -> Set a -> (Set a, Set a, Set a)
mergeSplit s1 s2 = (S.difference s1 s2, S.intersection s1 s2, S.difference s2 s1) This is some more proof that |
Set difference and union sometimes have to be computed together, such as in semi-naive fixed point computation:
If there was a combined operation:
Then I can write it more efficiently:
The text was updated successfully, but these errors were encountered: