base: Override Foldable.{toList,length} for NonEmpty
[ghc.git] / libraries / base / Data / List / NonEmpty.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 {-# LANGUAGE Trustworthy #-} -- can't use Safe due to IsList instance
4 {-# LANGUAGE TypeFamilies #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : Data.List.NonEmpty
9 -- Copyright : (C) 2011-2015 Edward Kmett,
10 -- (C) 2010 Tony Morris, Oliver Taylor, Eelis van der Weegen
11 -- License : BSD-style (see the file LICENSE)
12 --
13 -- Maintainer : libraries@haskell.org
14 -- Stability : provisional
15 -- Portability : portable
16 --
17 -- A 'NonEmpty' list is one which always has at least one element, but
18 -- is otherwise identical to the traditional list type in complexity
19 -- and in terms of API. You will almost certainly want to import this
20 -- module @qualified@.
21 --
22 -- @since 4.9.0.0
23 ----------------------------------------------------------------------------
24
25 module Data.List.NonEmpty (
26 -- * The type of non-empty streams
27 NonEmpty(..)
28
29 -- * Non-empty stream transformations
30 , map -- :: (a -> b) -> NonEmpty a -> NonEmpty b
31 , intersperse -- :: a -> NonEmpty a -> NonEmpty a
32 , scanl -- :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
33 , scanr -- :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
34 , scanl1 -- :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
35 , scanr1 -- :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
36 , transpose -- :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
37 , sortBy -- :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
38 , sortWith -- :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
39 -- * Basic functions
40 , length -- :: NonEmpty a -> Int
41 , head -- :: NonEmpty a -> a
42 , tail -- :: NonEmpty a -> [a]
43 , last -- :: NonEmpty a -> a
44 , init -- :: NonEmpty a -> [a]
45 , (<|), cons -- :: a -> NonEmpty a -> NonEmpty a
46 , uncons -- :: NonEmpty a -> (a, Maybe (NonEmpty a))
47 , unfoldr -- :: (a -> (b, Maybe a)) -> a -> NonEmpty b
48 , sort -- :: NonEmpty a -> NonEmpty a
49 , reverse -- :: NonEmpty a -> NonEmpty a
50 , inits -- :: Foldable f => f a -> NonEmpty a
51 , tails -- :: Foldable f => f a -> NonEmpty a
52 -- * Building streams
53 , iterate -- :: (a -> a) -> a -> NonEmpty a
54 , repeat -- :: a -> NonEmpty a
55 , cycle -- :: NonEmpty a -> NonEmpty a
56 , unfold -- :: (a -> (b, Maybe a) -> a -> NonEmpty b
57 , insert -- :: (Foldable f, Ord a) => a -> f a -> NonEmpty a
58 , some1 -- :: Alternative f => f a -> f (NonEmpty a)
59 -- * Extracting sublists
60 , take -- :: Int -> NonEmpty a -> [a]
61 , drop -- :: Int -> NonEmpty a -> [a]
62 , splitAt -- :: Int -> NonEmpty a -> ([a], [a])
63 , takeWhile -- :: Int -> NonEmpty a -> [a]
64 , dropWhile -- :: Int -> NonEmpty a -> [a]
65 , span -- :: Int -> NonEmpty a -> ([a],[a])
66 , break -- :: Int -> NonEmpty a -> ([a],[a])
67 , filter -- :: (a -> Bool) -> NonEmpty a -> [a]
68 , partition -- :: (a -> Bool) -> NonEmpty a -> ([a],[a])
69 , group -- :: Foldable f => Eq a => f a -> [NonEmpty a]
70 , groupBy -- :: Foldable f => (a -> a -> Bool) -> f a -> [NonEmpty a]
71 , groupWith -- :: (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a]
72 , groupAllWith -- :: (Foldable f, Ord b) => (a -> b) -> f a -> [NonEmpty a]
73 , group1 -- :: Eq a => NonEmpty a -> NonEmpty (NonEmpty a)
74 , groupBy1 -- :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
75 , groupWith1 -- :: (Foldable f, Eq b) => (a -> b) -> f a -> NonEmpty (NonEmpty a)
76 , groupAllWith1 -- :: (Foldable f, Ord b) => (a -> b) -> f a -> NonEmpty (NonEmpty a)
77 -- * Sublist predicates
78 , isPrefixOf -- :: Foldable f => f a -> NonEmpty a -> Bool
79 -- * \"Set\" operations
80 , nub -- :: Eq a => NonEmpty a -> NonEmpty a
81 , nubBy -- :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
82 -- * Indexing streams
83 , (!!) -- :: NonEmpty a -> Int -> a
84 -- * Zipping and unzipping streams
85 , zip -- :: NonEmpty a -> NonEmpty b -> NonEmpty (a,b)
86 , zipWith -- :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
87 , unzip -- :: NonEmpty (a, b) -> (NonEmpty a, NonEmpty b)
88 -- * Converting to and from a list
89 , fromList -- :: [a] -> NonEmpty a
90 , toList -- :: NonEmpty a -> [a]
91 , nonEmpty -- :: [a] -> Maybe (NonEmpty a)
92 , xor -- :: NonEmpty a -> Bool
93 ) where
94
95
96 import Prelude hiding (break, cycle, drop, dropWhile,
97 filter, foldl, foldr, head, init, iterate,
98 last, length, map, repeat, reverse,
99 scanl, scanl1, scanr, scanr1, span,
100 splitAt, tail, take, takeWhile,
101 unzip, zip, zipWith, (!!))
102 import qualified Prelude
103
104 import Control.Applicative (Alternative, many)
105 import Control.Monad (ap)
106 import Control.Monad.Fix
107 import Control.Monad.Zip (MonadZip(..))
108 import Data.Data (Data)
109 import Data.Foldable hiding (length, toList)
110 import qualified Data.Foldable as Foldable
111 import Data.Function (on)
112 import Data.Functor.Classes (Eq1(..), Ord1(..), Read1(..), Show1(..))
113 import qualified Data.List as List
114 import Data.Monoid ((<>))
115 import Data.Ord (comparing)
116 import qualified GHC.Exts as Exts (IsList(..))
117 import GHC.Generics (Generic, Generic1)
118
119 infixr 5 :|, <|
120
121 -- | Non-empty (and non-strict) list type.
122 --
123 -- @since 4.9.0.0
124 data NonEmpty a = a :| [a]
125 deriving ( Eq, Ord, Show, Read, Data, Generic, Generic1 )
126
127 -- | @since 4.10.0.0
128 instance Eq1 NonEmpty where
129 liftEq eq (a :| as) (b :| bs) = eq a b && liftEq eq as bs
130
131 -- | @since 4.10.0.0
132 instance Ord1 NonEmpty where
133 liftCompare cmp (a :| as) (b :| bs) = cmp a b <> liftCompare cmp as bs
134
135 -- | @since 4.10.0.0
136 instance Read1 NonEmpty where
137 liftReadsPrec rdP rdL p s = readParen (p > 5) (\s' -> do
138 (a, s'') <- rdP 6 s'
139 (":|", s''') <- lex s''
140 (as, s'''') <- rdL s'''
141 return (a :| as, s'''')) s
142
143 -- | @since 4.10.0.0
144 instance Show1 NonEmpty where
145 liftShowsPrec shwP shwL p (a :| as) = showParen (p > 5) $
146 shwP 6 a . showString " :| " . shwL as
147
148 -- | @since 4.9.0.0
149 instance Exts.IsList (NonEmpty a) where
150 type Item (NonEmpty a) = a
151 fromList = fromList
152 toList = toList
153
154 -- | @since 4.9.0.0
155 instance MonadFix NonEmpty where
156 mfix f = case fix (f . head) of
157 ~(x :| _) -> x :| mfix (tail . f)
158
159 -- | @since 4.9.0.0
160 instance MonadZip NonEmpty where
161 mzip = zip
162 mzipWith = zipWith
163 munzip = unzip
164
165 -- | Number of elements in 'NonEmpty' list.
166 length :: NonEmpty a -> Int
167 length (_ :| xs) = 1 + Prelude.length xs
168
169 -- | Compute n-ary logic exclusive OR operation on 'NonEmpty' list.
170 xor :: NonEmpty Bool -> Bool
171 xor (x :| xs) = foldr xor' x xs
172 where xor' True y = not y
173 xor' False y = y
174
175 -- | 'unfold' produces a new stream by repeatedly applying the unfolding
176 -- function to the seed value to produce an element of type @b@ and a new
177 -- seed value. When the unfolding function returns 'Nothing' instead of
178 -- a new seed value, the stream ends.
179 unfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b
180 unfold f a = case f a of
181 (b, Nothing) -> b :| []
182 (b, Just c) -> b <| unfold f c
183
184 -- | 'nonEmpty' efficiently turns a normal list into a 'NonEmpty' stream,
185 -- producing 'Nothing' if the input is empty.
186 nonEmpty :: [a] -> Maybe (NonEmpty a)
187 nonEmpty [] = Nothing
188 nonEmpty (a:as) = Just (a :| as)
189
190 -- | 'uncons' produces the first element of the stream, and a stream of the
191 -- remaining elements, if any.
192 uncons :: NonEmpty a -> (a, Maybe (NonEmpty a))
193 uncons ~(a :| as) = (a, nonEmpty as)
194
195 -- | The 'unfoldr' function is analogous to "Data.List"'s
196 -- 'Data.List.unfoldr' operation.
197 unfoldr :: (a -> (b, Maybe a)) -> a -> NonEmpty b
198 unfoldr f a = case f a of
199 (b, mc) -> b :| maybe [] go mc
200 where
201 go c = case f c of
202 (d, me) -> d : maybe [] go me
203
204 -- | @since 4.9.0.0
205 instance Functor NonEmpty where
206 fmap f ~(a :| as) = f a :| fmap f as
207 b <$ ~(_ :| as) = b :| (b <$ as)
208
209 -- | @since 4.9.0.0
210 instance Applicative NonEmpty where
211 pure a = a :| []
212 (<*>) = ap
213
214 -- | @since 4.9.0.0
215 instance Monad NonEmpty where
216 ~(a :| as) >>= f = b :| (bs ++ bs')
217 where b :| bs = f a
218 bs' = as >>= toList . f
219
220 -- | @since 4.9.0.0
221 instance Traversable NonEmpty where
222 traverse f ~(a :| as) = (:|) <$> f a <*> traverse f as
223
224 -- | @since 4.9.0.0
225 instance Foldable NonEmpty where
226 foldr f z ~(a :| as) = f a (foldr f z as)
227 foldl f z ~(a :| as) = foldl f (f z a) as
228 foldl1 f ~(a :| as) = foldl f a as
229 foldMap f ~(a :| as) = f a `mappend` foldMap f as
230 fold ~(m :| ms) = m `mappend` fold ms
231 length = length
232 toList = toList
233
234 -- | Extract the first element of the stream.
235 head :: NonEmpty a -> a
236 head ~(a :| _) = a
237
238 -- | Extract the possibly-empty tail of the stream.
239 tail :: NonEmpty a -> [a]
240 tail ~(_ :| as) = as
241
242 -- | Extract the last element of the stream.
243 last :: NonEmpty a -> a
244 last ~(a :| as) = List.last (a : as)
245
246 -- | Extract everything except the last element of the stream.
247 init :: NonEmpty a -> [a]
248 init ~(a :| as) = List.init (a : as)
249
250 -- | Prepend an element to the stream.
251 (<|) :: a -> NonEmpty a -> NonEmpty a
252 a <| ~(b :| bs) = a :| b : bs
253
254 -- | Synonym for '<|'.
255 cons :: a -> NonEmpty a -> NonEmpty a
256 cons = (<|)
257
258 -- | Sort a stream.
259 sort :: Ord a => NonEmpty a -> NonEmpty a
260 sort = lift List.sort
261
262 -- | Converts a normal list to a 'NonEmpty' stream.
263 --
264 -- Raises an error if given an empty list.
265 fromList :: [a] -> NonEmpty a
266 fromList (a:as) = a :| as
267 fromList [] = errorWithoutStackTrace "NonEmpty.fromList: empty list"
268
269 -- | Convert a stream to a normal list efficiently.
270 toList :: NonEmpty a -> [a]
271 toList ~(a :| as) = a : as
272
273 -- | Lift list operations to work on a 'NonEmpty' stream.
274 --
275 -- /Beware/: If the provided function returns an empty list,
276 -- this will raise an error.
277 lift :: Foldable f => ([a] -> [b]) -> f a -> NonEmpty b
278 lift f = fromList . f . Foldable.toList
279
280 -- | Map a function over a 'NonEmpty' stream.
281 map :: (a -> b) -> NonEmpty a -> NonEmpty b
282 map f ~(a :| as) = f a :| fmap f as
283
284 -- | The 'inits' function takes a stream @xs@ and returns all the
285 -- finite prefixes of @xs@.
286 inits :: Foldable f => f a -> NonEmpty [a]
287 inits = fromList . List.inits . Foldable.toList
288
289 -- | The 'tails' function takes a stream @xs@ and returns all the
290 -- suffixes of @xs@.
291 tails :: Foldable f => f a -> NonEmpty [a]
292 tails = fromList . List.tails . Foldable.toList
293
294 -- | @'insert' x xs@ inserts @x@ into the last position in @xs@ where it
295 -- is still less than or equal to the next element. In particular, if the
296 -- list is sorted beforehand, the result will also be sorted.
297 insert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a
298 insert a = fromList . List.insert a . Foldable.toList
299
300 -- | @'some1' x@ sequences @x@ one or more times.
301 some1 :: Alternative f => f a -> f (NonEmpty a)
302 some1 x = (:|) <$> x <*> many x
303
304 -- | 'scanl' is similar to 'foldl', but returns a stream of successive
305 -- reduced values from the left:
306 --
307 -- > scanl f z [x1, x2, ...] == z :| [z `f` x1, (z `f` x1) `f` x2, ...]
308 --
309 -- Note that
310 --
311 -- > last (scanl f z xs) == foldl f z xs.
312 scanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
313 scanl f z = fromList . List.scanl f z . Foldable.toList
314
315 -- | 'scanr' is the right-to-left dual of 'scanl'.
316 -- Note that
317 --
318 -- > head (scanr f z xs) == foldr f z xs.
319 scanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
320 scanr f z = fromList . List.scanr f z . Foldable.toList
321
322 -- | 'scanl1' is a variant of 'scanl' that has no starting value argument:
323 --
324 -- > scanl1 f [x1, x2, ...] == x1 :| [x1 `f` x2, x1 `f` (x2 `f` x3), ...]
325 scanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
326 scanl1 f ~(a :| as) = fromList (List.scanl f a as)
327
328 -- | 'scanr1' is a variant of 'scanr' that has no starting value argument.
329 scanr1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
330 scanr1 f ~(a :| as) = fromList (List.scanr1 f (a:as))
331
332 -- | 'intersperse x xs' alternates elements of the list with copies of @x@.
333 --
334 -- > intersperse 0 (1 :| [2,3]) == 1 :| [0,2,0,3]
335 intersperse :: a -> NonEmpty a -> NonEmpty a
336 intersperse a ~(b :| bs) = b :| case bs of
337 [] -> []
338 _ -> a : List.intersperse a bs
339
340 -- | @'iterate' f x@ produces the infinite sequence
341 -- of repeated applications of @f@ to @x@.
342 --
343 -- > iterate f x = x :| [f x, f (f x), ..]
344 iterate :: (a -> a) -> a -> NonEmpty a
345 iterate f a = a :| List.iterate f (f a)
346
347 -- | @'cycle' xs@ returns the infinite repetition of @xs@:
348 --
349 -- > cycle (1 :| [2,3]) = 1 :| [2,3,1,2,3,...]
350 cycle :: NonEmpty a -> NonEmpty a
351 cycle = fromList . List.cycle . toList
352
353 -- | 'reverse' a finite NonEmpty stream.
354 reverse :: NonEmpty a -> NonEmpty a
355 reverse = lift List.reverse
356
357 -- | @'repeat' x@ returns a constant stream, where all elements are
358 -- equal to @x@.
359 repeat :: a -> NonEmpty a
360 repeat a = a :| List.repeat a
361
362 -- | @'take' n xs@ returns the first @n@ elements of @xs@.
363 take :: Int -> NonEmpty a -> [a]
364 take n = List.take n . toList
365
366 -- | @'drop' n xs@ drops the first @n@ elements off the front of
367 -- the sequence @xs@.
368 drop :: Int -> NonEmpty a -> [a]
369 drop n = List.drop n . toList
370
371 -- | @'splitAt' n xs@ returns a pair consisting of the prefix of @xs@
372 -- of length @n@ and the remaining stream immediately following this prefix.
373 --
374 -- > 'splitAt' n xs == ('take' n xs, 'drop' n xs)
375 -- > xs == ys ++ zs where (ys, zs) = 'splitAt' n xs
376 splitAt :: Int -> NonEmpty a -> ([a],[a])
377 splitAt n = List.splitAt n . toList
378
379 -- | @'takeWhile' p xs@ returns the longest prefix of the stream
380 -- @xs@ for which the predicate @p@ holds.
381 takeWhile :: (a -> Bool) -> NonEmpty a -> [a]
382 takeWhile p = List.takeWhile p . toList
383
384 -- | @'dropWhile' p xs@ returns the suffix remaining after
385 -- @'takeWhile' p xs@.
386 dropWhile :: (a -> Bool) -> NonEmpty a -> [a]
387 dropWhile p = List.dropWhile p . toList
388
389 -- | @'span' p xs@ returns the longest prefix of @xs@ that satisfies
390 -- @p@, together with the remainder of the stream.
391 --
392 -- > 'span' p xs == ('takeWhile' p xs, 'dropWhile' p xs)
393 -- > xs == ys ++ zs where (ys, zs) = 'span' p xs
394 span :: (a -> Bool) -> NonEmpty a -> ([a], [a])
395 span p = List.span p . toList
396
397 -- | The @'break' p@ function is equivalent to @'span' (not . p)@.
398 break :: (a -> Bool) -> NonEmpty a -> ([a], [a])
399 break p = span (not . p)
400
401 -- | @'filter' p xs@ removes any elements from @xs@ that do not satisfy @p@.
402 filter :: (a -> Bool) -> NonEmpty a -> [a]
403 filter p = List.filter p . toList
404
405 -- | The 'partition' function takes a predicate @p@ and a stream
406 -- @xs@, and returns a pair of lists. The first list corresponds to the
407 -- elements of @xs@ for which @p@ holds; the second corresponds to the
408 -- elements of @xs@ for which @p@ does not hold.
409 --
410 -- > 'partition' p xs = ('filter' p xs, 'filter' (not . p) xs)
411 partition :: (a -> Bool) -> NonEmpty a -> ([a], [a])
412 partition p = List.partition p . toList
413
414 -- | The 'group' function takes a stream and returns a list of
415 -- streams such that flattening the resulting list is equal to the
416 -- argument. Moreover, each stream in the resulting list
417 -- contains only equal elements. For example, in list notation:
418 --
419 -- > 'group' $ 'cycle' "Mississippi"
420 -- > = "M" : "i" : "ss" : "i" : "ss" : "i" : "pp" : "i" : "M" : "i" : ...
421 group :: (Foldable f, Eq a) => f a -> [NonEmpty a]
422 group = groupBy (==)
423
424 -- | 'groupBy' operates like 'group', but uses the provided equality
425 -- predicate instead of `==`.
426 groupBy :: Foldable f => (a -> a -> Bool) -> f a -> [NonEmpty a]
427 groupBy eq0 = go eq0 . Foldable.toList
428 where
429 go _ [] = []
430 go eq (x : xs) = (x :| ys) : groupBy eq zs
431 where (ys, zs) = List.span (eq x) xs
432
433 -- | 'groupWith' operates like 'group', but uses the provided projection when
434 -- comparing for equality
435 groupWith :: (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a]
436 groupWith f = groupBy ((==) `on` f)
437
438 -- | 'groupAllWith' operates like 'groupWith', but sorts the list
439 -- first so that each equivalence class has, at most, one list in the
440 -- output
441 groupAllWith :: (Ord b) => (a -> b) -> [a] -> [NonEmpty a]
442 groupAllWith f = groupWith f . List.sortBy (compare `on` f)
443
444 -- | 'group1' operates like 'group', but uses the knowledge that its
445 -- input is non-empty to produce guaranteed non-empty output.
446 group1 :: Eq a => NonEmpty a -> NonEmpty (NonEmpty a)
447 group1 = groupBy1 (==)
448
449 -- | 'groupBy1' is to 'group1' as 'groupBy' is to 'group'.
450 groupBy1 :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
451 groupBy1 eq (x :| xs) = (x :| ys) :| groupBy eq zs
452 where (ys, zs) = List.span (eq x) xs
453
454 -- | 'groupWith1' is to 'group1' as 'groupWith' is to 'group'
455 groupWith1 :: (Eq b) => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
456 groupWith1 f = groupBy1 ((==) `on` f)
457
458 -- | 'groupAllWith1' is to 'groupWith1' as 'groupAllWith' is to 'groupWith'
459 groupAllWith1 :: (Ord b) => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
460 groupAllWith1 f = groupWith1 f . sortWith f
461
462 -- | The 'isPrefix' function returns @True@ if the first argument is
463 -- a prefix of the second.
464 isPrefixOf :: Eq a => [a] -> NonEmpty a -> Bool
465 isPrefixOf [] _ = True
466 isPrefixOf (y:ys) (x :| xs) = (y == x) && List.isPrefixOf ys xs
467
468 -- | @xs !! n@ returns the element of the stream @xs@ at index
469 -- @n@. Note that the head of the stream has index 0.
470 --
471 -- /Beware/: a negative or out-of-bounds index will cause an error.
472 (!!) :: NonEmpty a -> Int -> a
473 (!!) ~(x :| xs) n
474 | n == 0 = x
475 | n > 0 = xs List.!! (n - 1)
476 | otherwise = errorWithoutStackTrace "NonEmpty.!! negative argument"
477
478 -- | The 'zip' function takes two streams and returns a stream of
479 -- corresponding pairs.
480 zip :: NonEmpty a -> NonEmpty b -> NonEmpty (a,b)
481 zip ~(x :| xs) ~(y :| ys) = (x, y) :| List.zip xs ys
482
483 -- | The 'zipWith' function generalizes 'zip'. Rather than tupling
484 -- the elements, the elements are combined using the function
485 -- passed as the first argument.
486 zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
487 zipWith f ~(x :| xs) ~(y :| ys) = f x y :| List.zipWith f xs ys
488
489 -- | The 'unzip' function is the inverse of the 'zip' function.
490 unzip :: Functor f => f (a,b) -> (f a, f b)
491 unzip xs = (fst <$> xs, snd <$> xs)
492
493 -- | The 'nub' function removes duplicate elements from a list. In
494 -- particular, it keeps only the first occurence of each element.
495 -- (The name 'nub' means \'essence\'.)
496 -- It is a special case of 'nubBy', which allows the programmer to
497 -- supply their own inequality test.
498 nub :: Eq a => NonEmpty a -> NonEmpty a
499 nub = nubBy (==)
500
501 -- | The 'nubBy' function behaves just like 'nub', except it uses a
502 -- user-supplied equality predicate instead of the overloaded '=='
503 -- function.
504 nubBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
505 nubBy eq (a :| as) = a :| List.nubBy eq (List.filter (\b -> not (eq a b)) as)
506
507 -- | 'transpose' for 'NonEmpty', behaves the same as 'Data.List.transpose'
508 -- The rows/columns need not be the same length, in which case
509 -- > transpose . transpose /= id
510 transpose :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
511 transpose = fmap fromList
512 . fromList . List.transpose . toList
513 . fmap toList
514
515 -- | 'sortBy' for 'NonEmpty', behaves the same as 'Data.List.sortBy'
516 sortBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
517 sortBy f = lift (List.sortBy f)
518
519 -- | 'sortWith' for 'NonEmpty', behaves the same as:
520 --
521 -- > sortBy . comparing
522 sortWith :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
523 sortWith = sortBy . comparing