| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.Map.Merge.Set.Internal
Description
WARNING
This module is considered internal.
The Package Versioning Policy does not apply.
The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.
Authors importing this module are expected to track development closely.
Description
This module defines common constructs used by both Data.Map.Merge.Set.Lazy and Data.Map.Merge.Set.Strict.
Since: 0.8.1
Synopsis
- newtype WhenMatched (f :: Type -> Type) k a b = WhenMatched {
- matchedKey :: k -> a -> f (Maybe b)
- type SimpleWhenMatched = WhenMatched Identity
- dropMatched :: forall (f :: Type -> Type) k a b. Applicative f => WhenMatched f k a b
- filterMatched :: forall (f :: Type -> Type) k a. Applicative f => (k -> a -> Bool) -> WhenMatched f k a a
- filterAMatched :: Functor f => (k -> a -> f Bool) -> WhenMatched f k a a
- data WhenMissingSet (f :: Type -> Type) k a = WhenMissingSet {
- missingSubtree :: Set k -> f (Map k a)
- missingKey :: k -> f (Maybe a)
- type SimpleWhenMissingSet = WhenMissingSet Identity
- dropMissingSet :: forall (f :: Type -> Type) k a. Applicative f => WhenMissingSet f k a
- merge :: Ord k => SimpleWhenMissing k a b -> SimpleWhenMissingSet k b -> SimpleWhenMatched k a b -> Map k a -> Set k -> Map k b
- mergeA :: (Applicative f, Ord k) => WhenMissing f k a b -> WhenMissingSet f k b -> WhenMatched f k a b -> Map k a -> Set k -> f (Map k b)
- runWhenMatched :: WhenMatched f k a b -> k -> a -> f (Maybe b)
- runWhenMissingSet :: WhenMissingSet f k a -> k -> f (Maybe a)
Documentation
newtype WhenMatched (f :: Type -> Type) k a b Source #
A tactic for dealing with keys present in both the set and the map in
merge or mergeA.
A tactic of type WhenMatched f k a b is an abstract representation of
a function of type k -> a -> f (Maybe b).
Since: 0.8.1
Constructors
| WhenMatched | |
Fields
| |
type SimpleWhenMatched = WhenMatched Identity Source #
A tactic for dealing with keys present in both the set and the map in
merge.
A tactic of type SimpleWhenMatched k a b is an abstract representation of
a function of type k -> a -> Maybe b.
Since: 0.8.1
dropMatched :: forall (f :: Type -> Type) k a b. Applicative f => WhenMatched f k a b Source #
When a key is found in both the map and the set, drop the key and value.
Since: 0.8.1
filterMatched :: forall (f :: Type -> Type) k a. Applicative f => (k -> a -> Bool) -> WhenMatched f k a a Source #
When a key is found in both the map and the set, apply a function to the
key and the value in the map and keep the value in the merged map if the
result is True.
Since: 0.8.1
filterAMatched :: Functor f => (k -> a -> f Bool) -> WhenMatched f k a a Source #
When a key is found in both the map and the set, apply a function to the
key and the value in the map and keep the value in the merged map if the
result of the action is True.
Since: 0.8.1
data WhenMissingSet (f :: Type -> Type) k a Source #
A tactic for dealing with keys present in the set but not in the map in
merge or mergeA.
A tactic of type WhenMissingSet f k a is an abstract representation of
a function of type k -> f (Maybe a).
Since: 0.8.1
Constructors
| WhenMissingSet | |
Fields
| |
type SimpleWhenMissingSet = WhenMissingSet Identity Source #
A tactic for dealing with keys present in the set but not in the map in
merge.
A tactic of type SimpleWhenMissingSet k a is an abstract representation of
a function of type k -> Maybe a.
Since: 0.8.1
dropMissingSet :: forall (f :: Type -> Type) k a. Applicative f => WhenMissingSet f k a Source #
Drop keys that are present in the set but missing from the map.
Since: 0.8.1
Arguments
| :: Ord k | |
| => SimpleWhenMissing k a b | What to do with keys in |
| -> SimpleWhenMissingSet k b | What to do with keys in |
| -> SimpleWhenMatched k a b | What to do with keys in both |
| -> Map k a | Map |
| -> Set k | Set |
| -> Map k b |
Merge a map and a set into a map.
merge takes a SimpleWhenMissing tactic, a SimpleWhenMissingSet
tactic, a SimpleWhenMatched tactic, a map and a set. It uses the tactics to
merge the map and the set into a map.
Its behavior is best understood via the tactics mapMaybeMissing,
generateMaybeMissingSet, and mapMaybeMatched. Consider
merge (mapMaybeMissing g1) (generateMaybeMissingSet g2) (mapMaybeMatched f) m1 s2
g1 k x = if k == 2 then Just ("1" ++ x) else Nothing
g2 k = if k == 3 then Just "2" else Nothing
f k x = if k == 6 then Just ("3" ++ x) else Nothing
m1 = fromList [(2,"a"), (4,"b"), (6,"c"), (8,"d"), (10,"e"), (12,"f")]
s2 = fromList [3, 6, 9, 12]
merge will pass the keys and values to g1, g2, or f as appropriate,
producing a Maybe for each element.
m1: [ (2, "a"), (4, "b"), (6, "c"), (8, "d"), (10, "e"), (12, "f")]
s2: [ 3, 6, 9, 12]
result: [ g1 2 "a", g2 3, g1 4 "b", f 6 "c", g1 8 "d", g2 9, g1 10 "e", f 12 "f"]
= [Just "1a", Just "2", Nothing, Just "3c", Nothing, Nothing, Nothing, Nothing]
The result map contains the Just values.
>>>merge (mapMaybeMissing g1) (generateMaybeMissingSet g2) (mapMaybeMatched f) m1 s2fromList [(2,"1a"), (3,"2g"), (6,"3c")]
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
Arguments
| :: (Applicative f, Ord k) | |
| => WhenMissing f k a b | What to do with keys in |
| -> WhenMissingSet f k b | What to do with keys in |
| -> WhenMatched f k a b | What to do with keys in both |
| -> Map k a | Map |
| -> Set k | Set |
| -> f (Map k b) |
Merge a map and a set into a map. Applicative version of merge.
mergeA takes a WhenMissing tactic, a WhenMissingSet tactic, a
WhenMatched tactic, a map and a set. It uses the tactics to merge the map
and the set into a map.
Behaves just like merge while allowing Applicative effects. Effects are
performed in increasing order of keys.
Consider
mergeA (traverseMaybeMissing g1)
(generateMaybeAMissingSet g2)
(traverseMaybeMatched f)
m1
s2
g1 k x = let z = if k == 2 then Just ("1" ++ x) else Nothing
in z <$ putStrLn ("g1 " ++ show (k, x))
g2 k = let z = if k == 3 then Just "2" else Nothing
in z <$ putStrLn ("g2 " ++ show k)
f k x = let z = if k == 6 then Just ("3" ++ x) else Nothing
in z <$ putStrLn ("f " ++ show (k, x))
m1 = fromList [(2,"a"), (4,"b"), (6,"c"), (8,"d"), (10,"e"), (12,"f")]
m2 = fromList [3, 6, 9, 12]
As with merge, the result map is [(2,"1a"), (3,"2"), (6,"3c")].
Additionally, g1, g2, and f perform IO effects, printing in
increasing order of key.
>>>mergeA (traverseMaybeMissing g1) (generateMaybeAMissingSet g2) (traverseMaybeMatched f) m1 s2g1 (2,"a") g2 3 g1 (4,"b") f (6,"c") g1 (8,"d") g2 9 g1 (10,"e") f (12,"f") fromList [(2,"1a"),(3,"2"),(6,"3c")]
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)
-- | Partition the map according to whether the keys appear in the set.
partitionKeys :: Ord k => Map k a -> Set k -> (Map k a, Map k a)
partitionKeys m s =
case mergeA dropAndPreserveMissing dropMissingSet preserveAndDropMatched m s of
Pair m1 m2 -> (m1, m2)
where
dropAndPreserveMissing = whenMissing (\_k x -> Pair Nothing (Just x)) (\m -> Pair empty m)
preserveAndDropMatched = traverseMaybeMatched (\_k x -> Pair (Just x) Nothing)
import Data.Functor.Const (Const(..))
import Data.Monoid (All(..))
-- | Whether the keys of the map are a subset of the keys of the set.
keysAreSubsetOf :: Ord k => Map k a -> Set k -> Bool
keysAreSubsetOf m s =
getAll (getConst (mergeA isEmpty dropMissing dropMatched m1 m2))
where
isEmpty = whenMissing (\_k _x -> Const (All False)) (\m -> Const (All (null m)))
Since: 0.8.1
runWhenMatched :: WhenMatched f k a b -> k -> a -> f (Maybe b) Source #
Run WhenMatched.
Since: 0.8.1
runWhenMissingSet :: WhenMissingSet f k a -> k -> f (Maybe a) Source #
Run WhenMissingSet.
Since: 0.8.1