HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulerio-0.1.25.0Haskell2010

RIO.NonEmpty

NonEmpty list. Import as:

import qualified RIO.NonEmpty as NE

This module does not export any partial functions. For those, see RIO.NonEmpty.Partial

Contents
  1. The type of non-empty streams
  2. Non-empty stream transformations
  3. Basic functions
  4. Building streams
  5. Extracting sublists
  6. Sublist predicates
  7. Set-like operations
  8. Zipping and unzipping streams
  9. Converting to and from a list
Synopsis
  • Packagerio-0.1.25.0
  • Exports54
  • LanguageHaskell2010
  • LicenceMIT
  • SourceNonEmpty.hs

The type of non-empty streams

datadata NonEmpty a

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

Constructors

  • a :| [a]infixr 5
Instances34Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …

Non-empty stream transformations

valueintersperse :: a -> NonEmpty a -> NonEmpty a

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

intersperse 0 (1 :| [2,3]) == 1 :| [0,2,0,3]
valuescanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b

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.
valuescanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a

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), ...]

Basic functions

valueinit :: NonEmpty a -> [a]

Extract everything except the last element of the stream.

valueinits :: Foldable f => f a -> NonEmpty [a]

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 [] == [] :| []
valuetails :: Foldable f => f a -> NonEmpty [a]

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 [] == [] :| []

Building streams

valueiterate :: (a -> a) -> a -> NonEmpty a

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

iterate f x = x :| [f x, f (f x), ..]
valueinsert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a

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.

Extracting sublists

valuesplitAt :: Int -> NonEmpty a -> ([a], [a])

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
valuespan :: (a -> Bool) -> NonEmpty a -> ([a], [a])

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
valuepartition :: (a -> Bool) -> NonEmpty a -> ([a], [a])

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)
valuegroup :: (Foldable f, Eq a) => f a -> [NonEmpty a]

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, and consecutive equal elements of the input end up in the same stream of the output list. For example, in list notation:

Example1 expression
group "Mississippi"['M' :| "",'i' :| "",'s' :| "s",'i' :| "",'s' :| "s",'i' :| "",'p' :| "p",'i' :| ""]

Sublist predicates

Set-like operations

valuenub :: Eq a => NonEmpty a -> NonEmpty a

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.

This function knows too little about the elements to be efficient. Its asymptotic complexity is O(n ⋅ d), where d is the number of distinct elements in the list.

If there exists instance Ord a, it's faster to use nubOrd.

Zipping and unzipping streams

Converting to and from a list