| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Precursor.Data.Map
Description
An efficient implementation of ordered maps from keys to values (dictionaries).
API of this module is strict in both the keys and the values.
If you need value-lazy maps, use Data.Map.Lazy instead.
The Map type is shared between the lazy and strict modules,
meaning that the same Map value can be passed to functions in
both modules (although that is rarely needed).
These modules are intended to be imported qualified, to avoid name clashes with Prelude functions, e.g.
import qualified Data.Map.Strict 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.
Warning: The size of the map must not exceed maxBound::Int. Violation of
this condition is not detected and if the size limit is exceeded, its
behaviour is undefined.
Operation comments contain the operation time complexity in the Big-O notation (http://en.wikipedia.org/wiki/Big_O_notation).
Be aware that the Functor, Traversable and Data instances
are the same as for the Data.Map.Lazy module, so if they are used
on strict maps, the resulting maps will be lazy.
Documentation
A Map from keys k to values a.
Instances
| Functor (Map k) | |
| Foldable (Map k) | |
| Traversable (Map k) | |
| Ord k => IsList (Map k v) | |
| (Eq k, Eq a) => Eq (Map k a) | |
| (Data k, Data a, Ord k) => Data (Map k a) | |
| (Ord k, Ord v) => Ord (Map k v) | |
| (Ord k, Read k, Read e) => Read (Map k e) | |
| (Show k, Show a) => Show (Map k a) | |
| Ord k => Semigroup (Map k v) | |
| Ord k => Monoid (Map k v) | |
| (NFData k, NFData a) => NFData (Map k a) | |
| type Item (Map k v) | |
lookup :: Ord k => k -> Map k a -> Maybe a #
O(log n). 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
insert :: Ord k => k -> a -> Map k a -> Map k a #
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 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'
delete :: Ord k => k -> Map k a -> Map k a #
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