semigroups-0.6.1: Haskell 98 semigroups

Portabilityportable
Stabilityprovisional
MaintainerEdward Kmett <ekmett@gmail.com>

Data.List.NonEmpty

Contents

Description

A NonEmpty list forms a monad as per list, but always contains at least one element.

Synopsis

The type of streams

non-empty stream transformations

map :: (a -> b) -> NonEmpty a -> NonEmpty bSource

map a function over a NonEmpty stream

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

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 bSource

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 aSource

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 aSource

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

Basic functions

head :: NonEmpty a -> aSource

Extract the first element of the stream

tail :: NonEmpty a -> [a]Source

Extract the possibly empty tail of the stream

last :: NonEmpty a -> aSource

Extract the last element of the stream

init :: NonEmpty a -> [a]Source

Extract everything except the last element of the stream

(<|) :: a -> NonEmpty a -> NonEmpty aSource

cons onto a stream

sort :: Ord a => NonEmpty a -> NonEmpty aSource

Sort a stream

reverse :: NonEmpty a -> NonEmpty aSource

reverse a finite NonEmpty

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

The inits function takes a stream xs and returns all the finite prefixes of xs.

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

The tails function takes a stream xs and returns all the suffixes of xs.

Building streams

iterate :: (a -> a) -> a -> NonEmpty aSource

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 aSource

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

cycle :: NonEmpty a -> NonEmpty aSource

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 bSource

insert :: Foldable f => Ord a => a -> f a -> NonEmpty aSource

insert an item into a NonEmpty

Extracting sublists

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

take n xs returns the first n elements of xs.

Beware: passing a negative integer as the first argument will cause an error.

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

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

Beware: passing a negative integer as the first argument will cause an error.

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.

Beware: passing a negative integer as the first argument will cause an error.

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.

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 streams. The first stream corresponds to the elements of xs for which p holds; the second stream corresponds to the elements of xs for which p does not hold.

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

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

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

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

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

Sublist predicates

isPrefixOf :: Eq a => [a] -> NonEmpty a -> BoolSource

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

Indexing streams

(!!) :: NonEmpty a -> Int -> aSource

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

Beware: passing a negative integer as the first argument 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 list of corresponding pairs.

zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty cSource

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

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

The unzip function is the inverse of the zip function.

Functions on streams of characters

words :: NonEmpty Char -> NonEmpty StringSource

The words function breaks a stream of characters into a stream of words, which were delimited by white space.

unwords :: NonEmpty String -> NonEmpty CharSource

The unwords function is an inverse operation to words. It joins words with separating spaces.

lines :: NonEmpty Char -> NonEmpty StringSource

The lines function breaks a stream of characters into a list of strings at newline characters. The resulting strings do not contain newlines.

unlines :: NonEmpty String -> NonEmpty CharSource

The unlines function is an inverse operation to lines. It joins lines, after appending a terminating newline to each.

Converting to and from a list

fromList :: [a] -> NonEmpty aSource

Converts an non-empty list to a stream.

toList :: NonEmpty a -> [a]Source

Convert a stream to a list efficiently