GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing hackage.haskell.org — Data.Map.Strict
Data.Map.Strict Contents Description Finite Maps (strict interface)The Each function in this module is careful to force values before installing
them in a In particular, the functions in this module obey the following law:
When deciding if this is the correct data structure to use, consider:
For a walkthrough of the most commonly used functions see the maps introduction. This module is intended to be imported qualified, to avoid name clashes with Prelude functions: import qualified Data.Map.Strict as Map Note that the implementation is generally left-biased. Functions that take
two maps as arguments and combine them, such as Detailed performance informationThe amortized running time is given for each operation, with n referring to the number of entries in the map. Benchmarks comparing Data.Map.Strict with other dictionary implementations can be found at https://github.com/haskell-perf/dictionaries. WarningThe size of a The ImplementationThe implementation of
Bounds for
Synopsis
Map typeA Map from keys Instances
Constructionsingleton :: 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 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 From Unordered ListsfromList :: Ord k => [(k, a)] -> Map k a Source # O(n*log n). Build a map from a list of key/value pairs. See also If the keys of the list are ordered, linear-time implementation is used,
with the performance equal to 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(n*log n). Build a map from a list of key/value pairs with a combining function. See also fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] fromListWith (++) [] == empty fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a Source # O(n*log n). Build a map from a list of key/value pairs with a combining function. See also let f k a1 a2 = (show k) ++ a1 ++ a2 fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")] fromListWithKey f [] == empty From Ascending ListsfromAscList :: 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 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 From Descending ListsfromDescList :: 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,"a")] == fromList [(3, "b"), (5, "b")] valid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True valid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False 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 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 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"), (3,"b"), (3,"a")]) == False Insertioninsert :: Ord k => k -> a -> Map k a -> Map k a Source # O(log n). 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 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(log n). Insert with a function, combining new value and 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" insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a Source # O(log n). Insert with a function, combining key, new value and old value.
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" insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a) Source # O(log n). Combines insert operation with old value retrieval.
The expression ( 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 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")]) Deletion/Updatedelete :: Ord k => k -> Map k a -> Map k a Source # O(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")]) == 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(log n). 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 == emptyadjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a Source # O(log n). 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(log n). The expression ( 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(log n). The expression ( 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(log n). Lookup and update. See also 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(log n). The expression ( 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")] alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a) Source # O(log n). The expression ( 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)
Note on rewrite rules: This module includes GHC rewrite rules to optimize Note: QueryLookuplookup :: Ord k => k -> Map k a -> Maybe a Source # O(log n). Lookup the value at a key in the map. The function will return the corresponding value as An example of using 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 (!?) :: Ord k => Map k a -> k -> Maybe a infixl 9 Source # O(log n). Find the value at a key.
Returns fromList [(5, 'a'), (3, 'b')] !? 1 == Nothing fromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a' Since: containers-0.5.9 (!) :: Ord k => Map k a -> k -> a infixl 9 Source # O(log n). Find the value at a key.
Calls fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map fromList [(5,'a'), (3,'b')] ! 5 == 'a' findWithDefault :: Ord k => a -> k -> Map k a -> a Source # O(log n). The expression findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' member :: Ord k => k -> Map k a -> Bool Source # O(log n). Is the key a member of the map? See also 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(log n). Is the key not a member of the map? See also notMember 5 (fromList [(5,'a'), (3,'b')]) == False notMember 1 (fromList [(5,'a'), (3,'b')]) == True lookupLT :: Ord k => k -> Map k v -> Maybe (k, v) Source # O(log n). 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(log n). 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(log n). 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(log n). 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 Sizenull :: 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 CombineUnionunionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a Source # O(m*log(n/m + 1)), 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")] unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a Source # O(m*log(n/m + 1)), 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")] unions :: (Foldable f, Ord k) => f (Map k a) -> Map k a Source # The union of a list of maps:
( 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 (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
== fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]Differencedifference :: Ord k => Map k a -> Map k b -> Map k a Source # O(m*log(n/m + 1)), 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 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 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"Intersectionintersection :: Ord k => Map k a -> Map k b -> Map k a Source # O(m*log(n/m + 1)), m <= n. Intersection of two maps.
Return data in the first map for the keys existing in both maps.
( 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(m*log(n/m + 1)), 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(m*log(n/m + 1)), 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" General combining functionsDeprecated general combining functionmergeWithKey :: 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 universal combining function. WARNING: This function can produce corrupt maps and its results
may depend on the internal structures of its inputs. Users should
prefer When 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
The TraversalMapmap :: (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).
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 Since: containers-0.5.8 mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source # O(n). The function 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 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 mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a Source # O(n*log n).
The size of the result may be smaller if 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(n*log n).
The size of the result may be smaller if 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" mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a Source # O(n).
and [x < y ==> f x < f y | x <- ls, y <- ls]
==> mapKeysMonotonic f s == mapKeys f s
where ls = keys sThis means that 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 FoldsfoldrWithKey :: (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
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
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
This can be an asymptotically faster than Since: containers-0.5.4 Strict foldsfoldr' :: (a -> b -> b) -> b -> Map k a -> b Source # O(n). A strict version of foldl' :: (a -> b -> a) -> a -> Map k b -> a Source # O(n). A strict version of foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b Source # O(n). A strict version of foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a Source # O(n). A strict version of Conversionelems :: 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 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 ListstoList :: 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 == [] Ordered liststoAscList :: 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")] Filterfilter :: (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" restrictKeys :: Ord k => Map k a -> Set k -> Map k a Source # O(m*log(n/m + 1)), m <= n. Restrict a m `restrictKeys` s = Since: containers-0.5.8 withoutKeys :: Ord k => Map k a -> Set k -> Map k a Source # O(m*log(n/m + 1)), m <= n. Remove all keys in a m `withoutKeys` 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 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 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")]) takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a Source # O(log n). Take while a predicate on the keys holds.
The user is responsible for ensuring that for all keys takeWhileAntitone p = Since: containers-0.5.8 dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a Source # O(log n). Drop while a predicate on the keys holds.
The user is responsible for ensuring that for all keys dropWhileAntitone p = Since: containers-0.5.8 spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a) Source # O(log n). Divide a map at the point where a predicate on the keys stops holding.
The user is responsible for ensuring that for all keys spanAntitone p xs = ( Note: if Since: containers-0.5.8 mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b Source # O(n). Map values and collect the 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 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 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 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(log n). The expression ( 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(log n). The expression ( 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 SubmapisSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source # O(m*log(n/m + 1)), m <= n.
This function is defined as ( isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source # O(m*log(n/m + 1)), m <= n.
The expression ( 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 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)])isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source # O(m*log(n/m + 1)), m <= n. Is this a proper submap? (ie. a submap but not equal).
Defined as ( isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source # O(m*log(n/m + 1)), m <= n. Is this a proper submap? (ie. a submap but not equal).
The expression ( isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) But the following are all 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)]) IndexedlookupIndex :: Ord k => k -> Map k a -> Maybe Int Source # O(log n). 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 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(log n). 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 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(log n). 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 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(log n). Update the element at index. Calls 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(log n). 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 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 # Take a given number of entries in key order, beginning with the smallest keys. take n = Since: containers-0.5.8 drop :: Int -> Map k a -> Map k a Source # Drop a given number of entries in key order, beginning with the smallest keys. drop n = Since: containers-0.5.8 Min/MaxlookupMin :: Map k a -> Maybe (k, a) Source # O(log n). The minimal key of the map. Returns lookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b") findMin empty = Nothing Since: containers-0.5.9 lookupMax :: Map k a -> Maybe (k, a) Source # O(log n). The maximal key of the map. Returns 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(log n). The minimal key of the map. Calls findMin (fromList [(5,"a"), (3,"b")]) == (3,"b") findMin empty Error: empty map has no minimal element deleteMin :: Map k a -> Map k a Source # O(log n). 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(log n). 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(log n). Delete and find the minimal element. deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")]) deleteFindMin Error: can not return the minimal element of an empty map deleteFindMax :: Map k a -> ((k, a), Map k a) Source # O(log n). 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(log n). 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(log n). 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(log n). 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(log n). 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(log n). Retrieves the value associated with minimal key of the
map, and the map stripped of that element, or minView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a")
minView empty == NothingmaxView :: Map k a -> Maybe (a, Map k a) Source # O(log n). Retrieves the value associated with maximal key of the
map, and the map stripped of that element, or maxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b")
maxView empty == NothingminViewWithKey :: Map k a -> Maybe ((k, a), Map k a) Source # O(log n). Retrieves the minimal (key,value) pair of the map, and
the map stripped of that element, or 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(log n). Retrieves the maximal (key,value) pair of the map, and
the map stripped of that element, or maxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b") maxViewWithKey empty == Nothing DebuggingshowTree :: Whoops "showTree has moved to Data.Map.Internal.Debug.showTree." => Map k a -> String Source # This function has moved to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||