From 08d4c25e140431877ce473622f3705e7a05485bc Mon Sep 17 00:00:00 2001 From: "jeanphilippe.bernardy@gmail.com" Date: Sat, 10 Feb 2007 06:51:15 +0000 Subject: [PATCH] Fix the types of minView/maxView (ticket #1134) --- Data/Map.hs | 28 +++++++++++++++++++++------- Data/Set.hs | 11 ++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Data/Map.hs b/Data/Map.hs index 1d812fc..0710a28 100644 --- a/Data/Map.hs +++ b/Data/Map.hs @@ -594,19 +594,33 @@ updateMaxWithKey f t 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 +-- | /O(log n)/. Retrieves the minimal (key,value) pair 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)) +minViewWithKey :: Monad m => Map k a -> m ((k,a), Map k a) +minViewWithKey Tip = fail "Map.minView: empty map" +minViewWithKey x = return (deleteFindMin x) + +-- | /O(log n)/. Retrieves the maximal (key,value) pair of the map, and the map stripped from that element +-- @fail@s (in the monad) when passed an empty map. +maxViewWithKey :: Monad m => Map k a -> m ((k,a), Map k a) +maxViewWithKey Tip = fail "Map.maxView: empty map" +maxViewWithKey x = return (deleteFindMax x) + +-- | /O(log n)/. Retrieves the minimal key\'s value 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 (a, Map k a) minView Tip = fail "Map.minView: empty map" -minView x = return (swap $ deleteFindMin x) +minView x = return (first snd $ deleteFindMin x) --- | /O(log n)/. Retrieves the maximal key of the map, and the map stripped from that element +-- | /O(log n)/. Retrieves the maximal key\'s value 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 :: Monad m => Map k a -> m (a, Map k a) maxView Tip = fail "Map.maxView: empty map" -maxView x = return (swap $ deleteFindMax x) +maxView x = return (first snd $ deleteFindMax x) -swap (a,b) = (b,a) +-- Update the 1st component of a tuple (special case of Control.Arrow.first) +first :: (a -> b) -> (a,c) -> (b,c) +first f (x,y) = (f x, y) {-------------------------------------------------------------------- Union. diff --git a/Data/Set.hs b/Data/Set.hs index 5f45ce9..9ec9e4c 100644 --- a/Data/Set.hs +++ b/Data/Set.hs @@ -746,18 +746,15 @@ deleteFindMax t -- | /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 :: Monad m => Set a -> m (a, Set a) minView Tip = fail "Set.minView: empty set" -minView x = return (swap $ deleteFindMin x) +minView x = return (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 :: Monad m => Set a -> m (a, Set a) maxView Tip = fail "Set.maxView: empty set" -maxView x = return (swap $ deleteFindMax x) - -swap (a,b) = (b,a) - +maxView x = return (deleteFindMax x) {-------------------------------------------------------------------- -- 1.7.10.4