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

Modulerio-0.1.22.0Haskell2010

RIO.Seq

Seq. Import as:

import qualified RIO.Seq as Seq
  • 3 types
  • 72 values
  • Packagerio-0.1.22.0
  • Exports78
  • LanguageHaskell2010
  • LicenceMIT
  • SourceSeq.hs
newtypenewtype Seq a
#

General-purpose finite sequences.

Instances29Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
  • Monad SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Functor SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadFix SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Applicative SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Foldable SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Traversable SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Alternative SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadPlus SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadZip SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Eq1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Ord1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Read1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Show1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • UnzipWith SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Hashable1 SeqDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Lift a => Lift (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • IsList (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Eq a => Eq (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Data a => Data (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Ord a => Ord (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Read a => Read (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Show a => Show (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • a ~ Char => IsString (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Semigroup (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Monoid (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • NFData a => NFData (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Binary e => Binary (Seq e)Defined in binary-0.8.9.3 · Data.Binary.Class
  • Hashable v => Hashable (Seq v)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • type Item (Seq a) = aDefined in containers-0.7 · Data.Sequence.Internal
patternpattern Empty :: Seq a
#

A bidirectional pattern synonym matching an empty sequence.

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

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

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

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

Construction

8 declarations
valueempty :: Seq a
#

O(1) . The empty sequence.

value(<|) :: 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.

value(|>) :: 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.

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

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

valuefromList :: [a] -> Seq a
#

O(n) . Create a sequence from a finite list of elements. There is a function toList in the opposite direction for all instances of the Foldable class, including Seq.

valuefromFunction :: Int -> (Int -> a) -> Seq a
#

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

valuefromArray :: 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

valuereplicate :: Int -> a -> Seq a
#

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

valuecycleTaking :: 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.

Property
cycleTaking k = fromList . take k . cycle . toList

Iterative construction

valueiterateN :: Int -> (a -> a) -> a -> Seq a
#

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

iterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
valueunfoldr :: (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.

Deconstruction

0 declarations

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

Queries

valuenull :: Seq a -> Bool
#

O(1) . Is this the empty sequence?

valuelength :: Seq a -> Int
#

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

Views

datadata ViewL a
#

View of the left end of a sequence.

Constructors

  • EmptyL

    empty sequence

  • a :< Seq ainfixr 5

    leftmost element and the rest of the sequence

Instances13Functor, Foldable, Traversable, Generic1, Lift, Eq, …
valueviewl :: Seq a -> ViewL a
#

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

datadata ViewR a
#

View of the right end of a sequence.

Constructors

  • EmptyR

    empty sequence

  • Seq a :> ainfixl 5

    the sequence minus the rightmost element, and the rightmost element

Instances13Functor, Foldable, Traversable, Generic1, Lift, Eq, …
valueviewr :: Seq a -> ViewR a
#

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

Scans

4 declarations
valuescanl :: (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, ...]
valuescanl1 :: (a -> a -> a) -> Seq a -> Seq a
#

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

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

Sublists

3 declarations
valuetails :: 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.

valueinits :: 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.

valuechunksOf :: 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)

Sequential searches

valuetakeWhileL :: (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.

valuespanl :: (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.

valuespanr :: (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.

valuebreakl :: (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).

valuepartition :: (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.

valuefilter :: (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.

Sorting

4 declarations
valuesort :: Ord a => Seq a -> Seq a
#

O(n \log n) . sort sorts the specified Seq by the natural ordering of its elements. The sort is stable. If stability is not required, unstableSort can be slightly faster.

valuesortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
#

O(n \log n) . sortBy sorts the specified Seq according to the specified comparator. The sort is stable. If stability is not required, unstableSortBy can be slightly faster.

valueunstableSort :: Ord a => Seq a -> Seq a
#

O(n \log n) . unstableSort sorts the specified Seq by the natural ordering of its elements, but the sort is not stable. This algorithm is frequently faster and uses less memory than sort.

Indexing

11 declarations
valuelookup :: 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.

Property
0 <= i < length xs ==> lookup i xs == Just (toList xs !! i)
Property
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 Data.Map.Lazy.Map m at key k, you could use

case lookup 5 xs of
  Nothing -> m
  Just x -> insert k x m
value(!?) :: Seq a -> Int -> Maybe a
#

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

valueindex :: 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.

Property
xs `index` i = toList xs !! i

Caution: 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 (!?).

valueadjust :: (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.

valueadjust' :: (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
valueupdate :: 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.

valuetake :: 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.

valuedrop :: 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.

valueinsertAt :: 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]
Property
insertAt i x xs = take i xs >< singleton x >< drop i xs
valuedeleteAt :: 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]

Indexing with predicates

These functions perform sequential searches from the left or right ends of the sequence elements.

Folds

3 declarations

General folds are available via the Foldable instance of Seq.

Transformations

4 declarations
valuemapWithIndex :: (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.

valuereverse :: Seq a -> Seq a
#

O(n) . The reverse of a sequence.

valueintersperse :: 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]

Zips

valuezip :: 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.

valuezipWith :: (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.

valuezip3 :: 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.

valuezipWith3 :: (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.

valuezip4 :: 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.

valuezipWith4
  1. :: a -> b -> c -> d -> e
  2. -> Seq a
  3. -> Seq b
  4. -> Seq c
  5. -> Seq d
  6. -> 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.