Ticket #2580: folding.patch

File folding.patch, 6.4 KB (added by sedillard, 6 years ago)
Line 
1
2New patches:
3
4[increased folding options for Maps and Sets
5sedillard@gmail.com**20081002170452] {
6hunk ./Data/IntMap.hs 103
7+            , foldr
8+            , foldrWithKey
9+            , foldl
10+            , foldlWithKey
11hunk ./Data/IntMap.hs 175
12+import qualified Data.Foldable
13hunk ./Data/IntMap.hs 260
14+    foldr = Data.IntMap.foldr
15+    foldl = Data.IntMap.foldl
16hunk ./Data/IntMap.hs 1337
17+
18hunk ./Data/IntMap.hs 1342
19+
20+-- | /O(n)/. Fold the values in the map, such that
21+-- @'foldr' f z == 'Prelude.foldr' f z . 'elems'@.
22+
23+foldr :: (a -> b -> b) -> b -> IntMap a -> b
24+foldr f z t
25+  = foldrWithKey (const f) z t
26+
27+-- | /O(n)/. Fold the values in the map, such that
28+-- @'foldl' f z == 'Prelude.foldl' f z . 'elems'@.
29+
30+foldl :: (a -> b -> a) -> a -> IntMap b -> a
31+foldl f z t
32+  = foldlWithKey (\z _ x -> f z x) z t
33+
34hunk ./Data/IntMap.hs 1368
35-  = foldr f z t
36+  = foldrWithKey f z t
37+
38hunk ./Data/IntMap.hs 1371
39-foldr :: (Key -> a -> b -> b) -> b -> IntMap a -> b
40-foldr f z t
41+-- | /O(n)/. Fold the keys and values in the map, such that
42+-- @'foldrWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@.
43+
44+foldrWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b
45+foldrWithKey f z t
46hunk ./Data/IntMap.hs 1377
47-      Bin 0 m l r | m < 0 -> foldr' f (foldr' f z l) r  -- put negative numbers before.
48-      Bin _ _ _ _ -> foldr' f z t
49+      Bin 0 m l r | m < 0 -> go f (go f z l) r  -- negative keys first
50+      Bin _ _ _ _ -> go f z t
51hunk ./Data/IntMap.hs 1381
52+  where
53+    go :: (Key -> a -> b -> b) -> b -> IntMap a -> b
54+    go f z t
55+      = case t of
56+          Bin _ _ l r -> go f (go f z r) l
57+          Tip k x     -> f k x z
58+          Nil         -> z
59+
60+-- | /O(n)/. Fold the keys and values in the map, such that
61+-- @'foldlWithKey' f z == 'Prelude.foldl' ('uncurry' f) z . 'toAscList'@.
62hunk ./Data/IntMap.hs 1392
63-foldr' :: (Key -> a -> b -> b) -> b -> IntMap a -> b
64-foldr' f z t
65+foldlWithKey :: (a -> Key -> b -> a) -> a -> IntMap b -> a
66+foldlWithKey f z t
67hunk ./Data/IntMap.hs 1395
68-      Bin _ _ l r -> foldr' f (foldr' f z r) l
69-      Tip k x     -> f k x z
70+      Bin 0 m l r | m < 0 -> go f (go f z r) l  -- negative keys first
71+      Bin _ _ _ _ -> go f z t
72+      Tip k x     -> f z k x
73hunk ./Data/IntMap.hs 1399
74-
75+  where
76+    go :: (a -> Key -> b -> a) -> a -> IntMap b -> a
77+    go f z t
78+      = case t of
79+          Bin _ _ l r -> go f (go f z l) r
80+          Tip k x     -> f z k x
81+          Nil         -> z
82hunk ./Data/IntMap.hs 1469
83-    let (pos,neg) = span (\(k,_) -> k >=0) (foldr (\k x xs -> (k,x):xs) [] t) in neg ++ pos
84+    let (pos,neg) = span (\(k,_) -> k >=0) (foldrWithKey (\k x xs -> (k,x):xs) [] t) in neg ++ pos
85hunk ./Data/IntMap.hs 1905
86-
87hunk ./Data/IntSet.hs 86
88+            , foldr
89+            , foldl
90hunk ./Data/IntSet.hs 97
91+            , toDescList
92hunk ./Data/IntSet.hs 630
93
94+-- | /O(n)/. Fold over the elements of a set in an unspecified order.
95+fold :: (Int -> b -> b) -> b -> IntSet -> b
96+fold f z t = foldr f z t
97
98hunk ./Data/IntSet.hs 639
99-fold :: (Int -> b -> b) -> b -> IntSet -> b
100-fold f z t
101+foldr :: (Int -> b -> b) -> b -> IntSet -> b
102+foldr f z t
103hunk ./Data/IntSet.hs 642
104-      Bin 0 m l r | m < 0 -> foldr f (foldr f z l) r 
105+      Bin 0 m l r | m < 0 -> go f (go f z l) r 
106hunk ./Data/IntSet.hs 644
107-      Bin _ _ _ _ -> foldr f z t
108+      Bin _ _ _ _ -> go f z t
109hunk ./Data/IntSet.hs 647
110+  where
111+    go :: (Int -> b -> b) -> b -> IntSet -> b
112+    go f z t
113+      = case t of
114+          Bin _ _ l r -> go f (go f z r) l
115+          Tip x       -> f x z
116+          Nil         -> z
117hunk ./Data/IntSet.hs 655
118-foldr :: (Int -> b -> b) -> b -> IntSet -> b
119-foldr f z t
120+
121+-- | /O(n)/. Pre-order fold.
122+foldl :: (b -> Int -> b) -> b -> IntSet -> b
123+foldl f z t
124hunk ./Data/IntSet.hs 660
125-      Bin _ _ l r -> foldr f (foldr f z r) l
126-      Tip x       -> f x z
127+      Bin 0 m l r | m < 0 -> go f (go f z r) l 
128+      -- put negative numbers before.
129+      Bin _ _ _ _ -> go f z t
130+      Tip x       -> f z x
131hunk ./Data/IntSet.hs 665
132+  where
133+    go :: (b -> Int -> b) -> b -> IntSet -> b
134+    go f z t
135+      = case t of
136+          Bin _ _ l r -> go f (go f z l) r
137+          Tip x       -> f z x
138+          Nil         -> z
139hunk ./Data/IntSet.hs 693
140+-- | /O(n)/. Convert the set to a descending list of elements.
141+toDescList :: IntSet -> [Int]
142+toDescList t
143+  = foldl (flip (:)) [] t
144+
145hunk ./Data/Map.hs 105
146+            , foldr
147+            , foldl
148hunk ./Data/Map.hs 178
149-import Prelude hiding (lookup,map,filter,null)
150+import Prelude hiding (lookup,map,filter,null,foldl,foldr)
151hunk ./Data/Map.hs 185
152+import qualified Data.Foldable
153hunk ./Data/Map.hs 1451
154+-- | /O(n)/. Fold the values in the map, such that
155+-- @'foldr' f z == 'Prelude.foldr' f z . 'elems'@.
156+
157+foldr :: (a -> b -> b) -> b -> Map k a -> b
158+foldr f z t
159+  = foldrWithKey (const f) z t
160+
161+-- | /O(n)/. Fold the values in the map, such that
162+-- @'foldl' f z == 'Prelude.foldl' f z . 'elems'@.
163+
164+foldl :: (a -> b -> a) -> a -> Map k b -> a
165+foldl f z t
166+  = foldlWithKey (\z _ x -> f z x) z t
167+
168+
169+
170hunk ./Data/Map.hs 1996
171+  foldr = Data.Map.foldr
172+  foldl = Data.Map.foldl
173hunk ./Data/Set.hs 74
174+            , foldr
175+            , foldl
176hunk ./Data/Set.hs 105
177-import Prelude hiding (filter,foldr,null,map)
178+import Prelude hiding (filter,foldr,foldl,null,map)
179hunk ./Data/Set.hs 109
180+import qualified Data.Foldable
181hunk ./Data/Set.hs 154
182+    foldr = Data.Set.foldr
183+    foldl = Data.Set.foldl
184hunk ./Data/Set.hs 447
185+-- | /O(n)/. Pre-order fold.
186+foldl :: (a -> b -> a) -> a -> Set b -> a
187+foldl _ z Tip           = z
188+foldl f z (Bin _ x l r) = foldl f (f (foldl f z l) x) r
189+
190+
191+
192hunk ./Data/Set.hs 475
193+-- | /O(n)/. Convert the set to a descending list of elements.
194+toDescList :: Set a -> [a]
195+toDescList t   
196+  = foldl (flip (:)) [] t
197}
198
199Context:
200
201[export Data.Map.toDescList, foldlWithKey, and foldrWithKey (trac ticket 2580)
202qdunkan@gmail.com**20080922213200
203 
204 toDescList was previously implemented, but not exported.
205 
206 foldlWithKey was previously implemented, but not exported.  It can be used to
207 implement toDescList.
208 
209 foldrWithKey is already exported as foldWithKey, but foldrWithKey is explicitly
210 the mirror of foldlWithKey, and foldWithKey kept for compatibility.
211]
212[Bump version number to 0.2.0.0
213Ian Lynagh <igloo@earth.li>**20080920160016]
214[TAG 6.10 branch has been forked
215Ian Lynagh <igloo@earth.li>**20080919123438]
216Patch bundle hash:
2179b72f6db3b85b9ff0fb48beef4c5016eae3ab887