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

Modulerebase-1.21.2Haskell2010

Rebase.Data.Sequence

  • 3 types
  • 76 values
  • Packagerebase-1.21.2
  • Exports82
  • LanguageHaskell2010
  • LicenceMIT
  • SourceInternal.hs
valueunzip :: 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.

newtypenewtype Seq a
#

General-purpose finite sequences.

Instances49Monad, 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
  • Alt SeqDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Apply SeqDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind SeqDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Extend SeqDefined in semigroupoids-6.0.1 · Data.Functor.Extend
  • Plus SeqDefined in semigroupoids-6.0.1 · Data.Functor.Plus
  • Invariant SeqDefined in invariant-0.6.4 · Data.Functor.Invariant

    from the containers package

  • Adjustable SeqDefined in keys-3.12.3 · Data.Key
  • FoldableWithKey SeqDefined in keys-3.12.3 · Data.Key
  • Indexable SeqDefined in keys-3.12.3 · Data.Key
  • Keyed SeqDefined in keys-3.12.3 · Data.Key
  • Lookup SeqDefined in keys-3.12.3 · Data.Key
  • TraversableWithKey SeqDefined in keys-3.12.3 · Data.Key
  • Zip SeqDefined in keys-3.12.3 · Data.Key
  • ZipWithKey SeqDefined in keys-3.12.3 · Data.Key
  • Pointed SeqDefined in pointed-5.0.4 · Data.Pointed
  • FoldableWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndex
  • FunctorWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndex

    The position in the Seq is available as the index.

  • TraversableWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndex
  • 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
  • Default (Seq a)Defined in data-default-0.8.0.1 · Data.Default.Internal
  • type Item (Seq a) = aDefined in containers-0.7 · Data.Sequence.Internal
  • type Key Seq = IntDefined in keys-3.12.3 · Data.Key
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.

valueempty :: Seq a
#

O(1) . The empty sequence.

valuenull :: Seq a -> Bool
#

O(1) . Is this the empty sequence?

valuelength :: Seq a -> Int
#

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

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

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

valuereverse :: Seq a -> Seq a
#

O(n) . The reverse of a sequence.

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.

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.

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.

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

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.

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.

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

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

Instances15Functor, Foldable, Traversable, Invariant, Pointed, Generic1, …
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

Instances15Functor, Foldable, Traversable, Invariant, Pointed, Generic1, …
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]
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)

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.

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.

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

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.

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

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

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.

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

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.

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.

valueviewl :: Seq a -> ViewL a
#

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

valueviewr :: Seq a -> ViewR a
#

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

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.

valuesortOn :: Ord b => (a -> b) -> Seq a -> Seq a
#

O(n \log n) . sortOn sorts the specified Seq by comparing the results of a key function applied to each element. sortOn f is equivalent to sortBy (compare `Data.Function.on` f), but has the performance advantage of only evaluating f once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.

An example of using sortOn might be to sort a Seq of strings according to their length:

sortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]

If, instead, sortBy had been used, length would be evaluated on every comparison, giving O(n \log n) evaluations, rather than O(n) .

If f is very cheap (for example a record selector, or fst), sortBy (compare `Data.Function.on` f) will be faster than sortOn f.

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.

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.

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

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

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

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

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

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

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

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.

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.

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

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.

valueunstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a
#

O(n \log n) . unstableSortOn sorts the specified Seq by comparing the results of a key function applied to each element. unstableSortOn f is equivalent to unstableSortBy (compare `Data.Function.on` f), but has the performance advantage of only evaluating f once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.

An example of using unstableSortOn might be to sort a Seq of strings according to their length:

unstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]

If, instead, unstableSortBy had been used, length would be evaluated on every comparison, giving O(n \log n) evaluations, rather than O(n) .

If f is very cheap (for example a record selector, or fst), unstableSortBy (compare `Data.Function.on` f) will be faster than unstableSortOn f.