@@ -16,6 +16,7 @@ module Data.Map
1616 , fromList
1717 , fromListWith
1818 , delete
19+ , pop
1920 , member
2021 , alter
2122 , update
@@ -34,7 +35,7 @@ import Data.List (List(..), length, nub)
3435import Data.Maybe (Maybe (..), maybe , isJust )
3536import Data.Monoid (class Monoid )
3637import Data.Traversable (traverse , class Traversable )
37- import Data.Tuple (Tuple (..), uncurry )
38+ import Data.Tuple (Tuple (..), uncurry , snd )
3839
3940import Partial.Unsafe (unsafePartial )
4041
@@ -195,21 +196,26 @@ insert = down Nil
195196 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))
196197 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))
197198
198- -- | Delete a key and its corresponding value from a map
199+ -- | Delete a key and its corresponding value from a map.
199200delete :: 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
201207 where
202208 comp :: k -> k -> Ordering
203209 comp = compare
204210
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 ))
206212 down = unsafePartial \ctx k m -> case m of
207- Leaf -> fromZipper ctx Leaf
213+ Leaf -> Nothing
208214 Two left k1 v1 right ->
209215 case right, comp k k1 of
210- Leaf , EQ -> up ctx Leaf
216+ Leaf , EQ -> Just ( Tuple v1 ( up ctx Leaf ))
211217 _ , 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))
213219 _ , LT -> down (Cons (TwoLeft k1 v1 right) ctx) k left
214220 _ , _ -> down (Cons (TwoRight left k1 v1) ctx) k right
215221 Three left k1 v1 mid k2 v2 right ->
@@ -218,12 +224,12 @@ delete = down Nil
218224 Leaf , Leaf , Leaf -> true
219225 _ , _ , _ -> false
220226 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 )) )
223229 _ , 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))
225231 _ , _ , 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))
227233 _ , LT , _ -> down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) k left
228234 _ , GT , LT -> down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) k mid
229235 _ , _ , _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k right
0 commit comments