packages feed

Unique 0.3 → 0.4

raw patch · 4 files changed

+84/−16 lines, 4 filesdep +hashabledep +unordered-containersdep ~containers

Dependencies added: hashable, unordered-containers

Dependency ranges changed: containers

Files

Data/List/Unique.hs view
@@ -26,7 +26,7 @@  -- | 'sortUniq' sorts the list and removes the duplicates of elements. Example:  ----- sortUniq "foo bar" == " abfor"+-- > sortUniq "foo bar" == " abfor"  sortUniq :: Ord a => [a] -> [a] sortUniq = sort . nub@@ -36,14 +36,14 @@  -- | '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 (>2) "This is the test line" == " eist"  repeatedBy :: Ord a => (Int -> Bool) -> [a] -> [a] repeatedBy p = map head . filterByLength p@@ -51,28 +51,28 @@ -- | 'unique' gets only unique elements, that do not have duplicates.   -- It sorts them. Example:  -----  unique  "foo bar" == " abfr"+-- > 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:  -----  count "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('o',2),('r',1)]+-- > 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_' 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_ "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    -- | 'countElem' gets the number of occurrences of the specified element. Example:   -----  countElem 'o' "foo bar" == 2+-- > countElem 'o' "foo bar" == 2  countElem :: Eq a => a -> [a] -> Int  countElem x [] = 0
Data/List/UniqueStrict.hs view
@@ -28,14 +28,14 @@  -- | The 'repeatedBy' function behaves just like repeated, except it uses a user-supplied equality predicate. -- --- repeatedBy (>2) "This is the test line" == " eist"+-- > 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  "foo bar" == "o" +-- > repeated  "foo bar" == "o"   repeated :: Ord a => [a] -> [a] repeated = repeatedBy (>1)@@ -43,21 +43,21 @@ -- | 'unique' gets only unique elements, that do not have duplicates.   -- It sorts them. Example:  -----  unique  "foo bar" == " abfr"+-- > 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 "foo bar" == [(' ',1),('a',1),('b',1),('f',1),('o',2),('r',1)]+-- > 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_' 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_ "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 
+ Data/List/UniqueUnsorted.hs view
@@ -0,0 +1,66 @@+-----------------------------------------------------------------------------+-- |+-- Module      :  Data.List.UniqueUnsorted+-- 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 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). +-- This implementation is good for ByteStrings.++module  Data.List.UniqueUnsorted +                                ( repeated+                                , repeatedBy+                                , unique+                                , count+                                , count_ ) where ++import Data.Hashable+import qualified Data.HashMap.Strict as HS (HashMap (..),fromListWith,toList,filter,keys)+import qualified Data.IntMap.Strict as IM (IntMap (..), fromListWith,toList)++countMap :: (Hashable a, Eq a) => [a] -> HS.HashMap a Int+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  "foo bar" == "o" ++repeated :: (Hashable a, Eq a) => [a] -> [a]+repeated = repeatedBy (>1)++-- | 'unique' gets only unique elements, that do not have duplicates.+--+-- > unique  "foo bar" == " abrf"++unique :: (Hashable a, Eq a) => [a] -> [a]+unique = repeatedBy (==1)++-- | '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_ "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 +    where toIntMap = IM.fromListWith (++) . map (\(x,y) -> (y,[x]))+          fromIntMap = concatMap (\(x,y) -> zip y $ repeat x) . IM.toList  
Unique.cabal view
@@ -10,8 +10,7 @@ -- PVP summary:      +-+------- breaking API changes --                   | | +----- non-breaking API additions --                   | | | +--- code changes with no API change---version:             0.2.0.5-version:             0.3+version:             0.4  -- A short (one-line) description of the package. synopsis:           It provides the functionality like unix "uniq" utility@@ -50,7 +49,8 @@ library   -- Modules exported by the library.   exposed-modules:      Data.List.Unique, -                        Data.List.UniqueStrict+                        Data.List.UniqueStrict,+                        Data.List.UniqueUnsorted      -- Modules included in this library but not exported.   -- other-modules:      @@ -60,7 +60,9 @@      -- Other library packages from which modules are imported.   build-depends:       base >=4.0 && <= 4.8, -                       containers >= 0.4 && <= 0.9+                       containers ,+                       hashable ,+                       unordered-containers      -- Directories containing source files.   -- hs-source-dirs: