Implementation module for Data.List.Split, a combinator library
for splitting lists. See the Data.List.Split documentation for
more description and examples.
The default splitting strategy: keep delimiters in the output
as separate chunks, don't condense multiple consecutive
delimiters into one, keep initial and final blank chunks.
Default delimiter is the constantly false predicate.
The defaultSplitter strategy with any delimiter gives a
maximally information-preserving splitting strategy, in the sense
that (a) taking the concat of the output yields the original
list, and (b) given only the output list, we can reconstruct a
Splitter which would produce the same output list again given
the original input list. This default strategy can be overridden
to allow discarding various sorts of information.
Try to match a delimiter at the start of a list, either failing
or decomposing the list into the portion which matched the delimiter
and the remainder.
Given a delimiter to use, split a list into an internal
representation with chunks tagged as delimiters or text. This
transformation is lossless; in particular,
Given a split list in the internal tagged representation, produce
a new internal tagged representation corresponding to the final
output, according to the strategy defined by the given
Splitter.
Insert blank chunks between any remaining consecutive delimiters
(unless the condense policy is DropBlankFields), and at the
beginning or end if the first or last element is a delimiter.
Merge delimiters with adjacent chunks to the right (yes, that's
not a typo: the delimiters should end up on the left of the
chunks, so they are merged with chunks to their right).
Make a strategy that splits a list into chunks that all start
with the given subsequence (except possibly the first).
Equivalent to dropInitBlank . keepDelimsL . onSublist.
Make a strategy that splits a list into chunks that all start
with one of the given elements (except possibly the first).
Equivalent to dropInitBlank . keepDelimsL . oneOf.
example:
Make a strategy that splits a list into chunks that all end with
the given subsequence, except possibly the last. Equivalent to
dropFinalBlank . keepDelimsR . onSublist.
Make a strategy that splits a list into chunks that all end with
one of the given elements, except possibly the last. Equivalent
to dropFinalBlank . keepDelimsR . oneOf.
Example1 expression
>>> split (condense $ endsWithOneOf ".,?! ") "Hi, there! How are you?"["Hi, ","there! ","How ","are ","you?"]
In some parsing combinator frameworks this is also known as
sepBy.
Note that this is the right inverse of the intercalate function
from Data.List, that is,
intercalate x . splitOn x === id
splitOn x . intercalate x is the identity on
certain lists, but it is tricky to state the precise conditions
under which this holds. (For example, it is not enough to say
that x does not occur in any elements of the input list.
Working out why is left as an exercise for the reader.)
Split into "lines", with line boundaries indicated by the given
predicate. Satisfies lines === linesBy (=='n'); equivalent to
split . dropFinalBlank . dropDelims . whenElt.
Standard build function, specialized to building lists.
Usually build is given the rank-2 type
build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
but since we only use it when (b ~ [a]), we give it the more
restricted type signature in order to avoid needing a
non-Haskell2010 extension.
Note that the 0.1.4.3 release of this package did away with a
custom build implementation in favor of importing one from
GHC.Exts, which was (reportedly) faster for some applications.
However, in the interest of simplicity and complete Haskell2010
compliance as split is being included in the Haskel Platform,
version 0.2.1.0 has gone back to defining build manually. This
is in line with split's design philosophy of having efficiency
as a non-goal.
chunksOf n splits a list into length-n pieces. The last
piece will be shorter if n does not evenly divide the length of
the list. If n <= 0, chunksOf n l returns an infinite list
of empty lists.
If the input list is longer than the total of the given lengths,
then the remaining elements are dropped. If the list is shorter
than the total of the given lengths, then the result may contain
fewer chunks than requested, and the last chunk may be shorter
than requested.
Split a list into chunks of the given lengths. Unlike
splitPlaces, the output list will always be the same length as
the first input argument. If the input list is longer than the
total of the given lengths, then the remaining elements are
dropped. If the list is shorter than the total of the given
lengths, then the last several chunks will be shorter than
requested or empty.
A useful recursion pattern for processing a list to produce a new
list, often used for "chopping" up the input list. Typically
chop is called with some function that will consume an initial
prefix of the list and produce a value and the rest of the list.
For example, many common Prelude functions can be implemented in
terms of chop:
group :: (Eq a) => [a] -> [[a]]
group = chop (\ xs@(x:_) -> span (==x) xs)
words :: String -> [String]
words = filter (not . null) . chop (break isSpace . dropWhile isSpace)
Divides up an input list into a set of sublists, according to n and m
input specifications you provide. Each sublist will have n items, and the
start of each sublist will be offset by m items from the previous one.