HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleextra-1.8Haskell2010

Data.List.NonEmpty.Extra

Extra functions for working with NonEmpty lists. The package also exports the existing Data.List.NonEmpty functions.

  • 1 type
  • 86 values
  • Packageextra-1.8
  • Exports87
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceExtra.hs
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", "ss", "i", "ss", "i", "pp", "i"]
valuesortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
#

Sort a NonEmpty on a user-supplied projection of its elements. See sortOn for more detailed information.

Examples
Example1 expression
sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")](1,"Hello") :| [(2,"world"),(4,"!")]
Example1 expression
sortOn length $ "jim" :| ["creed", "pam", "michael", "dwight", "kevin"]"jim" :| ["pam","creed","kevin","dwight","michael"]
Performance notes

This function minimises the projections performed, by materialising the projections in an intermediate list.

For trivial projections, you should prefer using sortBy with comparing, for example:

Example1 expression
sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

Or, for the exact same API as sortOn, you can use `sortBy . comparing`:

Example1 expression
(sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

sortWith is an alias for `sortBy . comparing`.

datadata NonEmpty a
#

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

Constructors

  • a :| [a]infixr 5
Instances27Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
value(!!) :: HasCallStack => NonEmpty a -> Int -> a
#

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.

valuedrop :: Int -> NonEmpty a -> [a]
#

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

valuehead :: NonEmpty a -> a
#

Extract the first element of the stream.

valueinit :: NonEmpty a -> [a]
#

Extract everything except the last element of the stream.

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), ..]
valuelast :: NonEmpty a -> a
#

Extract the last element of the stream.

valuerepeat :: a -> NonEmpty a
#

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

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), ...]
valuescanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
#

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

head (scanr f z xs) == foldr f z 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
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
valuetail :: NonEmpty a -> [a]
#

Extract the possibly-empty tail of the stream.

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

This function will be made monomorphic in base-4.22, consider switching to Data.Functor.unzip

The unzip function is the inverse of the zip function.

valuezipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
#

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

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

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

valuetoList :: NonEmpty a -> [a]
#

Convert a stream to a normal list efficiently.

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

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

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)
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 [] == [] :| []
valueunfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b
#

Deprecated. Use unfoldr

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.

valueappendList :: NonEmpty a -> [a] -> NonEmpty a
#

Attach a list at the end of a NonEmpty.

Example1 expression
appendList (1 :| [2,3]) []1 :| [2,3]
Example1 expression
appendList (1 :| [2,3]) [4,5]1 :| [2,3,4,5]
valueinits1 :: NonEmpty a -> NonEmpty (NonEmpty a)
#

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 :| []) :| []
valueprependList :: [a] -> NonEmpty a -> NonEmpty a
#

Attach a list at the beginning of a NonEmpty.

Example1 expression
prependList [] (1 :| [2,3])1 :| [2,3]
Example1 expression
prependList [negate 1, 0] (1 :| [2, 3])-1 :| [0,1,2,3]
valuetails1 :: NonEmpty a -> NonEmpty (NonEmpty a)
#

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 :| []) :| []
value(|:) :: [a] -> a -> NonEmpty a
#

O(n). Append an element to a list.

[1,2,3] |: 4 |> 5 == 1 :| [2,3,4,5]
value(|>) :: NonEmpty a -> a -> NonEmpty a
#

O(n). Append an element to a non-empty list.

(1 :| [2,3]) |> 4 |> 5 == 1 :| [2,3,4,5]
value(!?) :: NonEmpty a -> Int -> Maybe a
#

A total variant of the list index function (!?).

(2 :| [3,4]) !? 1    == Just 3
(2 :| [3,4]) !? (-1) == Nothing
(1 :| [])    !? 1    == Nothing
valueappendl :: NonEmpty a -> [a] -> NonEmpty a
#

Append a list to a non-empty list.

appendl (1 :| [2,3]) [4,5] == 1 :| [2,3,4,5]
valueappendr :: [a] -> NonEmpty a -> NonEmpty a
#

Append a non-empty list to a list.

appendr [1,2,3] (4 :| [5]) == 1 :| [2,3,4,5]
valuesortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
#

Sort a NonEmpty on a user-supplied projection of its elements. See sortOn for more detailed information.

Examples
Example1 expression
sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")](1,"Hello") :| [(2,"world"),(4,"!")]
Example1 expression
sortOn length $ "jim" :| ["creed", "pam", "michael", "dwight", "kevin"]"jim" :| ["pam","creed","kevin","dwight","michael"]
Performance notes

This function minimises the projections performed, by materialising the projections in an intermediate list.

For trivial projections, you should prefer using sortBy with comparing, for example:

Example1 expression
sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

Or, for the exact same API as sortOn, you can use `sortBy . comparing`:

Example1 expression
(sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]

sortWith is an alias for `sortBy . comparing`.

valueunion :: Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a
#

Return the union of two non-empty lists. Duplicates, and elements of the first list, are removed from the the second list, but if the first list contains duplicates, so will the result.

(1 :| [3, 5, 3]) `union` (4 :| [5, 3, 5, 2]) == 1 :| [3, 5, 3, 4, 2]
valuenubOrd :: Ord a => NonEmpty a -> NonEmpty a
#

nubOrd for NonEmpty. Behaves the same as nubOrd.

Data.List.NonEmpty.Extra.nubOrd (1 :| [2, 3, 3, 4, 1, 2]) == 1 :| [2, 3, 4]
\xs -> Data.List.NonEmpty.Extra.nubOrd xs == Data.List.NonEmpty.Extra.nub xs
valuenubOrdOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
#

nubOrdOn for NonEmpty. Behaves the same as nubOrdOn.

Data.List.NonEmpty.Extra.nubOrdOn Data.List.length ("a" :| ["test","of","this"]) == "a" :| ["test","of"]
valuemaximumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
#

The largest element of a non-empty list with respect to the given comparison function.

valueminimumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
#

The least element of a non-empty list with respect to the given comparison function.

valuecompareLength :: NonEmpty a -> Int -> Ordering
#

Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10.

While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.

Example5 expressions
compareLength ('a' :| []) 1EQcompareLength ('a' :| ['b']) 3LTcompareLength (0 :| [1..]) 100GTcompareLength undefined 0GTcompareLength ('a' :| 'b' : undefined) 1GT