Skip to content

Commit 89ded1c

Browse files
TOTBWFtreeowl
authored andcommitted
Implement 'intersections', and add a newtype + semigroup instance
1 parent a3ec4a2 commit 89ded1c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

containers/src/Data/Set/Internal.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ module Data.Set.Internal (
155155
, unions
156156
, difference
157157
, intersection
158+
, intersections
158159
, cartesianProduct
159160
, disjointUnion
160161

@@ -234,10 +235,11 @@ import Control.Applicative (Const(..))
234235
import qualified Data.List as List
235236
import Data.Bits (shiftL, shiftR)
236237
import Data.Semigroup (Semigroup(stimes))
238+
import Data.List.NonEmpty (NonEmpty(..))
237239
#if !(MIN_VERSION_base(4,11,0))
238240
import Data.Semigroup (Semigroup((<>)))
239241
#endif
240-
import Data.Semigroup (stimesIdempotentMonoid)
242+
import Data.Semigroup (stimesIdempotentMonoid, stimesIdempotent)
241243
import Data.Functor.Classes
242244
import Data.Functor.Identity (Identity)
243245
import qualified Data.Foldable as Foldable
@@ -875,6 +877,20 @@ intersection t1@(Bin _ x l1 r1) t2
875877
{-# INLINABLE intersection #-}
876878
#endif
877879

880+
#if (MIN_VERSION_base(4,9,0))
881+
-- | The intersection of a series of sets.
882+
intersections :: Ord a => NonEmpty (Set a) -> Set a
883+
intersections (s :| ss) = Foldable.foldl' intersection s ss
884+
885+
-- | Sets form a 'Semigroup' under 'intersection'.
886+
newtype Intersection a = Intersection { getIntersection :: Set a }
887+
deriving (Show, Eq, Ord)
888+
889+
instance (Ord a) => Semigroup (Intersection a) where
890+
(Intersection a) <> (Intersection b) = Intersection $ intersection a b
891+
stimes = stimesIdempotent
892+
#endif
893+
878894
{--------------------------------------------------------------------
879895
Filter and partition
880896
--------------------------------------------------------------------}

0 commit comments

Comments
 (0)