| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Data.Set.Merge
Description
Synopsis
- type SimpleWhenMissing = WhenMissing Identity
- type SimpleWhenMatched = WhenMatched Identity
- merge :: Ord a => SimpleWhenMissing a -> SimpleWhenMissing a -> SimpleWhenMatched a -> Set a -> Set a -> Set a
- dropMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a
- preserveMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a
- filterMissing :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMissing f a
- dropMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a
- preserveMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a
- filterMatched :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMatched f a
- data WhenMissing (f :: Type -> Type) a
- data WhenMatched (f :: Type -> Type) a
- mergeA :: (Applicative f, Ord a) => WhenMissing f a -> WhenMissing f a -> WhenMatched f a -> Set a -> Set a -> f (Set a)
- filterAMissing :: Applicative f => (a -> f Bool) -> WhenMissing f a
- whenMissing :: (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a
- filterAMatched :: (a -> f Bool) -> WhenMatched f a
- runWhenMissing :: WhenMissing f a -> a -> f Bool
- runWhenMatched :: WhenMatched f a -> a -> f Bool
Simple merge tactic types
type SimpleWhenMissing = WhenMissing Identity Source #
A tactic for dealing with elements present in one set but not the other in
merge.
A tactic of type SimpleWhenMissing a is an abstract representation
of a function of type a -> Bool.
Since: 0.8.1
type SimpleWhenMatched = WhenMatched Identity Source #
A tactic for dealing with elements present in both sets in merge.
A tactic of type SimpleWhenMatched a is an abstract representation of a
function of type a -> Bool.
Since: 0.8.1
General combining function
Arguments
| :: Ord a | |
| => SimpleWhenMissing a | What to do with elements in |
| -> SimpleWhenMissing a | What to do with elements in |
| -> SimpleWhenMatched a | What to do with elements in both |
| -> Set a | Set |
| -> Set a | Set |
| -> Set a |
Merge two sets.
merge takes two SimpleWhenMissing tactics, a SimpleWhenMatched tactic,
and two sets. It uses the tactics to merge the sets.
Consider
merge (filterMissing g1) (filterMissing g2) (filterMatched f) s1 s2
g1 = (==2) g2 = (==3) f = (==6) s1 = [2, 4, 6, 8, 10, 12] s2 = [3, 6, 9, 12]
merge will pass the elements to g1, g2, or f as appropriate,
producing a Bool for each element.
m1: [ 2, 4, 6, 8, 10 12]
m2: [ 3, 6, 9, 12]
result: [g1 2, g2 3, g1 4, f 6, g1 8, g2 9, g1 10, f 12]
= [True, True, False, True, False, False, False, False]
The result set contains the element for which we have True.
>>>merge (filterMissing g1) (filterMissing g2) (filterMatched f) s1 s2fromList [2,3,6]
The other tactics below are optimizations or simplifications of
filterMissing for special cases. Most importantly,
dropMissingdrops all elements.preserveMissingleaves all elements alone.
When merge is given three arguments, it is inlined at the call
site. To prevent excessive inlining, you should typically use merge
to define your custom combining functions.
Since: 0.8.1
WhenMissing tactics
dropMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a Source #
Drop all the elements that are missing from the other set.
dropMissing :: SimpleWhenMissing a
dropMissing = filterMissing (\_ -> False)
but dropMissing is more efficient.
Since: 0.8.1
preserveMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a Source #
Preserve the elements that are missing from the other set.
preserveMissing :: SimpleWhenMissing a
preserveMissing = filterMissing (\_ -> True)
but preserveMissing is more efficient.
Since: 0.8.1
filterMissing :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMissing f a Source #
Filter the elements that are missing from the other set.
filterMissing :: (a -> Bool) -> SimpleWhenMissing a
Since: 0.8.1
WhenMatched tactics
dropMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a Source #
When an element is found in both sets, drop the element.
Since: 0.8.1
preserveMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a Source #
When an element is found in both sets, keep the element.
Since: 0.8.1
filterMatched :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMatched f a Source #
When an element is found in both sets, choose whether to keep the element in the merged set.
Since: 0.8.1
Applicative merge tactic types
data WhenMissing (f :: Type -> Type) a Source #
data WhenMatched (f :: Type -> Type) a Source #
Applicative general combining function
Arguments
| :: (Applicative f, Ord a) | |
| => WhenMissing f a | What to do with elements in |
| -> WhenMissing f a | What to do with elements in |
| -> WhenMatched f a | What to do with elements in both |
| -> Set a | Set |
| -> Set a | Set |
| -> f (Set a) |
An applicative version of merge.
mergeA takes two WhenMissing tactics, a WhenMatched tactic, and two
sets. It uses the tactics to merge the sets.
Behaves just like merge while allowing Applicative effects. Effects are
performed in increasing order of elements.
Consider,
mergeA (filterAMissing g1) (filterAMissing g2) (filterAMatched f) s1 s2
g1 x = (x == 2) <$ putStrLn ("g1 " ++ show x)
g2 x = (x == 3) <$ putStrLn ("g2 " ++ show x)
f x = (x == 6) <$ putStrLn ("f " ++ show x)
s1 = [2, 4, 6, 8, 10, 12]
s2 = [3, 6, 9, 12]
As with merge, the result set is [2,3,6]. Additionally, g1, g2, and
f perform IO effects, printing the elements in increasing order.
>>>mergeA (filterAMissing g1) (filterAMissing g2) (filterAMatched f) s1 s2g1 2 g2 3 g1 4 f 6 g1 8 g2 9 g1 10 f 12 fromList [2,3,6]
When mergeA is given three arguments, it is inlined at the call
site. To prevent excessive inlining, you should generally only use
mergeA to define custom combining functions.
Examples
data Pair a = Pair !a !a deriving Functor instance Applicative Pair where pure x = Pair x x liftA2 f (Pair x1 y1) (Pair x2 y2) = Pair (f x1 x2) (f y1 y2) -- | Calculate the union and intersection of two sets. unionIntersection :: Ord a => Set a -> Set a -> (Set a, Set a) unionIntersection m1 m2 = case mergeA preserveAndDropMissing preserveAndDropMissingpreserveMatchedm1 m2 of Pair mu mi -> (mu, mi) where -- use Pair to build the union and intersection together preserveAndDropMissing =whenMissing(\_x -> Pair True False) (\s -> Pair s empty)
import Data.Functor.Const (Const(..)) import Data.Monoid (All(..)) -- | Whether the first set is a subset of the second set. isSubsetOf :: Ord a => Set a -> Set a -> Bool isSubsetOf m1 m2 = getAll (getConst (mergeA isEmptydropMissingdropMatchedm1 m2)) where isEmpty =whenMissing(\_x -> Const (All False)) (\s -> Const (All (null s)))
Since: 0.8.1
WhenMissing tactics
filterAMissing :: Applicative f => (a -> f Bool) -> WhenMissing f a Source #
Filter the elements that are missing from the other set using some
Applicative action.
Since: 0.8.1
whenMissing :: (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a Source #
Create a WhenMissing from two functions.
whenMissing must be called with two functions f and g such that
g = . filterA fg may be a more efficient way of applying f to all
elements in a Set.
Warning: It is the caller's responsibility to ensure the above property.
Examples
preserveMissing :: Applicative f => WhenMissing f a
preserveMissing = whenMissing f g
where
f _x = pure True
g s = pure s
-- Note that this satisfies g = filterA f
import Data.Functor.Const (Const(..))
import Data.Monoid (All(..))
-- For a usage of this, see examples on mergeA
isEmpty :: WhenMissing (Const All) a
isEmpty = whenMissing f g
where
f _x = Const (All False)
g s = Const (All (null s))
-- Note that this satisfies g = filterA f
Since: 0.8.1
WhenMatched tactics
filterAMatched :: (a -> f Bool) -> WhenMatched f a Source #
When an element is found in both sets, choose whether to keep the element in the merged set.
Since: 0.8.1
Miscellaneous tactic functions
runWhenMissing :: WhenMissing f a -> a -> f Bool Source #
Along with filterAMissing, witnesses the isomorphism between
WhenMissing f a and a -> f Bool.
Since: 0.8.1
runWhenMatched :: WhenMatched f a -> a -> f Bool Source #
Along with filterAMatched, witnesses the isomorphism between
WhenMatched f a and a -> f Bool.
Since: 0.8.1