Unique 0.4 → 0.4.1
raw patch · 4 files changed
+93/−78 lines, 4 filesdep +extraPVP ok
version bump matches the API change (PVP)
Dependencies added: extra
API changes (from Hackage documentation)
Files
- Data/List/Unique.hs +45/−31
- Data/List/UniqueStrict.hs +16/−16
- Data/List/UniqueUnsorted.hs +11/−11
- Unique.cabal +21/−20
Data/List/Unique.hs view
@@ -3,79 +3,93 @@ -- Module : Data.List.Unique -- Copyright : (c) Volodymyr Yaschenko -- License : BSD3--- +-- -- Maintainer : ualinuxcn@gmail.com -- Stability : Unstable -- Portability : portable -- -- Library provides the functions to find unique and duplicate elements in the list -module Data.List.Unique - (sortUniq - , repeated- , repeatedBy- , unique- , count- , count_- , countElem- ) where +module Data.List.Unique+ (+ sortUniq+ , repeated+ , repeatedBy+ , unique+ , count+ , count_+ , countElem+ )+ where -import Data.List (sort,group,nub,filter,map,length)-import Prelude hiding (map,filter,length) +import Data.List+ (+ sort+ , sortBy+ , group+ , nub+ ) --- | 'sortUniq' sorts the list and removes the duplicates of elements. Example: +import Data.List.Extra (nubOrd)+import Data.Function (on)+import Control.Applicative (liftA2)++-- | 'sortUniq' sorts the list and removes the duplicates of elements. Example: -- -- > sortUniq "foo bar" == " abfor" sortUniq :: Ord a => [a] -> [a]-sortUniq = sort . nub+sortUniq = sort . nubOrd +sg :: Ord a => [a] -> [[a]]+sg = group . sort+ filterByLength :: Ord a => (Int -> Bool) -> [a] -> [[a]]-filterByLength p = filter (p . length) . group . sort+filterByLength p = filter (p . length) . sg --- | 'repeated' finds only the elements that are present more than once in the list. Example: +-- | 'repeated' finds only the elements that are present more than once in the list. Example: ----- > repeated "foo bar" == "o" +-- > repeated "foo bar" == "o" repeated :: Ord a => [a] -> [a] repeated = repeatedBy (>1) -- | The repeatedBy function behaves just like repeated, except it uses a user-supplied equality predicate.--- +-- -- > repeatedBy (>2) "This is the test line" == " eist" repeatedBy :: Ord a => (Int -> Bool) -> [a] -> [a] repeatedBy p = map head . filterByLength p --- | 'unique' gets only unique elements, that do not have duplicates. --- It sorts them. Example: +-- | 'unique' gets only unique elements, that do not have duplicates.+-- It sorts them. Example: -- -- > unique "foo bar" == " abfr"- + unique :: Ord a => [a] -> [a] unique = concat . filterByLength (==1) --- | 'count' of each element in the list, it sorts by keys (elements). Example: +lh :: [a] -> (a, Int)+lh = liftA2 (,) head length++-- | 'count' of each element in the list, it sorts by keys (elements). Example: -- -- > count "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('o',2),('r',1)] count :: Ord a => [a] -> [(a, Int)]-count = map (\x -> (head x,length x)) . group . sort+count = map lh . sg --- | 'count_' of each elements in the list, it sorts by their number. Example: +-- | 'count_' of each elements in the list, it sorts by their number. Example: -- -- > count_ "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('r',1),('o',2)] count_ :: Ord a => [a] -> [(a, Int)]-count_ = map (\(x,y) -> (y,x)) . sort . map (\x -> (length x,head x)) . group . sort +count_ = sortBy (compare `on` snd) . map lh . sg --- | 'countElem' gets the number of occurrences of the specified element. Example: +-- | 'countElem' gets the number of occurrences of the specified element. Example: -- -- > countElem 'o' "foo bar" == 2 -countElem :: Eq a => a -> [a] -> Int -countElem x [] = 0-countElem x (y:ys) = case x == y of - False -> countElem x ys- True -> 1 + countElem x ys+countElem :: Eq a => a -> [a] -> Int+countElem x = length . filter (== x)
Data/List/UniqueStrict.hs view
@@ -3,63 +3,63 @@ -- Module : Data.List.UniqueStrict -- Copyright : (c) Volodymyr Yaschenko -- License : BSD3--- +-- -- Maintainer : ualinuxcn@gmail.com -- Stability : Unstable -- Portability : portable -- -- Library provides functions to find unique and duplicate elements in the list. -- Unlike Data.List.Unique this one uses Data.Map.Strict for calculations.--- So it's much faster and it uses less memory. +-- So it's much faster and it uses less memory. -module Data.List.UniqueStrict +module Data.List.UniqueStrict ( repeated , repeatedBy , unique , count- , count_ ) where + , count_ ) where -import qualified Data.Map.Strict as MS (Map (..), fromListWith,toList,filter,keys) +import qualified Data.Map.Strict as MS (Map (..), fromListWith,toList,filter,keys) import qualified Data.IntMap.Strict as IM (IntMap (..), fromListWith,toList)-import qualified Data.List as L (sort) +import qualified Data.List as L (sort) countMap :: Ord a => [a] -> MS.Map a Int countMap = MS.fromListWith (+) . flip zip (repeat 1) -- | The 'repeatedBy' function behaves just like repeated, except it uses a user-supplied equality predicate.--- +-- -- > repeatedBy (>2) "This is the test line" == " eist" repeatedBy :: Ord a => (Int -> Bool) -> [a] -> [a] repeatedBy p = MS.keys . MS.filter p . countMap --- | 'repeated' finds only the elements that are present more than once in the list. Example: +-- | 'repeated' finds only the elements that are present more than once in the list. Example: ----- > repeated "foo bar" == "o" +-- > repeated "foo bar" == "o" repeated :: Ord a => [a] -> [a] repeated = repeatedBy (>1) --- | 'unique' gets only unique elements, that do not have duplicates. --- It sorts them. Example: +-- | 'unique' gets only unique elements, that do not have duplicates.+-- It sorts them. Example: -- -- > unique "foo bar" == " abfr" unique :: Ord a => [a] -> [a] unique = repeatedBy (==1) --- | 'count' of each element in the list, it sorts by keys (elements). Example: +-- | 'count' of each element in the list, it sorts by keys (elements). Example: -- -- > count "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('o',2),('r',1)] count :: Ord a => [a] -> [(a, Int)]-count = MS.toList . countMap +count = MS.toList . countMap --- | 'count_' of each elements in the list, it sorts by their number. Example: +-- | 'count_' of each elements in the list, it sorts by their number. Example: -- -- > count_ "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('r',1),('o',2)] count_ :: Ord a => [a] -> [(a, Int)]-count_ = fromIntMap . toIntMap . MS.toList . countMap +count_ = fromIntMap . toIntMap . MS.toList . countMap where toIntMap = IM.fromListWith (++) . map (\(x,y) -> (y,[x]))- fromIntMap = concatMap (\(x,y) -> L.sort . zip y $ repeat x) . IM.toList + fromIntMap = concatMap (\(x,y) -> L.sort . zip y $ repeat x) . IM.toList
Data/List/UniqueUnsorted.hs view
@@ -3,7 +3,7 @@ -- Module : Data.List.UniqueUnsorted -- Copyright : (c) Volodymyr Yaschenko -- License : BSD3--- +-- -- Maintainer : ualinuxcn@gmail.com -- Stability : Unstable -- Portability : portable@@ -11,15 +11,15 @@ -- Library provides functions to find unique and duplicate elements in the list. -- Unlike Unique or UniqueStrict modules this one uses Data.HashMap.Strict for calculation. ----- The elements in the list can be unsorted (do not have an instance of Ord class, but Hashable is needed). +-- The elements in the list can be unsorted (do not have an instance of Ord class, but Hashable is needed). -- This implementation is good for ByteStrings. -module Data.List.UniqueUnsorted +module Data.List.UniqueUnsorted ( repeated , repeatedBy , unique , count- , count_ ) where + , count_ ) where import Data.Hashable import qualified Data.HashMap.Strict as HS (HashMap (..),fromListWith,toList,filter,keys)@@ -29,15 +29,15 @@ countMap = HS.fromListWith (+) . flip zip (repeat 1) -- | The 'repeatedBy' function behaves just like 'repeated', except it uses a user-supplied equality predicate.--- +-- -- > repeatedBy (>2) "This is the test line" == " stei" repeatedBy :: (Hashable a, Eq a) => (Int -> Bool) -> [a] -> [a] repeatedBy p = HS.keys . HS.filter p . countMap --- | 'repeated' finds only the elements that are present more than once in the list. Example: +-- | 'repeated' finds only the elements that are present more than once in the list. Example: ----- > repeated "foo bar" == "o" +-- > repeated "foo bar" == "o" repeated :: (Hashable a, Eq a) => [a] -> [a] repeated = repeatedBy (>1)@@ -49,18 +49,18 @@ unique :: (Hashable a, Eq a) => [a] -> [a] unique = repeatedBy (==1) --- | 'count' of each element in the list. Example: +-- | 'count' of each element in the list. Example: -- -- > count "This is the test line" == [(' ',4),('s',3),('T',1),('t',3),('e',3),('h',2),('i',3),('l',1),('n',1)] count :: (Hashable a, Eq a) => [a] -> [(a, Int)] count = HS.toList . countMap --- | 'count_' of each elements in the list, it sorts by their number. Example: +-- | 'count_' of each elements in the list, it sorts by their number. Example: -- -- > count_ "This is the test line" == [('n',1),('l',1),('T',1),('h',2),('i',3),('e',3),('t',3),('s',3),(' ',4)] count_ :: (Hashable a, Eq a) => [a] -> [(a, Int)]-count_ = fromIntMap . toIntMap . HS.toList . countMap +count_ = fromIntMap . toIntMap . HS.toList . countMap where toIntMap = IM.fromListWith (++) . map (\(x,y) -> (y,[x]))- fromIntMap = concatMap (\(x,y) -> zip y $ repeat x) . IM.toList + fromIntMap = concatMap (\(x,y) -> zip y $ repeat x) . IM.toList
Unique.cabal view
@@ -1,16 +1,16 @@--- Initial Unique.cabal generated by cabal init. For further +-- Initial Unique.cabal generated by cabal init. For further -- documentation, see http://haskell.org/cabal/users-guide/ -- The name of the package. name: Unique --- The package version. See the Haskell package versioning policy (PVP) +-- The package version. See the Haskell package versioning policy (PVP) -- for standards guiding when and how versions should be incremented. -- http://www.haskell.org/haskellwiki/Package_versioning_policy -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.4+version: 0.4.1 -- A short (one-line) description of the package. synopsis: It provides the functionality like unix "uniq" utility@@ -27,7 +27,7 @@ -- The package author(s). author: Volodymyr Yaschenko --- An email address to which users can send suggestions, bug reports, and +-- An email address to which users can send suggestions, bug reports, and -- patches. maintainer: ualinuxcn@gmail.com @@ -35,9 +35,9 @@ build-type: Simple --- Extra files to be distributed with the package, such as examples or a +-- Extra files to be distributed with the package, such as examples or a -- README.--- extra-source-files: +-- extra-source-files: -- Constraint on the version of Cabal needed to build this package. cabal-version: >=1.10@@ -48,25 +48,26 @@ library -- Modules exported by the library.- exposed-modules: Data.List.Unique, + exposed-modules: Data.List.Unique, Data.List.UniqueStrict, Data.List.UniqueUnsorted- + -- Modules included in this library but not exported.- -- other-modules: - + -- other-modules:+ -- LANGUAGE extensions used by modules in this package.- -- other-extensions: - + -- other-extensions:+ -- Other library packages from which modules are imported.- build-depends: base >=4.0 && <= 4.8, - containers ,- hashable ,- unordered-containers- + build-depends: base >=4.0 && <= 4.8+ , extra+ , containers+ , hashable+ , unordered-containers+ -- Directories containing source files.- -- hs-source-dirs: - + -- hs-source-dirs:+ -- Base language which the package is written in. default-language: Haskell2010- +