{-# LANGUAGE ScopedTypeVariables
,TypeFamilies
,MultiParamTypeClasses
,FunctionalDependencies
,FlexibleInstances
,BangPatterns
,FlexibleContexts
,ConstraintKinds
,CPP #-}
{-# LANGUAGE TypeOperators #-}
module Data.ListLike.Base
(
ListLike(..), ListOps,
toList, fromList,
InfiniteListLike(..),
zip, zipWith, sequence_
) where
import Prelude
( Applicative(..), Bool(..), Eq(..), Int, Integer, Integral
, Maybe(..), Monad, Monoid(..), Num(..), Ord(..), Ordering(..)
, ($), (.), (&&), (||), (++), asTypeOf, error, flip, fst, snd
, id, maybe, max, min, not, otherwise
, sequenceA
)
#if MIN_VERSION_base(4,17,0)
import Data.Type.Equality
#endif
import qualified Data.List as L
import Data.ListLike.FoldableLL
( FoldableLL(foldr, foldr1, foldl), fold, foldMap, sequence_ )
import qualified Control.Applicative as A
import Data.Monoid ( All(All, getAll), Any(Any, getAny) )
import Data.Maybe ( listToMaybe )
import GHC.Exts (IsList(Item, fromList, toList))
class (IsList full, item ~ Item full, FoldableLL full item, Monoid full) =>
ListLike full item | full -> item where
empty :: full
empty = forall a. Monoid a => a
mempty
singleton :: item -> full
cons :: item -> full -> full
cons item
item full
l = forall full item. ListLike full item => full -> full -> full
append (forall full item. ListLike full item => item -> full
singleton item
item) full
l
snoc :: full -> item -> full
snoc full
l item
item = forall full item. ListLike full item => full -> full -> full
append full
l (forall full item. ListLike full item => item -> full
singleton item
item)
append :: full -> full -> full
append = forall a. Monoid a => a -> a -> a
mappend
head :: full -> item
head = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error [Char]
"head") forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall full item. ListLike full item => full -> Maybe (item, full)
uncons
uncons :: full -> Maybe (item, full)
uncons full
x = if forall full item. ListLike full item => full -> Bool
null full
x then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just (forall full item. ListLike full item => full -> item
head full
x, forall full item. ListLike full item => full -> full
tail full
x)
last :: full -> item
last full
l = case forall full item a. (ListLike full item, Num a) => full -> a
genericLength full
l of
(Integer
0::Integer) -> forall a. HasCallStack => [Char] -> a
error [Char]
"Called last on empty list"
Integer
1 -> forall full item. ListLike full item => full -> item
head full
l
Integer
_ -> forall full item. ListLike full item => full -> item
last (forall full item. ListLike full item => full -> full
tail full
l)
tail :: full -> full
tail = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error [Char]
"tail") forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall full item. ListLike full item => full -> Maybe (item, full)
uncons
init :: full -> full
init full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall a. HasCallStack => [Char] -> a
error [Char]
"init: empty list"
| forall full item. ListLike full item => full -> Bool
null full
xs = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
l) (forall full item. ListLike full item => full -> full
init full
xs)
where xs :: full
xs = forall full item. ListLike full item => full -> full
tail full
l
null :: full -> Bool
null full
x = forall full item a. (ListLike full item, Num a) => full -> a
genericLength full
x forall a. Eq a => a -> a -> Bool
== (Integer
0::Integer)
length :: full -> Int
length = forall full item a. (ListLike full item, Num a) => full -> a
genericLength
map :: ListLike full' item' => (item -> item') -> full -> full'
map item -> item'
func full
inp
| forall full item. ListLike full item => full -> Bool
null full
inp = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons (item -> item'
func (forall full item. ListLike full item => full -> item
head full
inp)) (forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map item -> item'
func (forall full item. ListLike full item => full -> full
tail full
inp))
rigidMap :: (item -> item) -> full -> full
rigidMap = forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map
reverse :: full -> full
reverse full
l = forall {full} {t}.
(ListLike full (Item t), ListLike t (Item t)) =>
full -> t -> t
rev full
l forall full item. ListLike full item => full
empty
where rev :: full -> t -> t
rev full
rl t
a
| forall full item. ListLike full item => full -> Bool
null full
rl = t
a
| Bool
otherwise = full -> t -> t
rev (forall full item. ListLike full item => full -> full
tail full
rl) (forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
rl) t
a)
intersperse :: item -> full -> full
intersperse item
sep full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| forall full item. ListLike full item => full -> Bool
null full
xs = forall full item. ListLike full item => item -> full
singleton item
x
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons item
x (forall full item. ListLike full item => item -> full -> full
cons item
sep (forall full item. ListLike full item => item -> full -> full
intersperse item
sep full
xs))
where x :: item
x = forall full item. ListLike full item => full -> item
head full
l
xs :: full
xs = forall full item. ListLike full item => full -> full
tail full
l
concat :: (ListLike full' full) => full' -> full
concat = forall full item.
(FoldableLL full item, Monoid item) =>
full -> item
fold
concatMap :: (ListLike full' item') =>
(item -> full') -> full -> full'
concatMap = forall full item m.
(FoldableLL full item, Monoid m) =>
(item -> m) -> full -> m
foldMap
rigidConcatMap :: (item -> full) -> full -> full
rigidConcatMap = forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> full') -> full -> full'
concatMap
any :: (item -> Bool) -> full -> Bool
any item -> Bool
p = Any -> Bool
getAny forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall full item m.
(FoldableLL full item, Monoid m) =>
(item -> m) -> full -> m
foldMap (Bool -> Any
Any forall b c a. (b -> c) -> (a -> b) -> a -> c
. item -> Bool
p)
all :: (item -> Bool) -> full -> Bool
all item -> Bool
p = All -> Bool
getAll forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall full item m.
(FoldableLL full item, Monoid m) =>
(item -> m) -> full -> m
foldMap (Bool -> All
All forall b c a. (b -> c) -> (a -> b) -> a -> c
. item -> Bool
p)
maximum :: Ord item => full -> item
maximum = forall full item.
FoldableLL full item =>
(item -> item -> item) -> full -> item
foldr1 forall a. Ord a => a -> a -> a
max
minimum :: Ord item => full -> item
minimum = forall full item.
FoldableLL full item =>
(item -> item -> item) -> full -> item
foldr1 forall a. Ord a => a -> a -> a
min
replicate :: Int -> item -> full
replicate = forall full item a.
(ListLike full item, Integral a) =>
a -> item -> full
genericReplicate
take :: Int -> full -> full
take = forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericTake
drop :: Int -> full -> full
drop = forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericDrop
splitAt :: Int -> full -> (full, full)
splitAt = forall full item a.
(ListLike full item, Integral a) =>
a -> full -> (full, full)
genericSplitAt
takeWhile :: (item -> Bool) -> full -> full
takeWhile item -> Bool
func full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| item -> Bool
func item
x = forall full item. ListLike full item => item -> full -> full
cons item
x (forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
takeWhile item -> Bool
func (forall full item. ListLike full item => full -> full
tail full
l))
| Bool
otherwise = forall full item. ListLike full item => full
empty
where x :: item
x = forall full item. ListLike full item => full -> item
head full
l
dropWhile :: (item -> Bool) -> full -> full
dropWhile item -> Bool
func full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| item -> Bool
func (forall full item. ListLike full item => full -> item
head full
l) = forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
dropWhile item -> Bool
func (forall full item. ListLike full item => full -> full
tail full
l)
| Bool
otherwise = full
l
dropWhileEnd :: (item -> Bool) -> full -> full
dropWhileEnd item -> Bool
func = forall full item b.
FoldableLL full item =>
(item -> b -> b) -> b -> full -> b
foldr (\item
x full
xs -> if item -> Bool
func item
x Bool -> Bool -> Bool
&& forall full item. ListLike full item => full -> Bool
null full
xs then forall full item. ListLike full item => full
empty else forall full item. ListLike full item => item -> full -> full
cons item
x full
xs) forall full item. ListLike full item => full
empty
span :: (item -> Bool) -> full -> (full, full)
span item -> Bool
func full
l
| forall full item. ListLike full item => full -> Bool
null full
l = (forall full item. ListLike full item => full
empty, forall full item. ListLike full item => full
empty)
| item -> Bool
func item
x = (forall full item. ListLike full item => item -> full -> full
cons item
x full
ys, full
zs)
| Bool
otherwise = (forall full item. ListLike full item => full
empty, full
l)
where (full
ys, full
zs) = forall full item.
ListLike full item =>
(item -> Bool) -> full -> (full, full)
span item -> Bool
func (forall full item. ListLike full item => full -> full
tail full
l)
x :: item
x = forall full item. ListLike full item => full -> item
head full
l
break :: (item -> Bool) -> full -> (full, full)
break item -> Bool
p = forall full item.
ListLike full item =>
(item -> Bool) -> full -> (full, full)
span (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. item -> Bool
p)
group :: (ListLike full' full, Eq item) => full -> full'
group = forall full item full'.
(ListLike full item, ListLike full' full, Eq item) =>
(item -> item -> Bool) -> full -> full'
groupBy forall a. Eq a => a -> a -> Bool
(==)
inits :: (ListLike full' full) => full -> full'
inits full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => item -> full
singleton forall full item. ListLike full item => full
empty
| Bool
otherwise =
forall full item. ListLike full item => full -> full -> full
append (forall full item. ListLike full item => item -> full
singleton forall full item. ListLike full item => full
empty)
(forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map (forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
l)) [full]
theinits)
where theinits :: [full]
theinits = forall a. a -> a -> a
asTypeOf (forall full item full'.
(ListLike full item, ListLike full' full) =>
full -> full'
inits (forall full item. ListLike full item => full -> full
tail full
l)) [full
l]
tails :: ListLike full' full => full -> full'
tails full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => item -> full
singleton forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons full
l (forall full item full'.
(ListLike full item, ListLike full' full) =>
full -> full'
tails (forall full item. ListLike full item => full -> full
tail full
l))
isPrefixOf :: Eq item => full -> full -> Bool
isPrefixOf full
needle full
haystack
| forall full item. ListLike full item => full -> Bool
null full
needle = Bool
True
| forall full item. ListLike full item => full -> Bool
null full
haystack = Bool
False
| Bool
otherwise = (forall full item. ListLike full item => full -> item
head full
needle) forall a. Eq a => a -> a -> Bool
== (forall full item. ListLike full item => full -> item
head full
haystack) Bool -> Bool -> Bool
&&
forall full item.
(ListLike full item, Eq item) =>
full -> full -> Bool
isPrefixOf (forall full item. ListLike full item => full -> full
tail full
needle) (forall full item. ListLike full item => full -> full
tail full
haystack)
isSuffixOf :: Eq item => full -> full -> Bool
isSuffixOf full
needle full
haystack = forall full item.
(ListLike full item, Eq item) =>
full -> full -> Bool
isPrefixOf (forall full item. ListLike full item => full -> full
reverse full
needle) (forall full item. ListLike full item => full -> full
reverse full
haystack)
isInfixOf :: Eq item => full -> full -> Bool
isInfixOf full
needle full
haystack =
forall full item.
ListLike full item =>
(item -> Bool) -> full -> Bool
any (forall full item.
(ListLike full item, Eq item) =>
full -> full -> Bool
isPrefixOf full
needle) [full]
thetails
where thetails :: [full]
thetails = forall a. a -> a -> a
asTypeOf (forall full item full'.
(ListLike full item, ListLike full' full) =>
full -> full'
tails full
haystack) [full
haystack]
stripPrefix :: Eq item => full -> full -> Maybe full
stripPrefix full
xs full
ys = if full
xs forall full item.
(ListLike full item, Eq item) =>
full -> full -> Bool
`isPrefixOf` full
ys
then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall full item. ListLike full item => Int -> full -> full
drop (forall full item. ListLike full item => full -> Int
length full
xs) full
ys
else forall a. Maybe a
Nothing
stripSuffix :: Eq item => full -> full -> Maybe full
stripSuffix full
xs full
ys = if full
xs forall full item.
(ListLike full item, Eq item) =>
full -> full -> Bool
`isSuffixOf` full
ys
then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall full item. ListLike full item => Int -> full -> full
take (forall full item. ListLike full item => full -> Int
length full
ys forall a. Num a => a -> a -> a
- forall full item. ListLike full item => full -> Int
length full
xs) full
ys
else forall a. Maybe a
Nothing
elem :: Eq item => item -> full -> Bool
elem item
i = forall full item.
ListLike full item =>
(item -> Bool) -> full -> Bool
any (forall a. Eq a => a -> a -> Bool
== item
i)
notElem :: Eq item => item -> full -> Bool
notElem item
i = forall full item.
ListLike full item =>
(item -> Bool) -> full -> Bool
all (forall a. Eq a => a -> a -> Bool
/= item
i)
find :: (item -> Bool) -> full -> Maybe item
find item -> Bool
f full
l = case forall full item.
ListLike full item =>
(item -> Bool) -> full -> Maybe Int
findIndex item -> Bool
f full
l of
Maybe Int
Nothing -> forall a. Maybe a
Nothing
Just Int
x -> forall a. a -> Maybe a
Just (forall full item. ListLike full item => full -> Int -> item
index full
l Int
x)
filter :: (item -> Bool) -> full -> full
filter item -> Bool
func full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| item -> Bool
func (forall full item. ListLike full item => full -> item
head full
l) = forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
l) (forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter item -> Bool
func (forall full item. ListLike full item => full -> full
tail full
l))
| Bool
otherwise = forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter item -> Bool
func (forall full item. ListLike full item => full -> full
tail full
l)
partition :: (item -> Bool) -> full -> (full, full)
partition item -> Bool
p full
xs = (forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter item -> Bool
p full
xs, forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. item -> Bool
p) full
xs)
index :: full -> Int -> item
index full
l Int
i
| forall full item. ListLike full item => full -> Bool
null full
l = forall a. HasCallStack => [Char] -> a
error [Char]
"index: index not found"
| Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"index: index must be >= 0"
| Int
i forall a. Eq a => a -> a -> Bool
== Int
0 = forall full item. ListLike full item => full -> item
head full
l
| Bool
otherwise = forall full item. ListLike full item => full -> Int -> item
index (forall full item. ListLike full item => full -> full
tail full
l) (Int
i forall a. Num a => a -> a -> a
- Int
1)
elemIndex :: Eq item => item -> full -> Maybe Int
elemIndex item
e full
l = forall full item.
ListLike full item =>
(item -> Bool) -> full -> Maybe Int
findIndex (forall a. Eq a => a -> a -> Bool
== item
e) full
l
elemIndices :: (Eq item, ListLike result Int) => item -> full -> result
elemIndices item
i full
l = forall full item result.
(ListLike full item, ListLike result Int) =>
(item -> Bool) -> full -> result
findIndices (forall a. Eq a => a -> a -> Bool
== item
i) full
l
findIndex :: (item -> Bool) -> full -> Maybe Int
findIndex item -> Bool
f = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall full item result.
(ListLike full item, ListLike result Int) =>
(item -> Bool) -> full -> result
findIndices item -> Bool
f
findIndices :: (ListLike result Int) => (item -> Bool) -> full -> result
findIndices item -> Bool
p full
xs = forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter (item -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ [(item, Int)]
thezips
where thezips :: [(item, Int)]
thezips = forall a. a -> a -> a
asTypeOf (forall full item fullb itemb result.
(ListLike full item, ListLike fullb itemb,
ListLike result (item, itemb)) =>
full -> fullb -> result
zip full
xs [Int
0..]) [(forall full item. ListLike full item => full -> item
head full
xs, Int
0::Int)]
sequence :: (Applicative m, ListLike fullinp (m item)) =>
fullinp -> m full
sequence = forall full item b.
FoldableLL full item =>
(item -> b -> b) -> b -> full -> b
foldr (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
A.liftA2 forall full item. ListLike full item => item -> full -> full
cons) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall full item. ListLike full item => full
empty)
mapM :: (Applicative m, ListLike full' item') =>
(item -> m item') -> full -> m full'
mapM item -> m item'
func full
l = forall full item (m :: * -> *) fullinp.
(ListLike full item, Applicative m, ListLike fullinp (m item)) =>
fullinp -> m full
sequence [m item']
mapresult
where mapresult :: [m item']
mapresult = forall a. a -> a -> a
asTypeOf (forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map item -> m item'
func full
l) []
rigidMapM :: Monad m => (item -> m item) -> full -> m full
rigidMapM = forall full item (m :: * -> *) full' item'.
(ListLike full item, Applicative m, ListLike full' item') =>
(item -> m item') -> full -> m full'
mapM
nub :: Eq item => full -> full
nub = forall full item.
ListLike full item =>
(item -> item -> Bool) -> full -> full
nubBy forall a. Eq a => a -> a -> Bool
(==)
delete :: Eq item => item -> full -> full
delete = forall full item.
ListLike full item =>
(item -> item -> Bool) -> item -> full -> full
deleteBy forall a. Eq a => a -> a -> Bool
(==)
deleteFirsts :: Eq item => full -> full -> full
deleteFirsts = forall full item a.
FoldableLL full item =>
(a -> item -> a) -> a -> full -> a
foldl (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall full item.
(ListLike full item, Eq item) =>
item -> full -> full
delete)
union :: Eq item => full -> full -> full
union = forall full item.
ListLike full item =>
(item -> item -> Bool) -> full -> full -> full
unionBy forall a. Eq a => a -> a -> Bool
(==)
intersect :: Eq item => full -> full -> full
intersect = forall full item.
ListLike full item =>
(item -> item -> Bool) -> full -> full -> full
intersectBy forall a. Eq a => a -> a -> Bool
(==)
sort :: Ord item => full -> full
sort = forall full item.
ListLike full item =>
(item -> item -> Ordering) -> full -> full
sortBy forall a. Ord a => a -> a -> Ordering
compare
insert :: Ord item => item -> full -> full
insert = forall full item.
ListLike full item =>
(item -> item -> Ordering) -> item -> full -> full
insertBy forall a. Ord a => a -> a -> Ordering
compare
toList' :: full -> [item]
toList' = forall full item full'.
(ListLike full item, ListLike full' item) =>
full -> full'
fromListLike
fromList' :: [item] -> full
fromList' [] = forall full item. ListLike full item => full
empty
fromList' (item
x:[item]
xs) = forall full item. ListLike full item => item -> full -> full
cons item
x (forall l. IsList l => [Item l] -> l
fromList [item]
xs)
fromListLike :: ListLike full' item => full -> full'
fromListLike = forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map forall a. a -> a
id
{-# INLINE fromListLike #-}
nubBy :: (item -> item -> Bool) -> full -> full
nubBy item -> item -> Bool
eq full
l = full -> full -> full
nubBy' full
l forall a. Monoid a => a
mempty
where
nubBy' :: full -> full -> full
nubBy' full
ys full
xs =
case forall full item. ListLike full item => full -> Maybe (item, full)
uncons full
ys of
Maybe (item, full)
Nothing -> forall a. Monoid a => a
mempty
Just (item
y, full
ys')
| item -> full -> Bool
elem_by item
y full
xs -> full -> full -> full
nubBy' full
ys' full
xs
| Bool
otherwise -> forall full item. ListLike full item => item -> full -> full
cons item
y (full -> full -> full
nubBy' full
ys' (forall full item. ListLike full item => item -> full -> full
cons item
y full
xs))
elem_by :: item -> full -> Bool
elem_by :: item -> full -> Bool
elem_by item
y full
xs =
case forall full item. ListLike full item => full -> Maybe (item, full)
uncons full
xs of
Maybe (item, full)
Nothing -> Bool
False
Just (item
x, full
xs') -> item
x item -> item -> Bool
`eq` item
y Bool -> Bool -> Bool
|| item -> full -> Bool
elem_by item
y full
xs'
deleteBy :: (item -> item -> Bool) -> item -> full -> full
deleteBy item -> item -> Bool
func item
i full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| Bool
otherwise =
if item -> item -> Bool
func item
i (forall full item. ListLike full item => full -> item
head full
l)
then forall full item. ListLike full item => full -> full
tail full
l
else forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
l) (forall full item.
ListLike full item =>
(item -> item -> Bool) -> item -> full -> full
deleteBy item -> item -> Bool
func item
i (forall full item. ListLike full item => full -> full
tail full
l))
deleteFirstsBy :: (item -> item -> Bool) -> full -> full -> full
deleteFirstsBy item -> item -> Bool
func = forall full item a.
FoldableLL full item =>
(a -> item -> a) -> a -> full -> a
foldl (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall full item.
ListLike full item =>
(item -> item -> Bool) -> item -> full -> full
deleteBy item -> item -> Bool
func))
unionBy :: (item -> item -> Bool) -> full -> full -> full
unionBy item -> item -> Bool
func full
x full
y =
forall full item. ListLike full item => full -> full -> full
append full
x forall a b. (a -> b) -> a -> b
$ forall full item a.
FoldableLL full item =>
(a -> item -> a) -> a -> full -> a
foldl (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall full item.
ListLike full item =>
(item -> item -> Bool) -> item -> full -> full
deleteBy item -> item -> Bool
func)) (forall full item.
ListLike full item =>
(item -> item -> Bool) -> full -> full
nubBy item -> item -> Bool
func full
y) full
x
intersectBy :: (item -> item -> Bool) -> full -> full -> full
intersectBy item -> item -> Bool
func full
xs full
ys = forall full item.
ListLike full item =>
(item -> Bool) -> full -> full
filter (\item
x -> forall full item.
ListLike full item =>
(item -> Bool) -> full -> Bool
any (item -> item -> Bool
func item
x) full
ys) full
xs
groupBy :: (ListLike full' full, Eq item) =>
(item -> item -> Bool) -> full -> full'
groupBy item -> item -> Bool
eq full
l
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => item -> full -> full
cons item
x full
ys) (forall full item full'.
(ListLike full item, ListLike full' full, Eq item) =>
(item -> item -> Bool) -> full -> full'
groupBy item -> item -> Bool
eq full
zs)
where (full
ys, full
zs) = forall full item.
ListLike full item =>
(item -> Bool) -> full -> (full, full)
span (item -> item -> Bool
eq item
x) full
xs
x :: item
x = forall full item. ListLike full item => full -> item
head full
l
xs :: full
xs = forall full item. ListLike full item => full -> full
tail full
l
sortBy :: (item -> item -> Ordering) -> full -> full
sortBy item -> item -> Ordering
cmp = forall full item b.
FoldableLL full item =>
(item -> b -> b) -> b -> full -> b
foldr (forall full item.
ListLike full item =>
(item -> item -> Ordering) -> item -> full -> full
insertBy item -> item -> Ordering
cmp) forall full item. ListLike full item => full
empty
insertBy :: (item -> item -> Ordering) -> item ->
full -> full
insertBy item -> item -> Ordering
cmp item
x full
ys =
case forall full item. ListLike full item => full -> Maybe (item, full)
uncons full
ys of
Maybe (item, full)
Nothing -> forall full item. ListLike full item => item -> full
singleton item
x
Just (item
ys_head,full
ys_tail) -> case item -> item -> Ordering
cmp item
x item
ys_head of
Ordering
GT -> forall full item. ListLike full item => item -> full -> full
cons item
ys_head (forall full item.
ListLike full item =>
(item -> item -> Ordering) -> item -> full -> full
insertBy item -> item -> Ordering
cmp item
x full
ys_tail)
Ordering
_ -> forall full item. ListLike full item => item -> full -> full
cons item
x full
ys
genericLength :: Num a => full -> a
genericLength full
l = forall {t} {t}. (ListLike t (Item t), Num t) => t -> t -> t
calclen a
0 full
l
where calclen :: t -> t -> t
calclen !t
accum t
cl =
if forall full item. ListLike full item => full -> Bool
null t
cl
then t
accum
else t -> t -> t
calclen (t
accum forall a. Num a => a -> a -> a
+ t
1) (forall full item. ListLike full item => full -> full
tail t
cl)
genericTake :: Integral a => a -> full -> full
genericTake a
n full
l
| a
n forall a. Ord a => a -> a -> Bool
<= a
0 = forall full item. ListLike full item => full
empty
| forall full item. ListLike full item => full -> Bool
null full
l = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons (forall full item. ListLike full item => full -> item
head full
l) (forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericTake (a
n forall a. Num a => a -> a -> a
- a
1) (forall full item. ListLike full item => full -> full
tail full
l))
genericDrop :: Integral a => a -> full -> full
genericDrop a
n full
l
| a
n forall a. Ord a => a -> a -> Bool
<= a
0 = full
l
| forall full item. ListLike full item => full -> Bool
null full
l = full
l
| Bool
otherwise = forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericDrop (a
n forall a. Num a => a -> a -> a
- a
1) (forall full item. ListLike full item => full -> full
tail full
l)
genericSplitAt :: Integral a => a -> full -> (full, full)
genericSplitAt a
n full
l = (forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericTake a
n full
l, forall full item a.
(ListLike full item, Integral a) =>
a -> full -> full
genericDrop a
n full
l)
genericReplicate :: Integral a => a -> item -> full
genericReplicate a
count item
x
| a
count forall a. Ord a => a -> a -> Bool
<= a
0 = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item full' item'.
(ListLike full item, ListLike full' item') =>
(item -> item') -> full -> full'
map (\a
_ -> item
x) [a
1..a
count]
{-# MINIMAL (singleton, uncons, null) |
(singleton, uncons, genericLength) |
(singleton, head, tail, null) |
(singleton, head, tail, genericLength) #-}
type ListOps full = (ListLike full (Item full))
class (ListLike full item) => InfiniteListLike full item | full -> item where
iterate :: (item -> item) -> item -> full
iterate item -> item
f item
x = forall full item. ListLike full item => item -> full -> full
cons item
x (forall full item.
InfiniteListLike full item =>
(item -> item) -> item -> full
iterate item -> item
f (item -> item
f item
x))
repeat :: item -> full
repeat item
x = full
xs
where xs :: full
xs = forall full item. ListLike full item => item -> full -> full
cons item
x full
xs
cycle :: full -> full
cycle full
xs
| forall full item. ListLike full item => full -> Bool
null full
xs = forall a. HasCallStack => [Char] -> a
error [Char]
"ListLike.cycle: empty list"
| Bool
otherwise = full
xs' where xs' :: full
xs' = forall full item. ListLike full item => full -> full -> full
append full
xs full
xs'
instance ListLike [a] a where
empty :: [a]
empty = []
singleton :: a -> [a]
singleton a
x = [a
x]
cons :: a -> [a] -> [a]
cons a
x [a]
l = a
x forall a. a -> [a] -> [a]
: [a]
l
snoc :: [a] -> a -> [a]
snoc [a]
l a
x = [a]
l forall a. [a] -> [a] -> [a]
++ [a
x]
append :: [a] -> [a] -> [a]
append = forall a. [a] -> [a] -> [a]
(++)
uncons :: [a] -> Maybe (a, [a])
uncons [] = forall a. Maybe a
Nothing
uncons (a
x : [a]
xs) = forall a. a -> Maybe a
Just (a
x, [a]
xs)
last :: [a] -> a
last = forall a. [a] -> a
L.last
init :: [a] -> [a]
init = forall a. [a] -> [a]
L.init
null :: [a] -> Bool
null = forall (t :: * -> *) a. Foldable t => t a -> Bool
L.null
length :: [a] -> Int
length = forall (t :: * -> *) a. Foldable t => t a -> Int
L.length
map :: forall full' item'.
ListLike full' item' =>
(a -> item') -> [a] -> full'
map a -> item'
f = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
L.map a -> item'
f
rigidMap :: (a -> a) -> [a] -> [a]
rigidMap = forall a b. (a -> b) -> [a] -> [b]
L.map
reverse :: [a] -> [a]
reverse = forall a. [a] -> [a]
L.reverse
intersperse :: a -> [a] -> [a]
intersperse = forall a. a -> [a] -> [a]
L.intersperse
concat :: forall full'. ListLike full' [a] => full' -> [a]
concat = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
L.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l. IsList l => l -> [Item l]
toList
rigidConcatMap :: (a -> [a]) -> [a] -> [a]
rigidConcatMap = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
L.concatMap
any :: (a -> Bool) -> [a] -> Bool
any = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
L.any
all :: (a -> Bool) -> [a] -> Bool
all = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
L.all
maximum :: Ord a => [a] -> a
maximum = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
L.maximum
minimum :: Ord a => [a] -> a
minimum = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
L.minimum
replicate :: Int -> a -> [a]
replicate = forall a. Int -> a -> [a]
L.replicate
take :: Int -> [a] -> [a]
take = forall a. Int -> [a] -> [a]
L.take
drop :: Int -> [a] -> [a]
drop = forall a. Int -> [a] -> [a]
L.drop
splitAt :: Int -> [a] -> ([a], [a])
splitAt = forall a. Int -> [a] -> ([a], [a])
L.splitAt
takeWhile :: (a -> Bool) -> [a] -> [a]
takeWhile = forall a. (a -> Bool) -> [a] -> [a]
L.takeWhile
dropWhile :: (a -> Bool) -> [a] -> [a]
dropWhile = forall a. (a -> Bool) -> [a] -> [a]
L.dropWhile
span :: (a -> Bool) -> [a] -> ([a], [a])
span = forall a. (a -> Bool) -> [a] -> ([a], [a])
L.span
break :: (a -> Bool) -> [a] -> ([a], [a])
break = forall a. (a -> Bool) -> [a] -> ([a], [a])
L.break
group :: forall full'. (ListLike full' [a], Eq a) => [a] -> full'
group = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [[a]]
L.group
inits :: forall full'. ListLike full' [a] => [a] -> full'
inits = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [[a]]
L.inits
tails :: forall full'. ListLike full' [a] => [a] -> full'
tails = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [[a]]
L.tails
isPrefixOf :: Eq a => [a] -> [a] -> Bool
isPrefixOf = forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf
isSuffixOf :: Eq a => [a] -> [a] -> Bool
isSuffixOf = forall a. Eq a => [a] -> [a] -> Bool
L.isSuffixOf
isInfixOf :: Eq a => [a] -> [a] -> Bool
isInfixOf = forall a. Eq a => [a] -> [a] -> Bool
L.isInfixOf
stripPrefix :: Eq a => [a] -> [a] -> Maybe [a]
stripPrefix = forall a. Eq a => [a] -> [a] -> Maybe [a]
L.stripPrefix
elem :: Eq a => a -> [a] -> Bool
elem = forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
L.elem
notElem :: Eq a => a -> [a] -> Bool
notElem = forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
L.notElem
find :: (a -> Bool) -> [a] -> Maybe a
find = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find
filter :: (a -> Bool) -> [a] -> [a]
filter = forall a. (a -> Bool) -> [a] -> [a]
L.filter
partition :: (a -> Bool) -> [a] -> ([a], [a])
partition = forall a. (a -> Bool) -> [a] -> ([a], [a])
L.partition
index :: [a] -> Int -> a
index = forall a. [a] -> Int -> a
(L.!!)
elemIndex :: Eq a => a -> [a] -> Maybe Int
elemIndex = forall a. Eq a => a -> [a] -> Maybe Int
L.elemIndex
elemIndices :: forall result. (Eq a, ListLike result Int) => a -> [a] -> result
elemIndices a
item = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> [a] -> [Int]
L.elemIndices a
item
findIndex :: (a -> Bool) -> [a] -> Maybe Int
findIndex = forall a. (a -> Bool) -> [a] -> Maybe Int
L.findIndex
sequence :: forall (m :: * -> *) fullinp.
(Applicative m, ListLike fullinp (m a)) =>
fullinp -> m [a]
sequence = forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l. IsList l => l -> [Item l]
toList
nub :: Eq a => [a] -> [a]
nub = forall a. Eq a => [a] -> [a]
L.nub
delete :: Eq a => a -> [a] -> [a]
delete = forall a. Eq a => a -> [a] -> [a]
L.delete
deleteFirsts :: Eq a => [a] -> [a] -> [a]
deleteFirsts = forall a. Eq a => [a] -> [a] -> [a]
(L.\\)
union :: Eq a => [a] -> [a] -> [a]
union = forall a. Eq a => [a] -> [a] -> [a]
L.union
intersect :: Eq a => [a] -> [a] -> [a]
intersect = forall a. Eq a => [a] -> [a] -> [a]
L.intersect
sort :: Ord a => [a] -> [a]
sort = forall a. Ord a => [a] -> [a]
L.sort
groupBy :: forall full'.
(ListLike full' [a], Eq a) =>
(a -> a -> Bool) -> [a] -> full'
groupBy a -> a -> Bool
func = forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Bool) -> [a] -> [[a]]
L.groupBy a -> a -> Bool
func
unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
unionBy = forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
L.unionBy
intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
intersectBy = forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
L.intersectBy
sortBy :: (a -> a -> Ordering) -> [a] -> [a]
sortBy = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy
insert :: Ord a => a -> [a] -> [a]
insert = forall a. Ord a => a -> [a] -> [a]
L.insert
genericLength :: forall a. Num a => [a] -> a
genericLength = forall i a. Num i => [a] -> i
L.genericLength
zip :: (ListLike full item,
ListLike fullb itemb,
ListLike result (item, itemb)) =>
full -> fullb -> result
zip :: forall full item fullb itemb result.
(ListLike full item, ListLike fullb itemb,
ListLike result (item, itemb)) =>
full -> fullb -> result
zip = forall full item fullb itemb result resultitem.
(ListLike full item, ListLike fullb itemb,
ListLike result resultitem) =>
(item -> itemb -> resultitem) -> full -> fullb -> result
zipWith (,)
zipWith :: (ListLike full item,
ListLike fullb itemb,
ListLike result resultitem) =>
(item -> itemb -> resultitem) -> full -> fullb -> result
zipWith :: forall full item fullb itemb result resultitem.
(ListLike full item, ListLike fullb itemb,
ListLike result resultitem) =>
(item -> itemb -> resultitem) -> full -> fullb -> result
zipWith item -> itemb -> resultitem
f full
a fullb
b
| forall full item. ListLike full item => full -> Bool
null full
a = forall full item. ListLike full item => full
empty
| forall full item. ListLike full item => full -> Bool
null fullb
b = forall full item. ListLike full item => full
empty
| Bool
otherwise = forall full item. ListLike full item => item -> full -> full
cons (item -> itemb -> resultitem
f (forall full item. ListLike full item => full -> item
head full
a) (forall full item. ListLike full item => full -> item
head fullb
b)) (forall full item fullb itemb result resultitem.
(ListLike full item, ListLike fullb itemb,
ListLike result resultitem) =>
(item -> itemb -> resultitem) -> full -> fullb -> result
zipWith item -> itemb -> resultitem
f (forall full item. ListLike full item => full -> full
tail full
a) (forall full item. ListLike full item => full -> full
tail fullb
b))