base-compat-batteries-0.13.1: base-compat with extra batteries

Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.List.NonEmpty.Compat

Contents

- The type of non-empty streams
- Non-empty stream transformations
- Basic functions
- Building streams
- Extracting sublists
- Sublist predicates
- "Set" operations
- Indexing streams
- Zipping and unzipping streams
- Converting to and from a list

Synopsis

- data NonEmpty a = a :| [a]
- map :: (a -> b) -> NonEmpty a -> NonEmpty b
- intersperse :: a -> NonEmpty a -> NonEmpty a
- scanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
- scanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
- scanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
- scanr1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
- transpose :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
- sortBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
- sortWith :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
- length :: NonEmpty a -> Int
- head :: NonEmpty a -> a
- tail :: NonEmpty a -> [a]
- last :: NonEmpty a -> a
- init :: NonEmpty a -> [a]
- singleton :: a -> NonEmpty a
- (<|) :: a -> NonEmpty a -> NonEmpty a
- cons :: a -> NonEmpty a -> NonEmpty a
- uncons :: NonEmpty a -> (a, Maybe (NonEmpty a))
- unfoldr :: (a -> (b, Maybe a)) -> a -> NonEmpty b
- sort :: Ord a => NonEmpty a -> NonEmpty a
- reverse :: NonEmpty a -> NonEmpty a
- inits :: Foldable f => f a -> NonEmpty [a]
- inits1 :: NonEmpty a -> NonEmpty (NonEmpty a)
- tails :: Foldable f => f a -> NonEmpty [a]
- tails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
- iterate :: (a -> a) -> a -> NonEmpty a
- repeat :: a -> NonEmpty a
- cycle :: NonEmpty a -> NonEmpty a
- unfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b
- insert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a
- some1 :: Alternative f => f a -> f (NonEmpty a)
- take :: Int -> NonEmpty a -> [a]
- drop :: Int -> NonEmpty a -> [a]
- splitAt :: Int -> NonEmpty a -> ([a], [a])
- takeWhile :: (a -> Bool) -> NonEmpty a -> [a]
- dropWhile :: (a -> Bool) -> NonEmpty a -> [a]
- span :: (a -> Bool) -> NonEmpty a -> ([a], [a])
- break :: (a -> Bool) -> NonEmpty a -> ([a], [a])
- filter :: (a -> Bool) -> NonEmpty a -> [a]
- partition :: (a -> Bool) -> NonEmpty a -> ([a], [a])
- group :: (Foldable f, Eq a) => f a -> [NonEmpty a]
- groupBy :: Foldable f => (a -> a -> Bool) -> f a -> [NonEmpty a]
- groupWith :: (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a]
- groupAllWith :: Ord b => (a -> b) -> [a] -> [NonEmpty a]
- group1 :: Eq a => NonEmpty a -> NonEmpty (NonEmpty a)
- groupBy1 :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
- groupWith1 :: Eq b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
- groupAllWith1 :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
- isPrefixOf :: Eq a => [a] -> NonEmpty a -> Bool
- nub :: Eq a => NonEmpty a -> NonEmpty a
- nubBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
- (!!) :: HasCallStack => NonEmpty a -> Int -> a
- zip :: NonEmpty a -> NonEmpty b -> NonEmpty (a, b)
- zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
- unzip :: Functor f => f (a, b) -> (f a, f b)
- fromList :: HasCallStack => [a] -> NonEmpty a
- toList :: NonEmpty a -> [a]
- nonEmpty :: [a] -> Maybe (NonEmpty a)
- xor :: NonEmpty Bool -> Bool

# The type of non-empty streams

data NonEmpty a Source #

Non-empty (and non-strict) list type.

Since: base-4.9.0.0

Constructors

a :| [a] infixr 5 |

#### Instances

Instances details

Foldable NonEmpty | Since: base-4.9.0.0

Instance details

Defined in Data.Foldable

Methods

fold :: Monoid m => NonEmpty m -> m Source #

foldMap :: Monoid m => (a -> m) -> NonEmpty a -> m Source #

