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

Modulemono-traversable-1.0.21.0Haskell2010

Data.Sequences

Abstractions over sequential data structures, like lists and vectors.

  • 5 classes
  • 44 values
classclass (Integral (Index seq), GrowingAppend seq) => SemiSequence seq where
#

SemiSequence was created to share code between IsSequence and NonNull.

Semi means SemiGroup A SemiSequence can accommodate a SemiGroup such as NonEmpty or NonNull 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

NOTE: Like GrowingAppend, ideally we'd have a Semigroup superclass constraint here, but that would pull in more dependencies to this package than desired.

Associated types

  • type family Index seq

    The type of the index of a sequence.

Methods

  • intersperse :: Element seq -> seq -> seq

    intersperse takes an element and intersperses that element between the elements of the sequence.

    > intersperse ',' "abcde"
    "a,b,c,d,e"
    
  • reverse :: seq -> seq

    Reverse a sequence

    > reverse "hello world"
    "dlrow olleh"
    
  • find :: (Element seq -> Bool) -> seq -> Maybe (Element seq)

    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]
    Just 5
    
    > find (== 15) [1 .. 10]
    Nothing
    
  • sortBy :: (Element seq -> Element seq -> Ordering) -> seq -> seq

    Sort a sequence using an supplied element ordering function.

    > let compare' x y = case compare x y of LT -> GT; EQ -> EQ; GT -> LT
    > sortBy compare' [5,3,6,1,2,4]
    [6,5,4,3,2,1]
    
  • cons :: Element seq -> seq -> seq

    Prepend an element onto a sequence.

    > 4 `cons` [1,2,3]
    [4,1,2,3]
    
  • snoc :: seq -> Element seq -> seq

    Append an element onto a sequence.

    > [1,2,3] `snoc` 4
    [1,2,3,4]
    
Instances12SemiSequence, …
classclass (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => IsSequence seq where
#

Sequence Laws:

fromList . otoList = id
fromList (x <> y) = fromList x <> fromList y
otoList (fromList x <> fromList y) = x <> y

Methods

  • fromList :: [Element seq] -> seq

    Convert a list to a sequence.

    > fromList [a, b, c] :: Text
    "abc"
    
  • lengthIndex :: seq -> Index seq

    lengthIndex returns the length of a sequence as Index seq.

  • break :: (Element seq -> Bool) -> seq -> (seq, seq)

    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.

    break p is equivalent to span (not . p)

    > break (> 3) (fromList [1,2,3,4,1,2,3,4] :: Vector Int)
    (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)

    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.

    span p xs is equivalent to (takeWhile p xs, dropWhile p xs)

    > span (< 3) (fromList [1,2,3,4,1,2,3,4] :: Vector Int)
    (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

    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

    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)

    splitAt n se returns a tuple where the first element is the prefix of the sequence se with length n, and the second element is the remainder of the sequence.

    > splitAt 6 "Hello world!"
    ("Hello ","world!")
    
    > splitAt 3 (fromList [1,2,3,4,5] :: Vector Int)
    (fromList [1,2,3],fromList [4,5])
    
  • unsafeSplitAt :: Index seq -> seq -> (seq, seq)

    Equivalent to splitAt.

  • take :: Index seq -> seq -> seq

    take n returns the prefix of a sequence of length n, or the sequence itself if n > olength seq.

    > take 3 "abcdefg"
    "abc"
    > take 4 (fromList [1,2,3,4,5,6] :: Vector Int)
    fromList [1,2,3,4]
    
  • unsafeTake :: Index seq -> seq -> seq

    Equivalent to take.

  • drop :: Index seq -> seq -> seq

    drop n returns the suffix of a sequence after the first n elements, or an empty sequence if n > olength seq.

    > drop 3 "abcdefg"
    "defg"
    > drop 4 (fromList [1,2,3,4,5,6] :: Vector Int)
    fromList [5,6]
    
  • unsafeDrop :: Index seq -> seq -> seq

    Equivalent to drop

  • dropEnd :: Index seq -> seq -> seq

    Same as drop but drops from the end of the sequence instead.

    > dropEnd 3 "abcdefg"
    "abcd"
    > dropEnd 4 (fromList [1,2,3,4,5,6] :: Vector Int)
    fromList [1,2]
    
  • partition :: (Element seq -> Bool) -> seq -> (seq, seq)

    partition takes a predicate and a sequence and returns the pair of sequences of elements which do and do not satisfy the predicate.

    partition p se = (filter p se, filter (not . p) se)
    
  • uncons :: seq -> Maybe (Element seq, seq)

    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] :: Vector Int)
    Just (1,fromList [2,3,4])
    
    > uncons ([] :: [Int])
    Nothing
    
  • unsnoc :: seq -> Maybe (seq, Element seq)

    unsnoc returns the tuple of the init of a sequence and the last element, or Nothing if the sequence is empty.

    > unsnoc (fromList [1,2,3,4] :: Vector Int)
    Just (fromList [1,2,3],4)
    
    > unsnoc ([] :: [Int])
    Nothing
    
  • filter :: (Element seq -> Bool) -> seq -> seq

    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

    The monadic version of filter.

  • replicate :: Index seq -> Element seq -> seq

    replicate n x is a sequence of length n with x as the value of every element.

    > replicate 10 a :: Text
    "aaaaaaaaaa"
    
  • replicateM :: Monad m => Index seq -> m (Element seq) -> m seq

    The monadic version of replicateM.

  • groupBy :: (Element seq -> Element seq -> Bool) -> seq -> [seq]

    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]

    Similar to standard groupBy, but operates on the whole collection, not just the consecutive items.

  • subsequences :: seq -> [seq]

    subsequences returns a list of all subsequences of the argument.

    > subsequences "abc"
    ["","a","b","ab","c","ac","bc","abc"]
    
  • permutations :: seq -> [seq]

    permutations returns a list of all permutations of the argument.

    > permutations "abc"
    ["abc","bac","cba","bca","cab","acb"]
    
  • tailEx :: seq -> seq

    Unsafe

    Get the tail of a sequence, throw an exception if the sequence is empty.

    > tailEx [1,2,3]
    [2,3]
    
  • tailMay :: seq -> Maybe seq

    Safe version of tailEx.

    Returns Nothing instead of throwing an exception when encountering an empty monomorphic container.

  • initEx :: seq -> seq

    Unsafe

    Get the init of a sequence, throw an exception if the sequence is empty.

    > initEx [1,2,3]
    [1,2]
    
  • initMay :: seq -> Maybe seq

    Safe version of initEx.

    Returns Nothing instead of throwing an exception when encountering an empty monomorphic container.

  • unsafeTail :: seq -> seq

    Equivalent to tailEx.

  • unsafeInit :: seq -> seq

    Equivalent to initEx.

  • index :: seq -> Index seq -> Maybe (Element seq)

    Get the element of a sequence at a certain index, returns Nothing if that index does not exist.

    > index (fromList [1,2,3] :: Vector Int) 1
    Just 2
    > index (fromList [1,2,3] :: Vector Int) 4
    Nothing
    
  • indexEx :: seq -> Index seq -> Element seq

    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

    Equivalent to indexEx.

  • splitWhen :: (Element seq -> Bool) -> seq -> [seq]

    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

  • tails :: seq -> [seq]

    Returns all the final segments of seq with the longest first.

    > tails [1,2]
    [[1,2],[2],[]]
    > tails []
    [[]]
    
  • inits :: seq -> [seq]

    Return all the initial segments of seq with the shortest first.

    > inits [1,2]
    [[],[1],[1,2]]
    > inits []
    [[]]
    
  • initTails :: seq -> [(seq, seq)]

    Return all the pairs of inital and final segments of seq.

    > initTails [1,2]
    [([],[1,2]),([1],[2]),([1,2],[])]
    > initTails []
    [([],[])]
    
