Community
Modules 31

Data.Sequence.Internal

WARNING

This module is considered internal.

The Package Versioning Policy does not apply.

The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.

Authors importing this module are expected to track development closely.

Finite sequences (internals)

The Seq a type represents a finite sequence of values of type a.

Implementation

The implementation uses 2-3 finger trees annotated with sizes, as described in section 4.2 of

4 generation diagnostics
  • unattached documentation comment at 1729:9
  • unattached documentation comment at 1889:1
  • unattached documentation comment at 1913:1
  • unattached documentation comment at 4911:1

data FingerTree a

Deep :: Int -> (Digit a) -> (FingerTree (Node a)) -> (Digit a) -> FingerTree a

data Node a

Node2 :: Int -> a -> a -> Node a

Node3 :: Int -> a -> a -> a -> Node a

data Digit a

One :: a -> Digit a

Two :: a -> a -> Digit a

Three :: a -> a -> a -> Digit a

Four :: a -> a -> a -> a -> Digit a

(:<|) :: a -> Seq a -> Seq a

A bidirectional pattern synonym viewing the front of a non-empty sequence.

(:|>) :: Seq a -> a -> Seq a

A bidirectional pattern synonym viewing the rear of a non-empty sequence.

Empty :: Seq a

A bidirectional pattern synonym matching an empty sequence.

newtype Seq a

General-purpose finite sequences.

Seq :: (FingerTree (Elem a)) -> Seq a

foldDigit :: (b -> b -> b) -> (a -> b) -> Digit a -> b

foldNode :: (b -> b -> b) -> (a -> b) -> Node a -> b

foldWithIndexDigit :: Sized a => (b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b

foldWithIndexNode :: Sized a => (m -> m -> m) -> (Int -> a -> m) -> Int -> Node a -> m

Construction

empty :: Seq a

O(1) . The empty sequence.

singleton :: a -> Seq a

O(1) . A singleton sequence.

(<|) :: a -> Seq a -> Seq a

O(1) . Add an element to the left end of a sequence. Mnemonic: a triangle with the single element at the pointy end.

(|>) :: Seq a -> a -> Seq a

O(1) . Add an element to the right end of a sequence. Mnemonic: a triangle with the single element at the pointy end.

(><) :: Seq a -> Seq a -> Seq a

O(\log(\min(n_1,n_2))) . Concatenate two sequences.

fromList :: [a] -> Seq a

O(n) . Create a sequence from a finite list of elements.

fromList . toList = id

For any finite list xs, toList (fromList xs) = xs.

fromFunction :: Int -> (Int -> a) -> Seq a

O(n) . Convert a given sequence length and a function representing that sequence into a sequence.

Calls error if n < 0.

Note: This function is partial.

fromArray :: Ix i => Array i a -> Seq a

O(n) . Create a sequence consisting of the elements of an Array. Note that the resulting sequence elements may be evaluated lazily (as on GHC), so you must force the entire structure to be sure that the original array can be garbage-collected.

Repetition

replicate :: Int -> a -> Seq a

O(\log n) . replicate n x is a sequence consisting of n copies of x.

Calls error if n < 0.

Note: This function is partial.

replicateA :: Applicative f => Int -> f a -> f (Seq a)

replicateA is an Applicative version of replicate, and makes O(\log n) calls to liftA2 and pure.

Calls error if n < 0.

Note: This function is partial.

replicateA n x = sequenceA (replicate n x)

replicateM :: Applicative m => Int -> m a -> m (Seq a)

Synonym for replicateA.

This definition exists for backwards compatibility.

cycleTaking :: Int -> Seq a -> Seq a

O(\log k). cycleTaking k xs forms a sequence of length k by repeatedly concatenating xs with itself. xs may only be empty if k is 0.

cycleTaking k = fromList . take k . cycle . toList

If you wish to concatenate a possibly empty sequence xs with itself precisely k times, use stimes k xs instead of this function.

Calls error if k > 0 and null xs.

Note: This function is partial.

Iterative construction

iterateN :: Int -> (a -> a) -> a -> Seq a

O(n) . Constructs a sequence by repeated application of a function to a seed value.

Calls error if n < 0.

Note: This function is partial.

unfoldr :: (b -> Maybe (a, b)) -> b -> Seq a

Builds a sequence from a seed value. Takes time linear in the number of generated elements. WARNING: If the number of generated elements is infinite, this method will not terminate.

unfoldl :: (b -> Maybe (b, a)) -> b -> Seq a

unfoldl f x is equivalent to reverse (unfoldr (fmap swap . f) x).

Deconstruction

| Additional functions for deconstructing sequences are available via the Foldable instance of Seq.

Queries

null :: Seq a -> Bool

O(1) . Is this the empty sequence?

length :: Seq a -> Int

O(1) . The number of elements in the sequence.

Views

data ViewL a

View of the left end of a sequence.

EmptyL :: ViewL a

empty sequence

(:<) :: a -> Seq a -> ViewL a

Seq a

leftmost element and the rest of the sequence

viewl :: Seq a -> ViewL a

O(1) . Analyse the left end of a sequence.

data ViewR a

View of the right end of a sequence.

EmptyR :: ViewR a

empty sequence

(:>) :: Seq a -> a -> ViewR a

a

the sequence minus the rightmost element, and the rightmost element

viewr :: Seq a -> ViewR a

O(1) . Analyse the right end of a sequence.

List

toList :: Seq a -> [a]

O(n). Convert to a list of elements.

Scans

scanl :: (a -> b -> a) -> a -> Seq b -> Seq a

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

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

scanl1 :: (a -> a -> a) -> Seq a -> Seq a

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

Calls error if the sequence is empty.

Note: This function is partial.

scanl1 f (fromList [x1, x2, ...]) = fromList [x1, x1 `f` x2, ...]

scanr :: (a -> b -> b) -> b -> Seq a -> Seq b

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

scanr1 :: (a -> a -> a) -> Seq a -> Seq a

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

Calls error if the sequence is empty.

Note: This function is partial.

Sublists

tails :: Seq a -> Seq (Seq a)

O(n) . Returns a sequence of all suffixes of this sequence, longest first. For example,

tails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]

