| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Sequences
Description
Warning: This module should be considered highly experimental.
- class (Integral (Index seq), GrowingAppend seq) => SemiSequence seq where
- singleton :: IsSequence seq => Element seq -> seq
- class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => IsSequence seq where
- fromList :: [Element seq] -> seq
- break :: (Element seq -> Bool) -> seq -> (seq, seq)
- span :: (Element seq -> Bool) -> seq -> (seq, seq)
- dropWhile :: (Element seq -> Bool) -> seq -> seq
- takeWhile :: (Element seq -> Bool) -> seq -> seq
- splitAt :: Index seq -> seq -> (seq, seq)
- unsafeSplitAt :: Index seq -> seq -> (seq, seq)
- take :: Index seq -> seq -> seq
- unsafeTake :: Index seq -> seq -> seq
- drop :: Index seq -> seq -> seq
- unsafeDrop :: Index seq -> seq -> seq
- partition :: (Element seq -> Bool) -> seq -> (seq, seq)
- uncons :: seq -> Maybe (Element seq, seq)
- unsnoc :: seq -> Maybe (seq, Element seq)
- filter :: (Element seq -> Bool) -> seq -> seq
- filterM :: Monad m => (Element seq -> m Bool) -> seq -> m seq
- replicate :: Index seq -> Element seq -> seq
- replicateM :: Monad m => Index seq -> m (Element seq) -> m seq
- groupBy :: (Element seq -> Element seq -> Bool) -> seq -> [seq]
- groupAllOn :: Eq b => (Element seq -> b) -> seq -> [seq]
- subsequences :: seq -> [seq]
- permutations :: seq -> [seq]
- tailEx :: seq -> seq
- initEx :: seq -> seq
- unsafeTail :: seq -> seq
- unsafeInit :: seq -> seq
- index :: seq -> Index seq -> Maybe (Element seq)
- indexEx :: seq -> Index seq -> Element seq
- unsafeIndex :: seq -> Index seq -> Element seq
- intercalate :: seq -> [seq] -> seq
- splitWhen :: (Element seq -> Bool) -> seq -> [seq]
- defaultFind :: MonoFoldable seq => (Element seq -> Bool) -> seq -> Maybe (Element seq)
- defaultIntersperse :: IsSequence seq => Element seq -> seq -> seq
- defaultReverse :: IsSequence seq => seq -> seq
- defaultSortBy :: IsSequence seq => (Element seq -> Element seq -> Ordering) -> seq -> seq
- defaultIntercalate :: IsSequence seq => seq -> [seq] -> seq
- defaultSplitWhen :: IsSequence seq => (Element seq -> Bool) -> seq -> [seq]
- vectorSortBy :: Vector v e => (e -> e -> Ordering) -> v e -> v e
- vectorSort :: (Vector v e, Ord e) => v e -> v e
- defaultCons :: IsSequence seq => Element seq -> seq -> seq
- defaultSnoc :: IsSequence seq => seq -> Element seq -> seq
- tailDef :: IsSequence seq => seq -> seq
- initDef :: IsSequence seq => seq -> seq
- class (MonoFoldableEq seq, IsSequence seq, Eq (Element seq)) => EqSequence seq where
- splitElem :: Element seq -> seq -> [seq]
- splitSeq :: seq -> seq -> [seq]
- stripPrefix :: seq -> seq -> Maybe seq
- stripSuffix :: seq -> seq -> Maybe seq
- isPrefixOf :: seq -> seq -> Bool
- isSuffixOf :: seq -> seq -> Bool
- isInfixOf :: seq -> seq -> Bool
- group :: seq -> [seq]
- groupAll :: seq -> [seq]
- elem :: EqSequence seq => Element seq -> seq -> Bool
- notElem :: EqSequence seq => Element seq -> seq -> Bool
- defaultSplitOn :: EqSequence s => s -> s -> [s]
- class (EqSequence seq, MonoFoldableOrd seq) => OrdSequence seq where
- sort :: seq -> seq
- class (IsSequence t, IsString t, Element t ~ Char) => Textual t where
- catMaybes :: (IsSequence (f (Maybe t)), Functor f, Element (f (Maybe t)) ~ Maybe t) => f (Maybe t) -> f t
- sortOn :: (Ord o, SemiSequence seq) => (Element seq -> o) -> seq -> seq
Documentation
class (Integral (Index seq), GrowingAppend seq) => SemiSequence seq where Source
SemiSequence was created to share code between IsSequence and MinLen.
Semi means SemiGroup
A SemiSequence can accomodate a SemiGroup such as NonEmpty or MinLen
A Monoid should be able to fill out IsSequence.
SemiSequence operations maintain the same type because they all maintain the same number of elements or increase them.
However, a decreasing function such as filter may change they type.
For example, from NonEmpty to '[]'
This type-changing function exists on NonNull as nfilter
filter and other such functions are placed in IsSequence
Methods
intersperse :: Element seq -> seq -> seq Source
intersperse takes an element and intersperses that element between
the elements of the sequence.
> intersperse ',' "abcde"
"a,b,c,d,e"
Reverse a sequence
> reverse "hello world"
"dlrow olleh"
find :: (Element seq -> Bool) -> seq -> Maybe (Element seq) Source
find takes a predicate and a sequence and returns the first element in
the sequence matching the predicate, or Nothing if there isn't an element
that matches the predicate.
>find(== 5) [1 .. 10]Just5 >find(== 15) [1 .. 10]Nothing
sortBy :: (Element seq -> Element seq -> Ordering) -> seq -> seq Source
Sort a sequence using an supplied element ordering function.
> let compare' x y = casecomparex y of LT -> GT; EQ -> EQ; GT -> LT >sortBycompare' [5,3,6,1,2,4] [6,5,4,3,2,1]
cons :: Element seq -> seq -> seq Source
Prepend an element onto a sequence.
> 4 `cons` [1,2,3]
[4,1,2,3]
snoc :: seq -> Element seq -> seq Source
Append an element onto a sequence.
> [1,2,3] `snoc` 4
[1,2,3,4]
Instances
| SemiSequence ByteString Source | |
| SemiSequence ByteString Source | |
| SemiSequence Text Source | |
| SemiSequence Text Source | |
| SemiSequence [a] Source | |
| SemiSequence (Seq a) Source | |
| SemiSequence (DList a) Source | |
| SemiSequence (NonEmpty a) Source | |
| SemiSequence (Vector a) Source | |
| Unbox a => SemiSequence (Vector a) Source | |
| Storable a => SemiSequence (Vector a) Source | |
| SemiSequence seq => SemiSequence (MinLen nat seq) Source |
singleton :: IsSequence seq => Element seq -> seq Source
class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => IsSequence seq where Source
Sequence Laws:
fromList.otoList=idfromList(x <> y) =fromListx <>fromListyotoList(fromListx <>fromListy) = x <> y
Minimal complete definition
Nothing
Methods
fromList :: [Element seq] -> seq Source
Convert a list to a sequence.
>fromList[a,b,c] :: Text "abc"
break :: (Element seq -> Bool) -> seq -> (seq, seq) Source
break applies a predicate to a sequence, and returns a tuple where
the first element is the longest prefix (possibly empty) of elements that
do not satisfy the predicate. The second element of the tuple is the
remainder of the sequence.
is equivalent to break pspan (not . p)
>break(> 3) (fromList[1,2,3,4,1,2,3,4] ::VectorInt) (fromList [1,2,3],fromList [4,1,2,3,4]) >break(<z) (fromList"abc" ::Text) ("","abc") >break(>z) (fromList"abc" ::Text) ("abc","")
span :: (Element seq -> Bool) -> seq -> (seq, seq) Source
span applies a predicate to a sequence, and returns a tuple where
the first element is the longest prefix (possibly empty) that
does satisfy the predicate. The second element of the tuple is the
remainder of the sequence.
is equivalent to span p xs(takeWhile p xs, dropWhile p xs)
>span(< 3) (fromList[1,2,3,4,1,2,3,4] ::VectorInt) (fromList [1,2],fromList [3,4,1,2,3,4]) >span(<z) (fromList"abc" ::Text) ("abc","") >span(< 0) 1,2,3
dropWhile :: (Element seq -> Bool) -> seq -> seq Source
dropWhile returns the suffix remaining after takeWhile.
>dropWhile(< 3) [1,2,3,4,5,1,2,3] [3,4,5,1,2,3] >dropWhile(<z) (fromList"abc" ::Text) ""
takeWhile :: (Element seq -> Bool) -> seq -> seq Source
takeWhile applies a predicate to a sequence, and returns the
longest prefix (possibly empty) of the sequence of elements that
satisfy the predicate.
>takeWhile(< 3) [1,2,3,4,5,1,2,3] [1,2] >takeWhile(<z) (fromList"abc" ::Text) "abc"
splitAt :: Index seq -> seq -> (seq, seq) Source
returns a tuple where the first element is the prefix of
the sequence splitAt n sese with length n, and the second element is the remainder of
the sequence.
>splitAt6 "Hello world!" ("Hello ","world!") >splitAt3 (fromList[1,2,3,4,5] ::VectorInt) (fromList [1,2,3],fromList [4,5])
unsafeSplitAt :: Index seq -> seq -> (seq, seq) Source
Equivalent to splitAt.
take :: Index seq -> seq -> seq Source
returns the prefix of a sequence of length take nn, or the
sequence itself if n > .olength seq
>take3 "abcdefg" "abc" >take4 (fromList[1,2,3,4,5,6] ::VectorInt) fromList [1,2,3,4]
unsafeTake :: Index seq -> seq -> seq Source
Equivalent to take.
drop :: Index seq -> seq -> seq Source
returns the suffix of a sequence after the first drop nn
elements, or an empty sequence if n > .olength seq
>drop3 "abcdefg" "defg" >drop4 (fromList[1,2,3,4,5,6] ::VectorInt) fromList [5,6]
unsafeDrop :: Index seq -> seq -> seq Source
Equivalent to drop
partition :: (Element seq -> Bool) -> seq -> (seq, seq) Source
partition takes a predicate and a sequence and returns the pair of
sequences of elements which do and do not satisfy the predicate.
partitionp se = (filterp se,filter(not. p) se)
uncons :: seq -> Maybe (Element seq, seq) Source
uncons returns the tuple of the first element of a sequence and the rest
of the sequence, or Nothing if the sequence is empty.
>uncons(fromList[1,2,3,4] ::VectorInt)Just(1,fromList [2,3,4]) >uncons([] :: [Int])Nothing
unsnoc :: seq -> Maybe (seq, Element seq) Source
unsnoc returns the tuple of the init of a sequence and the last element,
or Nothing if the sequence is empty.
>uncons(fromList[1,2,3,4] ::VectorInt)Just(fromList [1,2,3],4) >uncons([] :: [Int])Nothing
filter :: (Element seq -> Bool) -> seq -> seq Source
filter given a predicate returns a sequence of all elements that satisfy
the predicate.
> filter (< 5) [1 .. 10]
[1,2,3,4]
filterM :: Monad m => (Element seq -> m Bool) -> seq -> m seq Source
The monadic version of filter.
replicate :: Index seq -> Element seq -> seq Source
is a sequence of length replicate n xn with x as the
value of every element.
>replicate10a:: Text "aaaaaaaaaa"
replicateM :: Monad m => Index seq -> m (Element seq) -> m seq Source
The monadic version of replicateM.
groupBy :: (Element seq -> Element seq -> Bool) -> seq -> [seq] Source
group takes a sequence and returns a list of sequences such that the
concatenation of the result is equal to the argument. Each subsequence in
the result contains only equal elements, using the supplied equality test.
> groupBy (==) Mississippi
[M,"i","ss","i","ss","i","pp","i"]
groupAllOn :: Eq b => (Element seq -> b) -> seq -> [seq] Source
Similar to standard groupBy, but operates on the whole collection,
not just the consecutive items.
subsequences :: seq -> [seq] Source
subsequences returns a list of all subsequences of the argument.
> subsequences "abc"
["","a","b","ab","c","ac","bc","abc"]
permutations :: seq -> [seq] Source
permutations returns a list of all permutations of the argument.
> permutations "abc"
["abc","bac","cba","bca","cab","acb"]
Unsafe
Get the tail of a sequence, throw an exception if the sequence is empty.
> tailEx [1,2,3]
[2,3]
Unsafe
Get the init of a sequence, throw an exception if the sequence is empty.
> initEx [1,2,3]
[1,2]
unsafeTail :: seq -> seq Source
Equivalent to tailEx.
unsafeInit :: seq -> seq Source
Equivalent to initEx.
index :: seq -> Index seq -> Maybe (Element seq) Source
Get the element of a sequence at a certain index, returns Nothing
if that index does not exist.
>index(fromList[1,2,3] ::VectorInt) 1Just2 >index(fromList[1,2,3] ::VectorInt) 4Nothing
indexEx :: seq -> Index seq -> Element seq Source
Unsafe
Get the element of a sequence at a certain index, throws an exception if the index does not exist.
unsafeIndex :: seq -> Index seq -> Element seq Source
Equivalent to indexEx.
intercalate :: seq -> [seq] -> seq Source
intercalate seq seqs inserts seq in between seqs and
concatenates the result.
Since 0.9.3
splitWhen :: (Element seq -> Bool) -> seq -> [seq] Source
splitWhen splits a sequence into components delimited by separators,
where the predicate returns True for a separator element. The resulting
components do not contain the separators. Two adjacent separators result
in an empty component in the output. The number of resulting components
is greater by one than number of separators.
Since 0.9.3
Instances
| IsSequence ByteString Source | |
| IsSequence ByteString Source | |
| IsSequence Text Source | |
| IsSequence Text Source | |
| IsSequence [a] Source | |
| IsSequence (Seq a) Source | |
| IsSequence (DList a) Source | |
| IsSequence (Vector a) Source | |
| Unbox a => IsSequence (Vector a) Source | |
| Storable a => IsSequence (Vector a) Source |
defaultFind :: MonoFoldable seq => (Element seq -> Bool) -> seq -> Maybe (Element seq) Source
defaultIntersperse :: IsSequence seq => Element seq -> seq -> seq Source
Use Data.List's implementation of intersperse.
defaultReverse :: IsSequence seq => seq -> seq Source
defaultSortBy :: IsSequence seq => (Element seq -> Element seq -> Ordering) -> seq -> seq Source
defaultIntercalate :: IsSequence seq => seq -> [seq] -> seq Source
Default intercalate
defaultSplitWhen :: IsSequence seq => (Element seq -> Bool) -> seq -> [seq] Source
Use splitWhen from Data.List.Split
vectorSortBy :: Vector v e => (e -> e -> Ordering) -> v e -> v e Source
Sort a vector using an supplied element ordering function.
vectorSort :: (Vector v e, Ord e) => v e -> v e Source
Sort a vector.
defaultCons :: IsSequence seq => Element seq -> seq -> seq Source
defaultSnoc :: IsSequence seq => seq -> Element seq -> seq Source
tailDef :: IsSequence seq => seq -> seq Source
initDef :: IsSequence seq => seq -> seq Source
class (MonoFoldableEq seq, IsSequence seq, Eq (Element seq)) => EqSequence seq where Source
A typeclass for sequences whose elements have the Eq typeclass
Minimal complete definition
Nothing
Methods
splitElem :: Element seq -> seq -> [seq] Source
splits a sequence into components delimited by separator
element. It's equivalent to splitElemsplitWhen with equality predicate:
splitElem sep === splitWhen (== sep)
Since 0.9.3
splitSeq :: seq -> seq -> [seq] Source
splits a sequence into components delimited by
separator subsequence. splitSeqsplitSeq is the right inverse of intercalate:
intercalate x . splitSeq x === id
splitElem can be considered a special case of splitSeq
splitSeq (singleton sep) === splitElem sep
is another special case: it splits just before each
element, and in line with splitSeq memptysplitWhen rules, it has at least one output
component:
>splitSeq"" "" [""] >splitSeq"" "a" ["", "a"] >splitSeq"" "ab" ["", "a", "b"]
Since 0.9.3
stripPrefix :: seq -> seq -> Maybe seq Source
stripPrefix drops the given prefix from a sequence.
It returns Nothing if the sequence did not start with the prefix
given, or Just the sequence after the prefix, if it does.
>stripPrefix"foo" "foobar"Just"foo" >stripPrefix"abc" "foobar"Nothing
stripSuffix :: seq -> seq -> Maybe seq Source
stripSuffix drops the given suffix from a sequence.
It returns Nothing if the sequence did not end with the suffix
given, or Just the sequence before the suffix, if it does.
>stripSuffix"bar" "foobar"Just"foo" >stripSuffix"abc" "foobar"Nothing
isPrefixOf :: seq -> seq -> Bool Source
isPrefixOf takes two sequences and returns True if the first
sequence is a prefix of the second.
isSuffixOf :: seq -> seq -> Bool Source
isSuffixOf takes two sequences and returns True if the first
sequence is a suffix of the second.
isInfixOf :: seq -> seq -> Bool Source
isInfixOf takes two sequences and returns true if the first
sequence is contained, wholly and intact, anywhere within the second.
Equivalent to groupBy (==)
groupAll :: seq -> [seq] Source
Similar to standard group, but operates on the whole collection,
not just the consecutive items.
Equivalent to groupAllOn id
Instances
| EqSequence ByteString Source | |
| EqSequence ByteString Source | |
| EqSequence Text Source | |
| EqSequence Text Source | |
| Eq a => EqSequence [a] Source | |
| Eq a => EqSequence (Seq a) Source | |
| Eq a => EqSequence (Vector a) Source | |
| (Eq a, Unbox a) => EqSequence (Vector a) Source | |
| (Eq a, Storable a) => EqSequence (Vector a) Source |
elem :: EqSequence seq => Element seq -> seq -> Bool Source
Deprecated: use oelem
notElem :: EqSequence seq => Element seq -> seq -> Bool Source
Deprecated: use onotElem
defaultSplitOn :: EqSequence s => s -> s -> [s] Source
Use splitOn from Data.List.Split
class (EqSequence seq, MonoFoldableOrd seq) => OrdSequence seq where Source
A typeclass for sequences whose elements have the Ord typeclass
Minimal complete definition
Nothing
Instances
| OrdSequence ByteString Source | |
| OrdSequence ByteString Source | |
| OrdSequence Text Source | |
| OrdSequence Text Source | |
| Ord a => OrdSequence [a] Source | |
| Ord a => OrdSequence (Seq a) Source | |
| Ord a => OrdSequence (Vector a) Source | |
| (Ord a, Unbox a) => OrdSequence (Vector a) Source | |
| (Ord a, Storable a) => OrdSequence (Vector a) Source |
class (IsSequence t, IsString t, Element t ~ Char) => Textual t where Source
A typeclass for sequences whose elements are Chars.
Methods
Break up a textual sequence into a list of words, which were delimited by white space.
> words "abc def ghi"
["abc","def","ghi"]
Join a list of textual sequences using seperating spaces.
> unwords ["abc","def","ghi"]
"abc def ghi"
Break up a textual sequence at newline characters.
> lines "hello\nworld"
["hello","world"]
Join a list of textual sequences using newlines.
> unlines ["abc","def","ghi"]
"abc\ndef\nghi"
Convert a textual sequence to lower-case.
> toLower "HELLO WORLD"
"hello world"
Convert a textual sequence to upper-case.
> toUpper "hello world"
"HELLO WORLD"
toCaseFold :: t -> t Source
Convert a textual sequence to folded-case.
Slightly different from toLower, see Data.Text.toCaseFold
breakWord :: t -> (t, t) Source
Split a textual sequence into two parts, split at the first space.
> breakWord "hello world"
("hello","world")
breakLine :: t -> (t, t) Source
Split a textual sequence into two parts, split at the newline.
> breakLine "abc\ndef"
("abc","def")
catMaybes :: (IsSequence (f (Maybe t)), Functor f, Element (f (Maybe t)) ~ Maybe t) => f (Maybe t) -> f t Source
Takes all of the Just values from a sequence of Maybe ts and
concatenates them into an unboxed sequence of ts.
Since 0.6.2
sortOn :: (Ord o, SemiSequence seq) => (Element seq -> o) -> seq -> seq Source
Same as sortBy . comparing.
Since 0.7.0