# Ticket #1611: Map.patch

File Map.patch, 43.8 KB (added by , 10 years ago) |
---|

Line | |
---|---|

1 | |

2 | New patches: |

3 | |

4 | [Improved, fixed documentation, some minor issues for Data.Map, Data.IntMap. Added examples. |

5 | apa3a@yahoo.com**20080204184756] { |

6 | hunk ./Data/IntMap.hs 6 |

7 | +-- (c) Andriy Palamarchuk 2008 |

8 | hunk ./Data/IntMap.hs 34 |

9 | +-- Operation comments contain the operation time complexity in |

10 | +-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>. |

11 | hunk ./Data/IntMap.hs 223 |

12 | +-- |

13 | +-- > fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map |

14 | +-- > fromList [(5,'a'), (3,'b')] ! 5 == 'a' |

15 | hunk ./Data/IntMap.hs 230 |

16 | --- | /O(n+m)/. See 'difference'. |

17 | +-- | Same as 'difference'. |

18 | hunk ./Data/IntMap.hs 278 |

19 | +-- |

20 | +-- > Data.IntMap.null (empty) == True |

21 | +-- > Data.IntMap.null (singleton 1 'a') == False |

22 | + |

23 | hunk ./Data/IntMap.hs 287 |

24 | +-- |

25 | +-- > size empty == 0 |

26 | +-- > size (singleton 1 'a') == 1 |

27 | +-- > size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3 |

28 | hunk ./Data/IntMap.hs 299 |

29 | +-- |

30 | +-- > member 5 (fromList [(5,'a'), (3,'b')]) == True |

31 | +-- > member 1 (fromList [(5,'a'), (3,'b')]) == False |

32 | + |

33 | hunk ./Data/IntMap.hs 310 |

34 | +-- |

35 | +-- > notMember 5 (fromList [(5,'a'), (3,'b')]) == False |

36 | +-- > notMember 1 (fromList [(5,'a'), (3,'b')]) == True |

37 | + |

38 | hunk ./Data/IntMap.hs 317 |

39 | --- | /O(min(n,W))/. Lookup the value at a key in the map. |

40 | +-- | /O(min(n,W))/. Lookup the value at a key in the map. See also 'Data.Map.lookup'. |

41 | hunk ./Data/IntMap.hs 348 |

42 | +-- |

43 | +-- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' |

44 | +-- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' |

45 | + |

46 | hunk ./Data/IntMap.hs 362 |

47 | +-- |

48 | +-- > empty == fromList [] |

49 | +-- > size empty == 0 |

50 | + |

51 | hunk ./Data/IntMap.hs 371 |

52 | +-- |

53 | +-- > singleton 1 'a' == fromList [(1, 'a')] |

54 | +-- > size (singleton 1 'a') == 1 |

55 | + |

56 | hunk ./Data/IntMap.hs 386 |

57 | +-- |

58 | +-- > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')] |

59 | +-- > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')] |

60 | +-- > insert 5 'x' empty == singleton 5 'x' |

61 | + |

62 | hunk ./Data/IntMap.hs 409 |

63 | +-- |

64 | +-- > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")] |

65 | +-- > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] |

66 | +-- > insertWith (++) 5 "xxx" empty == singleton 5 "xxx" |

67 | + |

68 | hunk ./Data/IntMap.hs 423 |

69 | +-- |

70 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

71 | +-- > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")] |

72 | +-- > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] |

73 | +-- > insertWithKey f 5 "xxx" empty == singleton 5 "xxx" |

74 | + |

75 | hunk ./Data/IntMap.hs 445 |

76 | +-- |

77 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

78 | +-- > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")]) |

79 | +-- > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")]) |

80 | +-- > insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx") |

81 | +-- |

82 | +-- This is how to define @insertLookup@ using @insertLookupWithKey@: |

83 | +-- |

84 | +-- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t |

85 | +-- > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")]) |

86 | +-- > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")]) |

87 | + |

88 | hunk ./Data/IntMap.hs 476 |

89 | +-- |

90 | +-- > delete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

91 | +-- > delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

92 | +-- > delete 5 empty == empty |

93 | + |

94 | hunk ./Data/IntMap.hs 495 |

95 | +-- |

96 | +-- > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] |

97 | +-- > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

98 | +-- > adjust ("new " ++) 7 empty == empty |

99 | + |

100 | hunk ./Data/IntMap.hs 506 |

101 | +-- |

102 | +-- > let f key x = (show key) ++ ":new " ++ x |

103 | +-- > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] |

104 | +-- > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

105 | +-- > adjustWithKey f 7 empty == empty |

106 | + |

107 | hunk ./Data/IntMap.hs 519 |

108 | +-- |

109 | +-- > let f x = if x == "a" then Just "new a" else Nothing |

110 | +-- > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] |

111 | +-- > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

112 | +-- > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

113 | + |

114 | hunk ./Data/IntMap.hs 532 |

115 | +-- |

116 | +-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing |

117 | +-- > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] |

118 | +-- > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

119 | +-- > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

120 | + |

121 | hunk ./Data/IntMap.hs 553 |

122 | +-- The function returns original value, if it is updated. |

123 | +-- This is different behavior than 'Data.Map.updateLookupWithKey'. |

124 | +-- Returns the original key value if the map entry is deleted. |

125 | +-- |

126 | +-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing |

127 | +-- > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")]) |

128 | +-- > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")]) |

129 | +-- > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a") |

130 | + |

131 | hunk ./Data/IntMap.hs 580 |

132 | --- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@ |