foldMap' :: Monoid m => (a -> m) -> NonEmpty a -> m Source #

foldr :: (a -> b -> b) -> b -> NonEmpty a -> b Source #

foldr' :: (a -> b -> b) -> b -> NonEmpty a -> b Source #

foldl :: (b -> a -> b) -> b -> NonEmpty a -> b Source #

foldl' :: (b -> a -> b) -> b -> NonEmpty a -> b Source #

foldr1 :: (a -> a -> a) -> NonEmpty a -> a Source #

foldl1 :: (a -> a -> a) -> NonEmpty a -> a Source #

toList :: NonEmpty a -> [a] Source #

null :: NonEmpty a -> Bool Source #

length :: NonEmpty a -> Int Source #

elem :: Eq a => a -> NonEmpty a -> Bool Source #

maximum :: Ord a => NonEmpty a -> a Source #

minimum :: Ord a => NonEmpty a -> a Source #

sum :: Num a => NonEmpty a -> a Source #

product :: Num a => NonEmpty a -> a Source #

Foldable1 NonEmpty | Since: base-4.18.0.0

Instance details

Defined in Data.Foldable1

Methods

fold1 :: Semigroup m => NonEmpty m -> m Source #

foldMap1 :: Semigroup m => (a -> m) -> NonEmpty a -> m Source #

foldMap1' :: Semigroup m => (a -> m) -> NonEmpty a -> m Source #

toNonEmpty :: NonEmpty a -> NonEmpty a Source #

maximum :: Ord a => NonEmpty a -> a Source #

minimum :: Ord a => NonEmpty a -> a Source #

head :: NonEmpty a -> a Source #

last :: NonEmpty a -> a Source #

foldrMap1 :: (a -> b) -> (a -> b -> b) -> NonEmpty a -> b Source #

foldlMap1' :: (a -> b) -> (b -> a -> b) -> NonEmpty a -> b Source #

foldlMap1 :: (a -> b) -> (b -> a -> b) -> NonEmpty a -> b Source #

foldrMap1' :: (a -> b) -> (a -> b -> b) -> NonEmpty a -> b Source #

Traversable NonEmpty | Since: base-4.9.0.0
Instance details Defined in Data.Traversable Methods traverse :: Applicative f => (a -> f b) -> NonEmpty a -> f (NonEmpty b) Source # sequenceA :: Applicative f => NonEmpty (f a) -> f (NonEmpty a) Source # mapM :: Monad m => (a -> m b) -> NonEmpty a -> m (NonEmpty b) Source # sequence :: Monad m => NonEmpty (m a) -> m (NonEmpty a) Source #
Applicative NonEmpty | Since: base-4.9.0.0
Instance details Defined in GHC.Base Methods pure :: a -> NonEmpty a Source # (<*>) :: NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b Source # liftA2 :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c Source # (*>) :: NonEmpty a -> NonEmpty b -> NonEmpty b Source # (<*) :: NonEmpty a -> NonEmpty b -> NonEmpty a Source #
Functor NonEmpty | Since: base-4.9.0.0
Instance details Defined in GHC.Base Methods fmap :: (a -> b) -> NonEmpty a -> NonEmpty b Source # (<$) :: a -> NonEmpty b -> NonEmpty a Source #
Monad NonEmpty | Since: base-4.9.0.0
Instance details Defined in GHC.Base Methods (>>=) :: NonEmpty a -> (a -> NonEmpty b) -> NonEmpty b Source # (>>) :: NonEmpty a -> NonEmpty b -> NonEmpty b Source # return :: a -> NonEmpty a Source #
Semigroup (NonEmpty a) | Since: base-4.9.0.0
Instance details Defined in GHC.Base Methods (<>) :: NonEmpty a -> NonEmpty a -> NonEmpty a Source # sconcat :: NonEmpty (NonEmpty a) -> NonEmpty a Source # stimes :: Integral b => b -> NonEmpty a -> NonEmpty a Source #
Read a => Read (NonEmpty a) | Since: base-4.11.0.0
Instance details Defined in GHC.Read Methods readsPrec :: Int -> ReadS (NonEmpty a) Source # readList :: ReadS [NonEmpty a] Source # readPrec :: ReadPrec (NonEmpty a) Source # readListPrec :: ReadPrec [NonEmpty a] Source #
Show a => Show (NonEmpty a) | Since: base-4.11.0.0
Instance details Defined in GHC.Show Methods showsPrec :: Int -> NonEmpty a -> ShowS Source # show :: NonEmpty a -> String Source # showList :: [NonEmpty a] -> ShowS Source #
Eq a => Eq (NonEmpty a) | Since: base-4.9.0.0
Instance details Defined in GHC.Base Methods (==) :: NonEmpty a -> NonEmpty a -> Bool Source # (/=) :: NonEmpty a -> NonEmpty a -> Bool Source #
Ord a => Ord (NonEmpty a) | Since: base-4.9.0.0

