{-# OPTIONS_HADDOCK prune #-}
module Data.List.Split.Internals where
import Data.List (genericSplitAt)
data Splitter a = Splitter { Splitter a -> Delimiter a
delimiter :: Delimiter a
, Splitter a -> DelimPolicy
delimPolicy :: DelimPolicy
, Splitter a -> CondensePolicy
condensePolicy :: CondensePolicy
, Splitter a -> EndPolicy
initBlankPolicy :: EndPolicy
, Splitter a -> EndPolicy
finalBlankPolicy :: EndPolicy
}
defaultSplitter :: Splitter a
defaultSplitter :: Splitter a
defaultSplitter = Splitter :: forall a.
Delimiter a
-> DelimPolicy
-> CondensePolicy
-> EndPolicy
-> EndPolicy
-> Splitter a
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 :: 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 (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
xa -> [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
/= :: DelimPolicy -> DelimPolicy -> Bool
$c/= :: DelimPolicy -> DelimPolicy -> Bool
== :: DelimPolicy -> DelimPolicy -> Bool
$c== :: 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
showList :: [DelimPolicy] -> ShowS
$cshowList :: [DelimPolicy] -> ShowS
show :: DelimPolicy -> String
$cshow :: DelimPolicy -> String
showsPrec :: Int -> DelimPolicy -> ShowS
$cshowsPrec :: Int -> 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
/= :: CondensePolicy -> CondensePolicy -> Bool
$c/= :: CondensePolicy -> CondensePolicy -> Bool
== :: CondensePolicy -> CondensePolicy -> Bool
$c== :: 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
showList :: [CondensePolicy] -> ShowS
$cshowList :: [CondensePolicy] -> ShowS
show :: CondensePolicy -> String
$cshow :: CondensePolicy -> String
showsPrec :: Int -> CondensePolicy -> ShowS
$cshowsPrec :: Int -> 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
/= :: EndPolicy -> EndPolicy -> Bool
$c/= :: EndPolicy -> EndPolicy -> Bool
== :: EndPolicy -> EndPolicy -> Bool
$c== :: 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
showList :: [EndPolicy] -> ShowS
$cshowList :: [EndPolicy] -> ShowS
show :: EndPolicy -> String
$cshow :: EndPolicy -> String
showsPrec :: Int -> EndPolicy -> ShowS
$cshowsPrec :: Int -> 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
showList :: [Chunk a] -> ShowS
$cshowList :: forall a. Show a => [Chunk a] -> ShowS
show :: Chunk a -> String
$cshow :: forall a. Show a => Chunk a -> String
showsPrec :: Int -> Chunk a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> 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
/= :: Chunk a -> Chunk a -> Bool
$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
Eq)
type SplitList a = [Chunk a]
fromElem :: Chunk a -> [a]
fromElem :: Chunk a -> [a]
fromElem (Text [a]
as) = [a]
as
fromElem (Delim [a]
as) = [a]
as
isDelim :: Chunk a -> Bool
isDelim :: Chunk a -> Bool
isDelim (Delim [a]
_) = Bool
True
isDelim Chunk a
_ = Bool
False
isText :: Chunk a -> Bool
isText :: Chunk a -> Bool
isText (Text [a]
_) = Bool
True
isText Chunk a
_ = Bool
False
splitInternal :: Delimiter a -> [a] -> SplitList a
splitInternal :: Delimiter a -> [a] -> SplitList a
splitInternal Delimiter a
_ [] = []
splitInternal Delimiter a
d [a]
xxs
| [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = Maybe ([a], [a]) -> SplitList a
toSplitList Maybe ([a], [a])
match
| Bool
otherwise = [a] -> Chunk a
forall a. [a] -> Chunk a
Text [a]
xs Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: Maybe ([a], [a]) -> SplitList 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]) -> SplitList a
toSplitList Maybe ([a], [a])
Nothing = []
toSplitList (Just ([],a
r:[a]
rs)) = [a] -> Chunk a
forall a. [a] -> Chunk a
Delim [] Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: [a] -> Chunk a
forall a. [a] -> Chunk a
Text [a
r] Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: Delimiter a -> [a] -> SplitList 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 -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: Delimiter a -> [a] -> SplitList a
forall a. Delimiter a -> [a] -> SplitList a
splitInternal Delimiter a
d [a]
rest
breakDelim :: Delimiter a -> [a] -> ([a],Maybe ([a],[a]))
breakDelim :: 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
xa -> [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 :: 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 :: 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 :: 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 ([a] -> Chunk a) -> [a] -> Chunk a
forall a b. (a -> b) -> a -> b
$ (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 :: 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]
_) : SplitList a
l) = [a] -> Chunk a
forall a. [a] -> Chunk a
Text [] Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
dChunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
:SplitList a
l)
insertBlanks CondensePolicy
cp SplitList a
l = CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp SplitList a
l
insertBlanks' :: CondensePolicy -> SplitList a -> SplitList a
insertBlanks' :: 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]
_) : SplitList a
l)
= Chunk a
d1 Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
d2Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
:SplitList a
l)
insertBlanks' CondensePolicy
cp (d1 :: Chunk a
d1@(Delim [a]
_) : d2 :: Chunk a
d2@(Delim [a]
_) : SplitList a
l)
= Chunk a
d1 Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: [a] -> Chunk a
forall a. [a] -> Chunk a
Text [] Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp (Chunk a
d2Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
:SplitList 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 : SplitList a
l) = Chunk a
c Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: CondensePolicy -> SplitList a -> SplitList a
forall a. CondensePolicy -> SplitList a -> SplitList a
insertBlanks' CondensePolicy
cp SplitList a
l
doMerge :: DelimPolicy -> SplitList a -> SplitList a
doMerge :: 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 :: SplitList a -> SplitList a
mergeLeft [] = []
mergeLeft ((Delim [a]
d) : (Text [a]
c) : SplitList 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 -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: SplitList a -> SplitList a
forall a. [Chunk a] -> [Chunk a]
mergeLeft SplitList a
l
mergeLeft (Chunk a
c : SplitList a
l) = Chunk a
c Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: SplitList a -> SplitList a
forall a. [Chunk a] -> [Chunk a]
mergeLeft SplitList a
l
mergeRight :: SplitList a -> SplitList a
mergeRight :: SplitList a -> SplitList a
mergeRight [] = []
mergeRight ((Text [a]
c) : SplitList 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 -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: SplitList a -> SplitList a
forall a. [Chunk a] -> [Chunk a]
mergeRight SplitList a
lTail
where ([a]
d, SplitList a
lTail) = case SplitList a
l of
Delim [a]
d' : SplitList a
l' -> ([a]
d', SplitList a
l')
SplitList a
_ -> ([], SplitList a
l)
mergeRight (Chunk a
c : SplitList a
l) = Chunk a
c Chunk a -> SplitList a -> SplitList a
forall a. a -> [a] -> [a]
: SplitList a -> SplitList a
forall a. [Chunk a] -> [Chunk a]
mergeRight SplitList a
l
dropInitial :: EndPolicy -> SplitList a -> SplitList a
dropInitial :: EndPolicy -> SplitList a -> SplitList a
dropInitial EndPolicy
DropBlank (Text [] : SplitList a
l) = SplitList a
l
dropInitial EndPolicy
_ SplitList a
l = SplitList a
l
dropFinal :: EndPolicy -> SplitList a -> SplitList a
dropFinal :: EndPolicy -> SplitList a -> SplitList a
dropFinal EndPolicy
_ [] = []
dropFinal EndPolicy
DropBlank SplitList a
l = SplitList a -> SplitList a
forall a. [Chunk a] -> [Chunk a]
dropFinal' SplitList a
l
where dropFinal' :: [Chunk a] -> [Chunk a]
dropFinal' [] = []
dropFinal' [Text []] = []
dropFinal' (Chunk a
x:[Chunk a]
xs) = Chunk a
xChunk a -> [Chunk a] -> [Chunk a]
forall a. a -> [a] -> [a]
:[Chunk a] -> [Chunk a]
dropFinal' [Chunk a]
xs
dropFinal EndPolicy
_ SplitList a
l = SplitList a
l
split :: Splitter a -> [a] -> [[a]]
split :: 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 :: [a] -> Splitter a
oneOf [a]
elts = Splitter Any
forall a. Splitter a
defaultSplitter { delimiter :: Delimiter a
delimiter = [a -> Bool] -> Delimiter a
forall a. [a -> Bool] -> Delimiter a
Delimiter [(a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
elts)] }
onSublist :: Eq a => [a] -> Splitter a
onSublist :: [a] -> Splitter a
onSublist [a]
lst = Splitter Any
forall a. Splitter a
defaultSplitter { delimiter :: Delimiter a
delimiter = [a -> Bool] -> Delimiter a
forall a. [a -> Bool] -> Delimiter a
Delimiter ((a -> a -> Bool) -> [a] -> [a -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) [a]
lst) }
whenElt :: (a -> Bool) -> Splitter a
whenElt :: (a -> Bool) -> Splitter a
whenElt a -> Bool
p = Splitter Any
forall a. Splitter a
defaultSplitter { delimiter :: Delimiter a
delimiter = [a -> Bool] -> Delimiter a
forall a. [a -> Bool] -> Delimiter a
Delimiter [a -> Bool
p] }
dropDelims :: Splitter a -> Splitter a
dropDelims :: Splitter a -> Splitter a
dropDelims Splitter a
s = Splitter a
s { delimPolicy :: DelimPolicy
delimPolicy = DelimPolicy
Drop }
keepDelimsL :: Splitter a -> Splitter a
keepDelimsL :: Splitter a -> Splitter a
keepDelimsL Splitter a
s = Splitter a
s { delimPolicy :: DelimPolicy
delimPolicy = DelimPolicy
KeepLeft }
keepDelimsR :: Splitter a -> Splitter a
keepDelimsR :: Splitter a -> Splitter a
keepDelimsR Splitter a
s = Splitter a
s { delimPolicy :: DelimPolicy
delimPolicy = DelimPolicy
KeepRight }
condense :: Splitter a -> Splitter a
condense :: Splitter a -> Splitter a
condense Splitter a
s = Splitter a
s { condensePolicy :: CondensePolicy
condensePolicy = CondensePolicy
Condense }
dropInitBlank :: Splitter a -> Splitter a
dropInitBlank :: Splitter a -> Splitter a
dropInitBlank Splitter a
s = Splitter a
s { initBlankPolicy :: EndPolicy
initBlankPolicy = EndPolicy
DropBlank }
dropFinalBlank :: Splitter a -> Splitter a
dropFinalBlank :: Splitter a -> Splitter a
dropFinalBlank Splitter a
s = Splitter a
s { finalBlankPolicy :: EndPolicy
finalBlankPolicy = EndPolicy
DropBlank }
dropInnerBlanks :: Splitter a -> Splitter a
dropInnerBlanks :: Splitter a -> Splitter a
dropInnerBlanks Splitter a
s = Splitter a
s { condensePolicy :: CondensePolicy
condensePolicy = CondensePolicy
DropBlankFields }
dropBlanks :: Splitter a -> Splitter a
dropBlanks :: 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 :: [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 :: [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 :: [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 :: [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 :: [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 :: [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 :: (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 :: [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 :: [a] -> [a] -> [[a]]
sepByOneOf = [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOneOf
endBy :: Eq a => [a] -> [a] -> [[a]]
endBy :: [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 :: [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 :: [a] -> [a] -> [[a]]
unintercalate = [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn
wordsBy :: (a -> Bool) -> [a] -> [[a]]
wordsBy :: (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 :: (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 :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build (a -> [a] -> [a]) -> [a] -> [a]
g = (a -> [a] -> [a]) -> [a] -> [a]
g (:) []
chunksOf :: Int -> [e] -> [[e]]
chunksOf :: 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 :: [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 :: Int -> [e] -> [[e]]
chunk = Int -> [e] -> [[e]]
forall e. Int -> [e] -> [[e]]
chunksOf
{-# DEPRECATED splitEvery "Use chunksOf." #-}
splitEvery :: Int -> [e] -> [[e]]
splitEvery :: Int -> [e] -> [[e]]
splitEvery = Int -> [e] -> [[e]]
forall e. Int -> [e] -> [[e]]
chunksOf
splitPlaces :: Integral a => [a] -> [e] -> [[e]]
splitPlaces :: [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 :: [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 :: [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 :: [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 :: ([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 :: 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 (\[a]
ws -> (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ws)) [[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