133 | +-- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@. |

134 | hunk ./Data/IntMap.hs 605 |

135 | +-- |

136 | +-- > unions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] |

137 | +-- > == fromList [(3, "b"), (5, "a"), (7, "C")] |

138 | +-- > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])] |

139 | +-- > == fromList [(3, "B3"), (5, "A3"), (7, "C")] |

140 | + |

141 | hunk ./Data/IntMap.hs 615 |

142 | --- | The union of a list of maps, with a combining operation |

143 | +-- | The union of a list of maps, with a combining operation. |

144 | +-- |

145 | +-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] |

146 | +-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")] |

147 | + |

148 | hunk ./Data/IntMap.hs 624 |

149 | --- | /O(n+m)/. The (left-biased) union of two maps. |

150 | +-- | /O(n+m)/. The (left-biased) union of two maps. |

151 | hunk ./Data/IntMap.hs 627 |

152 | +-- |

153 | +-- > union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")] |

154 | + |

155 | hunk ./Data/IntMap.hs 650 |

156 | --- | /O(n+m)/. The union with a combining function. |

157 | +-- | /O(n+m)/. The union with a combining function. |

158 | +-- |

159 | +-- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")] |

160 | + |

161 | hunk ./Data/IntMap.hs 658 |

162 | --- | /O(n+m)/. The union with a combining function. |

163 | +-- | /O(n+m)/. The union with a combining function. |

164 | +-- |

165 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

166 | +-- > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")] |

167 | + |

168 | hunk ./Data/IntMap.hs 686 |

169 | --- | /O(n+m)/. Difference between two maps (based on keys). |

170 | +-- | /O(n+m)/. Difference between two maps (based on keys). |

171 | +-- |

172 | +-- > difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b" |

173 | + |

174 | hunk ./Data/IntMap.hs 713 |

175 | --- | /O(n+m)/. Difference with a combining function. |

176 | +-- | /O(n+m)/. Difference with a combining function. |

177 | +-- |

178 | +-- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing |

179 | +-- > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")]) |

180 | +-- > == singleton 3 "b:B" |

181 | + |

182 | hunk ./Data/IntMap.hs 727 |

183 | +-- |

184 | +-- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing |

185 | +-- > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")]) |

186 | +-- > == singleton 3 "3:b|B" |

187 | + |

188 | hunk ./Data/IntMap.hs 762 |

189 | --- | /O(n+m)/. The (left-biased) intersection of two maps (based on keys). |

190 | +-- | /O(n+m)/. The (left-biased) intersection of two maps (based on keys). |

191 | +-- |

192 | +-- > intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a" |

193 | + |

194 | hunk ./Data/IntMap.hs 791 |

195 | --- | /O(n+m)/. The intersection with a combining function. |

196 | +-- | /O(n+m)/. The intersection with a combining function. |

197 | +-- |

198 | +-- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA" |

199 | + |

200 | hunk ./Data/IntMap.hs 799 |

201 | --- | /O(n+m)/. The intersection with a combining function. |

202 | +-- | /O(n+m)/. The intersection with a combining function. |

203 | +-- |

204 | +-- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar |

205 | +-- > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A" |

206 | + |

207 | hunk ./Data/IntMap.hs 836 |

208 | +-- |

209 | +-- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")] |

210 | +-- > updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

211 | + |

212 | hunk ./Data/IntMap.hs 854 |

213 | +-- |

214 | +-- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")] |

215 | +-- > updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

216 | + |

217 | hunk ./Data/IntMap.hs 874 |

218 | +-- |

219 | +-- > v <- maxViewWithKey (fromList [(5,"a"), (3,"b")]) |

220 | +-- > v == ((5,"a"), singleton 3 "b") |

221 | +-- > maxViewWithKey empty Error: empty map |

222 | + |

223 | hunk ./Data/IntMap.hs 885 |

224 | - Nil -> fail "maxView: empty map has no maximal element" |

225 | + Nil -> fail "maxViewWithKey: empty map has no maximal element" |

226 | hunk ./Data/IntMap.hs 894 |

227 | +-- |

228 | +-- > v <- minViewWithKey (fromList [(5,"a"), (3,"b")]) |

229 | +-- > v == ((3,"b"), singleton 5 "a") |

230 | +-- > minViewWithKey empty Error: empty map |

231 | + |

232 | hunk ./Data/IntMap.hs 905 |

233 | - Nil -> fail "minView: empty map has no minimal element" |

234 | + Nil -> fail "minViewWithKey: empty map has no minimal element" |

235 | hunk ./Data/IntMap.hs 914 |

236 | +-- |

237 | +-- > updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")] |