Evaluating the i th suffix takes O(\log(\min(i, n-i))) , but evaluating every suffix in the sequence takes O(n) due to sharing.

inits :: Seq a -> Seq (Seq a)

O(n) . Returns a sequence of all prefixes of this sequence, shortest first. For example,

inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]

Evaluating the i th prefix takes O(\log(\min(i, n-i))) , but evaluating every prefix in the sequence takes O(n) due to sharing.

chunksOf :: Int -> Seq a -> Seq (Seq a)

O \Bigl(\bigl(\frac{n}{c}\bigr) \log c\Bigr). chunksOf c xs splits xs into chunks of size c>0. If c does not divide the length of xs evenly, then the last element of the result will be short.

Side note: the given performance bound is missing some messy terms that only really affect edge cases. Performance degrades smoothly from O(1) (for c = n ) to O(n) (for c = 1 ). The true bound is more like O \Bigl( \bigl(\frac{n}{c} - 1\bigr) (\log (c + 1)) + 1 \Bigr)

Calls error if n <= 0 and not (null xs).

Note: This function is partial.

Sequential searches

takeWhileL :: (a -> Bool) -> Seq a -> Seq a

O(i) where i is the prefix length. takeWhileL, applied to a predicate p and a sequence xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.

takeWhileR :: (a -> Bool) -> Seq a -> Seq a

O(i) where i is the suffix length. takeWhileR, applied to a predicate p and a sequence xs, returns the longest suffix (possibly empty) of xs of elements that satisfy p.

takeWhileR p xs is equivalent to reverse (takeWhileL p (reverse xs)).

dropWhileL :: (a -> Bool) -> Seq a -> Seq a

O(i) where i is the prefix length. dropWhileL p xs returns the suffix remaining after takeWhileL p xs.

dropWhileR :: (a -> Bool) -> Seq a -> Seq a

O(i) where i is the suffix length. dropWhileR p xs returns the prefix remaining after takeWhileR p xs.

dropWhileR p xs is equivalent to reverse (dropWhileL p (reverse xs)).

spanl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

O(i) where i is the prefix length. spanl, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest prefix (possibly empty) of xs of elements that satisfy p and the second element is the remainder of the sequence.

spanr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

O(i) where i is the suffix length. spanr, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest suffix (possibly empty) of xs of elements that satisfy p and the second element is the remainder of the sequence.

breakl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

O(i) where i is the breakpoint index. breakl, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest prefix (possibly empty) of xs of elements that do not satisfy p and the second element is the remainder of the sequence.

breakl p is equivalent to spanl (not . p).

breakr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

breakr p is equivalent to spanr (not . p).

