refactor(utils.set): new utils.set folder with set-functions added

This commit is contained in:
ros 2021-05-13 16:18:59 +02:00
parent 407aa5edde
commit a4bd1159c2
2 changed files with 57 additions and 26 deletions

View File

@ -40,6 +40,7 @@ import Utils.HttpConditional as Utils
import Utils.Persist as Utils
import Utils.ARC as Utils
import Utils.LRU as Utils
import Utils.Set as Utils
import Text.Blaze (Markup, ToMarkup(..))
@ -562,32 +563,6 @@ withoutSubsequenceBy cmp = go []
| otherwise = go (y:acc) a b
----------
-- Sets --
----------
-- | Intersection of multiple sets. Returns empty set for empty input list
setIntersections :: Ord a => [Set a] -> Set a
setIntersections [] = Set.empty
setIntersections (h:t) = foldl' Set.intersection h t
setMapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set b
setMapMaybe f = Set.fromList . mapMaybe f . Set.toList
-- | Symmetric difference of two sets.
setSymmDiff :: Ord a => Set a -> Set a -> Set a
setSymmDiff x y = (x `Set.difference` y) `Set.union` (y `Set.difference` x)
setProduct :: Set a -> Set b -> Set (a, b)
-- ^ Depends on the valid internal structure of the given sets
setProduct (Set.toAscList -> as) (Set.toAscList -> bs) = Set.fromDistinctAscList $ (,) <$> as <*> bs
setPartitionEithers :: (Ord a, Ord b) => Set (Either a b) -> (Set a, Set b)
setPartitionEithers = (,) <$> setMapMaybe (preview _Left) <*> setMapMaybe (preview _Right)
setFromFunc :: (Finite k, Ord k) => (k -> Bool) -> Set k
setFromFunc = Set.fromList . flip filter universeF
----------
-- Maps --
----------

56
src/Utils/Set.hs Normal file
View File

@ -0,0 +1,56 @@
module Utils.Set
( setIntersectAll
, setIntersectNotOne
, setIntersections
, setMapMaybe
, setSymmDiff
, setProduct
, setPartitionEithers
, setFromFunc
) where
import qualified Data.Set as Set
import qualified Data.Map.Strict()
import ClassyPrelude
import Data.Universe
import Control.Lens.Prism
import Control.Lens
-- Mächtigkeit Schnittmenge
setIntersectAll :: Ord a => [Set.Set a] -> Int
setIntersectAll [] = 0
setIntersectAll [x] = Set.size x
setIntersectAll (x:y:z) = setIntersectAll (xy:z) where xy = Set.intersection x y
-- Mächtigkeit Schnittmenge Set, Liste von Sets
setIntersectNotOne :: Ord a => Set.Set a -> [Set.Set a] -> Int
setIntersectNotOne _ [] = 0
setIntersectNotOne k r = Set.size $ Set.intersection k others where others = setUnionOthers r
-- Vereinigung von Sets
setUnionOthers :: Ord a => [Set.Set a] -> Set.Set a
setUnionOthers [] = Set.empty
setUnionOthers [x] = x
setUnionOthers (x:y:z) = setUnionOthers (xy:z) where xy = Set.union x y
-- | Intersection of multiple sets. Returns empty set for empty input list
setIntersections :: Ord a => [Set a] -> Set a
setIntersections [] = Set.empty
setIntersections (h:t) = foldl' Set.intersection h t
setMapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set b
setMapMaybe f = Set.fromList . mapMaybe f . Set.toList
-- | Symmetric difference of two sets.
setSymmDiff :: Ord a => Set a -> Set a -> Set a
setSymmDiff x y = (x `Set.difference` y) `Set.union` (y `Set.difference` x)
setProduct :: Set a -> Set b -> Set (a, b)
-- ^ Depends on the valid internal structure of the given sets
setProduct (Set.toAscList -> as) (Set.toAscList -> bs) = Set.fromDistinctAscList $ (,) <$> as <*> bs
setPartitionEithers :: (Ord a, Ord b) => Set (Either a b) -> (Set a, Set b)
setPartitionEithers = (,) <$> setMapMaybe (preview _Left) <*> setMapMaybe (preview _Right)
setFromFunc :: (Finite k, Ord k) => (k -> Bool) -> Set k
setFromFunc = Set.fromList . flip filter universeF