{-# OPTIONS_HADDOCK prune #-}
module Data.List.Split.Internals where
import Data.List (genericSplitAt)
data Splitter a = Splitter
{ forall a. Splitter a -> Delimiter a
delimiter :: Delimiter a
, forall a. Splitter a -> DelimPolicy
delimPolicy :: DelimPolicy
, forall a. Splitter a -> CondensePolicy
condensePolicy :: CondensePolicy
, forall a. Splitter a -> EndPolicy
initBlankPolicy :: EndPolicy
, forall a. Splitter a -> EndPolicy
finalBlankPolicy :: EndPolicy
}
defaultSplitter :: Splitter a
defaultSplitter :: forall a. Splitter a
defaultSplitter =
Splitter
{ delimiter :: Delimiter a
delimiter = [a -> Bool] -> Delimiter a
forall a. [a -> Bool] -> Delimiter a
Delimiter [Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
False]
, delimPolicy :: DelimPolicy
delimPolicy = DelimPolicy
Keep
, condensePolicy :: CondensePolicy
condensePolicy = CondensePolicy
KeepBlankFields
, initBlankPolicy :: EndPolicy
initBlankPolicy = EndPolicy
KeepBlank
, finalBlankPolicy :: EndPolicy
finalBlankPolicy = EndPolicy
KeepBlank
}
newtype Delimiter a = Delimiter [a -> Bool]
matchDelim :: Delimiter a -> [a] -> Maybe ([a], [a])
matchDelim :: forall a. Delimiter a -> [a] -> Maybe ([a], [a])
matchDelim (Delimiter []) [a]
xs = ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([], [a]
xs)
matchDelim (Delimiter [a -> Bool]
_) [] = Maybe ([a], [a])
forall a. Maybe a
Nothing
matchDelim (Delimiter (a -> Bool
p : [a -> Bool]
ps)) (a
x : [a]
xs)
| a -> Bool
p a
x = Delimiter a -> [a] -> Maybe ([a], [a])
forall a. Delimiter a -> [a] -> Maybe ([a], [a])
matchDelim ([a -> Bool] -> Delimiter a
forall a. [a -> Bool] -> Delimiter a
Delimiter [a -> Bool]
ps) [a]
xs Maybe ([a], [a])
-> (([a], [a]) -> Maybe ([a], [a])) -> Maybe ([a], [a])
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \([a]
h, [a]
t) -> ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
h, [a]
t)
| Bool
otherwise = Maybe ([a], [a])
forall a. Maybe a
Nothing
data DelimPolicy
=
Drop
|
Keep
|
KeepLeft
|
KeepRight
deriving (DelimPolicy -> DelimPolicy -> Bool
(DelimPolicy -> DelimPolicy -> Bool)
-> (DelimPolicy -> DelimPolicy -> Bool) -> Eq DelimPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DelimPolicy -> DelimPolicy -> Bool
== :: DelimPolicy -> DelimPolicy -> Bool
$c/= :: DelimPolicy -> DelimPolicy -> Bool
/= :: DelimPolicy -> DelimPolicy -> Bool
Eq, Int -> DelimPolicy -> ShowS
[DelimPolicy] -> ShowS
DelimPolicy -> String
(Int -> DelimPolicy -> ShowS)
-> (DelimPolicy -> String)
-> ([DelimPolicy] -> ShowS)
-> Show DelimPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DelimPolicy -> ShowS
showsPrec :: Int -> DelimPolicy -> ShowS
$cshow :: DelimPolicy -> String
show :: DelimPolicy -> String
$cshowList :: [DelimPolicy] -> ShowS
showList :: [DelimPolicy] -> ShowS
Show)
data CondensePolicy
=
Condense
|
DropBlankFields
|
KeepBlankFields
deriving (CondensePolicy -> CondensePolicy -> Bool
(CondensePolicy -> CondensePolicy -> Bool)
-> (CondensePolicy -> CondensePolicy -> Bool) -> Eq CondensePolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CondensePolicy -> CondensePolicy -> Bool
== :: CondensePolicy -> CondensePolicy -> Bool
$c/= :: CondensePolicy -> CondensePolicy -> Bool
/= :: CondensePolicy -> CondensePolicy -> Bool
Eq, Int -> CondensePolicy -> ShowS
[CondensePolicy] -> ShowS
CondensePolicy -> String
(Int -> CondensePolicy -> ShowS)
-> (CondensePolicy -> String)
-> ([CondensePolicy] -> ShowS)
-> Show CondensePolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CondensePolicy -> ShowS
showsPrec :: Int -> CondensePolicy -> ShowS
$cshow :: CondensePolicy -> String
show :: CondensePolicy -> String
$cshowList :: [CondensePolicy] -> ShowS
showList :: [CondensePolicy] -> ShowS
Show)
data EndPolicy = DropBlank | KeepBlank
deriving (EndPolicy -> EndPolicy -> Bool
(EndPolicy -> EndPolicy -> Bool)
-> (EndPolicy -> EndPolicy -> Bool) -> Eq EndPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EndPolicy -> EndPolicy -> Bool
== :: EndPolicy -> EndPolicy -> Bool
$c/= :: EndPolicy -> EndPolicy -> Bool
/= :: EndPolicy -> EndPolicy -> Bool
Eq, Int -> EndPolicy -> ShowS
[EndPolicy] -> ShowS
EndPolicy -> String
(Int -> EndPolicy -> ShowS)
-> (EndPolicy -> String)
-> ([EndPolicy] -> ShowS)
-> Show EndPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EndPolicy -> ShowS
showsPrec :: Int -> EndPolicy -> ShowS
$cshow :: EndPolicy -> String
show :: EndPolicy -> String
$cshowList :: [EndPolicy] -> ShowS
showList :: [EndPolicy] -> ShowS
Show)
data Chunk a = Delim [a] | Text [a]
deriving (Int -> Chunk a -> ShowS
[Chunk a] -> ShowS
Chunk a -> String
(Int -> Chunk a -> ShowS)
-> (Chunk a -> String) -> ([Chunk a] -> ShowS) -> Show (Chunk a)
forall a. Show a => Int -> Chunk a -> ShowS
forall a. Show a => [Chunk a] -> ShowS
forall a. Show a => Chunk a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Chunk a -> ShowS
showsPrec :: Int -> Chunk a -> ShowS
$cshow :: forall a. Show a => Chunk a -> String
show :: Chunk a -> String
$cshowList :: forall a. Show a => [Chunk a] -> ShowS
showList :: [Chunk a] -> ShowS
Show, Chunk a -> Chunk a -> Bool
(Chunk a -> Chunk a -> Bool)
-> (Chunk a -> Chunk a -> Bool) -> Eq (Chunk a)
forall a. Eq a => Chunk a -> Chunk a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Chunk a -> Chunk a -> Bool
== :: Chunk a -> Chunk a -> Bool
$c/= :: forall a. Eq a => Chunk a -> Chunk a -> Bool
/= :: Chunk a -> Chunk a -> Bool
Eq)
type SplitList a = [Chunk a]
fromElem :: Chunk a -> [a]
fromElem :: forall a. Chunk a -> [a]
fromElem (Text [a]
as) = [a]
as
fromElem (Delim [a]
as) = [a]
as
isDelim :: Chunk a -> Bool
isDelim :: forall a. Chunk a -> Bool
isDelim (Delim [a]
_) = Bool
True
isDelim Chunk a
_ = Bool
False
isText :: Chunk a -> Bool
isText :: forall a. Chunk a -> Bool
isText (Text [a]
_) = Bool
True
isText Chunk a
_ = Bool
False
splitInternal :: Delimiter a -> [a] -> SplitList a
splitInternal :: forall a. Delimiter a -> [a] -> SplitList a
splitInternal Delimiter a
_ [] = []
splitInternal Delimiter a
d [a]
xxs
| [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = Maybe ([a], [a]) -> [Chunk a]
toSplitList Maybe ([a], [a])
match
| Bool
otherwise = [a] -> Chunk a
forall a. [a] -> Chunk a
Text [a]
xs Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: Maybe ([a], [a]) -> [Chunk a]
toSplitList Maybe ([a], [a])
match
where
([a]
xs, Maybe ([a], [a])
match) = Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
forall a. Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
breakDelim Delimiter a
d [a]
xxs
toSplitList :: Maybe ([a], [a]) -> [Chunk a]
toSplitList Maybe ([a], [a])
Nothing = []
toSplitList (Just ([], a
r : [a]
rs)) = [a] -> Chunk a
forall a. [a] -> Chunk a
Delim [] Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [a] -> Chunk a
forall a. [a] -> Chunk a
Text [a
r] Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: Delimiter a -> [a] -> [Chunk a]
forall a. Delimiter a -> [a] -> SplitList a
splitInternal Delimiter a
d [a]
rs
toSplitList (Just ([a]
delim, [a]
rest)) = [a] -> Chunk a
forall a. [a] -> Chunk a
Delim [a]
delim Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: Delimiter a -> [a] -> [Chunk a]
forall a. Delimiter a -> [a] -> SplitList a
splitInternal Delimiter a
d [a]
rest
breakDelim :: Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
breakDelim :: forall a. Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
breakDelim (Delimiter []) [a]
xs = ([], ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([], [a]
xs))
breakDelim Delimiter a
_ [] = ([], Maybe ([a], [a])
forall a. Maybe a
Nothing)
breakDelim Delimiter a
d xxs :: [a]
xxs@(a
x : [a]
xs) =
case Delimiter a -> [a] -> Maybe ([a], [a])
forall a. Delimiter a -> [a] -> Maybe ([a], [a])
matchDelim Delimiter a
d [a]
xxs of
Maybe ([a], [a])
Nothing -> let ([a]
ys, Maybe ([a], [a])
match) = Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
forall a. Delimiter a -> [a] -> ([a], Maybe ([a], [a]))
breakDelim Delimiter a
d [a]
xs in (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys, Maybe ([a], [a])
match)
Just ([a], [a])
match -> ([], ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([a], [a])
match)
postProcess :: Splitter a -> SplitList a -> SplitList a
postProcess :: forall a. Splitter a -> SplitList a -> SplitList a
postProcess Splitter a
s =
EndPolicy -> SplitList a -> SplitList a
forall a. EndPolicy -> SplitList a -> SplitList a
dropFinal (Splitter a -> EndPolicy
forall a. Splitter a -> EndPolicy
finalBlankPolicy Splitter a
s)
(SplitList a -> SplitList a)
-> (SplitList a -> SplitList a) -> SplitList a -> SplitList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EndPolicy -> SplitList a -> SplitList a
forall a. EndPolicy -> SplitList a -> SplitList a
dropInitial (Splitter a -> EndPolicy
forall a. Splitter a -> EndPolicy
initBlankPolicy Splitter a
s)
(SplitList a -> SplitList a)
-> (SplitList a -> SplitList a) -> SplitList a -> SplitList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DelimPolicy -> SplitList a -> SplitList a
forall a. DelimPolicy -> SplitList a -> SplitList a
doMerge (Splitter a -> DelimPolicy
forall a. Splitter a -> DelimPolicy
delimPolicy Splitter a
s)
(SplitList a -> SplitList a)
-> (SplitList a -> SplitList a) -> SplitList a -> SplitList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DelimPolicy -> SplitList a -> SplitList a
forall a. DelimPolicy -> SplitList a -> SplitList a
doDrop (Splitter a -> DelimPolicy
forall a. Splitter a -> DelimPolicy
delimPolicy Splitter a
s)
(SplitList a -> SplitList a)
-> (SplitList a -> SplitList a) -> SplitList a -> SplitList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks (Splitter a -> CondensePolicy
forall a. Splitter a -> CondensePolicy
condensePolicy Splitter a
s)
(SplitList a -> SplitList a)
-> (SplitList a -> SplitList a) -> SplitList a -> SplitList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
doCondense (Splitter a -> CondensePolicy
forall a. Splitter a -> CondensePolicy
condensePolicy Splitter a
s)
doDrop :: DelimPolicy -> SplitList a -> SplitList a
doDrop :: forall a. DelimPolicy -> SplitList a -> SplitList a
doDrop DelimPolicy
Drop SplitList a
l = [Chunk a
c | c :: Chunk a
c@(Text [a]
_) <- SplitList a
l]
doDrop DelimPolicy
_ SplitList a
l = SplitList a
l
doCondense :: CondensePolicy -> SplitList a -> SplitList a
doCondense :: forall a. CondensePolicy -> SplitList a -> SplitList a
doCondense CondensePolicy
Condense SplitList a
ls = SplitList a -> SplitList a
forall {a}. [Chunk a] -> [Chunk a]
condense' SplitList a
ls
where
condense' :: [Chunk a] -> [Chunk a]
condense' [] = []
condense' (c :: Chunk a
c@(Text [a]
_) : [Chunk a]
l) = Chunk a
c Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
condense' [Chunk a]
l
condense' [Chunk a]
l = [a] -> Chunk a
forall a. [a] -> Chunk a
Delim ((Chunk a -> [a]) -> [Chunk a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Chunk a -> [a]
forall a. Chunk a -> [a]
fromElem [Chunk a]
ds) Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
condense' [Chunk a]
rest
where
([Chunk a]
ds, [Chunk a]
rest) = (Chunk a -> Bool) -> [Chunk a] -> ([Chunk a], [Chunk a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Chunk a -> Bool
forall a. Chunk a -> Bool
isDelim [Chunk a]
l
doCondense CondensePolicy
_ SplitList a
ls = SplitList a
ls
insertBlanks :: CondensePolicy -> SplitList a -> SplitList a
insertBlanks :: forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks CondensePolicy
_ [] = [[a] -> Chunk a
forall a. [a] -> Chunk a
Text []]
insertBlanks CondensePolicy
cp (d :: Chunk a
d@(Delim [a]
_) : [Chunk a]
l) = [a] -> Chunk a
forall a. [a] -> Chunk a
Text [] Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: CondensePolicy -> [Chunk a] -> [Chunk a]
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
d Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a]
l)
insertBlanks CondensePolicy
cp [Chunk a]
l = CondensePolicy -> [Chunk a] -> [Chunk a]
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp [Chunk a]
l
insertBlanks' :: CondensePolicy -> SplitList a -> SplitList a
insertBlanks' :: forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
_ [] = []
insertBlanks' cp :: CondensePolicy
cp@CondensePolicy
DropBlankFields (d1 :: Chunk a
d1@(Delim [a]
_) : d2 :: Chunk a
d2@(Delim [a]
_) : [Chunk a]
l) =
Chunk a
d1 Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: CondensePolicy -> [Chunk a] -> [Chunk a]
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
d2 Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a]
l)
insertBlanks' CondensePolicy
cp (d1 :: Chunk a
d1@(Delim [a]
_) : d2 :: Chunk a
d2@(Delim [a]
_) : [Chunk a]
l) =
Chunk a
d1 Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [a] -> Chunk a
forall a. [a] -> Chunk a
Text [] Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: CondensePolicy -> [Chunk a] -> [Chunk a]
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
d2 Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a]
l)
insertBlanks' CondensePolicy
_ [d :: Chunk a
d@(Delim [a]
_)] = [Chunk a
d, [a] -> Chunk a
forall a. [a] -> Chunk a
Text []]
insertBlanks' CondensePolicy
cp (Chunk a
c : [Chunk a]
l) = Chunk a
c Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: CondensePolicy -> [Chunk a] -> [Chunk a]
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp [Chunk a]
l
doMerge :: DelimPolicy -> SplitList a -> SplitList a
doMerge :: forall a. DelimPolicy -> SplitList a -> SplitList a
doMerge DelimPolicy
KeepLeft = SplitList a -> SplitList a
forall {a}. [Chunk a] -> [Chunk a]
mergeLeft
doMerge DelimPolicy
KeepRight = SplitList a -> SplitList a
forall {a}. [Chunk a] -> [Chunk a]
mergeRight
doMerge DelimPolicy
_ = SplitList a -> SplitList a
forall a. a -> a
id
mergeLeft :: SplitList a -> SplitList a
mergeLeft :: forall {a}. [Chunk a] -> [Chunk a]
mergeLeft [] = []
mergeLeft ((Delim [a]
d) : (Text [a]
c) : [Chunk a]
l) = [a] -> Chunk a
forall a. [a] -> Chunk a
Text ([a]
d [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
c) Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
forall {a}. [Chunk a] -> [Chunk a]
mergeLeft [Chunk a]
l
mergeLeft (Chunk a
c : [Chunk a]
l) = Chunk a
c Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
forall {a}. [Chunk a] -> [Chunk a]
mergeLeft [Chunk a]
l
mergeRight :: SplitList a -> SplitList a
mergeRight :: forall {a}. [Chunk a] -> [Chunk a]
mergeRight [] = []
mergeRight ((Text [a]
c) : [Chunk a]
l) = [a] -> Chunk a
forall a. [a] -> Chunk a
Text ([a]
c [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
d) Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
forall {a}. [Chunk a] -> [Chunk a]
mergeRight [Chunk a]
lTail
where
([a]
d, [Chunk a]
lTail) = case [Chunk a]
l of
Delim [a]
d' : [Chunk a]
l' -> ([a]
d', [Chunk a]
l')
[Chunk a]
_ -> ([], [Chunk a]
l)
mergeRight (Chunk a
c : [Chunk a]
l) = Chunk a
c Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
forall {a}. [Chunk a] -> [Chunk a]
mergeRight [Chunk a]
l
dropInitial :: EndPolicy -> SplitList a -> SplitList a
dropInitial :: forall a. EndPolicy -> SplitList a -> SplitList a
dropInitial EndPolicy
DropBlank (Text [] : [Chunk a]
l) = [Chunk a]
l
dropInitial EndPolicy
_ [Chunk a]
l = [Chunk a]
l
dropFinal :: EndPolicy -> SplitList a -> SplitList a
dropFinal :: forall a. EndPolicy -> SplitList a -> SplitList a
dropFinal EndPolicy
_ [] = []
dropFinal EndPolicy
DropBlank [Chunk a]
l = [Chunk a] -> [Chunk a]
forall {a}. [Chunk a] -> [Chunk a]
dropFinal' [Chunk a]
l
where
dropFinal' :: [Chunk a] -> [Chunk a]
dropFinal' [] = []
dropFinal' [Text []] = []
dropFinal' (Chunk a
x : [Chunk a]
xs) = Chunk a
x Chunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
: [Chunk a] -> [Chunk a]
dropFinal' [Chunk a]
xs
dropFinal EndPolicy
_ [Chunk a]
l = [Chunk a]
l
split :: Splitter a -> [a] -> [[a]]
split :: forall a. Splitter a -> [a] -> [[a]]
split Splitter a
s = (Chunk a -> [a]) -> [Chunk a] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map Chunk a -> [a]
forall a. Chunk a -> [a]
fromElem ([Chunk a] -> [[a]]) -> ([a] -> [Chunk a]) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> [Chunk a] -> [Chunk a]
forall a. Splitter a -> SplitList a -> SplitList a
postProcess Splitter a
s ([Chunk a] -> [Chunk a]) -> ([a] -> [Chunk a]) -> [a] -> [Chunk a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Delimiter a -> [a] -> [Chunk a]
forall a. Delimiter a -> [a] -> SplitList a
splitInternal (Splitter a -> Delimiter a
forall a. Splitter a -> Delimiter a
delimiter Splitter a
s)
oneOf :: (Eq a) => [a] -> Splitter a
oneOf :: forall a. Eq a => [a] -> Splitter a
oneOf [a]
elts = Splitter Any
forall a. Splitter a
defaultSplitter {delimiter = Delimiter [(`elem` elts)]}
onSublist :: (Eq a) => [a] -> Splitter a
onSublist :: forall a. Eq a => [a] -> Splitter a
onSublist [a]
lst = Splitter Any
forall a. Splitter a
defaultSplitter {delimiter = Delimiter (map (==) lst)}
whenElt :: (a -> Bool) -> Splitter a
whenElt :: forall a. (a -> Bool) -> Splitter a
whenElt a -> Bool
p = Splitter Any
forall a. Splitter a
defaultSplitter {delimiter = Delimiter [p]}
dropDelims :: Splitter a -> Splitter a
dropDelims :: forall a. Splitter a -> Splitter a
dropDelims Splitter a
s = Splitter a
s {delimPolicy = Drop}
keepDelimsL :: Splitter a -> Splitter a
keepDelimsL :: forall a. Splitter a -> Splitter a
keepDelimsL Splitter a
s = Splitter a
s {delimPolicy = KeepLeft}
keepDelimsR :: Splitter a -> Splitter a
keepDelimsR :: forall a. Splitter a -> Splitter a
keepDelimsR Splitter a
s = Splitter a
s {delimPolicy = KeepRight}
condense :: Splitter a -> Splitter a
condense :: forall a. Splitter a -> Splitter a
condense Splitter a
s = Splitter a
s {condensePolicy = Condense}
dropInitBlank :: Splitter a -> Splitter a
dropInitBlank :: forall a. Splitter a -> Splitter a
dropInitBlank Splitter a
s = Splitter a
s {initBlankPolicy = DropBlank}
dropFinalBlank :: Splitter a -> Splitter a
dropFinalBlank :: forall a. Splitter a -> Splitter a
dropFinalBlank Splitter a
s = Splitter a
s {finalBlankPolicy = DropBlank}
dropInnerBlanks :: Splitter a -> Splitter a
dropInnerBlanks :: forall a. Splitter a -> Splitter a
dropInnerBlanks Splitter a
s = Splitter a
s {condensePolicy = DropBlankFields}
mapSplitter :: (b -> a) -> Splitter a -> Splitter b
mapSplitter :: forall b a. (b -> a) -> Splitter a -> Splitter b
mapSplitter b -> a
f (Splitter Delimiter a
d DelimPolicy
dp CondensePolicy
cp EndPolicy
ibp EndPolicy
fbp) = Delimiter b
-> DelimPolicy
-> CondensePolicy
-> EndPolicy
-> EndPolicy
-> Splitter b
forall a.
Delimiter a
-> DelimPolicy
-> CondensePolicy
-> EndPolicy
-> EndPolicy
-> Splitter a
Splitter ((b -> a) -> Delimiter a -> Delimiter b
forall b a. (b -> a) -> Delimiter a -> Delimiter b
mapDelimiter b -> a
f Delimiter a
d) DelimPolicy
dp CondensePolicy
cp EndPolicy
ibp EndPolicy
fbp
where
mapDelimiter :: (b -> a) -> Delimiter a -> Delimiter b
mapDelimiter :: forall b a. (b -> a) -> Delimiter a -> Delimiter b
mapDelimiter b -> a
g (Delimiter [a -> Bool]
xs) = [b -> Bool] -> Delimiter b
forall a. [a -> Bool] -> Delimiter a
Delimiter ([b -> Bool] -> Delimiter b) -> [b -> Bool] -> Delimiter b
forall a b. (a -> b) -> a -> b
$ ((a -> Bool) -> b -> Bool) -> [a -> Bool] -> [b -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> Bool) -> (b -> a) -> b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
g) [a -> Bool]
xs
dropBlanks :: Splitter a -> Splitter a
dropBlanks :: forall a. Splitter a -> Splitter a
dropBlanks = Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropInitBlank (Splitter a -> Splitter a)
-> (Splitter a -> Splitter a) -> Splitter a -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> (Splitter a -> Splitter a) -> Splitter a -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
condense
startsWith :: (Eq a) => [a] -> Splitter a
startsWith :: forall a. Eq a => [a] -> Splitter a
startsWith = Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropInitBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
keepDelimsL (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
onSublist
startsWithOneOf :: (Eq a) => [a] -> Splitter a
startsWithOneOf :: forall a. Eq a => [a] -> Splitter a
startsWithOneOf = Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropInitBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
keepDelimsL (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
oneOf
endsWith :: (Eq a) => [a] -> Splitter a
endsWith :: forall a. Eq a => [a] -> Splitter a
endsWith = Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
keepDelimsR (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
onSublist
endsWithOneOf :: (Eq a) => [a] -> Splitter a
endsWithOneOf :: forall a. Eq a => [a] -> Splitter a
endsWithOneOf = Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
keepDelimsR (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
oneOf
splitOneOf :: (Eq a) => [a] -> [a] -> [[a]]
splitOneOf :: forall a. Eq a => [a] -> [a] -> [[a]]
splitOneOf = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ([a] -> Splitter a) -> [a] -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
oneOf
splitOn :: (Eq a) => [a] -> [a] -> [[a]]
splitOn :: forall a. Eq a => [a] -> [a] -> [[a]]
splitOn = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ([a] -> Splitter a) -> [a] -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
onSublist
splitWhen :: (a -> Bool) -> [a] -> [[a]]
splitWhen :: forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Splitter a
forall a. (a -> Bool) -> Splitter a
whenElt
{-# DEPRECATED sepBy "Use splitOn." #-}
sepBy :: (Eq a) => [a] -> [a] -> [[a]]
sepBy :: forall a. Eq a => [a] -> [a] -> [[a]]
sepBy = [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn
{-# DEPRECATED sepByOneOf "Use splitOneOf." #-}
sepByOneOf :: (Eq a) => [a] -> [a] -> [[a]]
sepByOneOf :: forall a. Eq a => [a] -> [a] -> [[a]]
sepByOneOf = [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOneOf
endBy :: (Eq a) => [a] -> [a] -> [[a]]
endBy :: forall a. Eq a => [a] -> [a] -> [[a]]
endBy = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ([a] -> Splitter a) -> [a] -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
onSublist
endByOneOf :: (Eq a) => [a] -> [a] -> [[a]]
endByOneOf :: forall a. Eq a => [a] -> [a] -> [[a]]
endByOneOf = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ([a] -> Splitter a) -> [a] -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ([a] -> Splitter a) -> [a] -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Splitter a
forall a. Eq a => [a] -> Splitter a
oneOf
{-# DEPRECATED unintercalate "Use splitOn." #-}
unintercalate :: (Eq a) => [a] -> [a] -> [[a]]
unintercalate :: forall a. Eq a => [a] -> [a] -> [[a]]
unintercalate = [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn
wordsBy :: (a -> Bool) -> [a] -> [[a]]
wordsBy :: forall a. (a -> Bool) -> [a] -> [[a]]
wordsBy = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropBlanks (Splitter a -> Splitter a)
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Splitter a
forall a. (a -> Bool) -> Splitter a
whenElt
linesBy :: (a -> Bool) -> [a] -> [[a]]
linesBy :: forall a. (a -> Bool) -> [a] -> [[a]]
linesBy = Splitter a -> [a] -> [[a]]
forall a. Splitter a -> [a] -> [[a]]
split (Splitter a -> [a] -> [[a]])
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropFinalBlank (Splitter a -> Splitter a)
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Splitter a -> Splitter a
forall a. Splitter a -> Splitter a
dropDelims (Splitter a -> Splitter a)
-> ((a -> Bool) -> Splitter a) -> (a -> Bool) -> Splitter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Splitter a
forall a. (a -> Bool) -> Splitter a
whenElt
build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build :: forall a. ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build (a -> [a] -> [a]) -> [a] -> [a]
g = (a -> [a] -> [a]) -> [a] -> [a]
g (:) []
chunksOf :: Int -> [e] -> [[e]]
chunksOf :: forall e. Int -> [e] -> [[e]]
chunksOf Int
i [e]
ls = ([e] -> [e]) -> [[e]] -> [[e]]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> [e] -> [e]
forall a. Int -> [a] -> [a]
take Int
i) ((([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]) -> [[e]]
forall a. ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build ([e] -> ([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]
forall e a. [e] -> ([e] -> a -> a) -> a -> a
splitter [e]
ls))
where
splitter :: [e] -> ([e] -> a -> a) -> a -> a
splitter :: forall e a. [e] -> ([e] -> a -> a) -> a -> a
splitter [] [e] -> a -> a
_ a
n = a
n
splitter [e]
l [e] -> a -> a
c a
n = [e]
l [e] -> a -> a
`c` [e] -> ([e] -> a -> a) -> a -> a
forall e a. [e] -> ([e] -> a -> a) -> a -> a
splitter (Int -> [e] -> [e]
forall a. Int -> [a] -> [a]
drop Int
i [e]
l) [e] -> a -> a
c a
n
{-# DEPRECATED chunk "Use chunksOf." #-}
chunk :: Int -> [e] -> [[e]]
chunk :: forall e. Int -> [e] -> [[e]]
chunk = Int -> [e] -> [[e]]
forall e. Int -> [e] -> [[e]]
chunksOf
{-# DEPRECATED splitEvery "Use chunksOf." #-}
splitEvery :: Int -> [e] -> [[e]]
splitEvery :: forall e. Int -> [e] -> [[e]]
splitEvery = Int -> [e] -> [[e]]
forall e. Int -> [e] -> [[e]]
chunksOf
splitPlaces :: (Integral a) => [a] -> [e] -> [[e]]
splitPlaces :: forall a e. Integral a => [a] -> [e] -> [[e]]
splitPlaces [a]
is [e]
ys = (([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]) -> [[e]]
forall a. ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build ([a] -> [e] -> ([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]
forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [a]
is [e]
ys)
where
splitPlacer :: (Integral i) => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer :: forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [] [b]
_ [b] -> t -> t
_ t
n = t
n
splitPlacer [i]
_ [] [b] -> t -> t
_ t
n = t
n
splitPlacer (i
l : [i]
ls) [b]
xs [b] -> t -> t
c t
n =
let ([b]
x1, [b]
x2) = i -> [b] -> ([b], [b])
forall i a. Integral i => i -> [a] -> ([a], [a])
genericSplitAt i
l [b]
xs
in [b]
x1 [b] -> t -> t
`c` [i] -> [b] -> ([b] -> t -> t) -> t -> t
forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [i]
ls [b]
x2 [b] -> t -> t
c t
n
splitPlacesBlanks :: (Integral a) => [a] -> [e] -> [[e]]
splitPlacesBlanks :: forall a e. Integral a => [a] -> [e] -> [[e]]
splitPlacesBlanks [a]
is [e]
ys = (([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]) -> [[e]]
forall a. ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build ([a] -> [e] -> ([e] -> [[e]] -> [[e]]) -> [[e]] -> [[e]]
forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [a]
is [e]
ys)
where
splitPlacer :: (Integral i) => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer :: forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [] [b]
_ [b] -> t -> t
_ t
n = t
n
splitPlacer (i
l : [i]
ls) [b]
xs [b] -> t -> t
c t
n =
let ([b]
x1, [b]
x2) = i -> [b] -> ([b], [b])
forall i a. Integral i => i -> [a] -> ([a], [a])
genericSplitAt i
l [b]
xs
in [b]
x1 [b] -> t -> t
`c` [i] -> [b] -> ([b] -> t -> t) -> t -> t
forall i b t. Integral i => [i] -> [b] -> ([b] -> t -> t) -> t -> t
splitPlacer [i]
ls [b]
x2 [b] -> t -> t
c t
n
chop :: ([a] -> (b, [a])) -> [a] -> [b]
chop :: forall a b. ([a] -> (b, [a])) -> [a] -> [b]
chop [a] -> (b, [a])
_ [] = []
chop [a] -> (b, [a])
f [a]
as = b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
: ([a] -> (b, [a])) -> [a] -> [b]
forall a b. ([a] -> (b, [a])) -> [a] -> [b]
chop [a] -> (b, [a])
f [a]
as'
where
(b
b, [a]
as') = [a] -> (b, [a])
f [a]
as
divvy :: Int -> Int -> [a] -> [[a]]
divvy :: forall a. Int -> Int -> [a] -> [[a]]
divvy Int
_ Int
_ [] = []
divvy Int
n Int
m [a]
lst = ([a] -> Bool) -> [[a]] -> [[a]]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==) (Int -> Bool) -> ([a] -> Int) -> [a] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length) [[a]]
choppedl
where
choppedl :: [[a]]
choppedl = ([a] -> ([a], [a])) -> [a] -> [[a]]
forall a b. ([a] -> (b, [a])) -> [a] -> [b]
chop (\[a]
xs -> (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
xs, Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
m [a]
xs)) [a]
lst