Ticket #2717: nub.patch
| File nub.patch, 4.2 KB (added by Bart Massey, 5 years ago) |
|---|
-
Data/List.hs
old new 157 157 -- ** \"Set\" operations 158 158 159 159 , nub -- :: (Eq a) => [a] -> [a] 160 , nubWith -- :: (a -> b -> Maybe b) -> b -> [a] -> [a] 160 161 161 162 , delete -- :: (Eq a) => a -> [a] -> [a] 162 163 , (\\) -- :: (Eq a) => [a] -> [a] -> [a] … … 302 303 isInfixOf :: (Eq a) => [a] -> [a] -> Bool 303 304 isInfixOf needle haystack = any (isPrefixOf needle) (tails haystack) 304 305 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. 310 318 nub :: (Eq a) => [a] -> [a] 311 319 #ifdef USE_REPORT_PRELUDE 312 320 nub = nubBy (==) … … 320 328 | otherwise = x : nub' xs (x:ls) -- ' 321 329 #endif 322 330 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. 341 nubWith :: (a -> b -> Maybe b) -> b -> [a] -> [a] 342 nubWith _ _ [] = [] 343 nubWith 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 323 348 -- | The 'nubBy' function behaves just like 'nub', except it uses a 324 349 -- user-supplied equality predicate instead of the overloaded '==' 325 350 -- function. -
Data/Set.hs
old new 93 93 , toAscList 94 94 , fromAscList 95 95 , fromDistinctAscList 96 96 97 -- * Misc 98 , nubOrd 99 97 100 -- * Debugging 98 101 , showTree 99 102 , showTreeWith … … 981 984 withEmpty bars = " ":bars 982 985 983 986 {-------------------------------------------------------------------- 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. 996 nubOrd :: Ord e => [e] -> [e] 997 nubOrd = List.nubWith f empty where 998 f e s = if member e s then Nothing else Just (insert e s) 999 1000 {-------------------------------------------------------------------- 984 1001 Assertions 985 1002 --------------------------------------------------------------------} 986 1003 -- | /O(n)/. Test if the internal set structure is valid. … … 1176 1193 prop_List :: [Int] -> Bool 1177 1194 prop_List xs 1178 1195 = (sort (nub xs) == toList (fromList xs)) 1196 1197 {-------------------------------------------------------------------- 1198 Misc 1199 --------------------------------------------------------------------} 1200 1201 nubl :: [a] -> Gen [a] 1202 nubl 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 1210 prop_NubOrd :: [Int] -> Property 1211 prop_NubOrd l = forAll (nubl l) $ \l0 -> nub l0 == nubOrd l0 1212 1179 1213 -}