238 | +-- > updateMax (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

239 | + |

240 | hunk ./Data/IntMap.hs 922 |

241 | +-- |

242 | +-- > updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")] |

243 | +-- > updateMin (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

244 | + |

245 | hunk ./Data/IntMap.hs 1029 |

246 | -{- | /O(n+m)/. |

247 | +{- | /O(n+m)/. |

248 | hunk ./Data/IntMap.hs 1045 |

249 | - |

250 | hunk ./Data/IntMap.hs 1061 |

251 | +-- |

252 | +-- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")] |

253 | + |

254 | hunk ./Data/IntMap.hs 1069 |

255 | +-- |

256 | +-- > let f key x = (show key) ++ ":" ++ x |

257 | +-- > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")] |

258 | + |

259 | hunk ./Data/IntMap.hs 1082 |

260 | +-- |

261 | +-- > let f a b = (a ++ b, b ++ "X") |

262 | +-- > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")]) |

263 | + |

264 | hunk ./Data/IntMap.hs 1092 |

265 | +-- |

266 | +-- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X") |

267 | +-- > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")]) |

268 | + |

269 | hunk ./Data/IntMap.hs 1127 |

270 | +-- |

271 | +-- > filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

272 | +-- > filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty |

273 | +-- > filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty |

274 | + |

275 | hunk ./Data/IntMap.hs 1137 |

276 | +-- |

277 | +-- > filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

278 | + |

279 | hunk ./Data/IntMap.hs 1150 |

280 | --- | /O(n)/. partition the map according to some predicate. The first |

281 | +-- | /O(n)/. Partition the map according to some predicate. The first |

282 | hunk ./Data/IntMap.hs 1153 |

283 | +-- |

284 | +-- > partition (> "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") |

285 | +-- > partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) |

286 | +-- > partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) |

287 | + |

288 | hunk ./Data/IntMap.hs 1162 |

289 | --- | /O(n)/. partition the map according to some predicate. The first |

290 | +-- | /O(n)/. Partition the map according to some predicate. The first |

291 | hunk ./Data/IntMap.hs 1165 |

292 | +-- |

293 | +-- > partitionWithKey (\ k _ -> k > 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b") |

294 | +-- > partitionWithKey (\ k _ -> k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) |

295 | +-- > partitionWithKey (\ k _ -> k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) |

296 | + |

297 | hunk ./Data/IntMap.hs 1183 |

298 | +-- |

299 | +-- > let f x = if x == "a" then Just "new a" else Nothing |

300 | +-- > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a" |

301 | + |

302 | hunk ./Data/IntMap.hs 1192 |

303 | +-- |

304 | +-- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing |

305 | +-- > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3" |

306 | + |

307 | hunk ./Data/IntMap.hs 1205 |

308 | +-- |

309 | +-- > let f a = if a < "c" then Left a else Right a |

310 | +-- > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

311 | +-- > == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")]) |

312 | +-- > |

313 | +-- > mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

314 | +-- > == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

315 | + |

316 | hunk ./Data/IntMap.hs 1218 |

317 | +-- |

318 | +-- > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a) |

319 | +-- > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

320 | +-- > == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")]) |

321 | +-- > |

322 | +-- > mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

323 | +-- > == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")]) |

324 | + |

325 | hunk ./Data/IntMap.hs 1240 |

326 | +-- |

327 | +-- > split 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")]) |

328 | +-- > split 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a") |

329 | +-- > split 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") |

330 | +-- > split 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty) |

331 | +-- > split 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty) |

332 | + |

333 | hunk ./Data/IntMap.hs 1276 |

334 | +-- |

335 | +-- > splitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")]) |

336 | +-- > splitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a") |

337 | +-- > splitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a") |

338 | +-- > splitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty) |

339 | +-- > splitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty) |

340 | + |

341 | hunk ./Data/IntMap.hs 1319 |

342 | +-- > let f a len = len + (length a) |

343 | +-- > fold f 0 (fromList [(5,"a"), (3,"bbb")]) == 4 |

344 | + |

345 | hunk ./Data/IntMap.hs 1332 |

346 | +-- > let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" |

347 | +-- > foldWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)" |

348 | + |

349 | hunk ./Data/IntMap.hs 1361 |

350 | +-- |

351 | +-- > elems (fromList [(5,"a"), (3,"b")]) == ["b","a"] |

352 | +-- > elems empty == [] |

353 | + |

354 | hunk ./Data/IntMap.hs 1370 |

355 | +-- |

356 | +-- > keys (fromList [(5,"a"), (3,"b")]) == [3,5] |

357 | +-- > keys empty == [] |

358 | + |

359 | hunk ./Data/IntMap.hs 1379 |

360 | +-- |

361 | +-- > keysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5] |

362 | +-- > keysSet empty == Data.IntSet.empty |

363 | + |

364 | hunk ./Data/IntMap.hs 1388 |

365 | +-- |

366 | +-- > assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

367 | +-- > assocs empty == [] |

368 | + |

369 | hunk ./Data/IntMap.hs 1401 |

370 | +-- |

371 | +-- > toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

372 | +-- > toList empty == [] |

373 | + |

374 | hunk ./Data/IntMap.hs 1411 |

375 | +-- |

376 | +-- > toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

377 | + |

378 | hunk ./Data/IntMap.hs 1420 |

379 | +-- |

380 | +-- > fromList [] == empty |

381 | +-- > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")] |

382 | +-- > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")] |

383 | + |

384 | hunk ./Data/IntMap.hs 1431 |

385 | --- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. |

386 | +-- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. |

387 | +-- |

388 | +-- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] |

389 | +-- > fromListWith (++) [] == empty |

390 | + |

391 | hunk ./Data/IntMap.hs 1440 |

392 | --- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'. |

393 | +-- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'. |

394 | +-- |

395 | +-- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] |

396 | +-- > fromListWith (++) [] == empty |

397 | + |

398 | hunk ./Data/IntMap.hs 1453 |

399 | +-- |

400 | +-- > fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] |

401 | +-- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")] |

402 | + |

403 | hunk ./Data/IntMap.hs 1463 |

404 | +-- |

405 | +-- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")] |

406 | + |

407 | hunk ./Data/IntMap.hs 1472 |

408 | +-- |

409 | +-- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")] |

410 | + |

411 | hunk ./Data/IntMap.hs 1481 |

412 | +-- |

413 | +-- > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] |