Instance details

Defined in GHC.Base

Methods

compare :: NonEmpty a -> NonEmpty a -> Ordering Source #

(<) :: NonEmpty a -> NonEmpty a -> Bool Source #

(<=) :: NonEmpty a -> NonEmpty a -> Bool Source #

(>) :: NonEmpty a -> NonEmpty a -> Bool Source #

(>=) :: NonEmpty a -> NonEmpty a -> Bool Source #

max :: NonEmpty a -> NonEmpty a -> NonEmpty a Source #

min :: NonEmpty a -> NonEmpty a -> NonEmpty a Source #

# Non-empty stream transformations

map :: (a -> b) -> NonEmpty a -> NonEmpty b Source #

Map a function over a `NonEmpty` stream.

intersperse :: a -> NonEmpty a -> NonEmpty a Source #

'intersperse x xs' alternates elements of the list with copies of `x`.

    intersperse 0 (1 :| [2,3]) == 1 :| [0,2,0,3]

scanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b Source #

`scanl` is similar to `foldl`, but returns a stream of successive reduced values from the left:

    scanl f z [x1, x2, ...] == z :| [z `f` x1, (z `f` x1) `f` x2, ...]

Note that

    last (scanl f z xs) == foldl f z xs.

scanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b Source #

`scanr` is the right-to-left dual of `scanl`. Note that

    head (scanr f z xs) == foldr f z xs.

scanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a Source #

`scanl1` is a variant of `scanl` that has no starting value argument:

    scanl1 f [x1, x2, ...] == x1 :| [x1 `f` x2, x1 `f` (x2 `f` x3), ...]

scanr1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a Source #

`scanr1` is a variant of `scanr` that has no starting value argument.

transpose :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a) Source #

`transpose` for `NonEmpty`, behaves the same as `transpose` The rows/columns need not be the same length, in which case > transpose . transpose /= id

sortBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a Source #

`sortBy` for `NonEmpty`, behaves the same as `sortBy`

sortWith :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a Source #

`sortWith` for `NonEmpty`, behaves the same as:

    sortBy . comparing

# Basic functions

length :: NonEmpty a -> Int Source #

Number of elements in `NonEmpty` list.

head :: NonEmpty a -> a Source #

Extract the first element of the stream.

tail :: NonEmpty a -> [a] Source #

Extract the possibly-empty tail of the stream.

last :: NonEmpty a -> a Source #

Extract the last element of the stream.

init :: NonEmpty a -> [a] Source #

Extract everything except the last element of the stream.

singleton :: a -> NonEmpty a Source #

Construct a `NonEmpty` list from a single element.

Since: base-4.15

(<|) :: a -> NonEmpty a -> NonEmpty a infixr 5 Source #

Prepend an element to the stream.

cons :: a -> NonEmpty a -> NonEmpty a Source #

Synonym for `<|`.

uncons :: NonEmpty a -> (a, Maybe (NonEmpty a)) Source #

`uncons` produces the first element of the stream, and a stream of the remaining elements, if any.

unfoldr :: (a -> (b, Maybe a)) -> a -> NonEmpty b Source #

The `unfoldr` function is analogous to Data.List's `unfoldr` operation.

sort :: Ord a => NonEmpty a -> NonEmpty a Source #

Sort a stream.

