1
- {-# LANGUAGE CPP #-}
1
+ {-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE DeriveDataTypeable #-}
3
- module Distribution.Compat.NonEmptySet (
4
- NonEmptySet ,
3
+
4
+ module Distribution.Compat.NonEmptySet
5
+ ( NonEmptySet
6
+
5
7
-- * Construction
6
- singleton ,
8
+ , singleton
9
+
7
10
-- * Insertion
8
- insert ,
11
+ , insert
12
+
9
13
-- * Deletion
10
- delete ,
14
+ , delete
15
+
11
16
-- * Conversions
12
- toNonEmpty ,
13
- fromNonEmpty ,
14
- toList ,
15
- toSet ,
17
+ , toNonEmpty
18
+ , fromNonEmpty
19
+ , toList
20
+ , toSet
21
+
16
22
-- * Query
17
- member ,
23
+ , member
24
+
18
25
-- * Map
19
- map ,
20
- ) where
26
+ , map
27
+ ) where
21
28
22
29
import Prelude (Bool (.. ), Eq , Maybe (.. ), Ord (.. ), Read , Show (.. ), String , error , otherwise , return , showParen , showString , ($) , (++) , (.) )
23
30
24
- import Control.DeepSeq (NFData (.. ))
25
- import Data.Data (Data )
31
+ import Control.DeepSeq (NFData (.. ))
32
+ import Data.Data (Data )
26
33
import Data.List.NonEmpty (NonEmpty (.. ))
27
- import Data.Semigroup (Semigroup (.. ))
28
- import Data.Typeable (Typeable )
34
+ import Data.Semigroup (Semigroup (.. ))
35
+ import Data.Typeable (Typeable )
29
36
30
37
import qualified Data.Foldable as F
31
- import qualified Data.Set as Set
38
+ import qualified Data.Set as Set
32
39
33
- import Distribution.Compat.Binary (Binary (.. ))
40
+ import Distribution.Compat.Binary (Binary (.. ))
34
41
import Distribution.Utils.Structured
35
42
36
43
#if MIN_VERSION_binary(0,6,0)
@@ -48,40 +55,43 @@ newtype NonEmptySet a = NES (Set.Set a)
48
55
-------------------------------------------------------------------------------
49
56
50
57
instance Show a => Show (NonEmptySet a ) where
51
- showsPrec d s = showParen (d > 10 )
52
- $ showString " fromNonEmpty "
58
+ showsPrec d s =
59
+ showParen (d > 10 ) $
60
+ showString " fromNonEmpty "
53
61
. showsPrec 11 (toNonEmpty s)
54
62
63
+ {- FOURMOLU_DISABLE -}
55
64
instance Binary a => Binary (NonEmptySet a ) where
56
- put (NES s) = put s
57
- get = do
58
- xs <- get
59
- if Set. null xs
65
+ put (NES s) = put s
66
+ get = do
67
+ xs <- get
68
+ if Set. null xs
60
69
#if MIN_VERSION_binary(0,6,0)
61
- then empty
70
+ then empty
62
71
#else
63
- then fail " NonEmptySet: empty"
72
+ then fail " NonEmptySet: empty"
64
73
#endif
65
- else return (NES xs)
74
+ else return (NES xs)
75
+ {- FOURMOLU_ENABLE -}
66
76
67
77
instance Structured a => Structured (NonEmptySet a ) where
68
- structure = containerStructure
78
+ structure = containerStructure
69
79
70
80
instance NFData a => NFData (NonEmptySet a ) where
71
- rnf (NES x) = rnf x
81
+ rnf (NES x) = rnf x
72
82
73
83
-- | Note: there aren't @Monoid@ instance.
74
84
instance Ord a => Semigroup (NonEmptySet a ) where
75
- NES x <> NES y = NES (Set. union x y)
85
+ NES x <> NES y = NES (Set. union x y)
76
86
77
87
instance F. Foldable NonEmptySet where
78
- foldMap f (NES s) = F. foldMap f s
79
- foldr f z (NES s) = F. foldr f z s
88
+ foldMap f (NES s) = F. foldMap f s
89
+ foldr f z (NES s) = F. foldr f z s
80
90
81
91
#if MIN_VERSION_base(4,8,0)
82
- toList = toList
83
- null _ = False
84
- length (NES s) = F. length s
92
+ toList = toList
93
+ null _ = False
94
+ length (NES s) = F. length s
85
95
#endif
86
96
87
97
-------------------------------------------------------------------------------
@@ -104,8 +114,8 @@ insert x (NES xs) = NES (Set.insert x xs)
104
114
105
115
delete :: Ord a => a -> NonEmptySet a -> Maybe (NonEmptySet a )
106
116
delete x (NES xs)
107
- | Set. null res = Nothing
108
- | otherwise = Just (NES xs)
117
+ | Set. null res = Nothing
118
+ | otherwise = Just (NES xs)
109
119
where
110
120
res = Set. delete x xs
111
121
@@ -118,8 +128,8 @@ fromNonEmpty (x :| xs) = NES (Set.fromList (x : xs))
118
128
119
129
toNonEmpty :: NonEmptySet a -> NonEmpty a
120
130
toNonEmpty (NES s) = case Set. toList s of
121
- [] -> panic " toNonEmpty"
122
- x : xs -> x :| xs
131
+ [] -> panic " toNonEmpty"
132
+ x : xs -> x :| xs
123
133
124
134
toList :: NonEmptySet a -> [a ]
125
135
toList (NES s) = Set. toList s
@@ -138,6 +148,7 @@ member x (NES xs) = Set.member x xs
138
148
-- Map
139
149
-------------------------------------------------------------------------------
140
150
151
+ {- FOURMOLU_DISABLE -}
141
152
map
142
153
:: ( Ord b
143
154
#if !MIN_VERSION_containers(0,5,2)
146
157
)
147
158
=> (a -> b) -> NonEmptySet a -> NonEmptySet b
148
159
map f (NES x) = NES (Set. map f x)
160
+ {- FOURMOLU_ENABLE -}
149
161
150
162
-------------------------------------------------------------------------------
151
163
-- Internal
0 commit comments