414 | + |

415 | hunk ./Data/Map.hs 7 |

416 | +-- (c) Andriy Palamarchuk 2008 |

417 | hunk ./Data/Map.hs 35 |

418 | +-- |

419 | +-- Operation comments contain the operation time complexity in |

420 | +-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>. |

421 | hunk ./Data/Map.hs 99 |

422 | - , mapKeys |

423 | - , mapKeysWith |

424 | - , mapKeysMonotonic |

425 | + , mapKeys |

426 | + , mapKeysWith |

427 | + , mapKeysMonotonic |

428 | hunk ./Data/Map.hs 110 |

429 | - , keysSet |

430 | + , keysSet |

431 | hunk ./Data/Map.hs 203 |

432 | +-- |

433 | +-- > fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map |

434 | +-- > fromList [(5,'a'), (3,'b')] ! 5 == 'a' |

435 | + |

436 | hunk ./Data/Map.hs 210 |

437 | --- | /O(n+m)/. See 'difference'. |

438 | +-- | Same as 'difference'. |

439 | hunk ./Data/Map.hs 250 |

440 | +-- |

441 | +-- > Data.Map.null (empty) == True |

442 | +-- > Data.Map.null (singleton 1 'a') == False |

443 | + |

444 | hunk ./Data/Map.hs 261 |

445 | +-- |

446 | +-- > size empty == 0 |

447 | +-- > size (singleton 1 'a') == 1 |

448 | +-- > size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3 |

449 | + |

450 | hunk ./Data/Map.hs 273 |

451 | --- | /O(log n)/. Lookup the value at a key in the map. |

452 | +-- | /O(log n)/. Lookup the value at a key in the map. |

453 | hunk ./Data/Map.hs 279 |

454 | +-- |

455 | +-- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] |

456 | +-- > value1 <- Data.Map.lookup 5 m |

457 | +-- > value1 |

458 | +-- > 'a' |

459 | +-- > value2 <- Data.Map.lookup 1 m |

460 | +-- > Error: Key not found |

461 | +-- |

462 | +-- An example of using @lookup@ with @Maybe@ monad: |

463 | +-- |

464 | +-- > import Prelude hiding (lookup) |

465 | +-- > import Data.Map |

466 | +-- > |

467 | +-- > employeeDept = fromList([("John","Sales"), ("Bob","IT")]) |

468 | +-- > deptCountry = fromList([("IT","USA"), ("Sales","France")]) |

469 | +-- > countryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")]) |

470 | +-- > |

471 | +-- > employeeCurrency :: String -> Maybe String |

472 | +-- > employeeCurrency name = do |

473 | +-- > dept <- lookup name employeeDept |

474 | +-- > country <- lookup dept deptCountry |

475 | +-- > lookup country countryCurrency |

476 | +-- > |

477 | +-- > main = do |

478 | +-- > putStrLn $ "John's currency: " ++ (show (employeeCurrency "John")) |

479 | +-- > putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete")) |

480 | +-- |

481 | +-- The output of this program: |

482 | +-- |

483 | +-- > John's currency: Just "Euro" |

484 | +-- > Pete's currency: Nothing |

485 | + |

486 | hunk ./Data/Map.hs 335 |

487 | --- | /O(log n)/. Is the key a member of the map? |

488 | +-- | /O(log n)/. Is the key a member of the map? See also 'notMember'. |

489 | +-- |

490 | +-- > member 5 (fromList [(5,'a'), (3,'b')]) == True |

491 | +-- > member 1 (fromList [(5,'a'), (3,'b')]) == False |

492 | + |

493 | hunk ./Data/Map.hs 346 |

494 | --- | /O(log n)/. Is the key not a member of the map? |

495 | +-- | /O(log n)/. Is the key not a member of the map? See also 'member'. |

496 | +-- |

497 | +-- > notMember 5 (fromList [(5,'a'), (3,'b')]) == False |

498 | +-- > notMember 1 (fromList [(5,'a'), (3,'b')]) == True |

499 | + |

500 | hunk ./Data/Map.hs 363 |

501 | --- the value at key @k@ or returns @def@ when the key is not in the map. |

502 | +-- the value at key @k@ or returns default value @def@ |

503 | +-- when the key is not in the map. |

504 | +-- |

505 | +-- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' |

506 | +-- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' |

507 | + |

508 | hunk ./Data/Map.hs 381 |

509 | +-- |

510 | +-- > empty == fromList [] |

511 | +-- > size empty == 0 |

512 | + |

513 | hunk ./Data/Map.hs 390 |

514 | +-- |

515 | +-- > singleton 1 'a' == fromList [(1, 'a')] |

516 | +-- > size (singleton 1 'a') == 1 |

517 | + |

518 | hunk ./Data/Map.hs 403 |

519 | --- replaced with the supplied value, i.e. 'insert' is equivalent to |

520 | +-- replaced with the supplied value. 'insert' is equivalent to |

521 | hunk ./Data/Map.hs 405 |

522 | +-- |

523 | +-- > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')] |

524 | +-- > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')] |

525 | +-- > insert 5 'x' empty == singleton 5 'x' |

526 | + |

527 | hunk ./Data/Map.hs 420 |

528 | --- | /O(log n)/. Insert with a combining function. |

529 | +-- | /O(log n)/. Insert with a function, combining new value and old value. |

530 | hunk ./Data/Map.hs 425 |

531 | +-- |

532 | +-- > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")] |

533 | +-- > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] |

534 | +-- > insertWith (++) 5 "xxx" empty == singleton 5 "xxx" |

