11module Data.Set.NonEmpty
22 ( NonEmptySet
3+ , singleton
4+ , cons
35 , fromSet
4- , toSet
56 , fromFoldable
67 , fromFoldable1
8+ , toSet
79 , toUnfoldable
810 , toUnfoldable1
9- , singleton
1011 , map
1112 , member
1213 , insert
1314 , delete
1415 , size
15- , findMin
16- , findMax
16+ , min
17+ , max
1718 , unionSet
18- , unions
1919 , difference
2020 , subset
2121 , properSubset
@@ -25,7 +25,7 @@ module Data.Set.NonEmpty
2525import Prelude hiding (map )
2626
2727import Data.Eq (class Eq1 )
28- import Data.Foldable (class Foldable , foldl )
28+ import Data.Foldable (class Foldable )
2929import Data.List (List , (:))
3030import Data.List as List
3131import Data.List.NonEmpty (NonEmptyList )
@@ -41,15 +41,32 @@ import Partial.Unsafe (unsafePartial)
4141-- | `NonEmptySet a` represents a non-empty set of values of type `a`
4242newtype NonEmptySet a = NonEmptySet (Set a )
4343
44+ derive newtype instance eqNonEmptySet :: Eq a => Eq (NonEmptySet a )
45+ derive newtype instance eq1NonEmptySet :: Eq1 NonEmptySet
46+ derive newtype instance ordNonEmptySet :: Ord a => Ord (NonEmptySet a )
47+ derive newtype instance ord1NonEmptySet :: Ord1 NonEmptySet
48+ derive newtype instance semigroupNonEmptySet :: Ord a => Semigroup (NonEmptySet a )
49+ derive newtype instance foldableNonEmptySet :: Foldable NonEmptySet
50+
51+ instance foldable1NonEmptySet :: Foldable1 NonEmptySet where
52+ foldMap1 f = foldMap1 f <<< toUnfoldable1 :: forall a . NonEmptySet a -> NonEmptyList a
53+ fold1 = foldMap1 identity
54+
55+ instance showNonEmptySet :: Show a => Show (NonEmptySet a ) where
56+ show s = " (fromFoldable1 " <> show (toUnfoldable1 s :: NonEmptyList a ) <> " )"
57+
58+ -- | Create a set with one element.
59+ singleton :: forall a . a -> NonEmptySet a
60+ singleton a = NonEmptySet (Set .singleton a)
61+
62+ -- | Creates a `NonEmptySet` from an item and a `Set`.
63+ cons :: forall a . Ord a => a -> Set a -> NonEmptySet a
64+ cons a = NonEmptySet <<< Set .insert a
65+
4466-- | Attempts to create a non-empty set from a possibly-empty set.
4567fromSet :: forall a . Set a -> Maybe (NonEmptySet a )
4668fromSet s = if Set .isEmpty s then Nothing else Just (NonEmptySet s)
4769
48- -- | Forgets the non-empty property of a set, giving a normal possibly-empty
49- -- | set.
50- toSet :: forall a . NonEmptySet a -> Set a
51- toSet (NonEmptySet s) = s
52-
5370-- | Create a set from a foldable structure.
5471fromFoldable :: forall f a . Foldable f => Ord a => f a -> Maybe (NonEmptySet a )
5572fromFoldable = fromSet <<< Set .fromFoldable
@@ -58,6 +75,11 @@ fromFoldable = fromSet <<< Set.fromFoldable
5875fromFoldable1 :: forall f a . Foldable1 f => Ord a => f a -> NonEmptySet a
5976fromFoldable1 = foldMap1 singleton
6077
78+ -- | Forgets the non-empty property of a set, giving a normal possibly-empty
79+ -- | set.
80+ toSet :: forall a . NonEmptySet a -> Set a
81+ toSet (NonEmptySet s) = s
82+
6183-- | Convert a set to an unfoldable structure.
6284toUnfoldable :: forall f a . Unfoldable f => NonEmptySet a -> f a
6385toUnfoldable (NonEmptySet s) = Set .toUnfoldable s
@@ -70,24 +92,6 @@ toUnfoldable1 (NonEmptySet s) = unfoldr1 go (Set.toUnfoldable s :: List a)
7092 x : List.Nil -> Tuple x Nothing
7193 x : tail -> Tuple x (Just tail)
7294
73- derive newtype instance eqNonEmptySet :: Eq a => Eq (NonEmptySet a )
74- derive newtype instance eq1NonEmptySet :: Eq1 NonEmptySet
75- derive newtype instance ordNonEmptySet :: Ord a => Ord (NonEmptySet a )
76- derive newtype instance ord1NonEmptySet :: Ord1 NonEmptySet
77- derive newtype instance semigroupNonEmptySet :: Ord a => Semigroup (NonEmptySet a )
78- derive newtype instance foldableNonEmptySet :: Foldable NonEmptySet
79-
80- instance foldable1NonEmptySet :: Foldable1 NonEmptySet where
81- foldMap1 f = foldMap1 f <<< toUnfoldable1 :: forall a . NonEmptySet a -> NonEmptyList a
82- fold1 = foldMap1 identity
83-
84- instance showNonEmptySet :: Show a => Show (NonEmptySet a ) where
85- show s = " (fromFoldable " <> show (toUnfoldable1 s :: NonEmptyList a ) <> " )"
86-
87- -- | Create a set with one element.
88- singleton :: forall a . a -> NonEmptySet a
89- singleton a = NonEmptySet (Set .singleton a)
90-
9195-- | Maps over the values in a set.
9296-- |
9397-- | This operation is not structure-preserving for sets, so is not a valid
@@ -114,22 +118,18 @@ size :: forall a. NonEmptySet a -> Int
114118size (NonEmptySet s) = Set .size s
115119
116120-- | The minimum value in the set.
117- findMin :: forall a . NonEmptySet a -> a
118- findMin (NonEmptySet s) = unsafePartial (fromJust (Set .findMin s))
121+ min :: forall a . NonEmptySet a -> a
122+ min (NonEmptySet s) = unsafePartial (fromJust (Set .findMin s))
119123
120124-- | The maximum value in the set.
121- findMax :: forall a . NonEmptySet a -> a
122- findMax (NonEmptySet s) = unsafePartial (fromJust (Set .findMax s))
125+ max :: forall a . NonEmptySet a -> a
126+ max (NonEmptySet s) = unsafePartial (fromJust (Set .findMax s))
123127
124128-- | Form the union of a set and the non-empty set.
125129unionSet :: forall a . Ord a => Set.Set a -> NonEmptySet a -> NonEmptySet a
126130unionSet s1 (NonEmptySet s2) = NonEmptySet (s1 <> s2)
127131
128- -- | Form the union of a non-empty collection of non-empty sets.
129- unions :: forall f a . Foldable1 f => Ord a => f (NonEmptySet a ) -> NonEmptySet a
130- unions = foldl append (NonEmptySet Set .empty)
131-
132- -- | Form the set difference. `Nothing` if the sets are identical.
132+ -- | Form the set difference. `Nothing` if the first is a subset of the second.
133133difference :: forall a . Ord a => NonEmptySet a -> NonEmptySet a -> Maybe (NonEmptySet a )
134134difference (NonEmptySet s1) (NonEmptySet s2) = fromSet (Set .difference s1 s2)
135135
0 commit comments