, updateMax
, updateMinWithKey
, updateMaxWithKey
+ , minView
+ , maxView
-- * Debugging
, showTree
findMin :: Map k a -> (k,a)
findMin (Bin _ kx x Tip r) = (kx,x)
findMin (Bin _ kx x l r) = findMin l
-findMin Tip = error "Map.findMin: empty tree has no minimal element"
+findMin Tip = error "Map.findMin: empty map has no minimal element"
-- | /O(log n)/. The maximal key of the map.
findMax :: Map k a -> (k,a)
findMax (Bin _ kx x l Tip) = (kx,x)
findMax (Bin _ kx x l r) = findMax r
-findMax Tip = error "Map.findMax: empty tree has no maximal element"
+findMax Tip = error "Map.findMax: empty map has no maximal element"
-- | /O(log n)/. Delete the minimal key.
deleteMin :: Map k a -> Map k a
Bin sx kx x l r -> balance kx x l (updateMaxWithKey f r)
Tip -> Tip
+-- | /O(log n)/. Retrieves the minimal key of the map, and the map stripped from that element
+-- @fail@s (in the monad) when passed an empty map.
+minView :: Monad m => Map k a -> m (Map k a, (k,a))
+minView Tip = fail "Map.minView: empty map"
+minView x = return (swap $ deleteFindMin x)
+
+-- | /O(log n)/. Retrieves the maximal key of the map, and the map stripped from that element
+-- @fail@s (in the monad) when passed an empty map.
+maxView :: Monad m => Map k a -> m (Map k a, (k,a))
+maxView Tip = fail "Map.maxView: empty map"
+maxView x = return (swap $ deleteFindMax x)
+
+swap (a,b) = (b,a)
{--------------------------------------------------------------------
Union.
, deleteMax
, deleteFindMin
, deleteFindMax
+ , maxView
+ , minView
-- * Conversion
Bin _ x l r -> let (xm,r') = deleteFindMax r in (xm,balance x l r')
Tip -> (error "Set.deleteFindMax: can not return the maximal element of an empty set", Tip)
+-- | /O(log n)/. Retrieves the minimal key of the set, and the set stripped from that element
+-- @fail@s (in the monad) when passed an empty set.
+minView :: Monad m => Set a -> m (Set a, a)
+minView Tip = fail "Set.minView: empty set"
+minView x = return (swap $ deleteFindMin x)
+
+-- | /O(log n)/. Retrieves the maximal key of the set, and the set stripped from that element
+-- @fail@s (in the monad) when passed an empty set.
+maxView :: Monad m => Set a -> m (Set a, a)
+maxView Tip = fail "Set.maxView: empty set"
+maxView x = return (swap $ deleteFindMax x)
+
+swap (a,b) = (b,a)
+
+
{--------------------------------------------------------------------
[balance x l r] balances two trees with value x.