reverse :: NonEmpty a -> NonEmpty a Source #

`reverse` a finite NonEmpty stream.

inits :: Foldable f => f a -> NonEmpty [a] Source #

The `inits` function takes a stream `xs` and returns all the finite prefixes of `xs`, starting with the shortest. The result is `NonEmpty` because the result always contains the empty list as the first element.

    inits [1,2,3] == [] :| [[1], [1,2], [1,2,3]]
    inits [1] == [] :| [[1]]
    inits [] == [] :| []

inits1 :: NonEmpty a -> NonEmpty (NonEmpty a) Source #

The `inits1` function takes a `NonEmpty` stream `xs` and returns all the `NonEmpty` finite prefixes of `xs`, starting with the shortest.

    inits1 (1 :| [2,3]) == (1 :| []) :| [1 :| [2], 1 :| [2,3]]
    inits1 (1 :| []) == (1 :| []) :| []

Since: base-4.18

tails :: Foldable f => f a -> NonEmpty [a] Source #

The `tails` function takes a stream `xs` and returns all the suffixes of `xs`, starting with the longest. The result is `NonEmpty` because the result always contains the empty list as the last element.

    tails [1,2,3] == [1,2,3] :| [[2,3], [3], []]
    tails [1] == [1] :| [[]]
    tails [] == [] :| []

tails1 :: NonEmpty a -> NonEmpty (NonEmpty a) Source #

The `tails1` function takes a `NonEmpty` stream `xs` and returns all the non-empty suffixes of `xs`, starting with the longest.

    tails1 (1 :| [2,3]) == (1 :| [2,3]) :| [2 :| [3], 3 :| []]
    tails1 (1 :| []) == (1 :| []) :| []

Since: base-4.18

# Building streams

iterate :: (a -> a) -> a -> NonEmpty a Source #

``iterate` f x` produces the infinite sequence of repeated applications of `f` to `x`.

    iterate f x = x :| [f x, f (f x), ..]

repeat :: a -> NonEmpty a Source #

``repeat` x` returns a constant stream, where all elements are equal to `x`.

cycle :: NonEmpty a -> NonEmpty a Source #

``cycle` xs` returns the infinite repetition of `xs`:

    cycle (1 :| [2,3]) = 1 :| [2,3,1,2,3,...]

unfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b Source #

`unfold` produces a new stream by repeatedly applying the unfolding function to the seed value to produce an element of type `b` and a new seed value. When the unfolding function returns `Nothing` instead of a new seed value, the stream ends.

insert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a Source #

``insert` x xs` inserts `x` into the last position in `xs` where it is still less than or equal to the next element. In particular, if the list is sorted beforehand, the result will also be sorted.

some1 :: Alternative f => f a -> f (NonEmpty a) Source #

``some1` x` sequences `x` one or more times.

# Extracting sublists

take :: Int -> NonEmpty a -> [a] Source #

``take` n xs` returns the first `n` elements of `xs`.

drop :: Int -> NonEmpty a -> [a] Source #

``drop` n xs` drops the first `n` elements off the front of the sequence `xs`.

splitAt :: Int -> NonEmpty a -> ([a], [a]) Source #

``splitAt` n xs` returns a pair consisting of the prefix of `xs` of length `n` and the remaining stream immediately following this prefix.

    'splitAt' n xs == ('take' n xs, 'drop' n xs)
    xs == ys ++ zs where (ys, zs) = 'splitAt' n xs

takeWhile :: (a -> Bool) -> NonEmpty a -> [a] Source #

``takeWhile` p xs` returns the longest prefix of the stream `xs` for which the predicate `p` holds.

dropWhile :: (a -> Bool) -> NonEmpty a -> [a] Source #

``dropWhile` p xs` returns the suffix remaining after ``takeWhile` p xs`.

span :: (a -> Bool) -> NonEmpty a -> ([a], [a]) Source #

``span` p xs` returns the longest prefix of `xs` that satisfies `p`, together with the remainder of the stream.

    'span' p xs == ('takeWhile' p xs, 'dropWhile' p xs)
    xs == ys ++ zs where (ys, zs) = 'span' p xs

