Ticket #2717: nub.patch

File nub.patch, 4.2 KB (added by Bart Massey, 5 years ago)
  • Data/List.hs

    old new  
    157157   -- ** \"Set\" operations 
    158158 
    159159   , nub               -- :: (Eq a) => [a] -> [a] 
     160   , nubWith           -- :: (a -> b -> Maybe b) -> b -> [a] -> [a] 
    160161 
    161162   , delete            -- :: (Eq a) => a -> [a] -> [a] 
    162163   , (\\)              -- :: (Eq a) => [a] -> [a] -> [a] 
     
    302303isInfixOf               :: (Eq a) => [a] -> [a] -> Bool 
    303304isInfixOf needle haystack = any (isPrefixOf needle) (tails haystack) 
    304305 
    305 -- | The 'nub' function removes duplicate elements from a list. 
    306 -- In particular, it keeps only the first occurrence of each element. 
    307 -- (The name 'nub' means \`essence\'.) 
    308 -- It is a special case of 'nubBy', which allows the programmer to supply 
    309 -- their own equality test. 
     306-- | The 'nub' function removes duplicate elements from a 
     307-- list.  In particular, it keeps only the first occurrence 
     308-- of each element.  (The name \"nub\" means \"essence\".) 
     309--  
     310-- The 'nub' function is a special case of 'nubBy', which 
     311-- allows the programmer to supply their own equality test. 
     312-- It is also a special case of 'nubWith', which allows the 
     313-- programmer to supply their own filtering function. 
     314-- 
     315-- The 'nub' function is /O(mn)/ on lists of length /n/ 
     316-- containing /m/ duplicate elements.  'nubOrd' 
     317-- is an /O(n log m)/ nub function for 'Ord' data. 
    310318nub                     :: (Eq a) => [a] -> [a] 
    311319#ifdef USE_REPORT_PRELUDE 
    312320nub                     =  nubBy (==) 
     
    320328        | otherwise     = x : nub' xs (x:ls)    -- ' 
    321329#endif 
    322330 
     331-- | The 'nubWith' function accepts as an argument a \"stop 
     332-- list\" update function and an initial stop list. The stop 
     333-- list is a set of list elements that 'nubWith' uses as a 
     334-- filter to remove duplicate elements.  The stop list is 
     335-- normally initially empty. 
     336--  
     337-- The stop list update function is given a list element a 
     338-- and the current stop list b, and returns 'Nothing' if the 
     339-- element is already in the stop list, else 'Just' b', where 
     340-- b' is a new stop list updated to contain a. 
     341nubWith :: (a -> b -> Maybe b) -> b -> [a] -> [a] 
     342nubWith _ _ [] = [] 
     343nubWith f s (e:es) = 
     344    case f e s of 
     345       Just s' -> e : nubWith f s' es 
     346       Nothing -> nubWith f s es 
     347 
    323348-- | The 'nubBy' function behaves just like 'nub', except it uses a 
    324349-- user-supplied equality predicate instead of the overloaded '==' 
    325350-- function. 
  • Data/Set.hs

    old new  
    9393            , toAscList 
    9494            , fromAscList 
    9595            , fromDistinctAscList 
    96                          
     96 
     97            -- * Misc 
     98            , nubOrd 
     99 
    97100            -- * Debugging 
    98101            , showTree 
    99102            , showTreeWith 
     
    981984withEmpty bars = "   ":bars 
    982985 
    983986{-------------------------------------------------------------------- 
     987  Misc 
     988--------------------------------------------------------------------} 
     989 
     990-- | The 'nubOrd' function is /O(n log m)/ on 'Ord' data, 
     991-- where /n/ is the length of the list and /m/ is the number 
     992-- of duplicate elements.  If your target type only supports 
     993-- 'Eq', use 'nub'.  The 'nubOrd' function is a 
     994-- special case of 'nubWith' with a 'Set' stop 
     995-- list. 
     996nubOrd :: Ord e => [e] -> [e] 
     997nubOrd = List.nubWith f empty where 
     998    f e s = if member e s then Nothing else Just (insert e s) 
     999 
     1000{-------------------------------------------------------------------- 
    9841001  Assertions 
    9851002--------------------------------------------------------------------} 
    9861003-- | /O(n)/. Test if the internal set structure is valid. 
     
    11761193prop_List :: [Int] -> Bool 
    11771194prop_List xs 
    11781195  = (sort (nub xs) == toList (fromList xs)) 
     1196 
     1197{-------------------------------------------------------------------- 
     1198  Misc 
     1199--------------------------------------------------------------------} 
     1200 
     1201nubl :: [a] -> Gen [a] 
     1202nubl l = do 
     1203  l' <- mapM rep l 
     1204  return (concat l') 
     1205    where 
     1206      rep e = do 
     1207        k <- choose (0, 10) 
     1208        return (replicate k e) 
     1209 
     1210prop_NubOrd :: [Int] -> Property 
     1211prop_NubOrd l = forAll (nubl l) $ \l0 -> nub l0 == nubOrd l0 
     1212 
    11791213-}