forked from kowainik/relude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
One.hs
282 lines (205 loc) Β· 6.1 KB
/
One.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{- |
Copyright: (c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2019 Kowainik
SPDX-License-Identifier: MIT
Maintainer: Kowainik <[email protected]>
Typeclass for creating structures from a singleton element. It has three main goals:
1. Give a shorter name for the construction: uses 'one' instead of common @singleton@.
2. Work with monomorphic structures like 'T.Text' or 'IntSet'.
3. Give a clearer and less scary name for cases where you can use 'Relude.pure' or @(:[])@.
-}
module Relude.Container.One
( One (..)
) where
import Relude.Base (Char)
import Relude.Container.Reexport (HashMap, HashSet, Hashable, IntMap, IntSet, Map, Set, uncurry)
import Relude.Numeric (Int, Word8)
import qualified Data.List.NonEmpty as NE
import qualified Data.Sequence as SEQ
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Short as SBS
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HashSet
import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.Map as M
import qualified Data.Set as Set
-- $setup
-- >>> import Relude
-- >>> import qualified Data.IntSet as IntSet
-- >>> import qualified Data.HashMap.Strict as HashMap
-- >>> import qualified Data.Text as Text
-- >>> import qualified Data.ByteString as ByteString
-- >>> import qualified Data.ByteString.Short as ShortByteString
-- >>> import qualified Data.Text.Lazy as LText
-- >>> import qualified Data.ByteString.Lazy as LByteString
{- | Typeclass for data types that can be created from one element.
>>> one True :: [Bool]
[True]
>>> one 'a' :: Text
"a"
>>> one (3, "hello") :: HashMap Int String
fromList [(3,"hello")]
__Laws:__
* __@single-size@__: @β x . size (one x) β‘ 1@
(where @size@ is a specific function for each container that returns the size of
this container)
-}
class One x where
-- | Type of single element of the structure.
type OneItem x
-- | Create a list, map, 'T.Text', etc from a single element.
one :: OneItem x -> x
-- Lists
{- | Allows to create a singleton list. You might prefer function with name 'one'
instead of 'Relude.pure' or @(:[])@.
>>> one 42 :: [Int]
[42]
prop> length (one @[Int] x) == 1
-}
instance One [a] where
type OneItem [a] = a
one :: a -> [a]
one = (:[])
{-# INLINE one #-}
{- | Allows to create singleton 'NE.NonEmpty' list. You might prefer function with
name 'one' instead of 'Relude.pure' or @(:|[])@.
>>> one 42 :: NonEmpty Int
42 :| []
prop> length (one @(NonEmpty Int) x) == 1
-}
instance One (NE.NonEmpty a) where
type OneItem (NE.NonEmpty a) = a
one :: a -> NE.NonEmpty a
one = (NE.:|[])
{-# INLINE one #-}
{- | Create singleton 'SEQ.Seq'.
>>> one 42 :: Seq Int
fromList [42]
prop> length (one @(Seq Int) x) == 1
-}
instance One (SEQ.Seq a) where
type OneItem (SEQ.Seq a) = a
one :: a -> SEQ.Seq a
one = SEQ.singleton
{-# INLINE one #-}
-- Monomorphic sequences
{- | Create singleton strict 'T.Text'.
>>> one 'a' :: Text
"a"
prop> Text.length (one x) == 1
-}
instance One T.Text where
type OneItem T.Text = Char
one :: Char -> T.Text
one = T.singleton
{-# INLINE one #-}
{- | Create singleton lazy 'TL.Text'.
>>> one 'a' :: LText
"a"
prop> LText.length (one x) == 1
-}
instance One TL.Text where
type OneItem TL.Text = Char
one :: Char -> TL.Text
one = TL.singleton
{-# INLINE one #-}
{- | Create singleton strict 'BS.ByteString'.
>>> one 97 :: ByteString
"a"
prop> ByteString.length (one x) == 1
-}
instance One BS.ByteString where
type OneItem BS.ByteString = Word8
one :: Word8 -> BS.ByteString
one = BS.singleton
{-# INLINE one #-}
{- | Create singleton lazy 'BSL.ByteString'.
>>> one 97 :: LByteString
"a"
prop> LByteString.length (one x) == 1
-}
instance One BSL.ByteString where
type OneItem BSL.ByteString = Word8
one :: Word8 -> BSL.ByteString
one = BSL.singleton
{-# INLINE one #-}
{- | Create singleton 'SBS.ShortByteString'.
>>> one 97 :: ShortByteString
"a"
prop> ShortByteString.length (one x) == 1
-}
instance One SBS.ShortByteString where
type OneItem SBS.ShortByteString = Word8
one :: Word8 -> SBS.ShortByteString
one x = SBS.pack [x]
{-# INLINE one #-}
-- Maps
{- | Create singleton 'Map' from key-value pair.
>>> one (3, "foo") :: Map Int Text
fromList [(3,"foo")]
prop> length (one @(Map Int String) x) == 1
-}
instance One (Map k v) where
type OneItem (Map k v) = (k, v)
one :: (k, v) -> Map k v
one = uncurry M.singleton
{-# INLINE one #-}
{- | Create singleton 'HashMap' from key-value pair.
>>> one (3, "foo") :: HashMap Int Text
fromList [(3,"foo")]
prop> length (one @(HashMap Int String) x) == 1
-}
instance Hashable k => One (HashMap k v) where
type OneItem (HashMap k v) = (k, v)
one :: (k, v) -> HashMap k v
one = uncurry HM.singleton
{-# INLINE one #-}
{- | Create singleton 'IntMap' from key-value pair.
>>> one (3, "foo") :: IntMap Text
fromList [(3,"foo")]
prop> length (one @(IntMap String) x) == 1
-}
instance One (IntMap v) where
type OneItem (IntMap v) = (Int, v)
one :: (Int, v) -> IntMap v
one = uncurry IM.singleton
{-# INLINE one #-}
-- Sets
{- | Create singleton 'Set'.
>>> one 42 :: Set Int
fromList [42]
prop> length (one @(Set Int) x) == 1
-}
instance One (Set a) where
type OneItem (Set a) = a
one :: a -> Set a
one = Set.singleton
{-# INLINE one #-}
{- | Create singleton 'HashSet'.
>>> one 42 :: HashSet Int
fromList [42]
prop> length (one @(HashSet Int) x) == 1
-}
instance Hashable a => One (HashSet a) where
type OneItem (HashSet a) = a
one :: a -> HashSet a
one = HashSet.singleton
{-# INLINE one #-}
{- | Create singleton 'IntSet'.
>>> one 42 :: IntSet
fromList [42]
prop> IntSet.size (one x) == 1
-}
instance One IntSet where
type OneItem IntSet = Int
one :: Int -> IntSet
one = IS.singleton
{-# INLINE one #-}