535 | + |

536 | hunk ./Data/Map.hs 440 |

537 | --- | /O(log n)/. Insert with a combining function. |

538 | +-- | /O(log n)/. Insert with a function, combining key, new value and old value. |

539 | hunk ./Data/Map.hs 446 |

540 | +-- |

541 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

542 | +-- > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")] |

543 | +-- > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] |

544 | +-- > insertWithKey f 5 "xxx" empty == singleton 5 "xxx" |

545 | + |

546 | hunk ./Data/Map.hs 474 |

547 | --- | /O(log n)/. The expression (@'insertLookupWithKey' f k x map@) |

548 | +-- | /O(log n)/. Combines insert operation with old value retrieval. |

549 | +-- The expression (@'insertLookupWithKey' f k x map@) |

550 | hunk ./Data/Map.hs 478 |

551 | +-- |

552 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

553 | +-- > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")]) |

554 | +-- > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")]) |

555 | +-- > insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx") |

556 | +-- |

557 | +-- This is how to define @insertLookup@ using @insertLookupWithKey@: |

558 | +-- |

559 | +-- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t |

560 | +-- > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")]) |

561 | +-- > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")]) |

562 | + |

563 | hunk ./Data/Map.hs 506 |

564 | +-- |

565 | +-- > delete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

566 | +-- > delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

567 | +-- > delete 5 empty == empty |

568 | + |

569 | hunk ./Data/Map.hs 521 |

570 | --- | /O(log n)/. Adjust a value at a specific key. When the key is not |

571 | +-- | /O(log n)/. Update a value at a specific key with the result of the provided function. |

572 | +-- When the key is not |

573 | hunk ./Data/Map.hs 524 |

574 | +-- |

575 | +-- > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] |

576 | +-- > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

577 | +-- > adjust ("new " ++) 7 empty == empty |

578 | + |

579 | hunk ./Data/Map.hs 535 |

580 | +-- |

581 | +-- > let f key x = (show key) ++ ":new " ++ x |

582 | +-- > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] |

583 | +-- > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

584 | +-- > adjustWithKey f 7 empty == empty |

585 | + |

586 | hunk ./Data/Map.hs 548 |

587 | +-- |

588 | +-- > let f x = if x == "a" then Just "new a" else Nothing |

589 | +-- > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] |

590 | +-- > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

591 | +-- > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

592 | + |

593 | hunk ./Data/Map.hs 562 |

594 | +-- |

595 | +-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing |

596 | +-- > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] |

597 | +-- > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

598 | +-- > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

599 | + |

600 | hunk ./Data/Map.hs 580 |

601 | --- | /O(log n)/. Lookup and update. |

602 | +-- | /O(log n)/. Lookup and update. See also 'updateWithKey'. |

603 | +-- The function returns changed value, if it is updated. |

604 | +-- Returns the original key value if the map entry is deleted. |

605 | +-- |

606 | +-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing |

607 | +-- > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")]) |

608 | +-- > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")]) |

609 | +-- > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a") |

610 | + |

611 | hunk ./Data/Map.hs 603 |

612 | --- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@ |

613 | +-- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@. |

614 | +-- |

615 | +-- > let f _ = Nothing |

616 | +-- > alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] |

617 | +-- > alter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

618 | +-- > |

619 | +-- > let f _ = Just "c" |

620 | +-- > alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")] |

621 | +-- > alter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")] |

622 | + |

623 | hunk ./Data/Map.hs 633 |

624 | +-- |

625 | +-- > findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map |

626 | +-- > findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0 |

627 | +-- > findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1 |

628 | +-- > findIndex 6 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map |

629 | + |

630 | hunk ./Data/Map.hs 646 |

631 | --- /0/ up to, but not including, the 'size' of the map. |

632 | +-- /0/ up to, but not including, the 'size' of the map. |

633 | +-- |

634 | +-- > isJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")])) == False |

635 | +-- > fromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0 |

636 | +-- > fromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1 |

637 | +-- > isJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")])) == False |

638 | + |

639 | hunk ./Data/Map.hs 667 |

640 | +-- |

641 | +-- > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b") |

642 | +-- > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a") |

643 | +-- > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range |

644 | + |

645 | hunk ./Data/Map.hs 684 |

646 | +-- |

647 | +-- > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")] |

648 | +-- > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")] |

649 | +-- > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range |