partition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

O(n) . The partition function takes a predicate p and a sequence xs and returns sequences of those elements which do and do not satisfy the predicate.

filter :: (a -> Bool) -> Seq a -> Seq a

O(n) . The filter function takes a predicate p and a sequence xs and returns a sequence of those elements which satisfy the predicate.

mapMaybe :: (a -> Maybe b) -> Seq a -> Seq b

O(n) . Map elements and collect the Just results.

Indexing

lookup :: Int -> Seq a -> Maybe a

O(\log(\min(i,n-i))) . The element at the specified position, counting from 0. If the specified position is negative or at least the length of the sequence, lookup returns Nothing.

0 <= i < length xs ==> lookup i xs == Just (toList xs !! i)i < 0 || i >= length xs ==> lookup i xs = Nothing

Unlike index, this can be used to retrieve an element without forcing it. For example, to insert the fifth element of a sequence xs into a Map m at key k, you could use

case lookup 5 xs of
  Nothing -> m
  Just x -> 'Data.Map.Lazy.insert' k x m

(!?) :: Seq a -> Int -> Maybe a

O(\log(\min(i,n-i))) . A flipped, infix version of `lookup`.

index :: Seq a -> Int -> a

O(\log(\min(i,n-i))) . The element at the specified position, counting from 0. The argument should thus be a non-negative integer less than the size of the sequence. If the position is out of range, index fails with an error.

xs `index` i = toList xs !! i

Note: This function is partial. Prefer lookup.

Note: index necessarily delays retrieving the requested element until the result is forced. It can therefore lead to a space leak if the result is stored, unforced, in another structure. To retrieve an element immediately without forcing it, use lookup or !?.

adjust :: (a -> a) -> Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Update the element at the specified position. If the position is out of range, the original sequence is returned. adjust can lead to poor performance and even memory leaks, because it does not force the new value before installing it in the sequence. adjust' should usually be preferred.

adjust' :: forall a. (a -> a) -> Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Update the element at the specified position. If the position is out of range, the original sequence is returned. The new value is forced before it is installed in the sequence.

adjust' f i xs =
 case xs !? i of
   Nothing -> xs
   Just x -> let !x' = f x
             in update i x' xs

update :: Int -> a -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Replace the element at the specified position. If the position is out of range, the original sequence is returned.

take :: Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . The first i elements of a sequence. If i is negative, take i s yields the empty sequence. If the sequence contains fewer than i elements, the whole sequence is returned.

takeR :: Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . The last i elements of a sequence. If i is negative, takeR i s yields the empty sequence. If the sequence contains fewer than i elements, the whole sequence is returned.

drop :: Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Elements of a sequence after the first i. If i is negative, drop i s yields the whole sequence. If the sequence contains fewer than i elements, the empty sequence is returned.

dropR :: Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Elements of a sequence before the last i. If i is negative, dropR i s yields the whole sequence. If the sequence contains fewer than i elements, the empty sequence is returned.

insertAt :: Int -> a -> Seq a -> Seq a

O(\log(\min(i,n-i))) . insertAt i x xs inserts x into xs at the index i, shifting the rest of the sequence over.

insertAt 2 x (fromList [a,b,c,d]) = fromList [a,b,x,c,d]
insertAt 4 x (fromList [a,b,c,d]) = insertAt 10 x (fromList [a,b,c,d])
                                  = fromList [a,b,c,d,x]
insertAt i x xs = take i xs >< singleton x >< drop i xs

deleteAt :: Int -> Seq a -> Seq a

O(\log(\min(i,n-i))) . Delete the element of a sequence at a given index. Return the original sequence if the index is out of range.

deleteAt 2 [a,b,c,d] = [a,b,d]
deleteAt 4 [a,b,c,d] = deleteAt (-1) [a,b,c,d] = [a,b,c,d]

splitAt :: Int -> Seq a -> (Seq a, Seq a)

O(\log(\min(i,n-i))) . Split a sequence at a given position. splitAt i s = (take i s, drop i s).

splitAtR :: Int -> Seq a -> (Seq a, Seq a)

O(\log(\min(i,n-i))) . Split a sequence at a given position, with the position being counted from the last (rightmost) element. splitAtR i s = (dropR i s, takeR i s).

Indexing with predicates

| These functions perform sequential searches from the left or right ends of the sequence, returning indices of matching elements.

elemIndexL :: Eq a => a -> Seq a -> Maybe Int

