{-# LANGUAGE Safe #-}
module Data.List.Utils(
merge, mergeBy,
startswith, endswith, contains, hasAny,
addToAL, delFromAL, flipAL, keysAL, valuesAL,
hasKeyAL,
strFromAL,
strToAL,
split, join, replace, genericJoin, takeWhileList,
dropWhileList, spanList, breakList,
WholeFunc(..), wholeMap, fixedWidth,
grab,
countElem, elemRIndex, alwaysElemRIndex, seqList,
subIndex, uniq
) where
import Control.Monad.State (State, get, put)
import Data.List (elemIndices, findIndex, intercalate,
isInfixOf, isPrefixOf, isSuffixOf, nub,
tails)
merge :: (Ord a) => [a] -> [a] -> [a]
merge :: forall a. Ord a => [a] -> [a] -> [a]
merge = forall a. (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy (forall a. Ord a => a -> a -> Ordering
compare)
mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy :: forall a. (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy a -> a -> Ordering
_ [] [a]
ys = [a]
ys
mergeBy a -> a -> Ordering
_ [a]
xs [] = [a]
xs
mergeBy a -> a -> Ordering
cmp (allx :: [a]
allx@(a
x:[a]
xs)) (ally :: [a]
ally@(a
y:[a]
ys))
| (a
x a -> a -> Ordering
`cmp` a
y) forall a. Ord a => a -> a -> Bool
<= Ordering
EQ = a
x forall a. a -> [a] -> [a]
: forall a. (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy a -> a -> Ordering
cmp [a]
xs [a]
ally
| Bool
otherwise = a
y forall a. a -> [a] -> [a]
: forall a. (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy a -> a -> Ordering
cmp [a]
allx [a]
ys
startswith :: Eq a => [a] -> [a] -> Bool
startswith :: forall a. Eq a => [a] -> [a] -> Bool
startswith = forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf
endswith :: Eq a => [a] -> [a] -> Bool
endswith :: forall a. Eq a => [a] -> [a] -> Bool
endswith = forall a. Eq a => [a] -> [a] -> Bool
isSuffixOf
hasAny :: Eq a => [a]
-> [a]
-> Bool
hasAny :: forall a. Eq a => [a] -> [a] -> Bool
hasAny [] [a]
_ = Bool
False
hasAny [a]
_ [] = Bool
False
hasAny [a]
search (a
x:[a]
xs) = if a
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
search then Bool
True else forall a. Eq a => [a] -> [a] -> Bool
hasAny [a]
search [a]
xs
takeWhileList :: ([a] -> Bool) -> [a] -> [a]
takeWhileList :: forall a. ([a] -> Bool) -> [a] -> [a]
takeWhileList [a] -> Bool
_ [] = []
takeWhileList [a] -> Bool
func list :: [a]
list@(a
x:[a]
xs) =
if [a] -> Bool
func [a]
list
then a
x forall a. a -> [a] -> [a]
: forall a. ([a] -> Bool) -> [a] -> [a]
takeWhileList [a] -> Bool
func [a]
xs
else []
dropWhileList :: ([a] -> Bool) -> [a] -> [a]
dropWhileList :: forall a. ([a] -> Bool) -> [a] -> [a]
dropWhileList [a] -> Bool
_ [] = []
dropWhileList [a] -> Bool
func list :: [a]
list@(a
_:[a]
xs) =
if [a] -> Bool
func [a]
list
then forall a. ([a] -> Bool) -> [a] -> [a]
dropWhileList [a] -> Bool
func [a]
xs
else [a]
list
spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
spanList :: forall a. ([a] -> Bool) -> [a] -> ([a], [a])
spanList [a] -> Bool
_ [] = ([],[])
spanList [a] -> Bool
func list :: [a]
list@(a
x:[a]
xs) =
if [a] -> Bool
func [a]
list
then (a
xforall a. a -> [a] -> [a]
:[a]
ys,[a]
zs)
else ([],[a]
list)
where ([a]
ys,[a]
zs) = forall a. ([a] -> Bool) -> [a] -> ([a], [a])
spanList [a] -> Bool
func [a]
xs
breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
breakList :: forall a. ([a] -> Bool) -> [a] -> ([a], [a])
breakList [a] -> Bool
func = forall a. ([a] -> Bool) -> [a] -> ([a], [a])
spanList (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Bool
func)
split :: Eq a => [a] -> [a] -> [[a]]
split :: forall a. Eq a => [a] -> [a] -> [[a]]
split [a]
_ [] = []
split [a]
delim [a]
str =
let ([a]
firstline, [a]
remainder) = forall a. ([a] -> Bool) -> [a] -> ([a], [a])
breakList (forall a. Eq a => [a] -> [a] -> Bool
startswith [a]
delim) [a]
str
in
[a]
firstline forall a. a -> [a] -> [a]
: case [a]
remainder of
[] -> []
[a]
x -> if [a]
x forall a. Eq a => a -> a -> Bool
== [a]
delim
then [] forall a. a -> [a] -> [a]
: []
else forall a. Eq a => [a] -> [a] -> [[a]]
split [a]
delim
(forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
delim) [a]
x)
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace :: forall a. Eq a => [a] -> [a] -> [a] -> [a]
replace [a]
old [a]
new [a]
l = forall a. [a] -> [[a]] -> [a]
join [a]
new forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [a] -> [[a]]
split [a]
old forall a b. (a -> b) -> a -> b
$ [a]
l
join :: [a] -> [[a]] -> [a]
join :: forall a. [a] -> [[a]] -> [a]
join = forall a. [a] -> [[a]] -> [a]
intercalate
genericJoin :: Show a => String -> [a] -> String
genericJoin :: forall a. Show a => String -> [a] -> String
genericJoin String
delim [a]
l = forall a. [a] -> [[a]] -> [a]
join String
delim (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [a]
l)
contains :: Eq a => [a] -> [a] -> Bool
contains :: forall a. Eq a => [a] -> [a] -> Bool
contains = forall a. Eq a => [a] -> [a] -> Bool
isInfixOf
addToAL :: Eq key => [(key, elt)] -> key -> elt -> [(key, elt)]
addToAL :: forall key elt.
Eq key =>
[(key, elt)] -> key -> elt -> [(key, elt)]
addToAL [(key, elt)]
l key
key elt
value = (key
key, elt
value) forall a. a -> [a] -> [a]
: forall key a. Eq key => [(key, a)] -> key -> [(key, a)]
delFromAL [(key, elt)]
l key
key
delFromAL :: Eq key => [(key, a)] -> key -> [(key, a)]
delFromAL :: forall key a. Eq key => [(key, a)] -> key -> [(key, a)]
delFromAL [(key, a)]
l key
key = forall a. (a -> Bool) -> [a] -> [a]
filter (\(key, a)
a -> (forall a b. (a, b) -> a
fst (key, a)
a) forall a. Eq a => a -> a -> Bool
/= key
key) [(key, a)]
l
keysAL :: [(key, a)] -> [key]
keysAL :: forall key a. [(key, a)] -> [key]
keysAL = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst
valuesAL :: [(a, value)] -> [value]
valuesAL :: forall a value. [(a, value)] -> [value]
valuesAL = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd
hasKeyAL :: Eq a => a -> [(a, b)] -> Bool
hasKeyAL :: forall a b. Eq a => a -> [(a, b)] -> Bool
hasKeyAL a
key [(a, b)]
list =
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
key (forall key a. [(key, a)] -> [key]
keysAL [(a, b)]
list)
flipAL :: (Eq key, Eq val) => [(key, val)] -> [(val, [key])]
flipAL :: forall key val. (Eq key, Eq val) => [(key, val)] -> [(val, [key])]
flipAL [(key, val)]
oldl =
let worker :: (Eq key, Eq val) => [(key, val)] -> [(val, [key])] -> [(val, [key])]
worker :: forall key val.
(Eq key, Eq val) =>
[(key, val)] -> [(val, [key])] -> [(val, [key])]
worker [] [(val, [key])]
accum = [(val, [key])]
accum
worker ((key
k, val
v):[(key, val)]
xs) [(val, [key])]
accum =
case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup val
v [(val, [key])]
accum of
Maybe [key]
Nothing -> forall key val.
(Eq key, Eq val) =>
[(key, val)] -> [(val, [key])] -> [(val, [key])]
worker [(key, val)]
xs ((val
v, [key
k]) forall a. a -> [a] -> [a]
: [(val, [key])]
accum)
Just [key]
y -> forall key val.
(Eq key, Eq val) =>
[(key, val)] -> [(val, [key])] -> [(val, [key])]
worker [(key, val)]
xs (forall key elt.
Eq key =>
[(key, elt)] -> key -> elt -> [(key, elt)]
addToAL [(val, [key])]
accum val
v (key
kforall a. a -> [a] -> [a]
:[key]
y))
in
forall key val.
(Eq key, Eq val) =>
[(key, val)] -> [(val, [key])] -> [(val, [key])]
worker [(key, val)]
oldl []
strFromAL :: (Show a, Show b) => [(a, b)] -> String
strFromAL :: forall a b. (Show a, Show b) => [(a, b)] -> String
strFromAL [(a, b)]
inp =
let worker :: (a, a) -> String
worker (a
key, a
val) = forall a. Show a => a -> String
show a
key forall a. [a] -> [a] -> [a]
++ String
"," forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show a
val
in [String] -> String
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall {a} {a}. (Show a, Show a) => (a, a) -> String
worker forall a b. (a -> b) -> a -> b
$ [(a, b)]
inp
strToAL :: (Read a, Read b) => String -> [(a, b)]
strToAL :: forall a b. (Read a, Read b) => String -> [(a, b)]
strToAL String
inp =
let worker :: String -> (a, b)
worker String
line =
case forall a. Read a => ReadS a
reads String
line of
[(a
key, String
remainder)] -> case String
remainder of
Char
',':String
valstr -> (a
key, forall a. Read a => String -> a
read String
valstr)
String
_ -> forall a. HasCallStack => String -> a
error String
"Data.List.Utils.strToAL: Parse error on value"
[(a, String)]
_ -> forall a. HasCallStack => String -> a
error String
"Data.List.Utils.strToAL: Parse error on key"
in forall a b. (a -> b) -> [a] -> [b]
map forall {a} {b}. (Read a, Read b) => String -> (a, b)
worker (String -> [String]
lines String
inp)
countElem :: Eq a => a -> [a] -> Int
countElem :: forall a. Eq a => a -> [a] -> Int
countElem a
i = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (a
iforall a. Eq a => a -> a -> Bool
==)
elemRIndex :: Eq a => a -> [a] -> Maybe Int
elemRIndex :: forall a. Eq a => a -> [a] -> Maybe Int
elemRIndex a
item [a]
l =
case forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. Eq a => a -> [a] -> [Int]
elemIndices a
item [a]
l of
[] -> forall a. Maybe a
Nothing
(Int
x:[Int]
_) -> forall a. a -> Maybe a
Just Int
x
alwaysElemRIndex :: Eq a => a -> [a] -> Int
alwaysElemRIndex :: forall a. Eq a => a -> [a] -> Int
alwaysElemRIndex a
item [a]
list =
case forall a. Eq a => a -> [a] -> Maybe Int
elemRIndex a
item [a]
list of
Maybe Int
Nothing -> -Int
1
Just Int
x -> Int
x
seqList :: [a] -> [a]
seqList :: forall a. [a] -> [a]
seqList [] = []
seqList list :: [a]
list@(a
_:[a]
xs) = seq :: forall a b. a -> b -> b
seq (forall a. [a] -> [a]
seqList [a]
xs) [a]
list
newtype WholeFunc a b = WholeFunc ([a] -> (WholeFunc a b, [a], [b]))
wholeMap :: WholeFunc a b -> [a] -> [b]
wholeMap :: forall a b. WholeFunc a b -> [a] -> [b]
wholeMap WholeFunc a b
_ [] = []
wholeMap (WholeFunc [a] -> (WholeFunc a b, [a], [b])
func) [a]
inplist =
let (WholeFunc a b
nextfunc, [a]
nextlist, [b]
output) = [a] -> (WholeFunc a b, [a], [b])
func [a]
inplist
in
[b]
output forall a. [a] -> [a] -> [a]
++ forall a b. WholeFunc a b -> [a] -> [b]
wholeMap WholeFunc a b
nextfunc [a]
nextlist
fixedWidth :: [Int] -> WholeFunc a [a]
fixedWidth :: forall a. [Int] -> WholeFunc a [a]
fixedWidth =
forall a b. ([a] -> (WholeFunc a b, [a], [b])) -> WholeFunc a b
WholeFunc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {a}. [Int] -> [a] -> (WholeFunc a [a], [a], [[a]])
fixedWidthFunc
where
fixedWidthFunc :: [Int] -> [a] -> (WholeFunc a [a], [a], [[a]])
fixedWidthFunc [Int]
_ [] = ((forall a. [Int] -> WholeFunc a [a]
fixedWidth []), [], [])
fixedWidthFunc [] [a]
x = ((forall a. [Int] -> WholeFunc a [a]
fixedWidth []), [], [[a]
x])
fixedWidthFunc (Int
len:[Int]
lenxs) [a]
input =
(forall a. [Int] -> WholeFunc a [a]
fixedWidth [Int]
lenxs, [a]
next, [[a]
this])
where ([a]
this, [a]
next) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
len [a]
input
grab :: Int -> State [a] [a]
grab :: forall a. Int -> State [a] [a]
grab Int
count =
do [a]
g <- forall s (m :: * -> *). MonadState s m => m s
get
([a]
x, [a]
g') <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> ([a], [a])
splitAt Int
count [a]
g
forall s (m :: * -> *). MonadState s m => s -> m ()
put [a]
g'
forall (m :: * -> *) a. Monad m => a -> m a
return [a]
x
subIndex :: Eq a => [a] -> [a] -> Maybe Int
subIndex :: forall a. Eq a => [a] -> [a] -> Maybe Int
subIndex [a]
substr [a]
str = forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
substr) (forall a. [a] -> [[a]]
tails [a]
str)
uniq :: Eq a => [a] -> [a]
uniq :: forall a. Eq a => [a] -> [a]
uniq = forall a. Eq a => [a] -> [a]
nub