packages feed

smartGroup 0.0.0 → 0.1.0

raw patch · 2 files changed

+17/−17 lines, 2 files

Files

SmartGroup.hs view
@@ -7,13 +7,13 @@  data StringL = StringL {str :: String, count :: Int} deriving (Eq, Show) instance Ord StringL where compare (StringL i a) (StringL x b) = compare a b `mappend` compare i x-data SizeMap = Unsplittable (Set String) | Splittable (Map StringL (Set String)) deriving (Eq, Show)-instance Ord SizeMap where+data SizeMap a = Unsplittable (Set a) | Splittable (Map StringL (Set a)) deriving (Eq, Show)+instance Ord s => Ord (SizeMap s) where          compare (Splittable a) (Splittable b) = compare (Map.size a) (Map.size b)          compare (Unsplittable a) (Unsplittable b) = compare a b          compare (Unsplittable _) (Splittable _) = LT          compare (Splittable _) (Unsplittable _) = GT-type WordAssoc = MaxHeap SizeMap+type WordAssoc a = MaxHeap (SizeMap a)  toSet (Unsplittable x) = x toSet (Splittable x) = Set.unions (Map.elems x)@@ -21,11 +21,11 @@ intLog :: Int -> Int intLog = truncate . logBase 2 . fromIntegral -groupWith :: (Int -> WordAssoc -> WordAssoc) -> Int -> [String] -> [[String]]-groupWith f i = mkList . f i . mkAssoc . mkMap+groupWith :: Ord a => (Int -> WordAssoc a -> WordAssoc a) -> Int -> (a -> String) -> [a] -> [[a]]+groupWith f i c = mkList . f i . mkAssoc . mkMap c  -- | Divide list into as many groups as possible-groupAll :: Int -> [String] -> [[String]]+groupAll :: Ord a => Int -> (a -> String) -> [a] -> [[a]] groupAll = groupWith $ \i x->          let cycleSplit n = case splitIt i n of                   (Just a) -> cycleSplit a@@ -33,7 +33,7 @@          in cycleSplit x  -- | Divide list into about n different groups-groupNum :: Int -> Int -> [String] -> [[String]]+groupNum :: Ord a => Int -> Int -> (a -> String) -> [a] -> [[a]] groupNum i = groupWith $ \x->          let splitTimes n m =                 if Heap.size m >= n then m else@@ -44,19 +44,19 @@  -- | Divide list into groups such that the amount of groups --   equals the log of the number of elements-groupLog :: Int -> [String] -> [[String]]-groupLog i s = groupNum (intLog (length s)) i s+groupLog :: Ord a => Int -> (a -> String) -> [a] -> [[a]]+groupLog i f s = groupNum (intLog (length s)) i f s -mkMap :: [String] -> Map String (Set String)-mkMap = foldl (\m x->+mkMap :: Ord a => (a -> String) -> [a] -> Map String (Set a)+mkMap f = foldl (\m x->         foldl (\m' i-> if length i > 3               then Map.alter (Just . maybe (Set.singleton x) (Set.insert x)) i m' else m')-        m (words x)) Map.empty+        m (words $ f x)) Map.empty -mkAssoc :: Map String (Set String) -> MaxHeap SizeMap+mkAssoc :: Ord a => Map String (Set a) -> MaxHeap (SizeMap a) mkAssoc m = Heap.singleton . Splittable . Map.mapKeys (\k-> StringL k (Set.size (m Map.! k))) $ m -splitIt :: Int -> WordAssoc -> Maybe WordAssoc+splitIt :: Ord a => Int -> WordAssoc a -> Maybe (WordAssoc a) splitIt i s = case Heap.view s of          (Just ((Unsplittable _),_)) -> Nothing          (Just ((Splittable x),xs)) ->@@ -72,10 +72,10 @@                in Just $ (if Set.null x3 then id else Heap.insert (Unsplittable x3)) $                    Heap.insert x1 $ Heap.insert x2 xs -sizeMap :: Int -> (Map StringL (Set String)) -> SizeMap+sizeMap :: Ord a => Int -> (Map StringL (Set a)) -> (SizeMap a) sizeMap i m = case Map.findMax m of         (StringL _ c,_) -> if c >= i then Splittable m                  else Unsplittable (Set.unions (Map.elems m)) -mkList :: WordAssoc -> [[String]]+mkList :: Ord a => WordAssoc a -> [[a]] mkList = Prelude.map (Set.toList . toSet) . Heap.toList
smartGroup.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version:             0.0.0+Version:             0.1.0  -- A short (one-line) description of the package. Synopsis:            group strings by words in common