Instances10IsSequence, …
valuesplitSeq :: (IsSequence seq, Eq (Element seq)) => seq -> seq -> [seq]
#

splitSeq splits a sequence into components delimited by separator subsequence. splitSeq is the right inverse of intercalate:

ointercalate x . splitSeq x === id

splitElem can be considered a special case of splitSeq

splitSeq (singleton sep) === splitElem sep

splitSeq mempty is another special case: it splits just before each element, and in line with splitWhen rules, it has at least one output component:

> splitSeq "" ""
[""]
> splitSeq "" "a"
["", "a"]
> splitSeq "" "ab"
["", "a", "b"]

Since 0.9.3

classclass (IsSequence t, IsString t, Element t ~ Char) => Textual t where
#

A typeclass for sequences whose elements are Chars.

Methods

  • words :: t -> [t]

    Break up a textual sequence into a list of words, which were delimited by white space.

    > words "abc  def ghi"
    ["abc","def","ghi"]
    
  • unwords :: (Element seq ~ t, MonoFoldable seq) => seq -> t

    Join a list of textual sequences using separating spaces.

    > unwords ["abc","def","ghi"]
    "abc def ghi"
    
  • lines :: t -> [t]

    Break up a textual sequence at newline characters.

    > lines "hello\nworld"
    ["hello","world"]
    
  • unlines :: (Element seq ~ t, MonoFoldable seq) => seq -> t

    Join a list of textual sequences using newlines.

    > unlines ["abc","def","ghi"]
    "abc\ndef\nghi"
    
  • toLower :: t -> t

    Convert a textual sequence to lower-case.

    > toLower "HELLO WORLD"
    "hello world"
    
  • toUpper :: t -> t

    Convert a textual sequence to upper-case.

    > toUpper "hello world"
    "HELLO WORLD"
    
  • toCaseFold :: t -> t

    Convert a textual sequence to folded-case.

    Slightly different from toLower, see Data.Text.toCaseFold

  • breakWord :: t -> (t, t)

    Split a textual sequence into two parts, split at the first space.

    > breakWord "hello world"
    ("hello","world")
    
  • breakLine :: t -> (t, t)

    Split a textual sequence into two parts, split at the newline.

    > breakLine "abc\ndef"
    ("abc","def")
    
Instances3Textual
  • Textual TextDefined in mono-traversable-1.0.21.0 · Data.Sequences
  • Textual TextDefined in mono-traversable-1.0.21.0 · Data.Sequences
  • c ~ Char => Textual [c]Defined in mono-traversable-1.0.21.0 · Data.Sequences
classclass (Textual textual, IsSequence binary) => Utf8 textual binary | textual -> binary, binary -> textual where
#

Textual data which can be encoded to and decoded from UTF8.

Methods

  • encodeUtf8 :: textual -> binary

    Encode from textual to binary using UTF-8 encoding

  • decodeUtf8 :: binary -> textual

    Note that this function is required to be pure. In the case of a decoding error, Unicode replacement characters must be used.

Instances3Utf8