Map
Prelude> import Data.Map as Map
Prelude Map> :set -XOverloadedLists
Prelude Map>
OverloadedLists
GHC hỗ trợ mở rộng ngôn ngữ OverloadedLists:
- Khi không bật tính năng này, tất cả hằng số dạng danh sách đều có kiểu
[] - Khi bật tính năng, các hằng số dạng danh sách sẽ có kiểu
IsList l => l - Các cấu trúc như Map, Set, Vector, Text, Array đều là instance của lớp
IsList
Toán tử
Prelude Map> [(2,'x'), (4,'y')] ! 2
'x'
Prelude Map> [(2, 'x'), (4, 'y')] !? 1
Nothing
Prelude Map> [(2, 'x'), (4, 'y')] !? 2
Just 'x'
Truy vấn
Prelude Map> Map.null (singleton 3 'z')
False
Prelude Map> size [(3,'z'), (6,'w'), (9,'v')]
3
Prelude Map> member 2 [(2,'x'), (4,'y')]
True
Prelude Map> Map.lookup "Alice" [("Alice","HR"), ("Bob","IT")]
Just "HR"
Prelude Map> findWithDefault 'm' 3 [(2,'x'), (4,'y')]
'm'
Prelude Map> findWithDefault 'm' 2 [(2,'x'), (4,'y')]
'x'
Xây dựng
Prelude Map> singleton 3 'z'
fromList [(3,'z')]
Prelude Map> empty
fromList []
Chèn dữ liệu
Prelude Map> insert 2 'a' [(2,'x'), (4,'y')]
fromList [(2,'a'),(4,'y')]
Prelude Map> insert 7 'b' [(2,'x'), (4,'y')]
fromList [(2,'x'),(4,'y'),(7,'b')]
Prelude Map> insert 2 'a' empty
fromList [(2,'a')]
Xóa/Sửa
Prelude Map> delete 2 [(2,"x"), (4,"y")]
fromList [(4,"y")]
Prelude Map> adjust ("prefix_" ++) 2 [(2,"x"), (4,"y")]
fromList [(2,"prefix_x"),(4,"y")]
Prelude Map> let f x = if x == "x" then Just "prefix_x" else Nothing
Prelude Map> update f 2 [(2,"x"), (4,"y")]
fromList [(2,"prefix_x"),(4,"y")]
Prelude Map> let f _ = Nothing
Prelude Map> alter f 2 [(2,"x"), (4,"y")]
fromList [(4,"y")]
Prelude Map> let f _ = Just "z"
Prelude Map> alter f 7 [(2,"x"), (4,"y")]
fromList [(2,"x"),(4,"y"),(7,"z")]
Kết hợp
Prelude Map> union [(2, "x"), (4, "y")] [(2, "X"), (7, "Z")]
fromList [(2,"x"),(4,"y"),(7,"Z")]
Prelude Map> unions [[(2, "x"), (4, "y")], [(2, "X"), (7, "Z")], [(2, "X2"), (4, "Y2")]]
fromList [(2,"x"),(4,"y"),(7,"Z")]
Hiệu
Prelude Map> difference [(2, "x"), (4, "y")] [(2, "X"), (7, "Z")]
fromList [(4,"y")]
Giao
Prelude Map> intersection [(2, "x"), (4, "y")] [(2, "X"), (7, "Z")]
fromList [(2,"x")]
Duyệt
Prelude Map> Map.map (++ "z") [(2,"x"), (4,"y")]
fromList [(2,"xz"),(4,"yz")]
Prelude Map> traverseWithKey (\k v -> if even k then Just (succ v) else Nothing) [(2, 'a'), (4, 'c')]
Just [(2,'b'),(4,'d')]
Prelude Map> let f a b = (a ++ b, b ++ "Y")
Prelude Map> mapAccum f "Start: " [(2,"x"), (4,"y")]
("Start: yx",fromList [(2,"xY"),(4,"yY")])
Prelude Map> mapKeys (+ 2) [(2,"x"), (4,"y")]
fromList [(4,"x"),(6,"y")]
Gấp
Prelude Map> let f a len = len + (length a)
Prelude Map> Map.foldr f 0 [(2,"x"), (4,"yy")]
3
Prelude Map> let f len a = len + (length a)
Prelude Map> Map.foldl f 0 [(2,"x"), (4,"yy")]
3
Chuyển đổi
Prelude Map> elems [(2,"x"), (4,"y")]
["y","x"]
Prelude Map> keys [(2,"x"), (4,"y")]
[2,4]
Prelude Map> assocs [(2,"x"), (4,"y")]
[(2,"x"),(4,"y")]
Danh sách
Prelude Map> toList [(2,"x"), (4,"y")]
[(2,"x"),(4,"y")]
Prelude Map> fromList [(2,"x"), (4,"y"), (2, "z")]
fromList [(2,"z"),(4,"y")]
Prelude Map> fromListWith (++) [(2,"x"), (2,"y"), (4,"y"), (4,"x"), (2,"x")]
fromList [(2,"xyx"),(4,"yx")]
Danh sách có thứ tự
Prelude Map> toAscList [(2,"x"), (4,"y")]
[(2,"x"),(4,"y")]
Prelude Map> toDescList [(2,"x"), (4,"y")]
[(4,"y"),(2,"x")]
Prelude Map> fromAscList [(2,"x"), (4,"y")]
fromList [(2,"x"),(4,"y")]
Prelude Map> fromDescList [(4,"y"), (2,"x")]
fromList [(2,"x"),(4,"y")]
Lọc
Prelude Map> Map.filter (> "x") [(2,"x"), (4,"y")]
fromList [(4,"y")]
Prelude Map> partition (> "x") [(2,"x"), (4,"y")]
[(4,"y")],fromList [(2,"x")]
Prelude Map> let f x = if x == "x" then Just "prefix_x" else Nothing
Prelude Map> mapMaybe f [(2,"x"), (4,"y")]
fromList [(2,"prefix_x")]
Prelude Map> split 3 [(2,"x"), (4,"y")]
[(2,"x")],fromList [(4,"y")]
Phạm vi con
Prelude Map> isSubmapOfBy (==) [('b',2)] [('b',2),('c',3)]
True
Prelude Map> isSubmapOfBy (<=) [('b',2)] [('b',2),('c',3)]
True
Prelude Map> isSubmapOfBy (<) [('b',2)] [('b',2),('c',3)]
False
Chỉ số
Prelude Map> lookupIndex 3 [(2,"x"), (4,"y")]
Nothing
Prelude Map> lookupIndex 2 [(2,"x"), (4,"y")]
Just 0
Prelude Map> findIndex 2 [(2,"x"), (4,"y")]
0
Prelude Map> elemAt 0 [(2,"x"), (4,"y")]
(2,"x")
Prelude Map> elemAt 1 [(2,"x"), (4,"y")]
(4,"y")
Prelude Map> updateAt (\ _ _ -> Just "z") 0 [(2,"x"), (4,"y")]
fromList [(2,"z"),(4,"y")]
Prelude Map> deleteAt 0 [(2,"x"), (4,"y")]
fromList [(4,"y")]
Cực trị
Prelude Map> lookupMin [(2,"x"), (4,"y")]
Just (2,"x")
Prelude Map> lookupMax [(2,"x"), (4,"y")]
Just (4,"y")
Prelude Map> findMin [(2,"x"), (4,"y")]
(2,"x")
Prelude Map> findMax [(2,"x"), (4,"y")]
(4,"y")
Prelude Map> deleteMin [(2,"x"), (4,"y"), (6,"z")]
fromList [(4,"y"),(6,"z")]
Prelude Map> updateMax (\ a -> Just ("Prefix_" ++ a)) [(2,"x"), (4,"y")]
fromList [(2,"x"),(4,"Prefix_y")]
Debug
Prelude Map> valid (fromAscList [(2,"x"), (4,"y")])
True
Prelude Map> valid (fromAscList [(4,"y"), (2,"x")])
False