{-# OPTIONS -fno-warn-orphans #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE StandaloneDeriving #-}
module Data.List where
import Prelude
import Data.Maybe
isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
isPrefixOf :: [a] -> [a] -> Bool
isPrefixOf [] [a]
_ = Bool
True
isPrefixOf [a]
_ [] = Bool
False
isPrefixOf (a
x:[a]
xs) (a
y:[a]
ys)= a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y Bool -> Bool -> Bool
&& [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
xs [a]
ys
isSuffixOf :: (Eq a) => [a] -> [a] -> Bool
isSuffixOf :: [a] -> [a] -> Bool
isSuffixOf [a]
x [a]
y = [a] -> [a]
forall a. [a] -> [a]
reverse [a]
x [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [a] -> [a]
forall a. [a] -> [a]
reverse [a]
y
stripPrefix :: Eq a => [a] -> [a] -> Maybe [a]
stripPrefix :: [a] -> [a] -> Maybe [a]
stripPrefix [] [a]
ys = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
ys
stripPrefix (a
x:[a]
xs) (a
y:[a]
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix [a]
xs [a]
ys
stripPrefix [a]
_ [a]
_ = Maybe [a]
forall a. Maybe a
Nothing
stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
stripSuffix :: [a] -> [a] -> Maybe [a]
stripSuffix [a]
x [a]
y = ([a] -> [a]) -> Maybe [a] -> Maybe [a]
forall a b. (a -> b) -> Maybe a -> Maybe b
onJust [a] -> [a]
forall a. [a] -> [a]
reverse (Maybe [a] -> Maybe [a]) -> Maybe [a] -> Maybe [a]
forall t1 t. (t1 -> t) -> t1 -> t
$ [a] -> [a]
forall a. [a] -> [a]
reverse [a]
x [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
`stripPrefix` [a] -> [a]
forall a. [a] -> [a]
reverse [a]
y
splitWhen :: (a -> Bool) -> [a] -> [[a]]
splitWhen :: (a -> Bool) -> [a] -> [[a]]
splitWhen a -> Bool
p [a]
s = case (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile a -> Bool
p [a]
s of
[] -> []
[a]
s' -> case (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p [a]
s' of
([a]
w, [a]
s'') -> [a]
w [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [[a]]
forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen a -> Bool
p [a]
s''
splitOn :: Eq a => a -> [a] -> [[a]]
splitOn :: a -> [a] -> [[a]]
splitOn a
c = (a -> Bool) -> [a] -> [[a]]
forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
c)
partition :: (a -> Bool) -> [a] -> ([a],[a])
partition :: (a -> Bool) -> [a] -> ([a], [a])
partition a -> Bool
p [a]
xs = ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
p [a]
xs, (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall t1 t t2. (t1 -> t) -> (t2 -> t1) -> t2 -> t
. a -> Bool
p) [a]
xs)
inits :: [a] -> [[a]]
inits :: [a] -> [[a]]
inits [a]
xs = [] [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: case [a]
xs of
[] -> []
a
x : [a]
xs' -> ([a] -> [a]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [[a]]
forall a. [a] -> [[a]]
inits [a]
xs')
groupSortBy :: (a -> a -> Ordering) -> [a] -> [[a]]
groupSortBy :: (a -> a -> Ordering) -> [a] -> [[a]]
groupSortBy a -> a -> Ordering
f = (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\a
x a
y -> a -> a -> Ordering
f a
x a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ) ([a] -> [[a]]) -> ([a] -> [a]) -> [a] -> [[a]]
forall t1 t t2. (t1 -> t) -> (t2 -> t1) -> t2 -> t
. (a -> a -> Ordering) -> [a] -> [a]
forall t. (t -> t -> Ordering) -> [t] -> [t]
sortBy a -> a -> Ordering
f
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy a -> a -> Bool
_ [] = []
groupBy a -> a -> Bool
eq (a
x:[a]
xs) = case (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (a -> a -> Bool
eq a
x) [a]
xs of
([a]
ys,[a]
zs) -> (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys) [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy a -> a -> Bool
eq [a]
zs
findM :: (a -> Fay (Maybe b)) -> [a] -> Fay (Maybe b)
findM :: (a -> Fay (Maybe b)) -> [a] -> Fay (Maybe b)
findM a -> Fay (Maybe b)
_ [] = Maybe b -> Fay (Maybe b)
forall a. a -> Fay a
return Maybe b
forall a. Maybe a
Nothing
findM a -> Fay (Maybe b)
f (a
x:[a]
xs) = do
Maybe b
b <- a -> Fay (Maybe b)
f a
x
case Maybe b
b of
Maybe b
Nothing -> (a -> Fay (Maybe b)) -> [a] -> Fay (Maybe b)
forall a b. (a -> Fay (Maybe b)) -> [a] -> Fay (Maybe b)
findM a -> Fay (Maybe b)
f [a]
xs
Just b
_ -> Maybe b -> Fay (Maybe b)
forall a. a -> Fay a
return Maybe b
b