Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module doesn't respect the PVP! Breaking changes may happen at any minor version (>= *.*.m.*)
- class SingIAreWeStrict (s :: AreWeStrict) where
- seq' :: SingIAreWeStrict s => Proxy# s -> a -> b -> b
- seqList :: [a] -> [a]
- data POMap k v = POMap !Int ![Map k v]
- mkPOMap :: [Map k v] -> POMap k v
- chainDecomposition :: POMap k v -> [Map k v]
- size :: POMap k v -> Int
- width :: POMap k v -> Int
- foldEntry :: (Monoid m, PartialOrd k) => k -> (v -> m) -> POMap k v -> m
- lookup :: PartialOrd k => k -> POMap k v -> Maybe v
- member :: PartialOrd k => k -> POMap k v -> Bool
- notMember :: PartialOrd k => k -> POMap k v -> Bool
- findWithDefault :: PartialOrd k => v -> k -> POMap k v -> v
- data RelationalOperator
- flipRelationalOperator :: RelationalOperator -> RelationalOperator
- containsOrdering :: Ordering -> RelationalOperator -> Bool
- comparePartial :: PartialOrd k => k -> k -> Maybe Ordering
- addToAntichain :: PartialOrd k => RelationalOperator -> (k, v) -> [(k, v)] -> [(k, v)]
- dedupAntichain :: PartialOrd k => RelationalOperator -> [(k, v)] -> [(k, v)]
- lookupX :: PartialOrd k => RelationalOperator -> k -> POMap k v -> [(k, v)]
- lookupLT :: PartialOrd k => k -> POMap k v -> [(k, v)]
- lookupLE :: PartialOrd k => k -> POMap k v -> [(k, v)]
- lookupGE :: PartialOrd k => k -> POMap k v -> [(k, v)]
- lookupGT :: PartialOrd k => k -> POMap k v -> [(k, v)]
- empty :: POMap k v
- singleton :: SingIAreWeStrict s => Proxy# s -> k -> v -> POMap k v
- insert :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> k -> v -> POMap k v -> POMap k v
- insertWith :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> k -> v -> POMap k v -> POMap k v
- insertWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> k -> v -> POMap k v -> POMap k v
- insertLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> k -> v -> POMap k v -> (Maybe v, POMap k v)
- keyedInsertAsAlter :: (k -> v -> v -> v) -> v -> k -> Maybe v -> Maybe v
- data LookupResult a
- = Incomparable
- | NotFound a
- | Found a
- overChains :: (Map k v -> LookupResult a) -> (Map k v -> b -> b) -> (a -> [Map k v] -> b) -> ([Map k v] -> b) -> POMap k v -> b
- delete :: PartialOrd k => k -> POMap k v -> POMap k v
- deleteLookup :: PartialOrd k => k -> POMap k v -> (Maybe v, POMap k v)
- adjust :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v) -> k -> POMap k v -> POMap k v
- adjustWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v) -> k -> POMap k v -> POMap k v
- adjustLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v) -> k -> POMap k v -> (Maybe v, POMap k v)
- update :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> Maybe v) -> k -> POMap k v -> POMap k v
- updateWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> Maybe v) -> k -> POMap k v -> POMap k v
- updateLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> Maybe v) -> k -> POMap k v -> (Maybe v, POMap k v)
- alter :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (Maybe v -> Maybe v) -> k -> POMap k v -> POMap k v
- alterWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> POMap k v -> POMap k v
- alterChain :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> Map k v -> LookupResult (Map k v)
- alterLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> POMap k v -> (Maybe v, POMap k v)
- alterLookupChain :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> Map k v -> LookupResult (Maybe v, Map k v)
- alterF :: (Functor f, PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (Maybe v -> f (Maybe v)) -> k -> POMap k v -> f (POMap k v)
- alterFChain :: (Functor f, PartialOrd k, SingIAreWeStrict s) => Proxy# s -> k -> Map k v -> LookupResult ((Maybe v -> f (Maybe v)) -> f (Map k v))
- union :: PartialOrd k => POMap k v -> POMap k v -> POMap k v
- unionWith :: PartialOrd k => (v -> v -> v) -> POMap k v -> POMap k v -> POMap k v
- unionWithKey :: PartialOrd k => (k -> v -> v -> v) -> POMap k v -> POMap k v -> POMap k v
- unions :: PartialOrd k => [POMap k v] -> POMap k v
- unionsWith :: PartialOrd k => (v -> v -> v) -> [POMap k v] -> POMap k v
- difference :: PartialOrd k => POMap k a -> POMap k b -> POMap k a
- differenceWith :: PartialOrd k => (a -> b -> Maybe a) -> POMap k a -> POMap k b -> POMap k a
- differenceWithKey :: PartialOrd k => (k -> a -> b -> Maybe a) -> POMap k a -> POMap k b -> POMap k a
- intersection :: PartialOrd k => POMap k a -> POMap k b -> POMap k a
- intersectionWith :: PartialOrd k => (a -> b -> c) -> POMap k a -> POMap k b -> POMap k c
- intersectionWithKey :: PartialOrd k => (k -> a -> b -> c) -> POMap k a -> POMap k b -> POMap k c
- map :: SingIAreWeStrict s => Proxy# s -> (a -> b) -> POMap k a -> POMap k b
- mapWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> b) -> POMap k a -> POMap k b
- traverseWithKey :: (Applicative t, SingIAreWeStrict s) => Proxy# s -> (k -> a -> t b) -> POMap k a -> t (POMap k b)
- mapAccum :: SingIAreWeStrict s => Proxy# s -> (a -> b -> (a, c)) -> a -> POMap k b -> (a, POMap k c)
- mapAccumWithKey :: SingIAreWeStrict s => Proxy# s -> (a -> k -> b -> (a, c)) -> a -> POMap k b -> (a, POMap k c)
- mapKeys :: PartialOrd k2 => (k1 -> k2) -> POMap k1 v -> POMap k2 v
- mapKeysWith :: (PartialOrd k2, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> (k1 -> k2) -> POMap k1 v -> POMap k2 v
- mapKeysMonotonic :: (k1 -> k2) -> POMap k1 v -> POMap k2 v
- foldr' :: (a -> b -> b) -> b -> POMap k a -> b
- foldrWithKey :: (k -> a -> b -> b) -> b -> POMap k a -> b
- foldrWithKey' :: (k -> a -> b -> b) -> b -> POMap k a -> b
- foldl' :: (b -> a -> b) -> b -> POMap k a -> b
- foldlWithKey :: (b -> k -> a -> b) -> b -> POMap k a -> b
- foldlWithKey' :: (b -> k -> a -> b) -> b -> POMap k a -> b
- foldMapWithKey :: Monoid m => (k -> a -> m) -> POMap k a -> m
- elems :: POMap k v -> [v]
- keys :: POMap k v -> [k]
- assocs :: POMap k v -> [(k, v)]
- toList :: POMap k v -> [(k, v)]
- fromListImpl :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> [(k, v)] -> POMap k v
- fromListWith :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> [(k, v)] -> POMap k v
- fromListWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> [(k, v)] -> POMap k v
- filter :: (v -> Bool) -> POMap k v -> POMap k v
- filterWithKey :: (k -> v -> Bool) -> POMap k v -> POMap k v
- partition :: (v -> Bool) -> POMap k v -> (POMap k v, POMap k v)
- partitionWithKey :: (k -> v -> Bool) -> POMap k v -> (POMap k v, POMap k v)
- mapMaybe :: SingIAreWeStrict s => Proxy# s -> (a -> Maybe b) -> POMap k a -> POMap k b
- mapMaybeWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> Maybe b) -> POMap k a -> POMap k b
- traverseMaybeWithKey :: (Applicative f, SingIAreWeStrict s) => Proxy# s -> (k -> a -> f (Maybe b)) -> POMap k a -> f (POMap k b)
- mapEither :: SingIAreWeStrict s => Proxy# s -> (a -> Either b c) -> POMap k a -> (POMap k b, POMap k c)
- mapEitherWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> Either b c) -> POMap k a -> (POMap k b, POMap k c)
- isSubmapOf :: (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool
- isSubmapOfBy :: PartialOrd k => (a -> b -> Bool) -> POMap k a -> POMap k b -> Bool
- isProperSubmapOf :: (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool
- isProperSubmapOfBy :: PartialOrd k => (a -> b -> Bool) -> POMap k a -> POMap k b -> Bool
- lookupMin :: PartialOrd k => POMap k v -> [(k, v)]
- lookupMax :: PartialOrd k => POMap k v -> [(k, v)]
Documentation
This is some setup code for doctest
.
>>> :set -XGeneralizedNewtypeDeriving
>>> import Algebra.PartialOrd
>>> import Data.POMap.Lazy
>>> import Data.POMap.Internal
>>> :{
newtype Divisibility
= Div Int
deriving (Eq, Num)
instance Show Divisibility where
show (Div a) = show a
instance PartialOrd Divisibility where
Div a leq
Div b = b mod
a == 0
type DivMap a = POMap Divisibility a
default (Divisibility, DivMap String)
:}
class SingIAreWeStrict (s :: AreWeStrict) where Source #
Allows us to abstract over value-strictness in a zero-cost manner.
GHC should always be able to specialise the two instances of this and
consequently inline areWeStrict
.
It's a little sad we can't just use regular singletons, for reasons outlined here.
areWeStrict :: Proxy# s -> AreWeStrict Source #
seq' :: SingIAreWeStrict s => Proxy# s -> a -> b -> b Source #
Should be inlined and specialised at all call sites.
A map from partially-ordered keys k
to values v
.
Functor (POMap k) Source # | |
Foldable (POMap k) Source # | |
Traversable (POMap k) Source # | |
PartialOrd k => IsList (POMap k v) Source # | |
(PartialOrd k, Eq v) => Eq (POMap k v) Source # | \(\mathcal{O}(wn\log n)\), where \(w=\max(w_1,w_2)), n=\max(n_1,n_2)\). |
(PartialOrd k, Read k, Read e) => Read (POMap k e) Source # | |
(Show k, Show v) => Show (POMap k v) Source # | |
(NFData k, NFData v) => NFData (POMap k v) Source # | |
(PartialOrd k, PartialOrd v) => PartialOrd (POMap k v) Source # | \(\mathcal{O}(wn\log n)\), where \(w=\max(w_1,w_2)), n=\max(n_1,n_2)\). |
type Item (POMap k v) Source # | |
mkPOMap :: [Map k v] -> POMap k v Source #
Internal smart constructor so that we can be sure that we are always spine-strict, discard empty maps and have appropriate size information.
chainDecomposition :: POMap k v -> [Map k v] Source #
Instances
Query
width :: POMap k v -> Int Source #
\(\mathcal{O}(w)\). The width \(w\) of the chain decomposition in the internal data structure. This is always at least as big as the size of the biggest possible anti-chain.
lookup :: PartialOrd k => k -> POMap k v -> Maybe v Source #
\(\mathcal{O}(w\log n)\). Is the key a member of the map?
member :: PartialOrd k => k -> POMap k v -> Bool Source #
\(\mathcal{O}(w\log n)\).
Is the key a member of the map? See also notMember
.
>>>
member 5 (fromList [(5,'a'), (3,'b')]) == True
True>>>
member 1 (fromList [(5,'a'), (3,'b')]) == False
True
notMember :: PartialOrd k => k -> POMap k v -> Bool Source #
\(\mathcal{O}(w\log n)\).
Is the key not a member of the map? See also member
.
>>>
notMember 5 (fromList [(5,'a'), (3,'b')]) == False
True>>>
notMember 1 (fromList [(5,'a'), (3,'b')]) == True
True
findWithDefault :: PartialOrd k => v -> k -> POMap k v -> v Source #
\(\mathcal{O}(w\log n)\).
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'
True>>>
findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
True
data RelationalOperator Source #
containsOrdering :: Ordering -> RelationalOperator -> Bool Source #
comparePartial :: PartialOrd k => k -> k -> Maybe Ordering Source #
addToAntichain :: PartialOrd k => RelationalOperator -> (k, v) -> [(k, v)] -> [(k, v)] Source #
dedupAntichain :: PartialOrd k => RelationalOperator -> [(k, v)] -> [(k, v)] Source #
lookupX :: PartialOrd k => RelationalOperator -> k -> POMap k v -> [(k, v)] Source #
lookupLT :: PartialOrd k => k -> POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). Find the largest set of keys smaller than the given one and return the corresponding list of (key, value) pairs.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupLT 3 (fromList [(3,'a'), (5,'b')])
[]>>>
lookupLT 9 (fromList [(3,'a'), (5,'b')])
[(3,'a')]
lookupLE :: PartialOrd k => k -> POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). Find the largest key smaller or equal to the given one and return the corresponding list of (key, value) pairs.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupLE 2 (fromList [(3,'a'), (5,'b')])
[]>>>
lookupLE 3 (fromList [(3,'a'), (5,'b')])
[(3,'a')]>>>
lookupLE 10 (fromList [(3,'a'), (5,'b')])
[(5,'b')]
lookupGE :: PartialOrd k => k -> POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). Find the smallest key greater or equal to the given one and return the corresponding list of (key, value) pairs.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupGE 3 (fromList [(3,'a'), (5,'b')])
[(3,'a')]>>>
lookupGE 5 (fromList [(3,'a'), (10,'b')])
[(10,'b')]>>>
lookupGE 6 (fromList [(3,'a'), (5,'b')])
[]
lookupGT :: PartialOrd k => k -> POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). Find the smallest key greater than the given one and return the corresponding list of (key, value) pairs.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupGT 5 (fromList [(3,'a'), (10,'b')])
[(10,'b')]>>>
lookupGT 5 (fromList [(3,'a'), (5,'b')])
[]
Construction
Insertion
insert :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> k -> v -> POMap k v -> POMap k v Source #
insertWith :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> k -> v -> POMap k v -> POMap k v Source #
insertWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> k -> v -> POMap k v -> POMap k v Source #
insertLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> k -> v -> POMap k v -> (Maybe v, POMap k v) Source #
keyedInsertAsAlter :: (k -> v -> v -> v) -> v -> k -> Maybe v -> Maybe v Source #
Deletion
data LookupResult a Source #
Functor LookupResult Source # | |
Eq a => Eq (LookupResult a) Source # | |
Ord a => Ord (LookupResult a) Source # | |
Show a => Show (LookupResult a) Source # | |
overChains :: (Map k v -> LookupResult a) -> (Map k v -> b -> b) -> (a -> [Map k v] -> b) -> ([Map k v] -> b) -> POMap k v -> b Source #
delete :: PartialOrd k => k -> POMap k v -> POMap k v Source #
\(\mathcal{O}(w\log n)\). 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")])
fromList [(3,"b")]>>>
delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
True>>>
delete 5 empty
fromList []
deleteLookup :: PartialOrd k => k -> POMap k v -> (Maybe v, POMap k v) Source #
adjust :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v) -> k -> POMap k v -> POMap k v Source #
adjustWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v) -> k -> POMap k v -> POMap k v Source #
adjustLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v) -> k -> POMap k v -> (Maybe v, POMap k v) Source #
update :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> Maybe v) -> k -> POMap k v -> POMap k v Source #
updateWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> Maybe v) -> k -> POMap k v -> POMap k v Source #
updateLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> Maybe v) -> k -> POMap k v -> (Maybe v, POMap k v) Source #
alter :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (Maybe v -> Maybe v) -> k -> POMap k v -> POMap k v Source #
alterWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> POMap k v -> POMap k v Source #
alterChain :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> Map k v -> LookupResult (Map k v) Source #
alterLookupWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> POMap k v -> (Maybe v, POMap k v) Source #
alterLookupChain :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> Maybe v -> Maybe v) -> k -> Map k v -> LookupResult (Maybe v, Map k v) Source #
alterF :: (Functor f, PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (Maybe v -> f (Maybe v)) -> k -> POMap k v -> f (POMap k v) Source #
alterFChain :: (Functor f, PartialOrd k, SingIAreWeStrict s) => Proxy# s -> k -> Map k v -> LookupResult ((Maybe v -> f (Maybe v)) -> f (Map k v)) Source #
Combine
Union
union :: PartialOrd k => POMap k v -> POMap k v -> POMap k v Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\).
The expression (
) takes the left-biased union of union
t1 t2t1
and t2
.
It prefers t1
when duplicate keys are encountered,
i.e. (
).union
== unionWith
const
>>>
union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
True
unionWith :: PartialOrd k => (v -> v -> v) -> POMap k v -> POMap k v -> POMap k v Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\). Union with a combining function.
>>>
unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
True
unionWithKey :: PartialOrd k => (k -> v -> v -> v) -> POMap k v -> POMap k v -> POMap k v Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\). 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")]
True
unions :: PartialOrd k => [POMap k v] -> POMap k v Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max_i n_i\) and \(w=\max_i w_i\).
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")] :} True
>>>
:{
unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])] == fromList [(3, "B3"), (5, "A3"), (7, "C")] :} True
unionsWith :: PartialOrd k => (v -> v -> v) -> [POMap k v] -> POMap k v Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max_i n_i\) and \(w=\max_i w_i\).
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")] :} True
Difference
difference :: PartialOrd k => POMap k a -> POMap k b -> POMap k a Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\). 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")])
fromList [(3,"b")]
differenceWith :: PartialOrd k => (a -> b -> Maybe a) -> POMap k a -> POMap k b -> POMap k a Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\).
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")])
fromList [(3,"b:B")]
differenceWithKey :: PartialOrd k => (k -> a -> b -> Maybe a) -> POMap k a -> POMap k b -> POMap k a Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\).
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")])
fromList [(3,"3:b|B")]
Intersection
intersection :: PartialOrd k => POMap k a -> POMap k b -> POMap k a Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\).
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")])
fromList [(5,"a")]
intersectionWith :: PartialOrd k => (a -> b -> c) -> POMap k a -> POMap k b -> POMap k c Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\). Intersection with a combining function.
>>>
intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")])
fromList [(5,"aA")]
intersectionWithKey :: PartialOrd k => (k -> a -> b -> c) -> POMap k a -> POMap k b -> POMap k c Source #
\(\mathcal{O}(wn\log n)\), where \(n=\max(n_1,n_2)\) and \(w=\max(w_1,w_2)\). 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")])
fromList [(5,"5:a|A")]
Traversals
mapWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> b) -> POMap k a -> POMap k b Source #
traverseWithKey :: (Applicative t, SingIAreWeStrict s) => Proxy# s -> (k -> a -> t b) -> POMap k a -> t (POMap k b) Source #
mapAccum :: SingIAreWeStrict s => Proxy# s -> (a -> b -> (a, c)) -> a -> POMap k b -> (a, POMap k c) Source #
mapAccumWithKey :: SingIAreWeStrict s => Proxy# s -> (a -> k -> b -> (a, c)) -> a -> POMap k b -> (a, POMap k c) Source #
mapKeys :: PartialOrd k2 => (k1 -> k2) -> POMap k1 v -> POMap k2 v Source #
\(\mathcal{O}(wn\log n)\).
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")]
True>>>
mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
fromList [(1,"c")]>>>
mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
fromList [(3,"c")]
mapKeysWith :: (PartialOrd k2, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> (k1 -> k2) -> POMap k1 v -> POMap k2 v Source #
mapKeysMonotonic :: (k1 -> k2) -> POMap k1 v -> POMap k2 v Source #
\(\mathcal{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, for every chain ls
in s
we have:
and [x < y ==> f x < f y | x <- ls, y <- ls] ==> mapKeysMonotonic f s == mapKeys f 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")]
True
Folds
foldr' :: (a -> b -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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.
foldrWithKey :: (k -> a -> b -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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)"
True
foldrWithKey' :: (k -> a -> b -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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.
foldl' :: (b -> a -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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.
foldlWithKey :: (b -> k -> a -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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
>>>
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)"
True
foldlWithKey' :: (b -> k -> a -> b) -> b -> POMap k a -> b Source #
\(\mathcal{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.
foldMapWithKey :: Monoid m => (k -> a -> m) -> POMap k a -> m Source #
\(\mathcal{O}(n)\). Fold the keys and values in the map using the given monoid, such that
foldMapWithKey
f =fold
.mapWithKey
f
Conversion
elems :: POMap k v -> [v] Source #
\(\mathcal{O}(n)\). Return all elements of the map in unspecified order.
>>>
elems (fromList [(5,"a"), (3,"b")])
["b","a"]>>>
elems empty
[]
keys :: POMap k v -> [k] Source #
\(\mathcal{O}(n)\). Return all keys of the map in unspecified order.
>>>
keys (fromList [(5,"a"), (3,"b")])
[3,5]>>>
keys empty
[]
assocs :: POMap k v -> [(k, v)] Source #
\(\mathcal{O}(n)\). Return all key/value pairs in the map in unspecified order.
>>>
assocs (fromList [(5,"a"), (3,"b")])
[(3,"b"),(5,"a")]>>>
assocs empty
[]
toList :: POMap k v -> [(k, v)] Source #
\(\mathcal{O}(n)\). Return all key/value pairs in the map in unspecified order.
Currently, toList =
.assocs
fromListImpl :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> [(k, v)] -> POMap k v Source #
Intentionally named this way, to disambiguate it from fromList
.
This is so that we can doctest this module.
fromListWith :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (v -> v -> v) -> [(k, v)] -> POMap k v Source #
fromListWithKey :: (PartialOrd k, SingIAreWeStrict s) => Proxy# s -> (k -> v -> v -> v) -> [(k, v)] -> POMap k v Source #
Filter
filter :: (v -> Bool) -> POMap k v -> POMap k v Source #
\(\mathcal{O}(n)\). Filter all values that satisfy the predicate.
>>>
filter (> "a") (fromList [(5,"a"), (3,"b")])
fromList [(3,"b")]>>>
filter (> "x") (fromList [(5,"a"), (3,"b")])
fromList []>>>
filter (< "a") (fromList [(5,"a"), (3,"b")])
fromList []
filterWithKey :: (k -> v -> Bool) -> POMap k v -> POMap k v Source #
\(\mathcal{O}(n)\). Filter all keys/values that satisfy the predicate.
>>>
filterWithKey (\(Div k) _ -> k > 4) (fromList [(5,"a"), (3,"b")])
fromList [(5,"a")]
partition :: (v -> Bool) -> POMap k v -> (POMap k v, POMap k v) Source #
\(\mathcal{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")]) == (fromList [(3, "b")], fromList [(5, "a")])
True>>>
partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
True>>>
partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
True
partitionWithKey :: (k -> v -> Bool) -> POMap k v -> (POMap k v, POMap k v) Source #
\(\mathcal{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 (\ (Div k) _ -> k > 3) (fromList [(5,"a"), (3,"b")]) == (fromList [(5, "a")], fromList [(3, "b")])
True>>>
partitionWithKey (\ (Div k) _ -> k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
True>>>
partitionWithKey (\ (Div k) _ -> k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
True
mapMaybeWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> Maybe b) -> POMap k a -> POMap k b Source #
traverseMaybeWithKey :: (Applicative f, SingIAreWeStrict s) => Proxy# s -> (k -> a -> f (Maybe b)) -> POMap k a -> f (POMap k b) Source #
mapEither :: SingIAreWeStrict s => Proxy# s -> (a -> Either b c) -> POMap k a -> (POMap k b, POMap k c) Source #
mapEitherWithKey :: SingIAreWeStrict s => Proxy# s -> (k -> a -> Either b c) -> POMap k a -> (POMap k b, POMap k c) Source #
Submap
isSubmapOf :: (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool Source #
\(\mathcal{O}(n_2 w_1 n_1 \log n_1)\).
This function is defined as (
).isSubmapOf
= isSubmapOfBy
(==)
isSubmapOfBy :: PartialOrd k => (a -> b -> Bool) -> POMap k a -> POMap k b -> Bool Source #
\(\mathcal{O}(n_2 w_1 n_1 \log n_1)\).
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 [(1,'a')]) (fromList [(1,'a'),(2,'b')])
True>>>
isSubmapOfBy (<=) (fromList [(1,'a')]) (fromList [(1,'b'),(2,'c')])
True>>>
isSubmapOfBy (==) (fromList [(1,'a'),(2,'b')]) (fromList [(1,'a'),(2,'b')])
True
But the following are all False
:
>>>
isSubmapOfBy (==) (fromList [(2,'a')]) (fromList [(1,'a'),(2,'b')])
False>>>
isSubmapOfBy (<) (fromList [(1,'a')]) (fromList [(1,'a'),(2,'b')])
False>>>
isSubmapOfBy (==) (fromList [(1,'a'),(2,'b')]) (fromList [(1,'a')])
False
isProperSubmapOf :: (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool Source #
\(\mathcal{O}(n_2 w_1 n_1 \log n_1)\).
Is this a proper submap? (ie. a submap but not equal).
Defined as (
).isProperSubmapOf
= isProperSubmapOfBy
(==)
isProperSubmapOfBy :: PartialOrd k => (a -> b -> Bool) -> POMap k a -> POMap k b -> Bool Source #
\(\mathcal{O}(n_2 w_1 n_1 \log n_1)\).
Is this a proper submap? (ie. a submap but not equal).
The expression (
) returns isProperSubmapOfBy
f m1 m2True
when
m1
and 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,'a')]) (fromList [(1,'a'),(2,'b')])
True>>>
isProperSubmapOfBy (<=) (fromList [(1,'a')]) (fromList [(1,'a'),(2,'b')])
True
But the following are all False
:
>>>
isProperSubmapOfBy (==) (fromList [(1,'a'),(2,'b')]) (fromList [(1,'a'),(2,'b')])
False>>>
isProperSubmapOfBy (==) (fromList [(1,'a'),(2,'b')]) (fromList [(1,'a')])
False>>>
isProperSubmapOfBy (<) (fromList [(1,'a')]) (fromList [(1,'a'),(2,'b')])
False
Min/Max
lookupMin :: PartialOrd k => POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). The minimal keys of the map.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupMin (fromList [(6,"a"), (3,"b")])
[(3,"b")]>>>
lookupMin empty
[]
lookupMax :: PartialOrd k => POMap k v -> [(k, v)] Source #
\(\mathcal{O}(w\log n)\). The maximal keys of the map.
Note that the following examples assume the Divisibility
partial order defined at the top.
>>>
lookupMax (fromList [(6,"a"), (3,"b")])
[(6,"a")]>>>
lookupMax empty
[]