@@ -16,6 +16,7 @@ module Data.Map
16
16
, fromList
17
17
, fromListWith
18
18
, delete
19
+ , pop
19
20
, member
20
21
, alter
21
22
, update
@@ -34,7 +35,7 @@ import Data.List (List(..), length, nub)
34
35
import Data.Maybe (Maybe (..), maybe , isJust )
35
36
import Data.Monoid (class Monoid )
36
37
import Data.Traversable (traverse , class Traversable )
37
- import Data.Tuple (Tuple (..), uncurry )
38
+ import Data.Tuple (Tuple (..), uncurry , snd )
38
39
39
40
import Partial.Unsafe (unsafePartial )
40
41
@@ -195,21 +196,26 @@ insert = down Nil
195
196
ThreeMiddle a k1 v1 k2 v2 d, KickUp b k v c -> up ctx (KickUp (Two a k1 v1 b) k v (Two c k2 v2 d))
196
197
ThreeRight a k1 v1 b k2 v2, KickUp c k v d -> up ctx (KickUp (Two a k1 v1 b) k2 v2 (Two c k v d))
197
198
198
- -- | Delete a key and its corresponding value from a map
199
+ -- | Delete a key and its corresponding value from a map.
199
200
delete :: forall k v . Ord k => k -> Map k v -> Map k v
200
- delete = down Nil
201
+ delete k m = maybe m snd (pop k m)
202
+
203
+ -- | Delete a key and its corresponding value from a map, returning the value
204
+ -- | as well as the subsequent map.
205
+ pop :: forall k v . Ord k => k -> Map k v -> Maybe (Tuple v (Map k v ))
206
+ pop = down Nil
201
207
where
202
208
comp :: k -> k -> Ordering
203
209
comp = compare
204
210
205
- down :: List (TreeContext k v ) -> k -> Map k v -> Map k v
211
+ down :: List (TreeContext k v ) -> k -> Map k v -> Maybe ( Tuple v ( Map k v ))
206
212
down = unsafePartial \ctx k m -> case m of
207
- Leaf -> fromZipper ctx Leaf
213
+ Leaf -> Nothing
208
214
Two left k1 v1 right ->
209
215
case right, comp k k1 of
210
- Leaf , EQ -> up ctx Leaf
216
+ Leaf , EQ -> Just ( Tuple v1 ( up ctx Leaf ))
211
217
_ , EQ -> let max = maxNode left
212
- in removeMaxNode (Cons (TwoLeft max.key max.value right) ctx) left
218
+ in Just ( Tuple v1 ( removeMaxNode (Cons (TwoLeft max.key max.value right) ctx) left))
213
219
_ , LT -> down (Cons (TwoLeft k1 v1 right) ctx) k left
214
220
_ , _ -> down (Cons (TwoRight left k1 v1) ctx) k right
215
221
Three left k1 v1 mid k2 v2 right ->
@@ -218,12 +224,12 @@ delete = down Nil
218
224
Leaf , Leaf , Leaf -> true
219
225
_ , _ , _ -> false
220
226
in case leaves, comp k k1, comp k k2 of
221
- true , EQ , _ -> fromZipper ctx (Two Leaf k2 v2 Leaf )
222
- true , _ , EQ -> fromZipper ctx (Two Leaf k1 v1 Leaf )
227
+ true , EQ , _ -> Just ( Tuple v1 ( fromZipper ctx (Two Leaf k2 v2 Leaf )) )
228
+ true , _ , EQ -> Just ( Tuple v2 ( fromZipper ctx (Two Leaf k1 v1 Leaf )) )
223
229
_ , EQ , _ -> let max = maxNode left
224
- in removeMaxNode (Cons (ThreeLeft max.key max.value mid k2 v2 right) ctx) left
230
+ in Just ( Tuple v1 ( removeMaxNode (Cons (ThreeLeft max.key max.value mid k2 v2 right) ctx) left))
225
231
_ , _ , EQ -> let max = maxNode mid
226
- in removeMaxNode (Cons (ThreeMiddle left k1 v1 max.key max.value right) ctx) mid
232
+ in Just ( Tuple v2 ( removeMaxNode (Cons (ThreeMiddle left k1 v1 max.key max.value right) ctx) mid))
227
233
_ , LT , _ -> down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) k left
228
234
_ , GT , LT -> down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) k mid
229
235
_ , _ , _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k right
0 commit comments