forked from JPMoresmau/scion-class-browser
/
containers.txt
2909 lines (2526 loc) · 113 KB
/
containers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Assorted concrete container types
--
-- This package contains efficient general-purpose implementations of
-- various basic immutable container types. The declared cost of each
-- operation is either worst-case or amortized, but remains valid even if
-- structures are shared.
@package containers
@version 0.4.0.0
-- | An efficient implementation of sets.
--
-- Since many function names (but not the type name) clash with
-- <a>Prelude</a> names, this module is usually imported
-- <tt>qualified</tt>, e.g.
--
-- <pre>
-- import Data.Set (Set)
-- import qualified Data.Set as Set
-- </pre>
--
-- The implementation of <a>Set</a> is based on <i>size balanced</i>
-- binary trees (or trees of <i>bounded balance</i>) as described by:
--
-- <ul>
-- <li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
-- of Functional Programming 3(4):553-562, October 1993,
-- <a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
-- <li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
-- bounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
-- </ul>
--
-- Note that the implementation is <i>left-biased</i> -- the elements of
-- a first argument are always preferred to the second, for example in
-- <a>union</a> or <a>insert</a>. Of course, left-biasing can only be
-- observed when equality is an equivalence relation instead of
-- structural equality.
module Data.Set
-- | A set of values <tt>a</tt>.
data Set a
-- | <i>O(n+m)</i>. See <a>difference</a>.
(\\) :: Ord a => Set a -> Set a -> Set a
-- | <i>O(1)</i>. Is this the empty set?
null :: Set a -> Bool
-- | <i>O(1)</i>. The number of elements in the set.
size :: Set a -> Int
-- | <i>O(log n)</i>. Is the element in the set?
member :: Ord a => a -> Set a -> Bool
-- | <i>O(log n)</i>. Is the element not in the set?
notMember :: Ord a => a -> Set a -> Bool
-- | <i>O(n+m)</i>. Is this a subset? <tt>(s1 <a>isSubsetOf</a> s2)</tt>
-- tells whether <tt>s1</tt> is a subset of <tt>s2</tt>.
isSubsetOf :: Ord a => Set a -> Set a -> Bool
-- | <i>O(n+m)</i>. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: Ord a => Set a -> Set a -> Bool
-- | <i>O(1)</i>. The empty set.
empty :: Set a
-- | <i>O(1)</i>. Create a singleton set.
singleton :: a -> Set a
-- | <i>O(log n)</i>. Insert an element in a set. If the set already
-- contains an element equal to the given value, it is replaced with the
-- new value.
insert :: Ord a => a -> Set a -> Set a
-- | <i>O(log n)</i>. Delete an element from a set.
delete :: Ord a => a -> Set a -> Set a
-- | <i>O(n+m)</i>. The union of two sets, preferring the first set when
-- equal elements are encountered. The implementation uses the efficient
-- <i>hedge-union</i> algorithm. Hedge-union is more efficient on (bigset
-- <a>union</a> smallset).
union :: Ord a => Set a -> Set a -> Set a
-- | The union of a list of sets: (<tt><a>unions</a> == <a>foldl</a>
-- <a>union</a> <a>empty</a></tt>).
unions :: Ord a => [Set a] -> Set a
-- | <i>O(n+m)</i>. Difference of two sets. The implementation uses an
-- efficient <i>hedge</i> algorithm comparable with <i>hedge-union</i>.
difference :: Ord a => Set a -> Set a -> Set a
-- | <i>O(n+m)</i>. The intersection of two sets. Elements of the result
-- come from the first set, so for example
--
-- <pre>
-- import qualified Data.Set as S
-- data AB = A | B deriving Show
-- instance Ord AB where compare _ _ = EQ
-- instance Eq AB where _ == _ = True
-- main = print (S.singleton A `S.intersection` S.singleton B,
-- S.singleton B `S.intersection` S.singleton A)
-- </pre>
--
-- prints <tt>(fromList [A],fromList [B])</tt>.
intersection :: Ord a => Set a -> Set a -> Set a
-- | <i>O(n)</i>. Filter all elements that satisfy the predicate.
filter :: Ord a => (a -> Bool) -> Set a -> Set a
-- | <i>O(n)</i>. Partition the set into two sets, one with all elements
-- that satisfy the predicate and one with all elements that don't
-- satisfy the predicate. See also <a>split</a>.
partition :: Ord a => (a -> Bool) -> Set a -> (Set a, Set a)
-- | <i>O(log n)</i>. The expression (<tt><a>split</a> x set</tt>) is a
-- pair <tt>(set1,set2)</tt> where <tt>set1</tt> comprises the elements
-- of <tt>set</tt> less than <tt>x</tt> and <tt>set2</tt> comprises the
-- elements of <tt>set</tt> greater than <tt>x</tt>.
split :: Ord a => a -> Set a -> (Set a, Set a)
-- | <i>O(log n)</i>. Performs a <a>split</a> but also returns whether the
-- pivot element was found in the original set.
splitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a)
-- | <i>O(n*log n)</i>. <tt><a>map</a> f s</tt> is the set obtained by
-- applying <tt>f</tt> to each element of <tt>s</tt>.
--
-- It's worth noting that the size of the result may be smaller if, for
-- some <tt>(x,y)</tt>, <tt>x /= y && f x == f y</tt>
map :: (Ord a, Ord b) => (a -> b) -> Set a -> Set b
-- | <i>O(n)</i>. The
--
-- <tt><a>mapMonotonic</a> f s == <a>map</a> f s</tt>, but works only
-- when <tt>f</tt> is monotonic. <i>The precondition is not checked.</i>
-- Semi-formally, we have:
--
-- <pre>
-- and [x < y ==> f x < f y | x <- ls, y <- ls]
-- ==> mapMonotonic f s == map f s
-- where ls = toList s
-- </pre>
mapMonotonic :: (a -> b) -> Set a -> Set b
-- | <i>O(n)</i>. Fold over the elements of a set in an unspecified order.
fold :: (a -> b -> b) -> b -> Set a -> b
-- | <i>O(log n)</i>. The minimal element of a set.
findMin :: Set a -> a
-- | <i>O(log n)</i>. The maximal element of a set.
findMax :: Set a -> a
-- | <i>O(log n)</i>. Delete the minimal element.
deleteMin :: Set a -> Set a
-- | <i>O(log n)</i>. Delete the maximal element.
deleteMax :: Set a -> Set a
-- | <i>O(log n)</i>. Delete and find the minimal element.
--
-- <pre>
-- deleteFindMin set = (findMin set, deleteMin set)
-- </pre>
deleteFindMin :: Set a -> (a, Set a)
-- | <i>O(log n)</i>. Delete and find the maximal element.
--
-- <pre>
-- deleteFindMax set = (findMax set, deleteMax set)
-- </pre>
deleteFindMax :: Set a -> (a, Set a)
-- | <i>O(log n)</i>. Retrieves the maximal key of the set, and the set
-- stripped of that element, or <a>Nothing</a> if passed an empty set.
maxView :: Set a -> Maybe (a, Set a)
-- | <i>O(log n)</i>. Retrieves the minimal key of the set, and the set
-- stripped of that element, or <a>Nothing</a> if passed an empty set.
minView :: Set a -> Maybe (a, Set a)
-- | <i>O(n)</i>. The elements of a set.
elems :: Set a -> [a]
-- | <i>O(n)</i>. Convert the set to a list of elements.
toList :: Set a -> [a]
-- | <i>O(n*log n)</i>. Create a set from a list of elements.
fromList :: Ord a => [a] -> Set a
-- | <i>O(n)</i>. Convert the set to an ascending list of elements.
toAscList :: Set a -> [a]
-- | <i>O(n)</i>. Build a set from an ascending list in linear time. <i>The
-- precondition (input list is ascending) is not checked.</i>
fromAscList :: Eq a => [a] -> Set a
-- | <i>O(n)</i>. Build a set from an ascending list of distinct elements
-- in linear time. <i>The precondition (input list is strictly ascending)
-- is not checked.</i>
fromDistinctAscList :: [a] -> Set a
-- | <i>O(n)</i>. Show the tree that implements the set. The tree is shown
-- in a compressed, hanging format.
showTree :: Show a => Set a -> String
-- | <i>O(n)</i>. The expression (<tt>showTreeWith hang wide map</tt>)
-- shows the tree that implements the set. If <tt>hang</tt> is
-- <tt>True</tt>, a <i>hanging</i> tree is shown otherwise a rotated tree
-- is shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
-- shown.
--
-- <pre>
-- Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]
-- 4
-- +--2
-- | +--1
-- | +--3
-- +--5
--
-- Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]
-- 4
-- |
-- +--2
-- | |
-- | +--1
-- | |
-- | +--3
-- |
-- +--5
--
-- Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]
-- +--5
-- |
-- 4
-- |
-- | +--3
-- | |
-- +--2
-- |
-- +--1
-- </pre>
showTreeWith :: Show a => Bool -> Bool -> Set a -> String
-- | <i>O(n)</i>. Test if the internal set structure is valid.
valid :: Ord a => Set a -> Bool
instance Typeable1 Set
instance (Read a, Ord a) => Read (Set a)
instance Show a => Show (Set a)
instance Ord a => Ord (Set a)
instance Eq a => Eq (Set a)
instance (Data a, Ord a) => Data (Set a)
instance Foldable Set
instance Ord a => Monoid (Set a)
-- | An efficient implementation of maps from keys to values
-- (dictionaries).
--
-- Since many function names (but not the type name) clash with
-- <a>Prelude</a> names, this module is usually imported
-- <tt>qualified</tt>, e.g.
--
-- <pre>
-- import Data.Map (Map)
-- import qualified Data.Map as Map
-- </pre>
--
-- The implementation of <a>Map</a> is based on <i>size balanced</i>
-- binary trees (or trees of <i>bounded balance</i>) as described by:
--
-- <ul>
-- <li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
-- of Functional Programming 3(4):553-562, October 1993,
-- <a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
-- <li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
-- bounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
-- </ul>
--
-- Note that the implementation is <i>left-biased</i> -- the elements of
-- a first argument are always preferred to the second, for example in
-- <a>union</a> or <a>insert</a>.
--
-- Operation comments contain the operation time complexity in the Big-O
-- notation <a>http://en.wikipedia.org/wiki/Big_O_notation</a>.
module Data.Map
-- | A Map from keys <tt>k</tt> to values <tt>a</tt>.
data Map k a
-- | <i>O(log n)</i>. Find the value at a key. Calls <a>error</a> when the
-- element can not be found.
--
-- <pre>
-- fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map
-- fromList [(5,'a'), (3,'b')] ! 5 == 'a'
-- </pre>
(!) :: Ord k => Map k a -> k -> a
-- | Same as <a>difference</a>.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
-- | <i>O(1)</i>. Is the map empty?
--
-- <pre>
-- Data.Map.null (empty) == True
-- Data.Map.null (singleton 1 'a') == False
-- </pre>
null :: Map k a -> Bool
-- | <i>O(1)</i>. The number of elements in the map.
--
-- <pre>
-- size empty == 0
-- size (singleton 1 'a') == 1
-- size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
-- </pre>
size :: Map k a -> Int
-- | <i>O(log n)</i>. Is the key a member of the map? See also
-- <a>notMember</a>.
--
-- <pre>
-- member 5 (fromList [(5,'a'), (3,'b')]) == True
-- member 1 (fromList [(5,'a'), (3,'b')]) == False
-- </pre>
member :: Ord k => k -> Map k a -> Bool
-- | <i>O(log n)</i>. Is the key not a member of the map? See also
-- <a>member</a>.
--
-- <pre>
-- notMember 5 (fromList [(5,'a'), (3,'b')]) == False
-- notMember 1 (fromList [(5,'a'), (3,'b')]) == True
-- </pre>
notMember :: Ord k => k -> Map k a -> Bool
-- | <i>O(log n)</i>. Lookup the value at a key in the map.
--
-- The function will return the corresponding value as <tt>(<a>Just</a>
-- value)</tt>, or <a>Nothing</a> if the key isn't in the map.
--
-- An example of using <tt>lookup</tt>:
--
-- <pre>
-- 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"))
-- </pre>
--
-- The output of this program:
--
-- <pre>
-- John's currency: Just "Euro"
-- Pete's currency: Nothing
-- </pre>
lookup :: Ord k => k -> Map k a -> Maybe a
-- | <i>O(log n)</i>. The expression <tt>(<a>findWithDefault</a> def k
-- map)</tt> returns the value at key <tt>k</tt> or returns default value
-- <tt>def</tt> when the key is not in the map.
--
-- <pre>
-- findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
-- findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
-- </pre>
findWithDefault :: Ord k => a -> k -> Map k a -> a
-- | <i>O(1)</i>. The empty map.
--
-- <pre>
-- empty == fromList []
-- size empty == 0
-- </pre>
empty :: Map k a
-- | <i>O(1)</i>. A map with a single element.
--
-- <pre>
-- singleton 1 'a' == fromList [(1, 'a')]
-- size (singleton 1 'a') == 1
-- </pre>
singleton :: k -> a -> Map k a
-- | <i>O(log n)</i>. 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. <a>insert</a> is equivalent to <tt><a>insertWith</a>
-- <a>const</a></tt>.
--
-- <pre>
-- 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'
-- </pre>
insert :: Ord k => k -> a -> Map k a -> Map k a
-- | <i>O(log n)</i>. Insert with a function, combining new value and old
-- value. <tt><a>insertWith</a> f key value mp</tt> will insert the pair
-- (key, value) into <tt>mp</tt> if key does not exist in the map. If the
-- key does exist, the function will insert the pair <tt>(key, f
-- new_value old_value)</tt>.
--
-- <pre>
-- 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"
-- </pre>
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
-- | Same as <a>insertWith</a>, but the combining function is applied
-- strictly. This is often the most desirable behavior.
--
-- For example, to update a counter:
--
-- <pre>
-- insertWith' (+) k 1 m
-- </pre>
insertWith' :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
-- | <i>O(log n)</i>. Insert with a function, combining key, new value and
-- old value. <tt><a>insertWithKey</a> f key value mp</tt> will insert
-- the pair (key, value) into <tt>mp</tt> if key does not exist in the
-- map. If the key does exist, the function will insert the pair
-- <tt>(key,f key new_value old_value)</tt>. Note that the key passed to
-- f is the same key passed to <a>insertWithKey</a>.
--
-- <pre>
-- 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"
-- </pre>
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
-- | Same as <a>insertWithKey</a>, but the combining function is applied
-- strictly.
insertWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
-- | <i>O(log n)</i>. Combines insert operation with old value retrieval.
-- The expression (<tt><a>insertLookupWithKey</a> f k x map</tt>) is a
-- pair where the first element is equal to (<tt><a>lookup</a> k
-- map</tt>) and the second element equal to (<tt><a>insertWithKey</a> f
-- k x map</tt>).
--
-- <pre>
-- 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")
-- </pre>
--
-- This is how to define <tt>insertLookup</tt> using
-- <tt>insertLookupWithKey</tt>:
--
-- <pre>
-- 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")])
-- </pre>
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
-- | <i>O(log n)</i>. A strict version of <a>insertLookupWithKey</a>.
insertLookupWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
-- | <i>O(log n)</i>. Delete a key and its value from the map. When the key
-- is not a member of the map, the original map is returned.
--
-- <pre>
-- 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
-- </pre>
delete :: Ord k => k -> Map k a -> Map k a
-- | <i>O(log n)</i>. 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.
--
-- <pre>
-- 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 == empty
-- </pre>
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a
-- | <i>O(log n)</i>. Adjust a value at a specific key. When the key is not
-- a member of the map, the original map is returned.
--
-- <pre>
-- 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
-- </pre>
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
-- | <i>O(log n)</i>. The expression (<tt><a>update</a> f k map</tt>)
-- updates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
-- (<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
-- (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
-- <tt>y</tt>.
--
-- <pre>
-- 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"
-- </pre>
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
-- | <i>O(log n)</i>. The expression (<tt><a>updateWithKey</a> f k
-- map</tt>) updates the value <tt>x</tt> at <tt>k</tt> (if it is in the
-- map). If (<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted.
-- If it is (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the
-- new value <tt>y</tt>.
--
-- <pre>
-- 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"
-- </pre>
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
-- | <i>O(log n)</i>. Lookup and update. See also <a>updateWithKey</a>. The
-- function returns changed value, if it is updated. Returns the original
-- key value if the map entry is deleted.
--
-- <pre>
-- 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")
-- </pre>
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
-- | <i>O(log n)</i>. The expression (<tt><a>alter</a> f k map</tt>) alters
-- the value <tt>x</tt> at <tt>k</tt>, or absence thereof. <a>alter</a>
-- can be used to insert, delete, or update a value in a <a>Map</a>. In
-- short : <tt><a>lookup</a> k (<a>alter</a> f k m) = f (<a>lookup</a> k
-- m)</tt>.
--
-- <pre>
-- 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")]
-- </pre>
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
-- | <i>O(n+m)</i>. The expression (<tt><a>union</a> t1 t2</tt>) takes the
-- left-biased union of <tt>t1</tt> and <tt>t2</tt>. It prefers
-- <tt>t1</tt> when duplicate keys are encountered, i.e.
-- (<tt><a>union</a> == <a>unionWith</a> <a>const</a></tt>). The
-- implementation uses the efficient <i>hedge-union</i> algorithm.
-- Hedge-union is more efficient on (bigset `<a>union</a>` smallset).
--
-- <pre>
-- union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
-- </pre>
union :: Ord k => Map k a -> Map k a -> Map k a
-- | <i>O(n+m)</i>. Union with a combining function. The implementation
-- uses the efficient <i>hedge-union</i> algorithm.
--
-- <pre>
-- unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
-- </pre>
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
-- | <i>O(n+m)</i>. Union with a combining function. The implementation
-- uses the efficient <i>hedge-union</i> algorithm. Hedge-union is more
-- efficient on (bigset `<a>union</a>` smallset).
--
-- <pre>
-- 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")]
-- </pre>
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
-- | The union of a list of maps: (<tt><a>unions</a> == <a>foldl</a>
-- <a>union</a> <a>empty</a></tt>).
--
-- <pre>
-- 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")]
-- </pre>
unions :: Ord k => [Map k a] -> Map k a
-- | The union of a list of maps, with a combining operation:
-- (<tt><a>unionsWith</a> f == <a>foldl</a> (<a>unionWith</a> f)
-- <a>empty</a></tt>).
--
-- <pre>
-- unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
-- == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
-- </pre>
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a
-- | <i>O(n+m)</i>. Difference of two maps. Return elements of the first
-- map not existing in the second map. The implementation uses an
-- efficient <i>hedge</i> algorithm comparable with <i>hedge-union</i>.
--
-- <pre>
-- difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
-- </pre>
difference :: Ord k => Map k a -> Map k b -> Map k a
-- | <i>O(n+m)</i>. 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 <a>Nothing</a>, the element is discarded
-- (proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
-- element is updated with a new value <tt>y</tt>. The implementation
-- uses an efficient <i>hedge</i> algorithm comparable with
-- <i>hedge-union</i>.
--
-- <pre>
-- 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"
-- </pre>
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
-- | <i>O(n+m)</i>. 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 <a>Nothing</a>, the element is discarded
-- (proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
-- element is updated with a new value <tt>y</tt>. The implementation
-- uses an efficient <i>hedge</i> algorithm comparable with
-- <i>hedge-union</i>.
--
-- <pre>
-- 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"
-- </pre>
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
-- | <i>O(n+m)</i>. Intersection of two maps. Return data in the first map
-- for the keys existing in both maps. (<tt><a>intersection</a> m1 m2 ==
-- <a>intersectionWith</a> <a>const</a> m1 m2</tt>).
--
-- <pre>
-- intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
-- </pre>
intersection :: Ord k => Map k a -> Map k b -> Map k a
-- | <i>O(n+m)</i>. Intersection with a combining function.
--
-- <pre>
-- intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
-- </pre>
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
-- | <i>O(n+m)</i>. Intersection with a combining function. Intersection is
-- more efficient on (bigset `<a>intersection</a>` smallset).
--
-- <pre>
-- 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"
-- </pre>
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
-- | <i>O(n)</i>. Map a function over all values in the map.
--
-- <pre>
-- map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
-- </pre>
map :: (a -> b) -> Map k a -> Map k b
-- | <i>O(n)</i>. Map a function over all values in the map.
--
-- <pre>
-- let f key x = (show key) ++ ":" ++ x
-- mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
-- </pre>
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
-- | <i>O(n)</i>. The function <a>mapAccum</a> threads an accumulating
-- argument through the map in ascending order of keys.
--
-- <pre>
-- let f a b = (a ++ b, b ++ "X")
-- mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
-- </pre>
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
-- | <i>O(n)</i>. The function <a>mapAccumWithKey</a> threads an
-- accumulating argument through the map in ascending order of keys.
--
-- <pre>
-- 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")])
-- </pre>
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
-- | <i>O(n)</i>. The function <tt>mapAccumR</tt> threads an accumulating
-- argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
-- | <i>O(n*log n)</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained by
-- applying <tt>f</tt> to each key of <tt>s</tt>.
--
-- The size of the result may be smaller if <tt>f</tt> maps two or more
-- distinct keys to the same new key. In this case the value at the
-- smallest of these keys is retained.
--
-- <pre>
-- 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"
-- </pre>
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
-- obtained by applying <tt>f</tt> to each key of <tt>s</tt>.
--
-- The size of the result may be smaller if <tt>f</tt> maps two or more
-- distinct keys to the same new key. In this case the associated values
-- will be combined using <tt>c</tt>.
--
-- <pre>
-- 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"
-- </pre>
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
-- | <i>O(n)</i>. <tt><a>mapKeysMonotonic</a> f s == <a>mapKeys</a> f
-- s</tt>, but works only when <tt>f</tt> is strictly monotonic. That is,
-- for any values <tt>x</tt> and <tt>y</tt>, if <tt>x</tt> <
-- <tt>y</tt> then <tt>f x</tt> < <tt>f y</tt>. <i>The precondition is
-- not checked.</i> Semi-formally, we have:
--
-- <pre>
-- and [x < y ==> f x < f y | x <- ls, y <- ls]
-- ==> mapKeysMonotonic f s == mapKeys f s
-- where ls = keys s
-- </pre>
--
-- This means that <tt>f</tt> maps distinct original keys to distinct
-- resulting keys. This function has better performance than
-- <a>mapKeys</a>.
--
-- <pre>
-- 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
-- </pre>
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a
-- | <i>O(n)</i>. Fold the values in the map, such that <tt><a>fold</a> f z
-- == <a>foldr</a> f z . <a>elems</a></tt>. For example,
--
-- <pre>
-- elems map = fold (:) [] map
-- </pre>
--
-- <pre>
-- let f a len = len + (length a)
-- fold f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
-- </pre>
fold :: (a -> b -> b) -> b -> Map k a -> b
-- | <i>O(n)</i>. Fold the keys and values in the map, such that
-- <tt><a>foldWithKey</a> f z == <a>foldr</a> (<a>uncurry</a> f) z .
-- <a>toAscList</a></tt>. For example,
--
-- <pre>
-- keys map = foldWithKey (\k x ks -> k:ks) [] map
-- </pre>
--
-- <pre>
-- let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
-- foldWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
-- </pre>
--
-- This is identical to <a>foldrWithKey</a>, and you should use that one
-- instead of this one. This name is kept for backward compatibility.
foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
-- | <i>O(n)</i>. Post-order fold. The function will be applied from the
-- lowest value to the highest.
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
-- | <i>O(n)</i>. Pre-order fold. The function will be applied from the
-- highest value to the lowest.
foldlWithKey :: (b -> k -> a -> b) -> b -> Map k a -> b
-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
-- their keys.
--
-- <pre>
-- elems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
-- elems empty == []
-- </pre>
elems :: Map k a -> [a]
-- | <i>O(n)</i>. Return all keys of the map in ascending order.
--
-- <pre>
-- keys (fromList [(5,"a"), (3,"b")]) == [3,5]
-- keys empty == []
-- </pre>
keys :: Map k a -> [k]
-- | <i>O(n)</i>. The set of all keys of the map.
--
-- <pre>
-- keysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5]
-- keysSet empty == Data.Set.empty
-- </pre>
keysSet :: Map k a -> Set k
-- | <i>O(n)</i>. Return all key/value pairs in the map in ascending key
-- order.
--
-- <pre>
-- assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
-- assocs empty == []
-- </pre>
assocs :: Map k a -> [(k, a)]
-- | <i>O(n)</i>. Convert to a list of key/value pairs.
--
-- <pre>
-- toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
-- toList empty == []
-- </pre>
toList :: Map k a -> [(k, a)]
-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs. See
-- also <a>fromAscList</a>. If the list contains more than one value for
-- the same key, the last value for the key is retained.
--
-- <pre>
-- 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")]
-- </pre>
fromList :: Ord k => [(k, a)] -> Map k a
-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
-- combining function. See also <a>fromAscListWith</a>.
--
-- <pre>
-- fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
-- fromListWith (++) [] == empty
-- </pre>
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
-- combining function. See also <a>fromAscListWithKey</a>.
--
-- <pre>
-- 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
-- </pre>
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
-- | <i>O(n)</i>. Convert to an ascending list.
--
-- <pre>
-- toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
-- </pre>
toAscList :: Map k a -> [(k, a)]
-- | <i>O(n)</i>. Convert to a descending list.
toDescList :: Map k a -> [(k, a)]
-- | <i>O(n)</i>. Build a map from an ascending list in linear time. <i>The
-- precondition (input list is ascending) is not checked.</i>
--
-- <pre>
-- 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
-- </pre>
fromAscList :: Eq k => [(k, a)] -> Map k a
-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
-- combining function for equal keys. <i>The precondition (input list is
-- ascending) is not checked.</i>
--
-- <pre>
-- 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
-- </pre>
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a
-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
-- combining function for equal keys. <i>The precondition (input list is
-- ascending) is not checked.</i>
--
-- <pre>
-- 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
-- </pre>
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
-- | <i>O(n)</i>. Build a map from an ascending list of distinct elements
-- in linear time. <i>The precondition is not checked.</i>
--
-- <pre>
-- 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
-- </pre>
fromDistinctAscList :: [(k, a)] -> Map k a
-- | <i>O(n)</i>. Filter all values that satisfy the predicate.
--
-- <pre>
-- 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
-- </pre>
filter :: Ord k => (a -> Bool) -> Map k a -> Map k a
-- | <i>O(n)</i>. Filter all keys/values that satisfy the predicate.
--
-- <pre>
-- filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
-- </pre>
filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a
-- | <i>O(n)</i>. 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 <a>split</a>.
--
-- <pre>
-- 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")])
-- </pre>
partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a, Map k a)
-- | <i>O(n)</i>. 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 <a>split</a>.
--
-- <pre>
-- 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")])
-- </pre>
partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
--
-- <pre>
-- let f x = if x == "a" then Just "new a" else Nothing
-- mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
-- </pre>
mapMaybe :: Ord k => (a -> Maybe b) -> Map k a -> Map k b
-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
--
-- <pre>
-- let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing
-- mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
-- </pre>
mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
-- results.
--
-- <pre>
-- 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")])