650 | +-- > updateAt (\ _ _ -> Just "x") (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range |

651 | +-- > updateAt (\_ _ -> Nothing) 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

652 | +-- > updateAt (\_ _ -> Nothing) 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

653 | +-- > updateAt (\_ _ -> Nothing) 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range |

654 | +-- > updateAt (\_ _ -> Nothing) (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range |

655 | + |

656 | hunk ./Data/Map.hs 708 |

657 | +-- |

658 | +-- > deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

659 | +-- > deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

660 | +-- > deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range |

661 | +-- > deleteAt (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range |

662 | + |

663 | hunk ./Data/Map.hs 722 |

664 | --- | /O(log n)/. The minimal key of the map. |

665 | +-- | /O(log n)/. The minimal key of the map. Calls 'error' is the map is empty. |

666 | +-- |

667 | +-- > findMin (fromList [(5,"a"), (3,"b")]) == (3,"b") |

668 | +-- > findMin empty Error: empty map has no minimal element |

669 | + |

670 | hunk ./Data/Map.hs 732 |

671 | --- | /O(log n)/. The maximal key of the map. |

672 | +-- | /O(log n)/. The maximal key of the map. Calls 'error' is the map is empty. |

673 | +-- |

674 | +-- > findMax (fromList [(5,"a"), (3,"b")]) == (5,"a") |

675 | +-- > findMax empty Error: empty map has no maximal element |

676 | + |

677 | hunk ./Data/Map.hs 742 |

678 | --- | /O(log n)/. Delete the minimal key. |

679 | +-- | /O(log n)/. Delete the minimal key. Returns an empty map if the map is empty. |

680 | +-- |

681 | +-- > deleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")] |

682 | +-- > deleteMin empty == empty |

683 | + |

684 | hunk ./Data/Map.hs 752 |

685 | --- | /O(log n)/. Delete the maximal key. |

686 | +-- | /O(log n)/. Delete the maximal key. Returns an empty map if the map is empty. |

687 | +-- |

688 | +-- > deleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")] |

689 | +-- > deleteMax empty == empty |

690 | + |

691 | hunk ./Data/Map.hs 763 |

692 | +-- |

693 | +-- > updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")] |

694 | +-- > updateMin (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

695 | + |

696 | hunk ./Data/Map.hs 772 |

697 | +-- |

698 | +-- > updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")] |

699 | +-- > updateMax (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

700 | + |

701 | hunk ./Data/Map.hs 782 |

702 | +-- |

703 | +-- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")] |

704 | +-- > updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

705 | + |

706 | hunk ./Data/Map.hs 796 |

707 | +-- |

708 | +-- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")] |

709 | +-- > updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

710 | + |

711 | hunk ./Data/Map.hs 811 |

712 | +-- |

713 | +-- > v <- minViewWithKey (fromList [(5,"a"), (3,"b")]) |

714 | +-- > v == ((3,"b"), singleton 5 "a") |

715 | +-- > minViewWithKey empty Error: empty map |

716 | + |

717 | hunk ./Data/Map.hs 817 |

718 | -minViewWithKey Tip = fail "Map.minView: empty map" |

719 | +minViewWithKey Tip = fail "Map.minViewWithKey: empty map" |

720 | hunk ./Data/Map.hs 822 |

721 | +-- |

722 | +-- > v <- maxViewWithKey (fromList [(5,"a"), (3,"b")]) |

723 | +-- > v == ((5,"a"), singleton 3 "b") |

724 | +-- > maxViewWithKey empty Error: empty map |

725 | + |

726 | hunk ./Data/Map.hs 828 |

727 | -maxViewWithKey Tip = fail "Map.maxView: empty map" |

728 | +maxViewWithKey Tip = fail "Map.maxViewWithKey: empty map" |

729 | hunk ./Data/Map.hs 833 |

730 | +-- |

731 | +-- > v <- minView (fromList [(5,"a"), (3,"b")]) |

732 | +-- > v == ("b", singleton 5 "a") |

733 | +-- > minView empty Error: empty map |

734 | + |

735 | hunk ./Data/Map.hs 844 |

736 | +-- |

737 | +-- > v <- maxView (fromList [(5,"a"), (3,"b")]) |

738 | +-- > v == ("a", singleton 3 "b") |

739 | +-- > maxView empty Error: empty map |

740 | + |

741 | hunk ./Data/Map.hs 862 |

742 | +-- |

743 | +-- > unions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] |

744 | +-- > == fromList [(3, "b"), (5, "a"), (7, "C")] |

745 | +-- > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])] |

746 | +-- > == fromList [(3, "B3"), (5, "A3"), (7, "C")] |

747 | + |

748 | hunk ./Data/Map.hs 874 |

749 | +-- |

750 | +-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] |

751 | +-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")] |

752 | + |

753 | hunk ./Data/Map.hs 887 |

754 | --- Hedge-union is more efficient on (bigset `union` smallset) |

