Modulerebase-1.21.2Haskell2010
Rebase.Data.Sequence
- 3 types
- 76 values
- Packagerebase-1.21.2
- Exports82
- LanguageHaskell2010
- LicenceMIT
- SourceInternal.hs
General-purpose finite sequences.
Instances49Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad SeqDefined in containers-0.7 · Data.Sequence.InternalFunctor SeqDefined in containers-0.7 · Data.Sequence.InternalMonadFix SeqDefined in containers-0.7 · Data.Sequence.InternalApplicative SeqDefined in containers-0.7 · Data.Sequence.InternalFoldable SeqDefined in containers-0.7 · Data.Sequence.InternalTraversable SeqDefined in containers-0.7 · Data.Sequence.InternalAlternative SeqDefined in containers-0.7 · Data.Sequence.InternalMonadPlus SeqDefined in containers-0.7 · Data.Sequence.InternalMonadZip SeqDefined in containers-0.7 · Data.Sequence.InternalEq1 SeqDefined in containers-0.7 · Data.Sequence.InternalOrd1 SeqDefined in containers-0.7 · Data.Sequence.InternalRead1 SeqDefined in containers-0.7 · Data.Sequence.InternalShow1 SeqDefined in containers-0.7 · Data.Sequence.InternalUnzipWith SeqDefined in containers-0.7 · Data.Sequence.InternalHashable1 SeqDefined in hashable-1.4.7.0 · Data.Hashable.ClassAlt SeqDefined in semigroupoids-6.0.1 · Data.Functor.AltApply SeqDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassBind SeqDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassExtend SeqDefined in semigroupoids-6.0.1 · Data.Functor.ExtendPlus SeqDefined in semigroupoids-6.0.1 · Data.Functor.PlusInvariant SeqDefined in invariant-0.6.4 · Data.Functor.Invariantfrom the
containerspackageAdjustable SeqDefined in keys-3.12.3 · Data.KeyFoldableWithKey SeqDefined in keys-3.12.3 · Data.KeyIndexable SeqDefined in keys-3.12.3 · Data.KeyKeyed SeqDefined in keys-3.12.3 · Data.KeyLookup SeqDefined in keys-3.12.3 · Data.KeyTraversableWithKey SeqDefined in keys-3.12.3 · Data.KeyZip SeqDefined in keys-3.12.3 · Data.KeyZipWithKey SeqDefined in keys-3.12.3 · Data.KeyPointed SeqDefined in pointed-5.0.4 · Data.PointedFoldableWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndexFunctorWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndexThe position in the Seq is available as the index.
TraversableWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndexLift a => Lift (Seq a)Defined in containers-0.7 · Data.Sequence.InternalIsList (Seq a)Defined in containers-0.7 · Data.Sequence.InternalEq a => Eq (Seq a)Defined in containers-0.7 · Data.Sequence.InternalData a => Data (Seq a)Defined in containers-0.7 · Data.Sequence.InternalOrd a => Ord (Seq a)Defined in containers-0.7 · Data.Sequence.InternalRead a => Read (Seq a)Defined in containers-0.7 · Data.Sequence.InternalShow a => Show (Seq a)Defined in containers-0.7 · Data.Sequence.Internala ~ Char => IsString (Seq a)Defined in containers-0.7 · Data.Sequence.InternalSemigroup (Seq a)Defined in containers-0.7 · Data.Sequence.InternalMonoid (Seq a)Defined in containers-0.7 · Data.Sequence.InternalNFData a => NFData (Seq a)Defined in containers-0.7 · Data.Sequence.InternalBinary e => Binary (Seq e)Defined in binary-0.8.9.3 · Data.Binary.ClassHashable v => Hashable (Seq v)Defined in hashable-1.4.7.0 · Data.Hashable.ClassDefault (Seq a)Defined in data-default-0.8.0.1 · Data.Default.Internaltype Item (Seq a) = aDefined in containers-0.7 · Data.Sequence.Internaltype Key Seq = IntDefined in keys-3.12.3 · Data.Key
A bidirectional pattern synonym matching an empty sequence.
A bidirectional pattern synonym viewing the front of a non-empty sequence.
A bidirectional pattern synonym viewing the rear of a non-empty sequence.
O(1) . The empty sequence.
O(1) . Is this the empty sequence?
O(1) . The number of elements in the sequence.
O(\log n) . replicate n x is a sequence consisting of n copies of x.
O(n) . The reverse of a sequence.
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.
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.
O(n) . The filter function takes a predicate p and a sequence
xs and returns a sequence of those elements which satisfy the
predicate.
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 !! iCaution: 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 (!?).
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.
replicateM is the Seq counterpart of
Control.Monad.replicateM.
replicateM n x = sequence (replicate n x)For base >= 4.8.0 and containers >= 0.5.11, replicateM
is a synonym for replicateA.
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.
O(1) . A singleton sequence.
View of the left end of a sequence.
Instances15Functor, Foldable, Traversable, Invariant, Pointed, Generic1, …
Functor ViewLDefined in containers-0.7 · Data.Sequence.InternalFoldable ViewLDefined in containers-0.7 · Data.Sequence.InternalTraversable ViewLDefined in containers-0.7 · Data.Sequence.InternalInvariant ViewLDefined in invariant-0.6.4 · Data.Functor.Invariantfrom the
containerspackagePointed ViewLDefined in pointed-5.0.4 · Data.PointedGeneric1 ViewLDefined in containers-0.7 · Data.Sequence.InternalLift a => Lift (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalEq a => Eq (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalData a => Data (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalOrd a => Ord (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalRead a => Read (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalShow a => Show (ViewL a)Defined in containers-0.7 · Data.Sequence.InternalGeneric (ViewL a)Defined in containers-0.7 · Data.Sequence.Internaltype Rep (ViewL a) = D1 ('MetaDataDefined in containers-0.7 · Data.Sequence.Internal"ViewL"
"Data.Sequence.Internal"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"EmptyL"
'PrefixI 'False) U1 :+: C1 ('MetaCons":<"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Seq a))))type Rep1 ViewL = D1 ('MetaDataDefined in containers-0.7 · Data.Sequence.Internal"ViewL"
"Data.Sequence.Internal"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"EmptyL"
'PrefixI 'False) U1 :+: C1 ('MetaCons":<"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Seq)))
View of the right end of a sequence.
Instances15Functor, Foldable, Traversable, Invariant, Pointed, Generic1, …
Functor ViewRDefined in containers-0.7 · Data.Sequence.InternalFoldable ViewRDefined in containers-0.7 · Data.Sequence.InternalTraversable ViewRDefined in containers-0.7 · Data.Sequence.InternalInvariant ViewRDefined in invariant-0.6.4 · Data.Functor.Invariantfrom the
containerspackagePointed ViewRDefined in pointed-5.0.4 · Data.PointedGeneric1 ViewRDefined in containers-0.7 · Data.Sequence.InternalLift a => Lift (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalEq a => Eq (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalData a => Data (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalOrd a => Ord (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalRead a => Read (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalShow a => Show (ViewR a)Defined in containers-0.7 · Data.Sequence.InternalGeneric (ViewR a)Defined in containers-0.7 · Data.Sequence.Internaltype Rep (ViewR a) = D1 ('MetaDataDefined in containers-0.7 · Data.Sequence.Internal"ViewR"
"Data.Sequence.Internal"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"EmptyR"
'PrefixI 'False) U1 :+: C1 ('MetaCons":>"
('InfixI 'LeftAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Seq a)) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 ViewR = D1 ('MetaDataDefined in containers-0.7 · Data.Sequence.Internal"ViewR"
"Data.Sequence.Internal"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"EmptyR"
'PrefixI 'False) U1 :+: C1 ('MetaCons":>"
('InfixI 'LeftAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Seq) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
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]
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)
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.
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.
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.
O(\log(\min(i,n-i))) . A flipped, infix version of lookup.
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.
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.
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 = NothingUnlike 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
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.
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.
O(1) . Add an element to the left end of a sequence.
Mnemonic: a triangle with the single element at the pointy end.
O(1) . Analyse the left end of a sequence.
O(1) . Analyse the right end of a sequence.
O(1) . Add an element to the right end of a sequence.
Mnemonic: a triangle with the single element at the pointy end.
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 , but has the
performance advantage of only evaluating `Data.Function.on` f)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 will be faster than
`Data.Function.on` f)sortOn f.
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]
O(\log(\min(n_1,n_2))) . Concatenate two sequences.
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
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.
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 O(i) where i is the prefix length. dropWhileL p xs returns
the suffix remaining after takeWhileL p xs.
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)).
elemIndexL finds the leftmost index of the specified element, if it is present, and otherwise Nothing.
elemIndexR finds the rightmost index of the specified element, if it is present, and otherwise Nothing.
elemIndicesL finds the indices of the specified element, from left to right (i.e. in ascending order).
elemIndicesR finds the indices of the specified element, from right to left (i.e. in descending order).
findIndexL p xs finds the index of the leftmost element that
satisfies p, if any exist.
findIndexR p xs finds the index of the rightmost element that
satisfies p, if any exist.
findIndicesL p finds all indices of elements that satisfy p,
in ascending order.
findIndicesR p finds all indices of elements that satisfy p,
in descending order.
foldlWithIndex is a version of foldl that also provides access to the index of each element.
foldrWithIndex is a version of foldr that also provides access to the index of each element.
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.
O(n) . Convert a given sequence length and a function representing that
sequence into a sequence.
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 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))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.
replicateA is an Applicative version of replicate, and makes
O(\log n) calls to liftA2 and pure.
replicateA n x = sequenceA (replicate n x) 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.
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.
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.
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)).
traverseWithIndex is a version of traverse that also offers access to the index of each element.
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.
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.
O(n \log n) . A generalization of unstableSort, unstableSortBy
takes an arbitrary comparator and sorts the specified sequence.
The sort is not stable. This algorithm is frequently faster and
uses less memory than sortBy.
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 ,
but has the performance advantage of only evaluating `Data.Function.on` f)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 will be faster than
`Data.Function.on` f)unstableSortOn f.