elemIndexL finds the leftmost index of the specified element, if it is present, and otherwise Nothing.

elemIndicesL :: Eq a => a -> Seq a -> [Int]

elemIndicesL finds the indices of the specified element, from left to right (i.e. in ascending order).

elemIndexR :: Eq a => a -> Seq a -> Maybe Int

elemIndexR finds the rightmost index of the specified element, if it is present, and otherwise Nothing.

elemIndicesR :: Eq a => a -> Seq a -> [Int]

elemIndicesR finds the indices of the specified element, from right to left (i.e. in descending order).

findIndexL :: (a -> Bool) -> Seq a -> Maybe Int

findIndexL p xs finds the index of the leftmost element that satisfies p, if any exist.

findIndicesL :: (a -> Bool) -> Seq a -> [Int]

findIndicesL p finds all indices of elements that satisfy p, in ascending order.

findIndexR :: (a -> Bool) -> Seq a -> Maybe Int

findIndexR p xs finds the index of the rightmost element that satisfies p, if any exist.

findIndicesR :: (a -> Bool) -> Seq a -> [Int]

findIndicesR p finds all indices of elements that satisfy p, in descending order.

Folds

| General folds are available via the Foldable instance of Seq.

foldlWithIndex :: (b -> Int -> a -> b) -> b -> Seq a -> b

foldlWithIndex is a version of foldl that also provides access to the index of each element.

foldrWithIndex :: (Int -> a -> b -> b) -> b -> Seq a -> b

foldrWithIndex is a version of foldr that also provides access to the index of each element.

Transformations

mapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b

A generalization of fmap, mapWithIndex takes a mapping function that also depends on the element's index, and applies it to every element in the sequence.

traverseWithIndex :: Applicative f => (Int -> a -> f b) -> Seq a -> f (Seq b)

traverseWithIndex is a version of traverse that also offers access to the index of each element.

reverse :: Seq a -> Seq a

O(n) . The reverse of a sequence.

intersperse :: a -> Seq a -> Seq a

O(n) . Intersperse an element between the elements of a sequence.

intersperse a empty = empty
intersperse a (singleton x) = singleton x
intersperse a (fromList [x,y]) = fromList [x,a,y]
intersperse a (fromList [x,y,z]) = fromList [x,a,y,a,z]

liftA2Seq :: (a -> b -> c) -> Seq a -> Seq b -> Seq c

Zips and unzips

zip :: Seq a -> Seq b -> Seq (a, b)

O(\min(n_1,n_2)) . zip takes two sequences and returns a sequence of corresponding pairs. If one input is short, excess elements are discarded from the right end of the longer sequence.

zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c

O(\min(n_1,n_2)) . zipWith generalizes zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two sequences to take the sequence of corresponding sums.

zip3 :: Seq a -> Seq b -> Seq c -> Seq (a, b, c)

O(\min(n_1,n_2,n_3)) . zip3 takes three sequences and returns a sequence of triples, analogous to zip.

zipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d

O(\min(n_1,n_2,n_3)) . zipWith3 takes a function which combines three elements, as well as three sequences and returns a sequence of their point-wise combinations, analogous to zipWith.

zip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a, b, c, d)

O(\min(n_1,n_2,n_3,n_4)) . zip4 takes four sequences and returns a sequence of quadruples, analogous to zip.

zipWith4 :: (a -> b -> c -> d -> e) -> Seq a -> Seq b -> Seq c -> Seq d -> Seq e

O(\min(n_1,n_2,n_3,n_4)) . zipWith4 takes a function which combines four elements, as well as four sequences and returns a sequence of their point-wise combinations, analogous to zipWith.

unzip :: Seq (a, b) -> (Seq a, Seq b)

Unzip a sequence of pairs.

unzip ps = ps ``seq`` ('fmap' 'fst' ps) ('fmap' 'snd' ps)

Example:

unzip $ fromList [(1,"a"), (2,"b"), (3,"c")] =
  (fromList [1,2,3], fromList ["a", "b", "c"])

See the note about efficiency at unzipWith.

unzipWith :: (a -> (b, c)) -> Seq a -> (Seq b, Seq c)

O(n) . Unzip a sequence using a function to divide elements.

unzipWith f xs == unzip (fmap f xs)

Efficiency note:

