@@ -18,6 +18,8 @@ module Data.Map
1818  , findMax 
1919  , deleteMin 
2020  , deleteMax 
21+   , minView 
22+   , maxView 
2123  , foldSubmap 
2224  , submap 
2325  , fromFoldable 
@@ -299,27 +301,75 @@ findMin = go Nothing
299301--  |
300302--  | Return an empty map if the map is empty.
301303deleteMin  ::  forall  k  v . Ord  k  =>  Map  k  v  ->  Map  k  v 
302- deleteMin Leaf  = Leaf 
303- deleteMin n = down Nil  n
304+ deleteMin = maybe Leaf  _.strippedMap <<< minView
305+ 
306+ --  | Delete the pair with the greatest key. O(logn).
307+ --  |
308+ --  | Return an empty map if the map is empty.
309+ deleteMax  ::  forall  k  v . Ord  k  =>  Map  k  v  ->  Map  k  v 
310+ deleteMax = maybe Leaf  _.strippedMap <<< maxView
311+ 
312+ --  | Retrieves the least key and the value corresponding to that key,
313+ --  | and the map stripped of that element. O(logn)
314+ --  |
315+ --  | Returns Nothing if the map is empty.
316+ minView
317+   ::  forall  k  v 
318+     . Ord  k 
319+    =>  Map  k  v 
320+    ->  Maybe  {  key  ::  k , value  ::  v , strippedMap  ::  Map  k  v } 
321+ minView Leaf  = Nothing 
322+ minView n = Just  $ down Nil  n
304323  where 
305-   down  ::  List  (TreeContext  k  v ) ->  Map  k  v  ->  Map  k  v 
324+   down
325+     ::  List  (TreeContext  k  v )
326+     ->  Map  k  v 
327+     ->  {  key  ::  k , value  ::  v , strippedMap  ::  Map  k  v } 
306328  down = unsafePartial \ctx ->  case  _ of 
307329    Two  left k v right -> 
308330      case  left, right of 
309-            Leaf , Leaf  ->  deleteUp ctx Leaf 
331+            Leaf , Leaf  ->  { key: k, value: v, strippedMap:  deleteUp ctx Leaf  } 
310332           _   , _    ->  down (Cons  (TwoLeft  k v right) ctx) left
311333    Three  left k1 v1 mid k2 v2 right -> 
312334      case  left, mid, right of 
313-            Leaf , Leaf , Leaf  ->  fromZipper ctx (Two  Leaf  k2 v2 Leaf )
335+            Leaf , Leaf , Leaf  -> 
336+              { key: k1
337+              , value: v1
338+              , strippedMap: fromZipper ctx (Two  Leaf  k2 v2 Leaf )
339+              }
314340           _   , _   , _    -> 
315341              down (Cons  (ThreeLeft  k1 v1 mid k2 v2 right) ctx) left
316342
317- --  | Delete the pair with the greatest key. O(logn).
343+ --  | Retrieves the greatest key and the value corresponding to that key,
344+ --  | and the map stripped of that element. O(logn)
318345--  |
319- --  | Return an empty map if the map is empty.
320- deleteMax  ::  forall  k  v . Ord  k  =>  Map  k  v  ->  Map  k  v 
321- deleteMax Leaf  = Leaf 
322- deleteMax n = removeMaxNode Nil  n
346+ --  | Returns Nothing if the map is empty.
347+ maxView
348+   ::  forall  k  v 
349+     . Ord  k 
350+    =>  Map  k  v 
351+    ->  Maybe  {  key  ::  k , value  ::  v , strippedMap  ::  Map  k  v } 
352+ maxView Leaf  = Nothing 
353+ maxView n = Just  $ down Nil  n
354+   where 
355+   down
356+     ::  List  (TreeContext  k  v )
357+     ->  Map  k  v 
358+     ->  {  key  ::  k , value  ::  v , strippedMap  ::  Map  k  v } 
359+   down = unsafePartial \ctx ->  case  _ of 
360+     Two  left k v right -> 
361+       case  left, right of 
362+            Leaf , Leaf  ->  { key: k, value: v, strippedMap: deleteUp ctx Leaf  }
363+            _   , _    ->  down (Cons  (TwoRight  left k v) ctx) right
364+     Three  left k1 v1 mid k2 v2 right -> 
365+       case  left, mid, right of 
366+            Leaf , Leaf , Leaf  -> 
367+              { key: k2
368+              , value: v2
369+              , strippedMap: fromZipper ctx (Two  Leaf  k1 v1 Leaf )
370+              }
371+            _   , _   , _    -> 
372+               down (Cons  (ThreeRight  left k1 v1 mid k2 v2) ctx) right
323373
324374--  | Fold over the entries of a given map where the key is between a lower and
325375--  | an upper bound. Passing `Nothing` as either the lower or upper bound
@@ -526,13 +576,13 @@ pop k = down Nil
526576    Three  _ _ _ _ k' v Leaf  ->  { key: k', value: v }
527577    Three  _ _ _ _ _ _ right ->  maxNode right
528578
529- 
530- removeMaxNode   ::   forall   k   v .  Ord   k   =>   List  ( TreeContext   k   v )  ->   Map   k   v   ->   Map   k   v 
531- removeMaxNode = unsafePartial \ctx  ->   case  _  of 
532-   Two  Leaf  _ _ Leaf  ->  deleteUp ctx Leaf 
533-   Two  left k' v right ->  removeMaxNode (Cons  (TwoRight  left k' v) ctx) right
534-   Three  Leaf  k1 v1 Leaf  _ _ Leaf  ->  deleteUp (Cons  (TwoRight  Leaf  k1 v1) ctx) Leaf 
535-   Three  left k1 v1 mid k2 v2 right ->  removeMaxNode (Cons  (ThreeRight  left k1 v1 mid k2 v2) ctx) right
579+    removeMaxNode   ::   List  ( TreeContext   k   v )  ->   Map   k   v   ->   Map   k   v 
580+   removeMaxNode = unsafePartial \ctx m  -> 
581+      case  m  of 
582+        Two  Leaf  _ _ Leaf  ->  deleteUp ctx Leaf 
583+        Two  left k' v right ->  removeMaxNode (Cons  (TwoRight  left k' v) ctx) right
584+        Three  Leaf  k1 v1 Leaf  _ _ Leaf  ->  deleteUp (Cons  (TwoRight  Leaf  k1 v1) ctx) Leaf 
585+        Three  left k1 v1 mid k2 v2 right ->  removeMaxNode (Cons  (ThreeRight  left k1 v1 mid k2 v2) ctx) right
536586
537587deleteUp  ::  forall  k  v . Ord  k  =>  List  (TreeContext  k  v ) ->  Map  k  v  ->  Map  k  v 
538588deleteUp = unsafePartial \ctxs tree -> 
0 commit comments