break :: (a -> Bool) -> NonEmpty a -> ([a], [a]) Source #

The ``break` p` function is equivalent to ``span` (not . p)`.

filter :: (a -> Bool) -> NonEmpty a -> [a] Source #

``filter` p xs` removes any elements from `xs` that do not satisfy `p`.

partition :: (a -> Bool) -> NonEmpty a -> ([a], [a]) Source #

The `partition` function takes a predicate `p` and a stream `xs`, and returns a pair of lists. The first list corresponds to the elements of `xs` for which `p` holds; the second corresponds to the elements of `xs` for which `p` does not hold.

    'partition' p xs = ('filter' p xs, 'filter' (not . p) xs)

group :: (Foldable f, Eq a) => f a -> [NonEmpty a] Source #

The `group` function takes a stream and returns a list of streams such that flattening the resulting list is equal to the argument. Moreover, each stream in the resulting list contains only equal elements. For example, in list notation:

    'group' $ 'cycle' "Mississippi"
      = "M" : "i" : "ss" : "i" : "ss" : "i" : "pp" : "i" : "M" : "i" : ...

groupBy :: Foldable f => (a -> a -> Bool) -> f a -> [NonEmpty a] Source #

`groupBy` operates like `group`, but uses the provided equality predicate instead of `==`.

groupWith :: (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a] Source #

`groupWith` operates like `group`, but uses the provided projection when comparing for equality

groupAllWith :: Ord b => (a -> b) -> [a] -> [NonEmpty a] Source #

`groupAllWith` operates like `groupWith`, but sorts the list first so that each equivalence class has, at most, one list in the output

group1 :: Eq a => NonEmpty a -> NonEmpty (NonEmpty a) Source #

`group1` operates like `group`, but uses the knowledge that its input is non-empty to produce guaranteed non-empty output.

groupBy1 :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a) Source #

`groupBy1` is to `group1` as `groupBy` is to `group`.

groupWith1 :: Eq b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a) Source #

`groupWith1` is to `group1` as `groupWith` is to `group`

groupAllWith1 :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a) Source #

`groupAllWith1` is to `groupWith1` as `groupAllWith` is to `groupWith`

# Sublist predicates

isPrefixOf :: Eq a => [a] -> NonEmpty a -> Bool Source #

The `isPrefixOf` function returns `True` if the first argument is a prefix of the second.

# "Set" operations

nub :: Eq a => NonEmpty a -> NonEmpty a Source #

The `nub` function removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element. (The name `nub` means 'essence'.) It is a special case of `nubBy`, which allows the programmer to supply their own inequality test.

nubBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a Source #

The `nubBy` function behaves just like `nub`, except it uses a user-supplied equality predicate instead of the overloaded `==` function.

# Indexing streams

(!!) :: HasCallStack => NonEmpty a -> Int -> a infixl 9 Source #

`xs !! n` returns the element of the stream `xs` at index `n`. Note that the head of the stream has index 0.

Beware: a negative or out-of-bounds index will cause an error.

# Zipping and unzipping streams

zip :: NonEmpty a -> NonEmpty b -> NonEmpty (a, b) Source #

The `zip` function takes two streams and returns a stream of corresponding pairs.

zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c Source #

The `zipWith` function generalizes `zip`. Rather than tupling the elements, the elements are combined using the function passed as the first argument.

unzip :: Functor f => f (a, b) -> (f a, f b) Source #

The `unzip` function is the inverse of the `zip` function.

# Converting to and from a list

fromList :: HasCallStack => [a] -> NonEmpty a Source #

Converts a normal list to a `NonEmpty` stream.

Raises an error if given an empty list.

toList :: NonEmpty a -> [a] Source #

Convert a stream to a normal list efficiently.

nonEmpty :: [a] -> Maybe (NonEmpty a) Source #

`nonEmpty` efficiently turns a normal list into a `NonEmpty` stream, producing `Nothing` if the input is empty.

xor :: NonEmpty Bool -> Bool Source #

Compute n-ary logic exclusive OR operation on `NonEmpty` list.