1
1
module Data.Set.NonEmpty
2
2
( NonEmptySet
3
+ , singleton
4
+ , cons
3
5
, fromSet
4
- , toSet
5
6
, fromFoldable
6
7
, fromFoldable1
8
+ , toSet
7
9
, toUnfoldable
8
10
, toUnfoldable1
9
- , singleton
10
11
, map
11
12
, member
12
13
, insert
13
14
, delete
14
15
, size
15
- , findMin
16
- , findMax
16
+ , min
17
+ , max
17
18
, unionSet
18
- , unions
19
19
, difference
20
20
, subset
21
21
, properSubset
@@ -25,7 +25,7 @@ module Data.Set.NonEmpty
25
25
import Prelude hiding (map )
26
26
27
27
import Data.Eq (class Eq1 )
28
- import Data.Foldable (class Foldable , foldl )
28
+ import Data.Foldable (class Foldable )
29
29
import Data.List (List , (:))
30
30
import Data.List as List
31
31
import Data.List.NonEmpty (NonEmptyList )
@@ -41,15 +41,32 @@ import Partial.Unsafe (unsafePartial)
41
41
-- | `NonEmptySet a` represents a non-empty set of values of type `a`
42
42
newtype NonEmptySet a = NonEmptySet (Set a )
43
43
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
+
44
66
-- | Attempts to create a non-empty set from a possibly-empty set.
45
67
fromSet :: forall a . Set a -> Maybe (NonEmptySet a )
46
68
fromSet s = if Set .isEmpty s then Nothing else Just (NonEmptySet s)
47
69
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
-
53
70
-- | Create a set from a foldable structure.
54
71
fromFoldable :: forall f a . Foldable f => Ord a => f a -> Maybe (NonEmptySet a )
55
72
fromFoldable = fromSet <<< Set .fromFoldable
@@ -58,6 +75,11 @@ fromFoldable = fromSet <<< Set.fromFoldable
58
75
fromFoldable1 :: forall f a . Foldable1 f => Ord a => f a -> NonEmptySet a
59
76
fromFoldable1 = foldMap1 singleton
60
77
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
+
61
83
-- | Convert a set to an unfoldable structure.
62
84
toUnfoldable :: forall f a . Unfoldable f => NonEmptySet a -> f a
63
85
toUnfoldable (NonEmptySet s) = Set .toUnfoldable s
@@ -70,24 +92,6 @@ toUnfoldable1 (NonEmptySet s) = unfoldr1 go (Set.toUnfoldable s :: List a)
70
92
x : List.Nil -> Tuple x Nothing
71
93
x : tail -> Tuple x (Just tail)
72
94
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
-
91
95
-- | Maps over the values in a set.
92
96
-- |
93
97
-- | This operation is not structure-preserving for sets, so is not a valid
@@ -114,22 +118,18 @@ size :: forall a. NonEmptySet a -> Int
114
118
size (NonEmptySet s) = Set .size s
115
119
116
120
-- | 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))
119
123
120
124
-- | 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))
123
127
124
128
-- | Form the union of a set and the non-empty set.
125
129
unionSet :: forall a . Ord a => Set.Set a -> NonEmptySet a -> NonEmptySet a
126
130
unionSet s1 (NonEmptySet s2) = NonEmptySet (s1 <> s2)
127
131
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.
133
133
difference :: forall a . Ord a => NonEmptySet a -> NonEmptySet a -> Maybe (NonEmptySet a )
134
134
difference (NonEmptySet s1) (NonEmptySet s2) = fromSet (Set .difference s1 s2)
135
135
0 commit comments