Copyright | (c) Daan Leijen 2002 (c) Andriy Palamarchuk 2008 |
---|---|
License | BSD-style |
Maintainer | libraries@haskell.org |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
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
An efficient implementation of maps from keys to values (dictionaries).
Since many function names (but not the type name) clash with
Prelude names, this module is usually imported qualified
, e.g.
import Data.Map (Map) import qualified Data.Map as Map
The implementation of Map
is based on size balanced binary trees (or
trees of bounded balance) as described by:
- Stephen Adams, "Efficient sets: a balancing act", Journal of Functional Programming 3(4):553-562, October 1993, http://www.swiss.ai.mit.edu/~adams/BB/.
- J. Nievergelt and E.M. Reingold, "Binary search trees of bounded balance", SIAM journal of computing 2(1), March 1973.
Bounds for union
, intersection
, and difference
are as given
by
- Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "Just Join for Parallel Ordered Sets", https://arxiv.org/abs/1602.02120v3.
Note that the implementation is left-biased -- the elements of a
first argument are always preferred to the second, for example in
union
or insert
.
Operation comments contain the operation time complexity in the Big-O notation http://en.wikipedia.org/wiki/Big_O_notation.
Since: containers-0.5.9
Synopsis
- data Map k a
- type Size = Int
- (!) :: Ord k => Map k a -> k -> a
- (!?) :: Ord k => Map k a -> k -> Maybe a
- (\\) :: Ord k => Map k a -> Map k b -> Map k a
- null :: Map k a -> Bool
- size :: Map k a -> Int
- member :: Ord k => k -> Map k a -> Bool
- notMember :: Ord k => k -> Map k a -> Bool
- lookup :: Ord k => k -> Map k a -> Maybe a
- findWithDefault :: Ord k => a -> k -> Map k a -> a
- lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)
- lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)
- lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)
- lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)
- empty :: Map k a
- singleton :: k -> a -> Map k a
- insert :: Ord k => k -> a -> Map k a -> Map k a
- insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
- insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
- insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
- delete :: Ord k => k -> Map k a -> Map k a
- adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a
- adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
- update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
- updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
- updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
- alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
- alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
- union :: Ord k => Map k a -> Map k a -> Map k a
- unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
- unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
- unions :: (Foldable f, Ord k) => f (Map k a) -> Map k a
- unionsWith :: (Foldable f, Ord k) => (a -> a -> a) -> f (Map k a) -> Map k a
- difference :: Ord k => Map k a -> Map k b -> Map k a
- differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
- differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
- intersection :: Ord k => Map k a -> Map k b -> Map k a
- intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
- intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
- disjoint :: Ord k => Map k a -> Map k b -> Bool
- compose :: Ord b => Map b c -> Map a b -> Map a c
- type SimpleWhenMissing = WhenMissing Identity
- type SimpleWhenMatched = WhenMatched Identity
- runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
- runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)
- merge :: Ord k => SimpleWhenMissing k a c -> SimpleWhenMissing k b c -> SimpleWhenMatched k a b c -> Map k a -> Map k b -> Map k c
- zipWithMaybeMatched :: forall (f :: Type -> Type) k x y z. Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z
- zipWithMatched :: forall (f :: Type -> Type) k x y z. Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z
- mapMaybeMissing :: forall (f :: Type -> Type) k x y. Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y
- dropMissing :: forall (f :: Type -> Type) k x y. Applicative f => WhenMissing f k x y
- preserveMissing :: forall (f :: Type -> Type) k x. Applicative f => WhenMissing f k x x
- preserveMissing' :: forall (f :: Type -> Type) k x. Applicative f => WhenMissing f k x x
- mapMissing :: forall (f :: Type -> Type) k x y. Applicative f => (k -> x -> y) -> WhenMissing f k x y
- filterMissing :: forall (f :: Type -> Type) k x. Applicative f => (k -> x -> Bool) -> WhenMissing f k x x
- data WhenMissing (f :: Type -> Type) k x y = WhenMissing {
- missingSubtree :: Map k x -> f (Map k y)
- missingKey :: k -> x -> f (Maybe y)
- newtype WhenMatched (f :: Type -> Type) k x y z = WhenMatched {
- matchedKey :: k -> x -> y -> f (Maybe z)
- mergeA :: (Applicative f, Ord k) => WhenMissing f k a c -> WhenMissing f k b c -> WhenMatched f k a b c -> Map k a -> Map k b -> f (Map k c)
- zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
- zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z
- traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y
- traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y
- filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x
- mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c
- map :: (a -> b) -> Map k a -> Map k b
- mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
- traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)
- traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
- mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
- mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
- mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
- mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
- mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
- mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a
- foldr :: (a -> b -> b) -> b -> Map k a -> b
- foldl :: (a -> b -> a) -> a -> Map k b -> a
- foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
- foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a
- foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m
- foldr' :: (a -> b -> b) -> b -> Map k a -> b
- foldl' :: (a -> b -> a) -> a -> Map k b -> a
- foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b
- foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a
- elems :: Map k a -> [a]
- keys :: Map k a -> [k]
- assocs :: Map k a -> [(k, a)]
- keysSet :: Map k a -> Set k
- argSet :: Map k a -> Set (Arg k a)
- fromSet :: (k -> a) -> Set k -> Map k a
- fromArgSet :: Set (Arg k a) -> Map k a
- toList :: Map k a -> [(k, a)]
- fromList :: Ord k => [(k, a)] -> Map k a
- fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
- fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
- toAscList :: Map k a -> [(k, a)]
- toDescList :: Map k a -> [(k, a)]
- fromAscList :: Eq k => [(k, a)] -> Map k a
- fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a
- fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
- fromDistinctAscList :: [(k, a)] -> Map k a
- fromDescList :: Eq k => [(k, a)] -> Map k a
- fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a
- fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
- fromDistinctDescList :: [(k, a)] -> Map k a
- filter :: (a -> Bool) -> Map k a -> Map k a
- filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a
- takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
- dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
- spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)
- restrictKeys :: Ord k => Map k a -> Set k -> Map k a
- withoutKeys :: Ord k => Map k a -> Set k -> Map k a
- partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)
- partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
- mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b
- mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b
- mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)
- mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
- split :: Ord k => k -> Map k a -> (Map k a, Map k a)
- splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
- splitRoot :: Map k b -> [Map k b]
- isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool
- isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
- isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool
- isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
- lookupIndex :: Ord k => k -> Map k a -> Maybe Int
- findIndex :: Ord k => k -> Map k a -> Int
- elemAt :: Int -> Map k a -> (k, a)
- updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
- deleteAt :: Int -> Map k a -> Map k a
- take :: Int -> Map k a -> Map k a
- drop :: Int -> Map k a -> Map k a
- splitAt :: Int -> Map k a -> (Map k a, Map k a)
- lookupMin :: Map k a -> Maybe (k, a)
- lookupMax :: Map k a -> Maybe (k, a)
- findMin :: Map k a -> (k, a)
- findMax :: Map k a -> (k, a)
- deleteMin :: Map k a -> Map k a
- deleteMax :: Map k a -> Map k a
- deleteFindMin :: Map k a -> ((k, a), Map k a)
- deleteFindMax :: Map k a -> ((k, a), Map k a)
- updateMin :: (a -> Maybe a) -> Map k a -> Map k a
- updateMax :: (a -> Maybe a) -> Map k a -> Map k a
- updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
- updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
- minView :: Map k a -> Maybe (a, Map k a)
- maxView :: Map k a -> Maybe (a, Map k a)
- minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)
- maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)
- data AreWeStrict
- atKeyImpl :: (Functor f, Ord k) => AreWeStrict -> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
- atKeyPlain :: Ord k => AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
- bin :: k -> a -> Map k a -> Map k a -> Map k a
- balance :: k -> a -> Map k a -> Map k a -> Map k a
- balanceL :: k -> a -> Map k a -> Map k a -> Map k a
- balanceR :: k -> a -> Map k a -> Map k a -> Map k a
- delta :: Int
- insertMax :: k -> a -> Map k a -> Map k a
- link :: k -> a -> Map k a -> Map k a -> Map k a
- link2 :: Map k a -> Map k a -> Map k a
- glue :: Map k a -> Map k a -> Map k a
- fromDistinctAscList_linkTop :: Map k a -> Stack k a -> FromDistinctMonoState k a
- fromDistinctAscList_linkAll :: FromDistinctMonoState k a -> Map k a
- fromDistinctDescList_linkTop :: Map k a -> Stack k a -> FromDistinctMonoState k a
- fromDistinctDescList_linkAll :: FromDistinctMonoState k a -> Map k a
- data MaybeS a
- newtype Identity a = Identity {
- runIdentity :: a
- data FromDistinctMonoState k a
- data Stack k a
- foldl'Stack :: (b -> k -> a -> Map k a -> b) -> b -> Stack k a -> b
- mapWhenMissing :: forall (f :: Type -> Type) a b k x. (Applicative f, Monad f) => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
- mapWhenMatched :: forall (f :: Type -> Type) a b k x y. Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
- lmapWhenMissing :: forall b a (f :: Type -> Type) k x. (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x
- contramapFirstWhenMatched :: forall b a (f :: Type -> Type) k y z. (b -> a) -> WhenMatched f k a y z -> WhenMatched f k b y z
- contramapSecondWhenMatched :: forall b a (f :: Type -> Type) k x z. (b -> a) -> WhenMatched f k x a z -> WhenMatched f k x b z
- mapGentlyWhenMissing :: forall (f :: Type -> Type) a b k x. Functor f => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
- mapGentlyWhenMatched :: forall (f :: Type -> Type) a b k x y. Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
Map type
A Map from keys k
to values a
.
The Semigroup
operation for Map
is union
, which prefers
values from the left operand. If m1
maps a key k
to a value
a1
, and m2
maps the same key to a different value a2
, then
their union m1 <> m2
maps k
to a1
.
Instances
Bifoldable Map Source # | Since: containers-0.6.3.1 |
Eq2 Map Source # | Since: containers-0.5.9 |
Ord2 Map Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal | |
Show2 Map Source # | Since: containers-0.5.9 |
(Lift k, Lift a) => Lift (Map k a :: Type) Source # | Since: containers-0.6.6 |
Foldable (Map k) Source # | Folds in order of increasing key. |
Defined in Data.Map.Internal fold :: Monoid m => Map k m -> m Source # foldMap :: Monoid m => (a -> m) -> Map k a -> m Source # foldMap' :: Monoid m => (a -> m) -> Map k a -> m Source # foldr :: (a -> b -> b) -> b -> Map k a -> b Source # foldr' :: (a -> b -> b) -> b -> Map k a -> b Source # foldl :: (b -> a -> b) -> b -> Map k a -> b Source # foldl' :: (b -> a -> b) -> b -> Map k a -> b Source # foldr1 :: (a -> a -> a) -> Map k a -> a Source # foldl1 :: (a -> a -> a) -> Map k a -> a Source # toList :: Map k a -> [a] Source # null :: Map k a -> Bool Source # length :: Map k a -> Int Source # elem :: Eq a => a -> Map k a -> Bool Source # maximum :: Ord a => Map k a -> a Source # minimum :: Ord a => Map k a -> a Source # | |
Eq k => Eq1 (Map k) Source # | Since: containers-0.5.9 |
Ord k => Ord1 (Map k) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal | |
(Ord k, Read k) => Read1 (Map k) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Map k a) Source # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Map k a] Source # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Map k a) Source # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Map k a] Source # | |
Show k => Show1 (Map k) Source # | Since: containers-0.5.9 |
Traversable (Map k) Source # | Traverses in order of increasing key. |
Functor (Map k) Source # | |
(Data k, Data a, Ord k) => Data (Map k a) Source # | |
Defined in Data.Map.Internal gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Map k a -> c (Map k a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Map k a) Source # toConstr :: Map k a -> Constr Source # dataTypeOf :: Map k a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Map k a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k a)) Source # gmapT :: (forall b. Data b => b -> b) -> Map k a -> Map k a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Map k a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Map k a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Map k a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Map k a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # | |
Ord k => Monoid (Map k v) Source # | |
Ord k => Semigroup (Map k v) Source # | |
Ord k => IsList (Map k v) Source # | Since: containers-0.5.6.2 |
(Ord k, Read k, Read e) => Read (Map k e) Source # | |
(Show k, Show a) => Show (Map k a) Source # | |
(NFData k, NFData a) => NFData (Map k a) Source # | |
Defined in Data.Map.Internal | |
(Eq k, Eq a) => Eq (Map k a) Source # | |
(Ord k, Ord v) => Ord (Map k v) Source # | |
type Item (Map k v) Source # | |
Defined in Data.Map.Internal |
Operators
(!) :: Ord k => Map k a -> k -> a infixl 9 Source #
O(logn). Find the value at a key.
Calls error
when the element can not be found.
fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map fromList [(5,'a'), (3,'b')] ! 5 == 'a'
(!?) :: Ord k => Map k a -> k -> Maybe a infixl 9 Source #
O(logn). Find the value at a key.
Returns Nothing
when the element can not be found.
fromList [(5, 'a'), (3, 'b')] !? 1 == Nothing
fromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a'
Since: containers-0.5.9
Query
null :: Map k a -> Bool Source #
O(1). Is the map empty?
Data.Map.null (empty) == True Data.Map.null (singleton 1 'a') == False
size :: Map k a -> Int Source #
O(1). The number of elements in the map.
size empty == 0 size (singleton 1 'a') == 1 size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
member :: Ord k => k -> Map k a -> Bool Source #
O(logn). Is the key a member of the map? See also notMember
.
member 5 (fromList [(5,'a'), (3,'b')]) == True member 1 (fromList [(5,'a'), (3,'b')]) == False
notMember :: Ord k => k -> Map k a -> Bool Source #
O(logn). Is the key not a member of the map? See also member
.
notMember 5 (fromList [(5,'a'), (3,'b')]) == False notMember 1 (fromList [(5,'a'), (3,'b')]) == True
lookup :: Ord k => k -> Map k a -> Maybe a Source #
O(logn). Lookup the value at a key in the map.
The function will return the corresponding value as (
,
or Just
value)Nothing
if the key isn't in the map.
An example of using lookup
:
import Prelude hiding (lookup) import Data.Map employeeDept = fromList([("John","Sales"), ("Bob","IT")]) deptCountry = fromList([("IT","USA"), ("Sales","France")]) countryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")]) employeeCurrency :: String -> Maybe String employeeCurrency name = do dept <- lookup name employeeDept country <- lookup dept deptCountry lookup country countryCurrency main = do putStrLn $ "John's currency: " ++ (show (employeeCurrency "John")) putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete"))
The output of this program:
John's currency: Just "Euro" Pete's currency: Nothing
findWithDefault :: Ord k => a -> k -> Map k a -> a Source #
O(logn). The expression (
returns
the value at key findWithDefault
def k map)k
or returns default value def
when the key is not in the map.
findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v) Source #
O(logn). Find largest key smaller than the given one and return the corresponding (key, value) pair.
lookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing lookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v) Source #
O(logn). Find smallest key greater than the given one and return the corresponding (key, value) pair.
lookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b') lookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v) Source #
O(logn). Find largest key smaller or equal to the given one and return the corresponding (key, value) pair.
lookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing lookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a') lookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v) Source #
O(logn). Find smallest key greater or equal to the given one and return the corresponding (key, value) pair.
lookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a') lookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b') lookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
Construction
singleton :: k -> a -> Map k a Source #
O(1). A map with a single element.
singleton 1 'a' == fromList [(1, 'a')] size (singleton 1 'a') == 1
Insertion
insert :: Ord k => k -> a -> Map k a -> Map k a Source #
O(logn). Insert a new key and value in the map.
If the key is already present in the map, the associated value is
replaced with the supplied value. insert
is equivalent to
.insertWith
const
insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')] insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')] insert 5 'x' empty == singleton 5 'x'
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a Source #
O(logn). Insert with a function, combining new value and old value.
will insert the pair (key, value) into insertWith
f key value mpmp
if key does
not exist in the map. If the key does exist, the function will
insert the pair (key, f new_value old_value)
.
insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")] insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] insertWith (++) 5 "xxx" empty == singleton 5 "xxx"
Also see the performance note on fromListWith
.
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a Source #
O(logn). Insert with a function, combining key, new value and old value.
will insert the pair (key, value) into insertWithKey
f key value mpmp
if key does
not exist in the map. If the key does exist, the function will
insert the pair (key,f key new_value old_value)
.
Note that the key passed to f is the same key passed to insertWithKey
.
let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")] insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] insertWithKey f 5 "xxx" empty == singleton 5 "xxx"
Also see the performance note on fromListWith
.
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a) Source #
O(logn). Combines insert operation with old value retrieval.
The expression (
)
is a pair where the first element is equal to (insertLookupWithKey
f k x map
)
and the second element equal to (lookup
k map
).insertWithKey
f k x map
let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")]) insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")]) insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx")
This is how to define insertLookup
using insertLookupWithKey
:
let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")]) insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")])
Also see the performance note on fromListWith
.
Delete/Update
delete :: Ord k => k -> Map k a -> Map k a Source #
O(logn). Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.
delete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] delete 5 empty == empty
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a Source #
O(logn). Update a value at a specific key with the result of the provided function. When the key is not a member of the map, the original map is returned.
adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] adjust ("new " ++) 7 empty == empty
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a Source #
O(logn). Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.
let f key x = (show key) ++ ":new " ++ x adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] adjustWithKey f 7 empty == empty
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a Source #
O(logn). The expression (
) updates the value update
f k mapx
at k
(if it is in the map). If (f x
) is Nothing
, the element is
deleted. If it is (
), the key Just
yk
is bound to the new value y
.
let f x = if x == "a" then Just "new a" else Nothing update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a Source #
O(logn). The expression (
) updates the
value updateWithKey
f k mapx
at k
(if it is in the map). If (f k x
) is Nothing
,
the element is deleted. If it is (
), the key Just
yk
is bound
to the new value y
.
let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a) Source #
O(logn). Lookup and update. See also updateWithKey
.
The function returns changed value, if it is updated.
Returns the original key value if the map entry is deleted.
let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")]) updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")]) updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a Source #
O(logn). The expression (
) alters the value alter
f k mapx
at k
, or absence thereof.
alter
can be used to insert, delete, or update a value in a Map
.
In short :
.lookup
k (alter
f k m) = f (lookup
k m)
let f _ = Nothing alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] alter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" let f _ = Just "c" alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")] alter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")]
Note that
.adjust
= alter . fmap
alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a) Source #
O(logn). The expression (
) alters the value alterF
f k mapx
at
k
, or absence thereof. alterF
can be used to inspect, insert, delete,
or update a value in a Map
. In short:
.lookup
k <$> alterF
f k m = f
(lookup
k m)
Example:
interactiveAlter :: Int -> Map Int String -> IO (Map Int String) interactiveAlter k m = alterF f k m where f Nothing = do putStrLn $ show k ++ " was not found in the map. Would you like to add it?" getUserResponse1 :: IO (Maybe String) f (Just old) = do putStrLn $ "The key is currently bound to " ++ show old ++ ". Would you like to change or delete it?" getUserResponse2 :: IO (Maybe String)
alterF
is the most general operation for working with an individual
key that may or may not be in a given map. When used with trivial
functors like Identity
and Const
, it is often slightly slower than
more specialized combinators like lookup
and insert
. However, when
the functor is non-trivial and key comparison is not particularly cheap,
it is the fastest way.
Note on rewrite rules:
This module includes GHC rewrite rules to optimize alterF
for
the Const
and Identity
functors. In general, these rules
improve performance. The sole exception is that when using
Identity
, deleting a key that is already absent takes longer
than it would without the rules. If you expect this to occur
a very large fraction of the time, you might consider using a
private copy of the Identity
type.
Note: alterF
is a flipped version of the at
combinator from
Control.Lens.At
.
Since: containers-0.5.8
Combine
Union
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Union with a combining function.
unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
Also see the performance note on fromListWith
.
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Union with a combining function.
let f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
Also see the performance note on fromListWith
.
unions :: (Foldable f, Ord k) => f (Map k a) -> Map k a Source #
The union of a list of maps:
(
).unions
== foldl
union
empty
unions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] == fromList [(3, "b"), (5, "a"), (7, "C")] unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])] == fromList [(3, "B3"), (5, "A3"), (7, "C")]
unionsWith :: (Foldable f, Ord k) => (a -> a -> a) -> f (Map k a) -> Map k a Source #
The union of a list of maps, with a combining operation:
(
).unionsWith
f == foldl
(unionWith
f) empty
unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
Difference
difference :: Ord k => Map k a -> Map k b -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Difference of two maps. Return elements of the first map not existing in the second map.
difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a Source #
O(n+m). Difference with a combining function.
When two equal keys are
encountered, the combining function is applied to the values of these keys.
If it returns Nothing
, the element is discarded (proper set difference). If
it returns (
), the element is updated with a new value Just
yy
.
let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")]) == singleton 3 "b:B"
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a Source #
O(n+m). Difference with a combining function. When two equal keys are
encountered, the combining function is applied to the key and both values.
If it returns Nothing
, the element is discarded (proper set difference). If
it returns (
), the element is updated with a new value Just
yy
.
let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")]) == singleton 3 "3:b|B"
Intersection
intersection :: Ord k => Map k a -> Map k b -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Intersection of two maps.
Return data in the first map for the keys existing in both maps.
(
).intersection
m1 m2 == intersectionWith
const
m1 m2
intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c Source #
O(mlog(nm+1)),0<m≤n. Intersection with a combining function.
intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c Source #
O(mlog(nm+1)),0<m≤n. Intersection with a combining function.
let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
Disjoint
disjoint :: Ord k => Map k a -> Map k b -> Bool Source #
O(mlog(nm+1)),0<m≤n. Check whether the key sets of two
maps are disjoint (i.e., their intersection
is empty).
disjoint (fromList [(2,'a')]) (fromList [(1,()), (3,())]) == True disjoint (fromList [(2,'a')]) (fromList [(1,'a'), (2,'b')]) == False disjoint (fromList []) (fromList []) == True
xs`disjoint`
ys = null (xs`intersection`
ys)
Since: containers-0.6.2.1
Compose
compose :: Ord b => Map b c -> Map a b -> Map a c Source #
Relate the keys of one map to the values of the other, by using the values of the former as keys for lookups in the latter.
Complexity: O(n∗log(m)), where m is the size of the first argument
compose (fromList [('a', "A"), ('b', "B")]) (fromList [(1,'a'),(2,'b'),(3,'z')]) = fromList [(1,"A"),(2,"B")]
(compose
bc ab!?
) = (bc!?
) <=< (ab!?
)
Note: Prior to v0.6.4, Data.Map.Strict exposed a version of
compose
that forced the values of the output Map
. This version does not
force these values.
Since: containers-0.6.3.1
General combining function
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: containers-0.5.9
type SimpleWhenMatched = WhenMatched Identity Source #
A tactic for dealing with keys present in both maps in merge
.
A tactic of type SimpleWhenMatched k x y z
is an abstract representation
of a function of type k -> x -> y -> Maybe z
.
Since: containers-0.5.9
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z) Source #
Along with zipWithMaybeAMatched, witnesses the isomorphism between
WhenMatched f k x y z
and k -> x -> y -> f (Maybe z)
.
Since: containers-0.5.9
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y) Source #
Along with traverseMaybeMissing, witnesses the isomorphism between
WhenMissing f k x y
and k -> x -> f (Maybe y)
.
Since: containers-0.5.9
:: Ord k | |
=> SimpleWhenMissing k a c | What to do with keys in |
-> SimpleWhenMissing k b c | What to do with keys in |
-> SimpleWhenMatched k a b c | What to do with keys in both |
-> Map k a | Map |
-> Map k b | Map |
-> Map k c |
Merge two maps.
merge
takes two WhenMissing
tactics, a WhenMatched
tactic and two maps. It uses the tactics to merge the maps.
Its behavior is best understood via its fundamental tactics,
mapMaybeMissing
and zipWithMaybeMatched
.
Consider
merge (mapMaybeMissing g1) (mapMaybeMissing g2) (zipWithMaybeMatched f) m1 m2
Take, for example,
m1 = [(0, 'a'), (1, 'b'), (3, 'c'), (4, 'd')] m2 = [(1, "one"), (2, "two"), (4, "three")]
merge
will first "align" these maps by key:
m1 = [(0, 'a'), (1, 'b'), (3, 'c'), (4, 'd')] m2 = [(1, "one"), (2, "two"), (4, "three")]
It will then pass the individual entries and pairs of entries
to g1
, g2
, or f
as appropriate:
maybes = [g1 0 'a', f 1 'b' "one", g2 2 "two", g1 3 'c', f 4 'd' "three"]
This produces a Maybe
for each key:
keys = 0 1 2 3 4 results = [Nothing, Just True, Just False, Nothing, Just True]
Finally, the Just
results are collected into a map:
return value = [(1, True), (2, False), (4, True)]
The other tactics below are optimizations or simplifications of
mapMaybeMissing
for special cases. Most importantly,
dropMissing
drops all the keys.preserveMissing
leaves all the entries 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.
Examples:
unionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
intersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
differenceWith f = merge preserveMissing dropMissing (zipWithMatched f)
symmetricDifference = merge preserveMissing preserveMissing (zipWithMaybeMatched $ \ _ _ _ -> Nothing)
mapEachPiece f g h = merge (mapMissing f) (mapMissing g) (zipWithMatched h)
Since: containers-0.5.9
WhenMatched
tactics
zipWithMaybeMatched :: forall (f :: Type -> Type) k x y z. Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z Source #
When a key is found in both maps, apply a function to the key and values and maybe use the result in the merged map.
zipWithMaybeMatched :: (k -> x -> y -> Maybe z) -> SimpleWhenMatched k x y z
Since: containers-0.5.9
zipWithMatched :: forall (f :: Type -> Type) k x y z. Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z Source #
When a key is found in both maps, apply a function to the key and values and use the result in the merged map.
zipWithMatched :: (k -> x -> y -> z) -> SimpleWhenMatched k x y z
Since: containers-0.5.9
WhenMissing
tactics
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: containers-0.5.9
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: containers-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: containers-0.5.9
preserveMissing' :: forall (f :: Type -> Type) k x. Applicative f => WhenMissing f k x x Source #
Force the entries whose keys are missing from the other map and otherwise preserve them unchanged.
preserveMissing' :: SimpleWhenMissing k x x
preserveMissing' = Merge.Lazy.mapMaybeMissing (\_ x -> Just $! x)
but preserveMissing'
is quite a bit faster.
Since: containers-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: containers-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: containers-0.5.9
Applicative general combining function
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: containers-0.5.9
WhenMissing | |
|
Instances
(Applicative f, Monad f) => Category (WhenMissing f k :: Type -> Type -> Type) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal id :: WhenMissing f k a a Source # (.) :: WhenMissing f k b c -> WhenMissing f k a b -> WhenMissing f k a c Source # | |
(Applicative f, Monad f) => Applicative (WhenMissing f k x) Source # | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal pure :: a -> WhenMissing f k x a Source # (<*>) :: WhenMissing f k x (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b Source # liftA2 :: (a -> b -> c) -> WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x c Source # (*>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b Source # (<*) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x a Source # | |
(Applicative f, Monad f) => Functor (WhenMissing f k x) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal fmap :: (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b Source # (<$) :: a -> WhenMissing f k x b -> WhenMissing f k x a Source # | |
(Applicative f, Monad f) => Monad (WhenMissing f k x) Source # | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal (>>=) :: WhenMissing f k x a -> (a -> WhenMissing f k x b) -> WhenMissing f k x b Source # (>>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b Source # return :: a -> WhenMissing f k x a Source # |
newtype WhenMatched (f :: Type -> Type) k x y z Source #
A tactic for dealing with keys present in both
maps in merge
or mergeA
.
A tactic of type WhenMatched f k x y z
is an abstract representation
of a function of type k -> x -> y -> f (Maybe z)
.
Since: containers-0.5.9
WhenMatched | |
|
Instances
(Monad f, Applicative f) => Category (WhenMatched f k x :: Type -> Type -> Type) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal id :: WhenMatched f k x a a Source # (.) :: WhenMatched f k x b c -> WhenMatched f k x a b -> WhenMatched f k x a c Source # | |
(Monad f, Applicative f) => Applicative (WhenMatched f k x y) Source # | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal pure :: a -> WhenMatched f k x y a Source # (<*>) :: WhenMatched f k x y (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b Source # liftA2 :: (a -> b -> c) -> WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y c Source # (*>) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y b Source # (<*) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y a Source # | |
Functor f => Functor (WhenMatched f k x y) Source # | Since: containers-0.5.9 |
Defined in Data.Map.Internal fmap :: (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b Source # (<$) :: a -> WhenMatched f k x y b -> WhenMatched f k x y a Source # | |
(Monad f, Applicative f) => Monad (WhenMatched f k x y) Source # | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal (>>=) :: WhenMatched f k x y a -> (a -> WhenMatched f k x y b) -> WhenMatched f k x y b Source # (>>) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y b Source # return :: a -> WhenMatched f k x y a Source # |
:: (Applicative f, Ord k) | |
=> WhenMissing f k a c | What to do with keys in |
-> WhenMissing f k b c | What to do with keys in |
-> WhenMatched f k a b c | What to do with keys in both |
-> Map k a | Map |
-> Map k b | Map |
-> f (Map k c) |
An applicative version of merge
.
mergeA
takes two WhenMissing
tactics, a WhenMatched
tactic and two maps. It uses the tactics to merge the maps.
Its behavior is best understood via its fundamental tactics,
traverseMaybeMissing
and zipWithMaybeAMatched
.
Consider
mergeA (traverseMaybeMissing g1) (traverseMaybeMissing g2) (zipWithMaybeAMatched f) m1 m2
Take, for example,
m1 = [(0, 'a'), (1, 'b'), (3, 'c'), (4, 'd')] m2 = [(1, "one"), (2, "two"), (4, "three")]
mergeA
will first "align" these maps by key:
m1 = [(0, 'a'), (1, 'b'), (3, 'c'), (4, 'd')] m2 = [(1, "one"), (2, "two"), (4, "three")]
It will then pass the individual entries and pairs of entries
to g1
, g2
, or f
as appropriate:
actions = [g1 0 'a', f 1 'b' "one", g2 2 "two", g1 3 'c', f 4 'd' "three"]
Next, it will perform the actions in the actions
list in order from
left to right.
keys = 0 1 2 3 4 results = [Nothing, Just True, Just False, Nothing, Just True]
Finally, the Just
results are collected into a map:
return value = [(1, True), (2, False), (4, True)]
The other tactics below are optimizations or simplifications of
traverseMaybeMissing
for special cases. Most importantly,
dropMissing
drops all the keys.preserveMissing
leaves all the entries alone.mapMaybeMissing
does not use theApplicative
context.
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.
Since: containers-0.5.9
WhenMatched
tactics
zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z Source #
When a key is found in both maps, apply a function to the key and values, perform the resulting action, and maybe use the result in the merged map.
This is the fundamental WhenMatched
tactic.
Since: containers-0.5.9
zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z Source #
When a key is found in both maps, apply a function to the key and values to produce an action and use its result in the merged map.
Since: containers-0.5.9
WhenMissing
tactics
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: containers-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: containers-0.5.9
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: containers-0.5.9
Deprecated general combining function
mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c Source #
O(n+m). An unsafe general combining function.
WARNING: This function can produce corrupt maps and its results
may depend on the internal structures of its inputs. Users should
prefer merge
or mergeA
.
When mergeWithKey
is given three arguments, it is inlined to the call
site. You should therefore use mergeWithKey
only to define custom
combining functions. For example, you could define unionWithKey
,
differenceWithKey
and intersectionWithKey
as
myUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) id id m1 m2 myDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2 myIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) (const empty) (const empty) m1 m2
When calling
, a function combining two
mergeWithKey
combine only1 only2Map
s is created, such that
- if a key is present in both maps, it is passed with both corresponding
values to the
combine
function. Depending on the result, the key is either present in the result with specified value, or is left out; - a nonempty subtree present only in the first map is passed to
only1
and the output is added to the result; - a nonempty subtree present only in the second map is passed to
only2
and the output is added to the result.
The only1
and only2
methods must return a map with a subset (possibly empty) of the keys of the given map.
The values can be modified arbitrarily. Most common variants of only1
and
only2
are id
and
, but for example const
empty
,
map
f
, or filterWithKey
f
could be used for any mapMaybeWithKey
ff
.
Traversal
Map
map :: (a -> b) -> Map k a -> Map k b Source #
O(n). Map a function over all values in the map.
map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b Source #
O(n). Map a function over all values in the map.
let f key x = (show key) ++ ":" ++ x mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b) Source #
O(n).
That is, behaves exactly like a regular traverseWithKey
f m == fromList
<$> traverse
(\(k, v) -> (,) k <$> f k v) (toList
m)traverse
except that the traversing
function also has access to the key associated with a value.
traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')]) traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')]) == Nothing
traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b) Source #
O(n). Traverse keys/values and collect the Just
results.
Since: containers-0.5.8
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source #
O(n). The function mapAccum
threads an accumulating
argument through the map in ascending order of keys.
let f a b = (a ++ b, b ++ "X") mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source #
O(n). The function mapAccumWithKey
threads an accumulating
argument through the map in ascending order of keys.
let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X") mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source #
O(n). The function mapAccumRWithKey
threads an accumulating
argument through the map in descending order of keys.
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a Source #
O(nlogn).
is the map obtained by applying mapKeys
f sf
to each key of s
.
The size of the result may be smaller if f
maps two or more distinct
keys to the same new key. In this case the value at the greatest of the
original keys is retained.
mapKeys (+ 1) (fromList [(5,"a"), (3,"b")]) == fromList [(4, "b"), (6, "a")] mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c" mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a Source #
O(nlogn).
is the map obtained by applying mapKeysWith
c f sf
to each key of s
.
The size of the result may be smaller if f
maps two or more distinct
keys to the same new key. In this case the associated values will be
combined using c
. The value at the greater of the two original keys
is used as the first argument to c
.
mapKeysWith (++) (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab" mapKeysWith (++) (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
Also see the performance note on fromListWith
.
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a Source #
O(n).
, but works only when mapKeysMonotonic
f s == mapKeys
f sf
is strictly monotonic.
That is, for any values x
and y
, if x
< y
then f x
< f y
.
The precondition is not checked.
Semi-formally, we have:
and [x < y ==> f x < f y | x <- ls, y <- ls] ==> mapKeysMonotonic f s == mapKeys f s where ls = keys s
This means that f
maps distinct original keys to distinct resulting keys.
This function has better performance than mapKeys
.
mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")] valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False
Folds
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b Source #
O(n). Fold the keys and values in the map using the given right-associative
binary operator, such that
.foldrWithKey
f z == foldr
(uncurry
f) z . toAscList
For example,
keys map = foldrWithKey (\k x ks -> k:ks) [] map
let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" foldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a Source #
O(n). Fold the keys and values in the map using the given left-associative
binary operator, such that
.foldlWithKey
f z == foldl
(\z' (kx, x) -> f z' kx x) z . toAscList
For example,
keys = reverse . foldlWithKey (\ks k x -> k:ks) []
let f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" foldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m Source #
O(n). Fold the keys and values in the map using the given monoid, such that
foldMapWithKey
f =fold
.mapWithKey
f
This can be an asymptotically faster than foldrWithKey
or foldlWithKey
for some monoids.
Since: containers-0.5.4
Strict folds
foldr' :: (a -> b -> b) -> b -> Map k a -> b Source #
O(n). A strict version of foldr
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Map k b -> a Source #
O(n). A strict version of foldl
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b Source #
O(n). A strict version of foldrWithKey
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a Source #
O(n). A strict version of foldlWithKey
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
Conversion
elems :: Map k a -> [a] Source #
O(n). Return all elements of the map in the ascending order of their keys. Subject to list fusion.
elems (fromList [(5,"a"), (3,"b")]) == ["b","a"] elems empty == []
keys :: Map k a -> [k] Source #
O(n). Return all keys of the map in ascending order. Subject to list fusion.
keys (fromList [(5,"a"), (3,"b")]) == [3,5] keys empty == []
assocs :: Map k a -> [(k, a)] Source #
O(n). An alias for toAscList
. Return all key/value pairs in the map
in ascending key order. Subject to list fusion.
assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] assocs empty == []
keysSet :: Map k a -> Set k Source #
O(n). The set of all keys of the map.
keysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5] keysSet empty == Data.Set.empty
argSet :: Map k a -> Set (Arg k a) Source #
O(n). The set of all elements of the map contained in Arg
s.
argSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [Arg 3 "b",Arg 5 "a"] argSet empty == Data.Set.empty
fromSet :: (k -> a) -> Set k -> Map k a Source #
O(n). Build a map from a set of keys and a function which for each key computes its value.
fromSet (\k -> replicate k 'a') (Data.Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")] fromSet undefined Data.Set.empty == empty
fromArgSet :: Set (Arg k a) -> Map k a Source #
O(n). Build a map from a set of elements contained inside Arg
s.
fromArgSet (Data.Set.fromList [Arg 3 "aaa", Arg 5 "aaaaa"]) == fromList [(5,"aaaaa"), (3,"aaa")] fromArgSet Data.Set.empty == empty
Lists
toList :: Map k a -> [(k, a)] Source #
O(n). Convert the map to a list of key/value pairs. Subject to list fusion.
toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] toList empty == []
fromList :: Ord k => [(k, a)] -> Map k a Source #
O(nlogn). Build a map from a list of key/value pairs. See also fromAscList
.
If the list contains more than one value for the same key, the last value
for the key is retained.
If the keys of the list are ordered, a linear-time implementation is used.
fromList [] == empty fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")] fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a Source #
O(nlogn). Build a map from a list of key/value pairs with a combining function. See also fromAscListWith
.
fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")] fromListWith (++) [] == empty
Note the reverse ordering of "cba"
in the example.
The symmetric combining function f
is applied in a left-fold over the list, as f new old
.
Performance
You should ensure that the given f
is fast with this order of arguments.
Symmetric functions may be slow in one order, and fast in another. For the common case of collecting values of matching keys in a list, as above:
The complexity of (++) a b
is O(a), so it is fast when given a short list as its first argument.
Thus:
fromListWith (++) (replicate 1000000 (3, "x")) -- O(n), fast fromListWith (flip (++)) (replicate 1000000 (3, "x")) -- O(n²), extremely slow
because they evaluate as, respectively:
fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))] -- O(n) fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")] -- O(n²)
Thus, to get good performance with an operation like (++)
while also preserving
the same order as in the input list, reverse the input:
fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
and it is always fast to combine singleton-list values [v]
with fromListWith (++)
, as in:
fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a Source #
O(nlogn). Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey
.
let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")] fromListWithKey f [] == empty
Also see the performance note on fromListWith
.
Ordered lists
toAscList :: Map k a -> [(k, a)] Source #
O(n). Convert the map to a list of key/value pairs where the keys are in ascending order. Subject to list fusion.
toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
toDescList :: Map k a -> [(k, a)] Source #
O(n). Convert the map to a list of key/value pairs where the keys are in descending order. Subject to list fusion.
toDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
fromAscList :: Eq k => [(k, a)] -> Map k a Source #
O(n). Build a map from an ascending list in linear time. The precondition (input list is ascending) is not checked.
fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")] valid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True valid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a Source #
O(n). Build a map from an ascending list in linear time with a combining function for equal keys. The precondition (input list is ascending) is not checked.
fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")] valid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True valid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a Source #
O(n). Build a map from an ascending list in linear time with a combining function for equal keys. The precondition (input list is ascending) is not checked.
let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2 fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")] valid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True valid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
Also see the performance note on fromListWith
.
fromDistinctAscList :: [(k, a)] -> Map k a Source #
O(n). Build a map from an ascending list of distinct elements in linear time. The precondition is not checked.
fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] valid (fromDistinctAscList [(3,"b"), (5,"a")]) == True valid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
fromDescList :: Eq k => [(k, a)] -> Map k a Source #
O(n). Build a map from a descending list in linear time. The precondition (input list is descending) is not checked.
fromDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")] fromDescList [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "b")] valid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True valid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
Since: containers-0.5.8
fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a Source #
O(n). Build a map from a descending list in linear time with a combining function for equal keys. The precondition (input list is descending) is not checked.
fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")] valid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True valid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
Also see the performance note on fromListWith
.
Since: containers-0.5.8
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a Source #
O(n). Build a map from a descending list in linear time with a combining function for equal keys. The precondition (input list is descending) is not checked.
let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2 fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")] valid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True valid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
Also see the performance note on fromListWith
.
fromDistinctDescList :: [(k, a)] -> Map k a Source #
O(n). Build a map from a descending list of distinct elements in linear time. The precondition is not checked.
fromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")] valid (fromDistinctDescList [(5,"a"), (3,"b")]) == True valid (fromDistinctDescList [(5,"a"), (5,"b"), (3,"b")]) == False
Since: containers-0.5.8
Filter
filter :: (a -> Bool) -> Map k a -> Map k a Source #
O(n). Filter all values that satisfy the predicate.
filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a Source #
O(n). Filter all keys/values that satisfy the predicate.
filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a Source #
O(logn). Take while a predicate on the keys holds.
The user is responsible for ensuring that for all keys j
and k
in the map,
j < k ==> p j >= p k
. See note at spanAntitone
.
takeWhileAntitone p =fromDistinctAscList
.takeWhile
(p . fst) .toList
takeWhileAntitone p =filterWithKey
(k _ -> p k)
Since: containers-0.5.8
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a Source #
O(logn). Drop while a predicate on the keys holds.
The user is responsible for ensuring that for all keys j
and k
in the map,
j < k ==> p j >= p k
. See note at spanAntitone
.
dropWhileAntitone p =fromDistinctAscList
.dropWhile
(p . fst) .toList
dropWhileAntitone p =filterWithKey
(\k _ -> not (p k))
Since: containers-0.5.8
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a) Source #
O(logn). Divide a map at the point where a predicate on the keys stops holding.
The user is responsible for ensuring that for all keys j
and k
in the map,
j < k ==> p j >= p k
.
spanAntitone p xs = (takeWhileAntitone
p xs,dropWhileAntitone
p xs) spanAntitone p xs = partitionWithKey (\k _ -> p k) xs
Note: if p
is not actually antitone, then spanAntitone
will split the map
at some unspecified point where the predicate switches from holding to not
holding (where the predicate is seen to hold before the first key and to fail
after the last key).
Since: containers-0.5.8
restrictKeys :: Ord k => Map k a -> Set k -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Restrict a Map
to only those keys
found in a Set
.
m `restrictKeys` s =filterWithKey
(\k _ -> k`member`
s) m m `restrictKeys` s = m`intersection`
fromSet
(const ()) s
Since: containers-0.5.8
withoutKeys :: Ord k => Map k a -> Set k -> Map k a Source #
O(mlog(nm+1)),0<m≤n. Remove all keys in a Set
from a Map
.
m `withoutKeys` s =filterWithKey
(\k _ -> k`notMember`
s) m m `withoutKeys` s = m`difference`
fromSet
(const ()) s
Since: containers-0.5.8
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a) Source #
O(n). Partition the map according to a predicate. The first
map contains all elements that satisfy the predicate, the second all
elements that fail the predicate. See also split
.
partition (> "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a) Source #
O(n). Partition the map according to a predicate. The first
map contains all elements that satisfy the predicate, the second all
elements that fail the predicate. See also split
.
partitionWithKey (\ k _ -> k > 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b") partitionWithKey (\ k _ -> k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) partitionWithKey (\ k _ -> k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b Source #
O(n). Map values and collect the Just
results.
let f x = if x == "a" then Just "new a" else Nothing mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b Source #
O(n). Map keys/values and collect the Just
results.
let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c) Source #
O(n). Map values and separate the Left
and Right
results.
let f a = if a < "c" then Left a else Right a mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")]) mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c) Source #
O(n). Map keys/values and separate the Left
and Right
results.
let f k a = if k < 5 then Left (k * 2) else Right (a ++ a) mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")]) mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
split :: Ord k => k -> Map k a -> (Map k a, Map k a) Source #
O(logn). The expression (
) is a pair split
k map(map1,map2)
where
the keys in map1
are smaller than k
and the keys in map2
larger than k
.
Any key equal to k
is found in neither map1
nor map2
.
split 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")]) split 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a") split 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") split 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty) split 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a) Source #
O(logn). The expression (
) splits a map just
like splitLookup
k mapsplit
but also returns
.lookup
k map
splitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")]) splitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a") splitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a") splitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty) splitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
splitRoot :: Map k b -> [Map k b] Source #
O(1). Decompose a map into pieces based on the structure of the underlying tree. This function is useful for consuming a map in parallel.
No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first submap less than all elements in the second, and so on).
Examples:
splitRoot (fromList (zip [1..6] ['a'..])) == [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d')],fromList [(5,'e'),(6,'f')]]
splitRoot empty == []
Note that the current implementation does not return more than three submaps, but you should not depend on this behaviour because it can change in the future without notice.
Since: containers-0.5.4
Submap
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source #
O(mlog(nm+1)),0<m≤n.
This function is defined as (
).isSubmapOf
= isSubmapOfBy
(==)
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source #
O(mlog(nm+1)),0<m≤n.
The expression (
) returns isSubmapOfBy
f t1 t2True
if
all keys in t1
are in tree t2
, and when f
returns True
when
applied to their respective values. For example, the following
expressions are all True
:
isSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)]) isSubmapOfBy (<=) (fromList [('a',1)]) (fromList [('a',1),('b',2)]) isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
But the following are all False
:
isSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)]) isSubmapOfBy (<) (fromList [('a',1)]) (fromList [('a',1),('b',2)]) isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
Note that isSubmapOfBy (_ _ -> True) m1 m2
tests whether all the keys
in m1
are also keys in m2
.
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source #
O(mlog(nm+1)),0<m≤n. Is this a proper submap? (ie. a submap but not equal).
Defined as (
).isProperSubmapOf
= isProperSubmapOfBy
(==)
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source #
O(mlog(nm+1)),0<m≤n. Is this a proper submap? (ie. a submap but not equal).
The expression (
) returns isProperSubmapOfBy
f m1 m2True
when
keys m1
and keys m2
are not equal,
all keys in m1
are in m2
, and when f
returns True
when
applied to their respective values. For example, the following
expressions are all True
:
isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
But the following are all False
:
isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)]) isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)]) isProperSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
Indexed
lookupIndex :: Ord k => k -> Map k a -> Maybe Int Source #
O(logn). Lookup the index of a key, which is its zero-based index in
the sequence sorted by keys. The index is a number from 0 up to, but not
including, the size
of the map.
isJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")])) == False fromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0 fromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1 isJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")])) == False
findIndex :: Ord k => k -> Map k a -> Int Source #
O(logn). Return the index of a key, which is its zero-based index in
the sequence sorted by keys. The index is a number from 0 up to, but not
including, the size
of the map. Calls error
when the key is not
a member
of the map.
findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0 findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1 findIndex 6 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map
elemAt :: Int -> Map k a -> (k, a) Source #
O(logn). Retrieve an element by its index, i.e. by its zero-based
index in the sequence sorted by keys. If the index is out of range (less
than zero, greater or equal to size
of the map), error
is called.
elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b") elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a") elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a Source #
O(logn). Update the element at index, i.e. by its zero-based index in
the sequence sorted by keys. If the index is out of range (less than zero,
greater or equal to size
of the map), error
is called.
updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")] updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")] updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range updateAt (\ _ _ -> Just "x") (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range updateAt (\_ _ -> Nothing) 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" updateAt (\_ _ -> Nothing) 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" updateAt (\_ _ -> Nothing) 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range updateAt (\_ _ -> Nothing) (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range
deleteAt :: Int -> Map k a -> Map k a Source #
O(logn). Delete the element at index, i.e. by its zero-based index in
the sequence sorted by keys. If the index is out of range (less than zero,
greater or equal to size
of the map), error
is called.
deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range deleteAt (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range
take :: Int -> Map k a -> Map k a Source #
O(logn). Take a given number of entries in key order, beginning with the smallest keys.
take n =fromDistinctAscList
.take
n .toAscList
Since: containers-0.5.8
drop :: Int -> Map k a -> Map k a Source #
O(logn). Drop a given number of entries in key order, beginning with the smallest keys.
drop n =fromDistinctAscList
.drop
n .toAscList
Since: containers-0.5.8
Min/Max
lookupMin :: Map k a -> Maybe (k, a) Source #
O(logn). The minimal key of the map. Returns Nothing
if the map is empty.
lookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b") lookupMin empty = Nothing
Since: containers-0.5.9
lookupMax :: Map k a -> Maybe (k, a) Source #
O(logn). The maximal key of the map. Returns Nothing
if the map is empty.
lookupMax (fromList [(5,"a"), (3,"b")]) == Just (5,"a") lookupMax empty = Nothing
Since: containers-0.5.9
findMin :: Map k a -> (k, a) Source #
O(logn). The minimal key of the map. Calls error
if the map is empty.
findMin (fromList [(5,"a"), (3,"b")]) == (3,"b") findMin empty Error: empty map has no minimal element
findMax :: Map k a -> (k, a) Source #
O(logn). The maximal key of the map. Calls error
if the map is empty.
findMax (fromList [(5,"a"), (3,"b")]) == (5,"a") findMax empty Error: empty map has no maximal element
deleteMin :: Map k a -> Map k a Source #
O(logn). Delete the minimal key. Returns an empty map if the map is empty.
deleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")] deleteMin empty == empty
deleteMax :: Map k a -> Map k a Source #
O(logn). Delete the maximal key. Returns an empty map if the map is empty.
deleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")] deleteMax empty == empty
deleteFindMin :: Map k a -> ((k, a), Map k a) Source #
O(logn). Delete and find the minimal element.
deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")]) deleteFindMin empty Error: can not return the minimal element of an empty map
deleteFindMax :: Map k a -> ((k, a), Map k a) Source #
O(logn). Delete and find the maximal element.
deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")]) deleteFindMax empty Error: can not return the maximal element of an empty map
updateMin :: (a -> Maybe a) -> Map k a -> Map k a Source #
O(logn). Update the value at the minimal key.
updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")] updateMin (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
updateMax :: (a -> Maybe a) -> Map k a -> Map k a Source #
O(logn). Update the value at the maximal key.
updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")] updateMax (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a Source #
O(logn). Update the value at the minimal key.
updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")] updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a Source #
O(logn). Update the value at the maximal key.
updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")] updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
minView :: Map k a -> Maybe (a, Map k a) Source #
O(logn). Retrieves the value associated with minimal key of the
map, and the map stripped of that element, or Nothing
if passed an
empty map.
minView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a") minView empty == Nothing
maxView :: Map k a -> Maybe (a, Map k a) Source #
O(logn). Retrieves the value associated with maximal key of the
map, and the map stripped of that element, or Nothing
if passed an
empty map.
maxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b") maxView empty == Nothing
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a) Source #
O(logn). Retrieves the minimal (key,value) pair of the map, and
the map stripped of that element, or Nothing
if passed an empty map.
minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a") minViewWithKey empty == Nothing
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a) Source #
O(logn). Retrieves the maximal (key,value) pair of the map, and
the map stripped of that element, or Nothing
if passed an empty map.
maxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b") maxViewWithKey empty == Nothing
data AreWeStrict Source #
atKeyImpl :: (Functor f, Ord k) => AreWeStrict -> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a) Source #
atKeyPlain :: Ord k => AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a Source #
fromDistinctAscList_linkTop :: Map k a -> Stack k a -> FromDistinctMonoState k a Source #
fromDistinctAscList_linkAll :: FromDistinctMonoState k a -> Map k a Source #
fromDistinctDescList_linkTop :: Map k a -> Stack k a -> FromDistinctMonoState k a Source #
fromDistinctDescList_linkAll :: FromDistinctMonoState k a -> Map k a Source #
Instances
Foldable MaybeS Source # | |
Defined in Utils.Containers.Internal.StrictMaybe fold :: Monoid m => MaybeS m -> m Source # foldMap :: Monoid m => (a -> m) -> MaybeS a -> m Source # foldMap' :: Monoid m => (a -> m) -> MaybeS a -> m Source # foldr :: (a -> b -> b) -> b -> MaybeS a -> b Source # foldr' :: (a -> b -> b) -> b -> MaybeS a -> b Source # foldl :: (b -> a -> b) -> b -> MaybeS a -> b Source # foldl' :: (b -> a -> b) -> b -> MaybeS a -> b Source # foldr1 :: (a -> a -> a) -> MaybeS a -> a Source # foldl1 :: (a -> a -> a) -> MaybeS a -> a Source # toList :: MaybeS a -> [a] Source # null :: MaybeS a -> Bool Source # length :: MaybeS a -> Int Source # elem :: Eq a => a -> MaybeS a -> Bool Source # maximum :: Ord a => MaybeS a -> a Source # minimum :: Ord a => MaybeS a -> a Source # |
Identity functor and monad. (a non-strict monad)
Since: base-4.8.0.0
Identity | |
|
Instances
foldl'Stack :: (b -> k -> a -> Map k a -> b) -> b -> Stack k a -> b Source #
mapWhenMissing :: forall (f :: Type -> Type) a b k x. (Applicative f, Monad f) => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b Source #
Map covariantly over a
.WhenMissing
f k x
Since: containers-0.5.9
mapWhenMatched :: forall (f :: Type -> Type) a b k x y. Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b Source #
Map covariantly over a
.WhenMatched
f k x y
Since: containers-0.5.9
lmapWhenMissing :: forall b a (f :: Type -> Type) k x. (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x Source #
Map contravariantly over a
.WhenMissing
f k _ x
Since: containers-0.5.9
contramapFirstWhenMatched :: forall b a (f :: Type -> Type) k y z. (b -> a) -> WhenMatched f k a y z -> WhenMatched f k b y z Source #
Map contravariantly over a
.WhenMatched
f k _ y z
Since: containers-0.5.9
contramapSecondWhenMatched :: forall b a (f :: Type -> Type) k x z. (b -> a) -> WhenMatched f k x a z -> WhenMatched f k x b z Source #
Map contravariantly over a
.WhenMatched
f k x _ z
Since: containers-0.5.9
mapGentlyWhenMissing :: forall (f :: Type -> Type) a b k x. Functor f => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b Source #
Map covariantly over a
, using only a 'Functor f'
constraint.WhenMissing
f k x
mapGentlyWhenMatched :: forall (f :: Type -> Type) a b k x y. Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b Source #
Map covariantly over a
, using only a 'Functor f'
constraint.WhenMatched
f k x