containers-0.8.1: Assorted concrete container types
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Map.Merge.Set.Lazy

Description

This module defines an API for writing functions that merge a map and a set into a map. The key functions are merge and mergeA. Each of these can be used with several different "merge tactics".

The merge and mergeA functions are shared by the lazy and strict modules. Only the choice of merge tactics determines strictness. If you use mapMissing from Data.Map.Merge.Set.Strict then the results will be forced before they are inserted. If you use mapMissing from this module then they will not.

Since: 0.8.1

Synopsis

Simple merge tactic types

type SimpleWhenMissing = WhenMissing Identity Source #

A tactic for dealing with keys present in one map but not the other in merge.

A tactic of type SimpleWhenMissing k x z is an abstract representation of a function of type k -> x -> Maybe z .

Since: 0.5.9

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

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

General combining function

merge Source #

Arguments

:: Ord k 
=> SimpleWhenMissing k a b

What to do with keys in m1 but not s2

-> SimpleWhenMissingSet k b

What to do with keys in s2 but not m1

-> SimpleWhenMatched k a b

What to do with keys in both m1 and s2

-> Map k a

Map m1

-> Set k

Set s2

-> 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 s2
fromList [(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

WhenMatched tactics

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

mapMatched :: forall (f :: Type -> Type) k a b. Applicative f => (k -> a -> b) -> WhenMatched f k a b 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 use the result as the value for the merged map.

Since: 0.8.1

mapMaybeMatched :: forall (f :: Type -> Type) k a b. Applicative f => (k -> a -> Maybe b) -> WhenMatched f k a b 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 maybe use the result as the value for the merged map.

Since: 0.8.1

WhenMissing tactics

dropMissing :: forall (f :: Type -> Type) k x y. Applicative f => WhenMissing f k x y Source #

Drop all the entries whose keys are missing from the other map.

dropMissing :: SimpleWhenMissing k x y
dropMissing = mapMaybeMissing (\_ _ -> Nothing)

but dropMissing is much faster.

Since: 0.5.9

preserveMissing :: forall (f :: Type -> Type) k x. Applicative f => WhenMissing f k x x Source #

Preserve, unchanged, the entries whose keys are missing from the other map.

preserveMissing :: SimpleWhenMissing k x x
preserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -> Just x)

but preserveMissing is much faster.

Since: 0.5.9

mapMissing :: forall (f :: Type -> Type) k x y. Applicative f => (k -> x -> y) -> WhenMissing f k x y Source #

Map over the entries whose keys are missing from the other map.

mapMissing :: (k -> x -> y) -> SimpleWhenMissing k x y
mapMissing f = mapMaybeMissing (\k x -> Just $ f k x)

but mapMissing is somewhat faster.

Since: 0.5.9

filterMissing :: forall (f :: Type -> Type) k x. Applicative f => (k -> x -> Bool) -> WhenMissing f k x x Source #

Filter the entries whose keys are missing from the other map.

filterMissing :: (k -> x -> Bool) -> SimpleWhenMissing k x x
filterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -> guard (f k x) *> Just x

but this should be a little faster.

Since: 0.5.9

mapMaybeMissing :: forall (f :: Type -> Type) k x y. Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y Source #

Map over the entries whose keys are missing from the other map, optionally removing some. This is the most powerful SimpleWhenMissing tactic, but others are usually more efficient.

mapMaybeMissing :: (k -> x -> Maybe y) -> SimpleWhenMissing k x y
mapMaybeMissing f = traverseMaybeMissing (\k x -> pure (f k x))

but mapMaybeMissing uses fewer unnecessary Applicative operations.

Since: 0.5.9

WhenMissingSet tactics

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

generateMissingSet :: forall (f :: Type -> Type) k a. Applicative f => (k -> a) -> WhenMissingSet f k a Source #

For keys that are present in the set but missing from the map, apply a function and use the result as the value for the merged map.

Since: 0.8.1

generateMaybeMissingSet :: forall (f :: Type -> Type) k a. Applicative f => (k -> Maybe a) -> WhenMissingSet f k a Source #

For keys that are present in the set but missing from the map, apply a function and maybe use the result as the value for the merged map.

Since: 0.8.1

Applicative merge tactic types

data WhenMissing (f :: Type -> Type) k x y Source #

A tactic for dealing with keys present in one map but not the other in merge or mergeA.

A tactic of type WhenMissing f k x z is an abstract representation of a function of type k -> x -> f (Maybe z) .

Since: 0.5.9

Instances

Instances details
Monad f => Category (WhenMissing f k :: Type -> Type -> Type) Source #

Since: 0.5.9

Instance details

Defined in Data.Map.Internal

Methods

id :: WhenMissing f k a a #

(.) :: WhenMissing f k b c -> WhenMissing f k a b -> WhenMissing f k a c #

Monad f => Applicative (WhenMissing f k x) Source #

Equivalent to ReaderT k (ReaderT x (MaybeT f)) .

Since: 0.5.9

Instance details

Defined in Data.Map.Internal

Methods

pure :: a -> WhenMissing f k x a #

(<*>) :: WhenMissing f k x (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b #

liftA2 :: (a -> b -> c) -> WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x c #

(*>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b #

(<*) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x a #

Monad f => Functor (WhenMissing f k x) Source #

Since: 0.5.9

Instance details

Defined in Data.Map.Internal

Methods

fmap :: (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b #

(<$) :: a -> WhenMissing f k x b -> WhenMissing f k x a #

Monad f => Monad (WhenMissing f k x) Source #

Equivalent to ReaderT k (ReaderT x (MaybeT f)) .

Since: 0.5.9

Instance details

Defined in Data.Map.Internal

Methods

(>>=) :: WhenMissing f k x a -> (a -> WhenMissing f k x b) -> WhenMissing f k x b #

(>>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b #

return :: a -> WhenMissing f k x a #

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

data 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

General combining function

mergeA Source #

Arguments

:: (Applicative f, Ord k) 
=> WhenMissing f k a b

What to do with keys in m1 but not s2

-> WhenMissingSet f k b

What to do with keys in s2 but not m1

-> WhenMatched f k a b

What to do with keys in both m1 and s2

-> Map k a

Map m1

-> Set k

Set s2

-> 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 s2
g1 (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

Expand
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

WhenMatched tactics

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

traverseMatched :: Functor f => (k -> a -> f b) -> WhenMatched f k a b 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 use the result of the action as the value for the merged map.

Since: 0.8.1

traverseMaybeMatched :: (k -> a -> f (Maybe b)) -> WhenMatched f k a b 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 maybe use the result of the action as the value for the merged map.

Since: 0.8.1

WhenMissing tactics

filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x Source #

Filter the entries whose keys are missing from the other map using some Applicative action.

filterAMissing f = Merge.Lazy.traverseMaybeMissing $
  \k x -> (\b -> guard b *> Just x) <$> f k x

but this should be a little faster.

Since: 0.5.9

traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y Source #

Traverse over the entries whose keys are missing from the other map.

Since: 0.5.9

traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y Source #

Traverse over the entries whose keys are missing from the other map, optionally producing values to put in the result. This is the most powerful WhenMissing tactic, but others are usually more efficient.

Since: 0.5.9

whenMissing :: (k -> x -> f (Maybe y)) -> (Map k x -> f (Map k y)) -> WhenMissing f k x y Source #

Create a WhenMissing from two functions.

whenMissing must be called with two functions f and g such that g = traverseMaybeWithKey f. g may be a more efficient way of applying f to all key-value pairs in a Map.

Warning: It is the caller's responsibility to ensure the above property.

Examples

Expand
preserveMissing :: Applicative f => WhenMissing f k x x
preserveMissing = whenMissing f g
  where
    f _k x = pure (Just x)
    g m = pure m
    -- Note that this satisfies g = traverseMaybeWithKey f
import Data.Functor.Const (Const(..))
import Data.Monoid (All(..))

-- For a usage of this, see examples on mergeA
isEmpty :: WhenMissing (Const All) k x y
isEmpty = whenMissing f g
  where
    f _k _x = Const (All False)
    g m = Const (All (null m))
    -- Note that this satisfies g = traverseMaybeWithKey f

Since: 0.8.1

WhenMissingSet tactics

generateAMissingSet :: Applicative f => (k -> f a) -> WhenMissingSet f k a Source #

For keys that are present in the set but missing from the map, apply a function, and use the result of the action as the value for the merged map.

Since: 0.8.1

generateMaybeAMissingSet :: Applicative f => (k -> f (Maybe a)) -> WhenMissingSet f k a Source #

For keys that are present in the set but missing from the map, apply a function, and maybe use the result of the action as the value for the merged map.

Since: 0.8.1

Miscellaneous

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