unzipWith produces its two results in lockstep. If you calculate unzipWith f xs and fully force either of the results, then the entire structure of the other one will be built as well. This behavior allows the garbage collector to collect each calculated pair component as soon as it dies, without having to wait for its mate to die. If you do not need this behavior, you may be better off simply calculating the sequence of pairs and using fmap to extract each component sequence.

Instances

91
  • instance MaybeForce (Elem a)
  • instance MaybeForce (Node a)
  • instance MaybeForce (ForceBox a)
  • instance Sized (ForceBox a)
  • instance TH.Lift a => TH.Lift (Seq a)
  • instance Functor Seq
  • instance Foldable Seq
  • instance Traversable Seq
  • instance NFData a => NFData (Seq a)
  • instance NFData1 Seq
  • instance Monad Seq
  • instance MonadFix Seq
  • instance Applicative Seq
  • instance MonadPlus Seq
  • instance Alternative Seq
  • instance Eq a => Eq (Seq a)
  • instance Ord a => Ord (Seq a)
  • instance Show a => Show (Seq a)
  • instance Show1 Seq
  • instance Eq1 Seq
  • instance Ord1 Seq
  • instance Read a => Read (Seq a)
  • instance Read1 Seq
  • instance Monoid (Seq a)

    mempty = empty

  • instance Semigroup.Semigroup (Seq a)

    (<>) = ><

  • instance Data a => Data (Seq a)
  • instance Generic1 FingerTree
  • instance Generic (FingerTree a)
  • instance TH.Lift a => TH.Lift (FingerTree a)
  • instance Sized a => Sized (FingerTree a)
  • instance Foldable FingerTree
  • instance Functor FingerTree
  • instance Traversable FingerTree
  • instance NFData a => NFData (FingerTree a)
  • instance NFData1 FingerTree
  • instance Generic1 Digit
  • instance Generic (Digit a)
  • instance TH.Lift a => TH.Lift (Digit a)
  • instance Foldable Digit
  • instance Functor Digit
  • instance Traversable Digit
  • instance NFData a => NFData (Digit a)
  • instance NFData1 Digit
  • instance Sized a => Sized (Digit a)
  • instance Generic1 Node
  • instance Generic (Node a)
  • instance TH.Lift a => TH.Lift (Node a)
  • instance Foldable Node
  • instance Functor Node
  • instance Traversable Node
  • instance NFData a => NFData (Node a)
  • instance NFData1 Node
  • instance Sized (Node a)
  • instance Generic1 Elem
  • instance Generic (Elem a)
  • instance Sized (Elem a)
  • instance Functor Elem
  • instance Foldable Elem
  • instance Traversable Elem
  • instance NFData a => NFData (Elem a)
  • instance NFData1 Elem
  • instance Eq a => Eq (ViewL a)
  • instance Ord a => Ord (ViewL a)
  • instance Show a => Show (ViewL a)
  • instance Read a => Read (ViewL a)
  • instance Data a => Data (ViewL a)
  • instance Generic1 ViewL
  • instance Generic (ViewL a)
  • instance TH.Lift a => TH.Lift (ViewL a)
  • instance Functor ViewL
  • instance Foldable ViewL
  • instance Traversable ViewL
  • instance Eq a => Eq (ViewR a)
  • instance Ord a => Ord (ViewR a)
  • instance Show a => Show (ViewR a)
  • instance Read a => Read (ViewR a)
  • instance Data a => Data (ViewR a)
  • instance Generic1 ViewR
  • instance Generic (ViewR a)
  • instance TH.Lift a => TH.Lift (ViewR a)
  • instance Functor ViewR
  • instance Foldable ViewR
  • instance Traversable ViewR
  • instance GHC.Exts.IsList (Seq a)
  • instance a ~ Char => IsString (Seq a)
  • instance MonadZip Seq

    mzipWith = zipWith

    munzip = unzip

  • instance UnzipWith Elem
  • instance UnzipWith Node
  • instance UnzipWith Digit
  • instance UnzipWith FingerTree
  • instance UnzipWith Seq
Maintainer
libraries@haskell.org
Portability
portable
Copyright
(c) Ross Paterson 2005 (c) Louis Wasserman 2009 (c) Bertram Felgenhauer, David Feuer, Ross Paterson, and Milan Straka 2014
License
BSD-style
Build record and source files

Community contribution. The service has not verified this documentation. The generator reported 15 diagnostics.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-aa6debbd-web-3

Metadata snapshot: 634f718c0d8cd970c4e2c511d2345d8fd36c6dc32b06343c2b4e914ffccbce7f

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.