Safe Haskell | Safe |
---|---|
Language | GHC2021 |
Antelude.Sequence
Description
Synopsis
- data ViewR a
- data ViewL a
- data Seq a where
- zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c
- sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
- length :: Seq a -> Int
- filter :: (a -> Bool) -> Seq a -> Seq a
- unfoldr :: (b -> Maybe (a, b)) -> b -> Seq a
- sortOn :: Ord b => (a -> b) -> Seq a -> Seq a
- zip :: Seq a -> Seq b -> Seq (a, b)
- fromList :: [a] -> Seq a
- empty :: Seq a
- null :: Seq a -> Bool
- scanl :: (a -> b -> a) -> a -> Seq b -> Seq a
- scanl1 :: (a -> a -> a) -> Seq a -> Seq a
- scanr :: (a -> b -> b) -> b -> Seq a -> Seq b
- scanr1 :: (a -> a -> a) -> Seq a -> Seq a
- replicate :: Int -> a -> Seq a
- take :: Int -> Seq a -> Seq a
- drop :: Int -> Seq a -> Seq a
- splitAt :: Int -> Seq a -> (Seq a, Seq a)
- reverse :: Seq a -> Seq a
- lookup :: Int -> Seq a -> Maybe a
- zip3 :: Seq a -> Seq b -> Seq c -> Seq (a, b, c)
- zipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d
- unzip :: Seq (a, b) -> (Seq a, Seq b)
- adjust :: (a -> a) -> Int -> Seq a -> Seq a
- intersperse :: a -> Seq a -> Seq a
- partition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
- zip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a, b, c, d)
- zipWith4 :: (a -> b -> c -> d -> e) -> Seq a -> Seq b -> Seq c -> Seq d -> Seq e
- inits :: Seq a -> Seq (Seq a)
- tails :: Seq a -> Seq (Seq a)
- sort :: Ord a => Seq a -> Seq a
- singleton :: a -> Seq a
- replicateM :: Applicative m => Int -> m a -> m (Seq a)
- foldMapWithIndex :: Monoid m => (Int -> a -> m) -> Seq a -> m
- deleteAt :: Int -> Seq a -> Seq a
- replicateA :: Applicative f => Int -> f a -> f (Seq a)
- cycleTaking :: Int -> Seq a -> Seq a
- unfoldl :: (b -> Maybe (b, a)) -> b -> Seq a
- iterateN :: Int -> (a -> a) -> a -> Seq a
- viewl :: Seq a -> ViewL a
- viewr :: Seq a -> ViewR a
- update :: Int -> a -> Seq a -> Seq a
- adjust' :: (a -> a) -> Int -> Seq a -> Seq a
- insertAt :: Int -> a -> Seq a -> Seq a
- mapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b
- traverseWithIndex :: Applicative f => (Int -> a -> f b) -> Seq a -> f (Seq b)
- fromFunction :: Int -> (Int -> a) -> Seq a
- fromArray :: Ix i => Array i a -> Seq a
- chunksOf :: Int -> Seq a -> Seq (Seq a)
- foldlWithIndex :: (b -> Int -> a -> b) -> b -> Seq a -> b
- foldrWithIndex :: (Int -> a -> b -> b) -> b -> Seq a -> b
- takeWhileL :: (a -> Bool) -> Seq a -> Seq a
- takeWhileR :: (a -> Bool) -> Seq a -> Seq a
- dropWhileL :: (a -> Bool) -> Seq a -> Seq a
- dropWhileR :: (a -> Bool) -> Seq a -> Seq a
- spanl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
- spanr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
- breakl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
- breakr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
- elemIndexL :: Eq a => a -> Seq a -> Maybe Int
- elemIndexR :: Eq a => a -> Seq a -> Maybe Int
- elemIndicesL :: Eq a => a -> Seq a -> [Int]
- elemIndicesR :: Eq a => a -> Seq a -> [Int]
- findIndexL :: (a -> Bool) -> Seq a -> Maybe Int
- findIndexR :: (a -> Bool) -> Seq a -> Maybe Int
- findIndicesL :: (a -> Bool) -> Seq a -> [Int]
- findIndicesR :: (a -> Bool) -> Seq a -> [Int]
- unzipWith :: (a -> (b, c)) -> Seq a -> (Seq b, Seq c)
- unstableSort :: Ord a => Seq a -> Seq a
- unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
- unstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a
- append :: a -> Seq a -> Seq a
- concat :: Seq a -> Seq a -> Seq a
- prepend :: a -> Seq a -> Seq a
Documentation
View of the right end of a sequence.
Constructors
EmptyR | empty sequence |
(Seq a) :> a infixl 5 | the sequence minus the rightmost element, and the rightmost element |
Instances
Foldable ViewR | |
Defined in Data.Sequence.Internal Methods fold :: Monoid m => ViewR m -> m # foldMap :: Monoid m => (a -> m) -> ViewR a -> m # foldMap' :: Monoid m => (a -> m) -> ViewR a -> m # foldr :: (a -> b -> b) -> b -> ViewR a -> b # foldr' :: (a -> b -> b) -> b -> ViewR a -> b # foldl :: (b -> a -> b) -> b -> ViewR a -> b # foldl' :: (b -> a -> b) -> b -> ViewR a -> b # foldr1 :: (a -> a -> a) -> ViewR a -> a # foldl1 :: (a -> a -> a) -> ViewR a -> a # elem :: Eq a => a -> ViewR a -> Bool # maximum :: Ord a => ViewR a -> a # minimum :: Ord a => ViewR a -> a # | |
Traversable ViewR | |
Functor ViewR | |
Generic1 ViewR | |
Lift a => Lift (ViewR a :: Type) | Since: containers-0.6.6 |
Data a => Data (ViewR a) | |
Defined in Data.Sequence.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ViewR a -> c (ViewR a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ViewR a) # toConstr :: ViewR a -> Constr # dataTypeOf :: ViewR a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (ViewR a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ViewR a)) # gmapT :: (forall b. Data b => b -> b) -> ViewR a -> ViewR a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ViewR a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ViewR a -> r # gmapQ :: (forall d. Data d => d -> u) -> ViewR a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ViewR a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ViewR a -> m (ViewR a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ViewR a -> m (ViewR a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ViewR a -> m (ViewR a) # | |
Generic (ViewR a) | |
Read a => Read (ViewR a) | |
Show a => Show (ViewR a) | |
Eq a => Eq (ViewR a) | |
Ord a => Ord (ViewR a) | |
Defined in Data.Sequence.Internal | |
type Rep1 ViewR | Since: containers-0.5.8 |
Defined in Data.Sequence.Internal type Rep1 ViewR = D1 ('MetaData "ViewR" "Data.Sequence.Internal" "containers-0.6.7" 'False) (C1 ('MetaCons "EmptyR" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":>" ('InfixI 'LeftAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Seq) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1)) | |
type Rep (ViewR a) | Since: containers-0.5.8 |
Defined in Data.Sequence.Internal type Rep (ViewR a) = D1 ('MetaData "ViewR" "Data.Sequence.Internal" "containers-0.6.7" 'False) (C1 ('MetaCons "EmptyR" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":>" ('InfixI 'LeftAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Seq a)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))) |
View of the left end of a sequence.
Instances
Foldable ViewL | |
Defined in Data.Sequence.Internal Methods fold :: Monoid m => ViewL m -> m # foldMap :: Monoid m => (a -> m) -> ViewL a -> m # foldMap' :: Monoid m => (a -> m) -> ViewL a -> m # foldr :: (a -> b -> b) -> b -> ViewL a -> b # foldr' :: (a -> b -> b) -> b -> ViewL a -> b # foldl :: (b -> a -> b) -> b -> ViewL a -> b # foldl' :: (b -> a -> b) -> b -> ViewL a -> b # foldr1 :: (a -> a -> a) -> ViewL a -> a # foldl1 :: (a -> a -> a) -> ViewL a -> a # elem :: Eq a => a -> ViewL a -> Bool # maximum :: Ord a => ViewL a -> a # minimum :: Ord a => ViewL a -> a # | |
Traversable ViewL | |
Functor ViewL | |
Generic1 ViewL | |
Lift a => Lift (ViewL a :: Type) | Since: containers-0.6.6 |
Data a => Data (ViewL a) | |
Defined in Data.Sequence.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ViewL a -> c (ViewL a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ViewL a) # toConstr :: ViewL a -> Constr # dataTypeOf :: ViewL a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (ViewL a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ViewL a)) # gmapT :: (forall b. Data b => b -> b) -> ViewL a -> ViewL a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ViewL a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ViewL a -> r # gmapQ :: (forall d. Data d => d -> u) -> ViewL a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ViewL a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ViewL a -> m (ViewL a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ViewL a -> m (ViewL a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ViewL a -> m (ViewL a) # | |
Generic (ViewL a) | |
Read a => Read (ViewL a) | |
Show a => Show (ViewL a) | |
Eq a => Eq (ViewL a) | |
Ord a => Ord (ViewL a) | |
Defined in Data.Sequence.Internal | |
type Rep1 ViewL | Since: containers-0.5.8 |
Defined in Data.Sequence.Internal type Rep1 ViewL = D1 ('MetaData "ViewL" "Data.Sequence.Internal" "containers-0.6.7" 'False) (C1 ('MetaCons "EmptyL" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":<" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Seq))) | |
type Rep (ViewL a) | Since: containers-0.5.8 |
Defined in Data.Sequence.Internal type Rep (ViewL a) = D1 ('MetaData "ViewL" "Data.Sequence.Internal" "containers-0.6.7" 'False) (C1 ('MetaCons "EmptyL" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":<" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Seq a)))) |
General-purpose finite sequences.
Bundled Patterns
pattern Empty :: Seq a | A bidirectional pattern synonym matching an empty sequence. Since: containers-0.5.8 |
pattern (:|>) :: Seq a -> a -> Seq a infixl 5 | A bidirectional pattern synonym viewing the rear of a non-empty sequence. Since: containers-0.5.8 |
pattern (:<|) :: a -> Seq a -> Seq a infixr 5 | A bidirectional pattern synonym viewing the front of a non-empty sequence. Since: containers-0.5.8 |
Instances
MonadFix Seq | Since: containers-0.5.11 |
Defined in Data.Sequence.Internal | |
MonadZip Seq |
|
Foldable Seq | |
Defined in Data.Sequence.Internal Methods fold :: Monoid m => Seq m -> m # foldMap :: Monoid m => (a -> m) -> Seq a -> m # foldMap' :: Monoid m => (a -> m) -> Seq a -> m # foldr :: (a -> b -> b) -> b -> Seq a -> b # foldr' :: (a -> b -> b) -> b -> Seq a -> b # foldl :: (b -> a -> b) -> b -> Seq a -> b # foldl' :: (b -> a -> b) -> b -> Seq a -> b # foldr1 :: (a -> a -> a) -> Seq a -> a # foldl1 :: (a -> a -> a) -> Seq a -> a # elem :: Eq a => a -> Seq a -> Bool # maximum :: Ord a => Seq a -> a # | |
Eq1 Seq | Since: containers-0.5.9 |
Ord1 Seq | Since: containers-0.5.9 |
Defined in Data.Sequence.Internal | |
Read1 Seq | Since: containers-0.5.9 |
Defined in Data.Sequence.Internal | |
Show1 Seq | Since: containers-0.5.9 |
Traversable Seq | |
Alternative Seq | Since: containers-0.5.4 |
Applicative Seq | Since: containers-0.5.4 |
Functor Seq | |
Monad Seq | |
MonadPlus Seq | |
UnzipWith Seq | |
Defined in Data.Sequence.Internal Methods unzipWith' :: (x -> (a, b)) -> Seq x -> (Seq a, Seq b) | |
Lift a => Lift (Seq a :: Type) | Since: containers-0.6.6 |
Data a => Data (Seq a) | |
Defined in Data.Sequence.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Seq a -> c (Seq a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Seq a) # dataTypeOf :: Seq a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Seq a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Seq a)) # gmapT :: (forall b. Data b => b -> b) -> Seq a -> Seq a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Seq a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Seq a -> r # gmapQ :: (forall d. Data d => d -> u) -> Seq a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Seq a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) # | |
a ~ Char => IsString (Seq a) | Since: containers-0.5.7 |
Defined in Data.Sequence.Internal Methods fromString :: String -> Seq a # | |
Monoid (Seq a) | |
Semigroup (Seq a) | Since: containers-0.5.7 |
IsList (Seq a) | |
Read a => Read (Seq a) | |
Show a => Show (Seq a) | |
NFData a => NFData (Seq a) | |
Defined in Data.Sequence.Internal | |
Eq a => Eq (Seq a) | |
Ord a => Ord (Seq a) | |
type Item (Seq a) | |
Defined in Data.Sequence.Internal |
sortBy :: (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.
Since: containers-0.3.0
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.
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.
sortOn :: 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.
is
equivalent to sortOn
f
, but has the
performance advantage of only evaluating sortBy
(compare
`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
),
will be faster than
sortBy
(compare
`on`
f)
.sortOn
f
Since: containers-0.5.11
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.
replicate :: Int -> a -> Seq a #
\( O(\log n) \). replicate n x
is a sequence consisting of n
copies of x
.
take :: Int -> Seq a -> Seq a #
\( O(\log(\min(i,n-i))) \). The first i
elements of a sequence.
If i
is negative,
yields the empty sequence.
If the sequence contains fewer than take
i si
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,
yields the whole sequence.
If the sequence contains fewer than drop
i si
elements, the empty sequence
is returned.
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 -> insert
k x m
Since: containers-0.5.8
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.
Since: containers-0.5.8
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]
Since: containers-0.5.8
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.
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.
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.
sort :: 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.
Since: containers-0.3.0
replicateM :: Applicative m => Int -> m a -> m (Seq a) #
replicateM
is a sequence counterpart of replicateM
.
replicateM n x = sequence (replicate n x)
For base >= 4.8.0
and containers >= 0.5.11
, replicateM
is a synonym for replicateA
.
foldMapWithIndex :: Monoid m => (Int -> a -> m) -> Seq a -> m #
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]
Since: containers-0.5.8
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
.
replicateA n x = sequenceA (replicate n x)
cycleTaking :: Int -> Seq a -> Seq a #
\(O(\log k)\).
forms a sequence of length cycleTaking
k xsk
by
repeatedly concatenating xs
with itself. xs
may only be empty if
k
is 0.
cycleTaking k = fromList . take k . cycle . toList
iterateN :: 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))
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.
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. 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
Since: containers-0.5.8
insertAt :: Int -> a -> Seq a -> Seq a #
\( O(\log(\min(i,n-i))) \).
inserts insertAt
i x xsx
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
Since: containers-0.5.8
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.
Since: containers-0.5.8
fromFunction :: Int -> (Int -> a) -> Seq a #
\( O(n) \). Convert a given sequence length and a function representing that sequence into a sequence.
Since: containers-0.5.6.2
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.
Since: containers-0.5.6.2
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) \)
Since: containers-0.5.8
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.
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
.
is equivalent to takeWhileR
p xs
.reverse
(takeWhileL
p (reverse
xs))
dropWhileL :: (a -> Bool) -> Seq a -> Seq a #
\( O(i) \) where \( i \) is the prefix length.
returns
the suffix remaining after dropWhileL
p xs
.takeWhileL
p xs
dropWhileR :: (a -> Bool) -> Seq a -> Seq a #
\( O(i) \) where \( i \) is the suffix length.
returns
the prefix remaining after dropWhileR
p xs
.takeWhileR
p xs
is equivalent to dropWhileR
p xs
.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.
elemIndexL :: Eq a => a -> Seq a -> Maybe Int #
elemIndexL
finds the leftmost index of the specified element,
if it is present, and otherwise Nothing
.
elemIndexR :: Eq a => a -> Seq a -> Maybe Int #
elemIndexR
finds the rightmost 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).
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 #
finds the index of the leftmost element that
satisfies findIndexL
p xsp
, if any exist.
findIndexR :: (a -> Bool) -> Seq a -> Maybe Int #
finds the index of the rightmost element that
satisfies findIndexR
p xsp
, if any exist.
findIndicesL :: (a -> Bool) -> Seq a -> [Int] #
finds all indices of elements that satisfy findIndicesL
pp
,
in ascending order.
findIndicesR :: (a -> Bool) -> Seq a -> [Int] #
finds all indices of elements that satisfy findIndicesR
pp
,
in descending order.
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.
Since: containers-0.5.11
unstableSort :: 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
.
unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a #
\( 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
.
Since: containers-0.3.0
unstableSortOn :: 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.
is equivalent to unstableSortOn
f
,
but has the performance advantage of only evaluating unstableSortBy
(compare
`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
),
will be faster than
unstableSortBy
(compare
`on`
f)
.unstableSortOn
f
Since: containers-0.5.11