755 | +-- Hedge-union is more efficient on (bigset \``union`\` smallset). |

756 | +-- |

757 | +-- > union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")] |

758 | + |

759 | hunk ./Data/Map.hs 927 |

760 | +-- |

761 | +-- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")] |

762 | + |

763 | hunk ./Data/Map.hs 936 |

764 | --- Hedge-union is more efficient on (bigset `union` smallset). |

765 | +-- Hedge-union is more efficient on (bigset \``union`\` smallset). |

766 | +-- |

767 | +-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value |

768 | +-- > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")] |

769 | + |

770 | hunk ./Data/Map.hs 965 |

771 | +-- Return elements of the first map not existing in the second map. |

772 | hunk ./Data/Map.hs 967 |

773 | +-- |

774 | +-- > difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b" |

775 | + |

776 | hunk ./Data/Map.hs 986 |

777 | +-- When two equal keys are |

778 | +-- encountered, the combining function is applied to the values of these keys. |

779 | +-- If it returns 'Nothing', the element is discarded (proper set difference). If |

780 | +-- it returns (@'Just' y@), the element is updated with a new value @y@. |

781 | hunk ./Data/Map.hs 991 |

782 | +-- |

783 | +-- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing |

784 | +-- > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")]) |

785 | +-- > == singleton 3 "b:B" |

786 | + |

787 | hunk ./Data/Map.hs 1005 |

788 | +-- |

789 | +-- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing |

790 | +-- > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")]) |

791 | +-- > == singleton 3 "3:b|B" |

792 | + |

793 | hunk ./Data/Map.hs 1038 |

794 | --- | /O(n+m)/. Intersection of two maps. The values in the first |

795 | --- map are returned, i.e. (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). |

796 | +-- | /O(n+m)/. Intersection of two maps. |

797 | +-- Return data in the first map for the keys existing in both maps. |

798 | +-- (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). |

799 | +-- |

800 | +-- > intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a" |

801 | + |

802 | hunk ./Data/Map.hs 1049 |

803 | +-- |

804 | +-- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA" |

805 | + |

806 | hunk ./Data/Map.hs 1057 |

807 | --- Intersection is more efficient on (bigset `intersection` smallset) |

808 | +-- Intersection is more efficient on (bigset \``intersection`\` smallset). |

809 | +-- |

810 | +-- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar |

811 | +-- > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A" |

812 | + |

813 | hunk ./Data/Map.hs 1078 |

814 | - |

815 | hunk ./Data/Map.hs 1101 |

816 | --- | /O(n+m)/. |

817 | +-- | /O(n+m)/. |

818 | hunk ./Data/Map.hs 1103 |

819 | +-- |

820 | hunk ./Data/Map.hs 1108 |

821 | -{- | /O(n+m)/. |

822 | +{- | /O(n+m)/. |

823 | hunk ./Data/Map.hs 1123 |

824 | + |

825 | + |

826 | hunk ./Data/Map.hs 1160 |

827 | + |

828 | + |

829 | hunk ./Data/Map.hs 1171 |

830 | +-- |

831 | +-- > filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" |

832 | +-- > filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty |

833 | +-- > filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty |

834 | + |

835 | hunk ./Data/Map.hs 1181 |

836 | +-- |

837 | +-- > filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" |

838 | + |

839 | hunk ./Data/Map.hs 1191 |

840 | --- | /O(n)/. partition the map according to a predicate. The first |

841 | +-- | /O(n)/. Partition the map according to a predicate. The first |

842 | hunk ./Data/Map.hs 1194 |

843 | +-- |

844 | +-- > partition (> "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") |

845 | +-- > partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) |

846 | +-- > partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) |

847 | + |

848 | hunk ./Data/Map.hs 1203 |

849 | --- | /O(n)/. partition the map according to a predicate. The first |

850 | +-- | /O(n)/. Partition the map according to a predicate. The first |

851 | hunk ./Data/Map.hs 1206 |

852 | +-- |

853 | +-- > partitionWithKey (\ k _ -> k > 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b") |

854 | +-- > partitionWithKey (\ k _ -> k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) |

855 | +-- > partitionWithKey (\ k _ -> k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) |

856 | + |

857 | hunk ./Data/Map.hs 1221 |

858 | +-- |

859 | +-- > let f x = if x == "a" then Just "new a" else Nothing |

860 | +-- > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a" |

861 | + |

862 | hunk ./Data/Map.hs 1230 |

863 | +-- |

864 | +-- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing |

865 | +-- > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3" |

866 | + |

867 | hunk ./Data/Map.hs 1241 |

868 | +-- |

869 | +-- > let f a = if a < "c" then Left a else Right a |

870 | +-- > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

871 | +-- > == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")]) |

872 | +-- > |

873 | +-- > mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

874 | +-- > == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

875 | + |

876 | hunk ./Data/Map.hs 1254 |

877 | +-- |

878 | +-- > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a) |

879 | +-- > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

880 | +-- > == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")]) |

881 | +-- > |

882 | +-- > mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) |

883 | +-- > == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")]) |

884 | + |

885 | hunk ./Data/Map.hs 1276 |

886 | +-- |

887 | +-- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")] |

888 | + |

889 | hunk ./Data/Map.hs 1284 |

890 | +-- |

891 | +-- > let f key x = (show key) ++ ":" ++ x |

892 | +-- > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")] |

893 | + |

894 | hunk ./Data/Map.hs 1295 |

895 | +-- |

896 | +-- > let f a b = (a ++ b, b ++ "X") |

897 | +-- > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")]) |

898 | + |

899 | hunk ./Data/Map.hs 1305 |

900 | +-- |

901 | +-- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X") |

902 | +-- > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")]) |

903 | + |

904 | hunk ./Data/Map.hs 1337 |

905 | --- | /O(n*log n)/. |

906 | +-- | /O(n*log n)/. |

907 | hunk ./Data/Map.hs 1343 |

908 | +-- |

909 | +-- > mapKeys (+ 1) (fromList [(5,"a"), (3,"b")]) == fromList [(4, "b"), (6, "a")] |

910 | +-- > mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c" |

911 | +-- > mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c" |

912 | hunk ./Data/Map.hs 1351 |

913 | --- | /O(n*log n)/. |

914 | +-- | /O(n*log n)/. |

915 | hunk ./Data/Map.hs 1357 |

916 | +-- |

917 | +-- > mapKeysWith (++) (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab" |

918 | +-- > mapKeysWith (++) (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab" |

919 | hunk ./Data/Map.hs 1369 |

920 | +-- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@. |

921 | hunk ./Data/Map.hs 1376 |

922 | +-- |

923 | +-- This means that @f@ maps distinct original keys to distinct resulting keys. |

924 | +-- This function has better performance than 'mapKeys'. |

925 | +-- |

926 | +-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")] |

927 | +-- > valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True |

928 | +-- > valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False |

929 | hunk ./Data/Map.hs 1399 |

930 | +-- > let f a len = len + (length a) |

931 | +-- > fold f 0 (fromList [(5,"a"), (3,"bbb")]) == 4 |

932 | + |

933 | hunk ./Data/Map.hs 1412 |

934 | +-- > let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" |

935 | +-- > foldWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)" |

936 | + |

937 | hunk ./Data/Map.hs 1439 |

938 | +-- |

939 | +-- > elems (fromList [(5,"a"), (3,"b")]) == ["b","a"] |

940 | +-- > elems empty == [] |

941 | + |

942 | hunk ./Data/Map.hs 1448 |

943 | +-- |

944 | +-- > keys (fromList [(5,"a"), (3,"b")]) == [3,5] |

945 | +-- > keys empty == [] |

946 | + |

947 | hunk ./Data/Map.hs 1457 |

948 | +-- |

949 | +-- > keysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5] |

950 | +-- > keysSet empty == Data.Set.empty |

951 | + |

952 | hunk ./Data/Map.hs 1465 |

953 | +-- |

954 | +-- > assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

955 | +-- > assocs empty == [] |

956 | + |

957 | hunk ./Data/Map.hs 1478 |

958 | +-- If the list contains more than one value for the same key, the last value |

959 | +-- for the key is retained. |

960 | +-- |

961 | +-- > fromList [] == empty |

962 | +-- > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")] |

963 | +-- > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")] |

964 | + |

965 | hunk ./Data/Map.hs 1492 |

966 | +-- |

967 | +-- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] |

968 | +-- > fromListWith (++) [] == empty |

969 | + |

970 | hunk ./Data/Map.hs 1501 |

971 | +-- |

972 | +-- > let f k a1 a2 = (show k) ++ a1 ++ a2 |

973 | +-- > fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")] |

974 | +-- > fromListWithKey f [] == empty |

975 | + |

976 | hunk ./Data/Map.hs 1513 |

977 | +-- |

978 | +-- > toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

979 | +-- > toList empty == [] |

980 | + |

981 | hunk ./Data/Map.hs 1521 |

982 | +-- |

983 | +-- > toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] |

984 | + |

985 | hunk ./Data/Map.hs 1527 |

986 | --- | /O(n)/. |

987 | +-- | /O(n)/. |

988 | hunk ./Data/Map.hs 1541 |

989 | +-- |

990 | +-- > fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] |

991 | +-- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")] |

992 | +-- > valid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True |

993 | +-- > valid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False |

994 | + |

995 | hunk ./Data/Map.hs 1553 |

996 | +-- |

997 | +-- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")] |

998 | +-- > valid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True |

999 | +-- > valid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False |

1000 | + |

1001 | hunk ./Data/Map.hs 1565 |

1002 | +-- |

1003 | +-- > let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2 |

1004 | +-- > fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")] |

1005 | +-- > valid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True |

1006 | +-- > valid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False |

1007 | + |

1008 | hunk ./Data/Map.hs 1590 |

1009 | +-- |

1010 | +-- > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] |

1011 | +-- > valid (fromDistinctAscList [(3,"b"), (5,"a")]) == True |

1012 | +-- > valid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False |

1013 | + |

1014 | hunk ./Data/Map.hs 1681 |

1015 | --- 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@. |

1016 | +-- the keys in @map1@ are smaller than @k@ and the keys in @map2@ larger than @k@. |

1017 | +-- Any key equal to @k@ is found in neither @map1@ nor @map2@. |

1018 | +-- |

1019 | +-- > split 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")]) |

1020 | +-- > split 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a") |

1021 | +-- > split 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") |

1022 | +-- > split 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty) |

1023 | +-- > split 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty) |

1024 | + |

1025 | hunk ./Data/Map.hs 1700 |

1026 | +-- |

1027 | +-- > splitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")]) |

1028 | +-- > splitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a") |

1029 | +-- > splitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a") |

1030 | +-- > splitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty) |

1031 | +-- > splitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty) |

1032 | + |

1033 | hunk ./Data/Map.hs 1810 |

1034 | +-- |

1035 | +-- > deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")]) |

1036 | +-- > deleteFindMin Error: can not return the minimal element of an empty map |

1037 | + |

1038 | hunk ./Data/Map.hs 1822 |

1039 | +-- |

1040 | +-- > deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")]) |

1041 | +-- > deleteFindMax empty Error: can not return the maximal element of an empty map |

1042 | + |

1043 | hunk ./Data/Map.hs 1980 |

1044 | --- in a compressed, hanging format. |

1045 | +-- in a compressed, hanging format. See 'showTreeWith'. |

1046 | hunk ./Data/Map.hs 2081 |

1047 | +-- |

1048 | +-- > valid (fromAscList [(3,"b"), (5,"a")]) == True |

1049 | +-- > valid (fromAscList [(5,"a"), (3,"b")]) == False |

1050 | + |

1051 | } |

1052 | |

1053 | Context: |

1054 | |

1055 | [Fix a link in haddock docs |

1056 | Ian Lynagh <igloo@earth.li>**20071126184450] |

1057 | [Fix some URLs |

1058 | Ian Lynagh <igloo@earth.li>**20071126214233] |

1059 | [Add tiny regression test |

1060 | David Benbennick <dbenbenn@gmail.com>**20071113045358] |

1061 | [Fix ticket 1762 |

1062 | David Benbennick <dbenbenn@gmail.com>**20071111201939] |

1063 | [Specify build-type: Simple |

1064 | Duncan Coutts <duncan@haskell.org>**20071018125404] |

1065 | [Add a boring file |

1066 | Ian Lynagh <igloo@earth.li>**20070913204647] |

1067 | [TAG 2007-09-13 |

1068 | Ian Lynagh <igloo@earth.li>**20070913215901] |

1069 | Patch bundle hash: |

1070 | 245f259beed9afa1702486da444186d49a821a33 |