{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
module Slist
(
Slist
, Size
, slist
, infiniteSlist
, one
, iterate
#if ( __GLASGOW_HASKELL__ > 802 )
, iterate'
#endif
, repeat
, replicate
, cycle
, fromRange
, len
, size
, isEmpty
, head
, safeHead
, last
, safeLast
, init
, tail
, append'
, cons
, cons'
, uncons
, map
, reverse
, safeReverse
, intersperse
, intercalate
, transpose
, subsequences
, permutations
, concat
, concat'
, concatMap
, concatMap'
, scanl
, scanl'
, scanl1
, scanr
, scanr1
, unfoldr
, take
, drop
, splitAt
, takeWhile
, dropWhile
, span
, break
, stripPrefix
, safeStripPrefix
, group
, groupBy
, inits
, tails
, chunksOf
, listChunksOf
, isPrefixOf
, safeIsPrefixOf
, isSuffixOf
, safeIsSuffixOf
, isInfixOf
, safeIsInfixOf
, isSubsequenceOf
, safeIsSubsequenceOf
, lookup
, filter
, partition
, partitionWith
, listPartitionWith
, at
, unsafeAt
, elemIndex
, elemIndices
, findIndex
, findIndices
, zip
, zip3
, zipWith
, zipWith3
, unzip
, unzip3
, nub
, nubBy
, ordNub
, delete
, deleteBy
, deleteFirstsBy
, diff
, union
, unionBy
, intersect
, intersectBy
, sort
, sortBy
, sortOn
, sortWith
, insert
, insertBy
, genericLength
, genericTake
, genericDrop
, genericSplitAt
, genericAt
, genericUnsafeAt
, genericReplicate
, maybeToSlist
, slistToMaybe
, catMaybes
, mapMaybe
, slistWith
, mapToVals
, mapToKeys
, mapToPairs
, setToSlist
) where
import Data.Bifunctor (bimap, first, second)
import Data.Either (partitionEithers)
import Data.Foldable (foldl')
#if ( __GLASGOW_HASKELL__ == 802 )
import Data.Semigroup (Semigroup (..))
#endif
import GHC.Exts (fromListN)
import Prelude hiding (break, concat, concatMap, cycle, drop, dropWhile, filter, head, init,
iterate, last, lookup, map, repeat, replicate, reverse, scanl, scanl1, scanr,
scanr1, span, splitAt, tail, take, takeWhile, unzip, unzip3, zip, zip3, zipWith,
zipWith3)
import Slist.Containers (mapToKeys, mapToPairs, mapToVals, setToSlist)
import Slist.Maybe (catMaybes, mapMaybe, maybeToSlist, slistToMaybe, slistWith)
import Slist.Size (Size (..), sizes)
import Slist.Type (Slist (..), cons, infiniteSlist, isEmpty, len, map, one, size, slist)
import qualified Data.List as L
import qualified Data.Set as Set
import qualified GHC.Exts as Exts
import qualified Prelude as P
iterate :: (a -> a) -> a -> Slist a
iterate :: (a -> a) -> a -> Slist a
iterate a -> a
f = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> (a -> [a]) -> a -> Slist a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
L.iterate a -> a
f
{-# INLINE iterate #-}
#if ( __GLASGOW_HASKELL__ > 802 )
iterate' :: (a -> a) -> a -> Slist a
iterate' :: (a -> a) -> a -> Slist a
iterate' a -> a
f = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> (a -> [a]) -> a -> Slist a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
L.iterate' a -> a
f
{-# INLINE iterate' #-}
#endif
repeat :: a -> Slist a
repeat :: a -> Slist a
repeat = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> (a -> [a]) -> a -> Slist a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. a -> [a]
L.repeat
{-# INLINE repeat #-}
replicate :: Int -> a -> Slist a
replicate :: Int -> a -> Slist a
replicate Int
n a
x
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist (Int -> a -> [a]
forall a. Int -> a -> [a]
L.replicate Int
n a
x) (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
n
{-# INLINE replicate #-}
cycle :: Slist a -> Slist a
cycle :: Slist a -> Slist a
cycle sl :: Slist a
sl@(Slist [a]
_ Size
Infinity) = Slist a
sl
cycle Slist{[a]
Size
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
sSize :: Size
sList :: [a]
..} = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> [a] -> Slist a
forall a b. (a -> b) -> a -> b
$ [a] -> [a]
forall a. [a] -> [a]
L.cycle [a]
sList
{-# INLINE cycle #-}
fromRange :: Enum a => a -> a -> Slist a
fromRange :: a -> a -> Slist a
fromRange a
from a
to = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a
from..a
to] Size
s
where
s :: Size
s :: Size
s = Int -> Size
Size (Int -> Size) -> Int -> Size
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (a -> Int
forall a. Enum a => a -> Int
fromEnum a
to Int -> Int -> Int
forall a. Num a => a -> a -> a
- a -> Int
forall a. Enum a => a -> Int
fromEnum a
from Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE fromRange #-}
head :: Slist a -> a
head :: Slist a -> a
head = [a] -> a
forall a. [a] -> a
P.head ([a] -> a) -> (Slist a -> [a]) -> Slist a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> [a]
forall a. Slist a -> [a]
sList
{-# INLINE head #-}
safeHead :: Slist a -> Maybe a
safeHead :: Slist a -> Maybe a
safeHead Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = case Size
sSize of
Size Int
0 -> Maybe a
forall a. Maybe a
Nothing
Size
_ -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a] -> a
forall a. [a] -> a
P.head [a]
sList
{-# INLINE safeHead #-}
last :: Slist a -> a
last :: Slist a -> a
last = [a] -> a
forall a. [a] -> a
P.last ([a] -> a) -> (Slist a -> [a]) -> Slist a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> [a]
forall a. Slist a -> [a]
sList
{-# INLINE last #-}
safeLast :: Slist a -> Maybe a
safeLast :: Slist a -> Maybe a
safeLast Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = case Size
sSize of
Size
Infinity -> Maybe a
forall a. Maybe a
Nothing
Size Int
0 -> Maybe a
forall a. Maybe a
Nothing
Size
_ -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a] -> a
forall a. [a] -> a
P.last [a]
sList
{-# INLINE safeLast #-}
tail :: Slist a -> Slist a
tail :: Slist a -> Slist a
tail Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = case Size
sSize of
Size Int
0 -> Slist a
forall a. Monoid a => a
mempty
Size
_ -> [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
P.drop Int
1 [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
1)
{-# INLINE tail #-}
init :: Slist a -> Slist a
init :: Slist a -> Slist a
init sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = case Size
sSize of
Size
Infinity -> Slist a
sl
Size Int
0 -> Slist a
forall a. Monoid a => a
mempty
Size
_ -> [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ([a] -> [a]
forall a. [a] -> [a]
P.init [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
1)
{-# INLINE init #-}
append' :: Slist a -> Slist a -> Slist a
append' :: Slist a -> Slist a -> Slist a
append' Slist a
sl1 Slist a
sl2
| Slist a -> Size
forall a. Slist a -> Size
sSize Slist a
sl1 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
0 = Slist a
sl2
| Slist a -> Size
forall a. Slist a -> Size
sSize Slist a
sl2 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
0 = Slist a
sl1
| Bool
otherwise = let !newSize :: Size
newSize = Slist a -> Size
forall a. Slist a -> Size
sSize Slist a
sl1 Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Slist a -> Size
forall a. Slist a -> Size
sSize Slist a
sl2 in Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = Slist a -> [a]
forall a. Slist a -> [a]
sList Slist a
sl1 [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> Slist a -> [a]
forall a. Slist a -> [a]
sList Slist a
sl2
, sSize :: Size
sSize = Size
newSize
}
cons' :: a -> Slist a -> Slist a
cons' :: a -> Slist a -> Slist a
cons' a
x (Slist [a]
xs !Size
s) = let !newSize :: Size
newSize = Size
s Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1 in [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) Size
newSize
{-# INLINE cons' #-}
uncons :: Slist a -> Maybe (a, Slist a)
uncons :: Slist a -> Maybe (a, Slist a)
uncons (Slist [] Size
_) = Maybe (a, Slist a)
forall a. Maybe a
Nothing
uncons (Slist (a
x:[a]
xs) Size
s) = (a, Slist a) -> Maybe (a, Slist a)
forall a. a -> Maybe a
Just (a
x, [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
xs (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Size
s Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
1)
{-# INLINE uncons #-}
reverse :: Slist a -> Slist a
reverse :: Slist a -> Slist a
reverse Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ([a] -> [a]
forall a. [a] -> [a]
L.reverse [a]
sList) Size
sSize
{-# INLINE reverse #-}
safeReverse :: Slist a -> Slist a
safeReverse :: Slist a -> Slist a
safeReverse sl :: Slist a
sl@(Slist [a]
_ Size
Infinity) = Slist a
sl
safeReverse Slist a
sl = Slist a -> Slist a
forall a. Slist a -> Slist a
reverse Slist a
sl
{-# INLINE safeReverse #-}
intersperse :: a -> Slist a -> Slist a
intersperse :: a -> Slist a -> Slist a
intersperse a
_ sl :: Slist a
sl@(Slist [a]
_ (Size Int
0)) = Slist a
sl
intersperse a
a Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist (a -> [a] -> [a]
forall a. a -> [a] -> [a]
L.intersperse a
a [a]
sList) (Size
2 Size -> Size -> Size
forall a. Num a => a -> a -> a
* Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
1)
{-# INLINE intersperse #-}
intercalate :: Slist a -> Slist (Slist a) -> Slist a
intercalate :: Slist a -> Slist (Slist a) -> Slist a
intercalate Slist a
x = (Slist a -> Slist a -> Slist a)
-> Slist a -> Slist (Slist a) -> Slist a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Slist a -> Slist a -> Slist a
forall a. Semigroup a => a -> a -> a
(<>) Slist a
forall a. Monoid a => a
mempty (Slist (Slist a) -> Slist a)
-> (Slist (Slist a) -> Slist (Slist a))
-> Slist (Slist a)
-> Slist a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> Slist (Slist a) -> Slist (Slist a)
forall a. a -> Slist a -> Slist a
intersperse Slist a
x
{-# INLINE intercalate #-}
transpose :: Slist (Slist a) -> Slist (Slist a)
transpose :: Slist (Slist a) -> Slist (Slist a)
transpose (Slist [Slist a]
l Size
_) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [Slist a]
sList = ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map [a] -> Slist a
forall a. [a] -> Slist a
slist ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
L.transpose ([[a]] -> [[a]]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> a -> b
$ (Slist a -> [a]) -> [Slist a] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
P.map Slist a -> [a]
forall a. Slist a -> [a]
sList [Slist a]
l
, sSize :: Size
sSize = [Size] -> Size
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Size] -> Size) -> [Size] -> Size
forall a b. (a -> b) -> a -> b
$ (Slist a -> Size) -> [Slist a] -> [Size]
forall a b. (a -> b) -> [a] -> [b]
P.map Slist a -> Size
forall a. Slist a -> Size
sSize [Slist a]
l
}
{-# INLINE transpose #-}
subsequences :: Slist a -> Slist (Slist a)
subsequences :: Slist a -> Slist (Slist a)
subsequences Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [Slist a]
sList = ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map [a] -> Slist a
forall a. [a] -> Slist a
slist ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ [a] -> [[a]]
forall a. [a] -> [[a]]
L.subsequences [a]
sList
, sSize :: Size
sSize = Size -> Size
newSize Size
sSize
}
where
newSize :: Size -> Size
newSize :: Size -> Size
newSize Size
Infinity = Size
Infinity
newSize (Size Int
n) = Int -> Size
Size (Int -> Size) -> Int -> Size
forall a b. (a -> b) -> a -> b
$ Int
2 Int -> Integer -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
n
{-# INLINE subsequences #-}
permutations :: Slist a -> Slist (Slist a)
permutations :: Slist a -> Slist (Slist a)
permutations (Slist [a]
l Size
s) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [Slist a]
sList = ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map ([a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
`Slist` Size
s) ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ [a] -> [[a]]
forall a. [a] -> [[a]]
L.permutations [a]
l
, sSize :: Size
sSize = Size -> Size
fact Size
s
}
where
fact :: Size -> Size
fact :: Size -> Size
fact Size
Infinity = Size
Infinity
fact (Size Int
n) = Int -> Size
Size (Int -> Size) -> Int -> Size
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
go Int
1 Int
n
go :: Int -> Int -> Int
go :: Int -> Int -> Int
go !Int
acc Int
0 = Int
acc
go !Int
acc Int
n = Int -> Int -> Int
go (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
{-# INLINE permutations #-}
concat :: Foldable t => t (Slist a) -> Slist a
concat :: t (Slist a) -> Slist a
concat = (Slist a -> Slist a -> Slist a)
-> Slist a -> t (Slist a) -> Slist a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Slist a -> Slist a -> Slist a
forall a. Semigroup a => a -> a -> a
(<>) Slist a
forall a. Monoid a => a
mempty
{-# INLINE concat #-}
concat' :: Foldable t => t (Slist a) -> Slist a
concat' :: t (Slist a) -> Slist a
concat' = (Slist a -> Slist a -> Slist a)
-> Slist a -> t (Slist a) -> Slist a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Slist a -> Slist a -> Slist a
forall a. Slist a -> Slist a -> Slist a
append' Slist a
forall a. Monoid a => a
mempty
{-# INLINE concat' #-}
concatMap :: Foldable t => (a -> Slist b) -> t a -> Slist b
concatMap :: (a -> Slist b) -> t a -> Slist b
concatMap = (a -> Slist b) -> t a -> Slist b
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap
{-# INLINE concatMap #-}
concatMap' :: Foldable t => (a -> Slist b) -> t a -> Slist b
concatMap' :: (a -> Slist b) -> t a -> Slist b
concatMap' a -> Slist b
f = (Slist b -> a -> Slist b) -> Slist b -> t a -> Slist b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Slist b
acc a
x -> Slist b
acc Slist b -> Slist b -> Slist b
forall a. Slist a -> Slist a -> Slist a
`append'` a -> Slist b
f a
x) Slist b
forall a. Monoid a => a
mempty
{-# INLINE concatMap' #-}
scanl :: (b -> a -> b) -> b -> Slist a -> Slist b
scanl :: (b -> a -> b) -> b -> Slist a -> Slist b
scanl b -> a -> b
f b
b Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [b] -> Size -> Slist b
forall a. [a] -> Size -> Slist a
Slist ((b -> a -> b) -> b -> [a] -> [b]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
L.scanl b -> a -> b
f b
b [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1)
{-# INLINE scanl #-}
scanl' :: (b -> a -> b) -> b -> Slist a -> Slist b
scanl' :: (b -> a -> b) -> b -> Slist a -> Slist b
scanl' b -> a -> b
f b
b Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [b] -> Size -> Slist b
forall a. [a] -> Size -> Slist a
Slist ((b -> a -> b) -> b -> [a] -> [b]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
L.scanl' b -> a -> b
f b
b [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1)
{-# INLINE scanl' #-}
scanl1 :: (a -> a -> a) -> Slist a -> Slist a
scanl1 :: (a -> a -> a) -> Slist a -> Slist a
scanl1 a -> a -> a
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> a -> a) -> [a] -> [a]
forall a. (a -> a -> a) -> [a] -> [a]
L.scanl1 a -> a -> a
f [a]
sList) Size
sSize
{-# INLINE scanl1 #-}
scanr :: (a -> b -> b) -> b -> Slist a -> Slist b
scanr :: (a -> b -> b) -> b -> Slist a -> Slist b
scanr a -> b -> b
f b
b Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [b] -> Size -> Slist b
forall a. [a] -> Size -> Slist a
Slist ((a -> b -> b) -> b -> [a] -> [b]
forall a b. (a -> b -> b) -> b -> [a] -> [b]
L.scanr a -> b -> b
f b
b [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1)
{-# INLINE scanr #-}
scanr1 :: (a -> a -> a) -> Slist a -> Slist a
scanr1 :: (a -> a -> a) -> Slist a -> Slist a
scanr1 a -> a -> a
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> a -> a) -> [a] -> [a]
forall a. (a -> a -> a) -> [a] -> [a]
L.scanr1 a -> a -> a
f [a]
sList) Size
sSize
{-# INLINE scanr1 #-}
unfoldr :: forall a b . (b -> Maybe (a, b)) -> b -> Slist a
unfoldr :: (b -> Maybe (a, b)) -> b -> Slist a
unfoldr b -> Maybe (a, b)
f b
def = let (Int
s, [a]
l) = b -> (Int, [a])
go b
def in [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s
where
go :: b -> (Int, [a])
go :: b -> (Int, [a])
go b
b = case b -> Maybe (a, b)
f b
b of
Just (a
a, b
newB) -> (Int -> Int) -> ([a] -> [a]) -> (Int, [a]) -> (Int, [a])
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a]) -> (Int, [a])) -> (Int, [a]) -> (Int, [a])
forall a b. (a -> b) -> a -> b
$ b -> (Int, [a])
go b
newB
Maybe (a, b)
Nothing -> (Int
0, [])
{-# INLINE unfoldr #-}
take :: Int -> Slist a -> Slist a
take :: Int -> Slist a -> Slist a
take Int
i sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = Slist a
sl
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
P.take Int
i [a]
sList
, sSize :: Size
sSize = Int -> Size
Size Int
i
}
{-# INLINE take #-}
drop :: Int -> Slist a -> Slist a
drop :: Int -> Slist a -> Slist a
drop Int
i sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist a
sl
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
P.drop Int
i [a]
sList
, sSize :: Size
sSize = Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
i
}
{-# INLINE drop #-}
splitAt :: Int -> Slist a -> (Slist a, Slist a)
splitAt :: Int -> Slist a -> (Slist a, Slist a)
splitAt Int
i sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Slist a
forall a. Monoid a => a
mempty, Slist a
sl)
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = (Slist a
sl, Slist a
forall a. Monoid a => a
mempty)
| Bool
otherwise =
let ([a]
l1, [a]
l2) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
P.splitAt Int
i [a]
sList
s2 :: Size
s2 = Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
i
in ([a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l1 (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
i, [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l2 Size
s2)
{-# INLINE splitAt #-}
takeWhile :: forall a . (a -> Bool) -> Slist a -> Slist a
takeWhile :: (a -> Bool) -> Slist a -> Slist a
takeWhile a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s, [a]
l) = Int -> [a] -> (Int, [a])
go Int
0 [a]
sList in
[a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s
where
go :: Int -> [a] -> (Int, [a])
go :: Int -> [a] -> (Int, [a])
go !Int
n [] = (Int
n, [])
go !Int
n (a
x:[a]
xs) =
if a -> Bool
p a
x
then let (Int
i, [a]
l) = Int -> [a] -> (Int, [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs in (Int
i, a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l)
else (Int
n, [])
{-# INLINE takeWhile #-}
dropWhile :: forall a . (a -> Bool) -> Slist a -> Slist a
dropWhile :: (a -> Bool) -> Slist a -> Slist a
dropWhile a -> Bool
p (Slist [a]
l Size
Infinity) = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
P.dropWhile a -> Bool
p [a]
l) Size
Infinity
dropWhile a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s, [a]
l) = Int -> [a] -> (Int, [a])
go Int
0 [a]
sList in
[a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
s)
where
go :: Int -> [a] -> (Int, [a])
go :: Int -> [a] -> (Int, [a])
go !Int
n [] = (Int
n, [])
go !Int
n (a
x:[a]
xs) =
if a -> Bool
p a
x
then Int -> [a] -> (Int, [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else (Int
n, a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
{-# INLINE dropWhile #-}
span :: forall a . (a -> Bool) -> Slist a -> (Slist a, Slist a)
span :: (a -> Bool) -> Slist a -> (Slist a, Slist a)
span a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s, [a]
l, [a]
r) = Int -> [a] -> (Int, [a], [a])
go Int
0 [a]
sList in
( [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s
, [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
r (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
s)
)
where
go :: Int -> [a] -> (Int, [a], [a])
go :: Int -> [a] -> (Int, [a], [a])
go !Int
n [] = (Int
n, [], [])
go !Int
n (a
x:[a]
xs) =
if a -> Bool
p a
x
then let (Int
s, [a]
l, [a]
r) = Int -> [a] -> (Int, [a], [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs in (Int
s, a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l, [a]
r)
else (Int
n, [], a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
{-# INLINE span #-}
break :: (a -> Bool) -> Slist a -> (Slist a, Slist a)
break :: (a -> Bool) -> Slist a -> (Slist a, Slist a)
break a -> Bool
p = (a -> Bool) -> Slist a -> (Slist a, Slist a)
forall a. (a -> Bool) -> Slist a -> (Slist a, Slist a)
span (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)
{-# INLINE break #-}
chunksOf :: Int -> Slist a -> Slist (Slist a)
chunksOf :: Int -> Slist a -> Slist (Slist a)
chunksOf Int
i sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist (Slist a)
forall a. Monoid a => a
mempty
| Size
sSize Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity = [Slist a] -> Size -> Slist (Slist a)
forall a. [a] -> Size -> Slist a
Slist (([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map (Int -> [Item (Slist a)] -> Slist a
forall l. IsList l => Int -> [Item l] -> l
fromListN Int
i) ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
listChunksOf Int
i [a]
sList) Size
Infinity
| Bool
otherwise = Slist a -> Slist (Slist a)
forall a. Slist a -> Slist (Slist a)
go Slist a
sl
where
go :: Slist a -> Slist (Slist a)
go :: Slist a -> Slist (Slist a)
go x :: Slist a
x@(Slist [a]
_ Size
s)
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
s = Slist a -> Slist (Slist a)
forall a. a -> Slist a
one Slist a
x
| Bool
otherwise =
let (Slist a
chunk, Slist a
rest) = Int -> Slist a -> (Slist a, Slist a)
forall a. Int -> Slist a -> (Slist a, Slist a)
splitAt Int
i Slist a
x
in Slist a -> Slist (Slist a) -> Slist (Slist a)
forall a. a -> Slist a -> Slist a
cons Slist a
chunk (Slist (Slist a) -> Slist (Slist a))
-> Slist (Slist a) -> Slist (Slist a)
forall a b. (a -> b) -> a -> b
$ Slist a -> Slist (Slist a)
forall a. Slist a -> Slist (Slist a)
go Slist a
rest
{-# INLINE chunksOf #-}
listChunksOf :: Int -> [a] -> [[a]]
listChunksOf :: Int -> [a] -> [[a]]
listChunksOf Int
i [a]
l
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = [[a]]
forall a. Monoid a => a
mempty
| Bool
otherwise = [a] -> [[a]]
forall a. [a] -> [[a]]
go [a]
l
where
go :: [a] -> [[a]]
go :: [a] -> [[a]]
go [] = []
go [a]
x =
let ([a]
chunk, [a]
rest) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
P.splitAt Int
i [a]
x
in [a]
chunk [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> [[a]]
forall a. [a] -> [[a]]
go [a]
rest
{-# INLINE listChunksOf #-}
stripPrefix :: Eq a => Slist a -> Slist a -> Maybe (Slist a)
stripPrefix :: Slist a -> Slist a -> Maybe (Slist a)
stripPrefix (Slist [a]
l1 Size
s1) f :: Slist a
f@(Slist [a]
l2 Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Size
Size Int
0 = Slist a -> Maybe (Slist a)
forall a. a -> Maybe a
Just Slist a
f
| Size
s1 Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
> Size
s2 = Maybe (Slist a)
forall a. Maybe a
Nothing
| Bool
otherwise = (\[a]
l -> [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Size
s2 Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
s1) ([a] -> Slist a) -> Maybe [a] -> Maybe (Slist a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
L.stripPrefix [a]
l1 [a]
l2
{-# INLINE stripPrefix #-}
safeStripPrefix :: Eq a => Slist a -> Slist a -> Maybe (Slist a)
safeStripPrefix :: Slist a -> Slist a -> Maybe (Slist a)
safeStripPrefix (Slist [a]
_ Size
Infinity) (Slist [a]
_ Size
Infinity) = Maybe (Slist a)
forall a. Maybe a
Nothing
safeStripPrefix Slist a
sl1 Slist a
sl2 = Slist a -> Slist a -> Maybe (Slist a)
forall a. Eq a => Slist a -> Slist a -> Maybe (Slist a)
stripPrefix Slist a
sl1 Slist a
sl2
{-# INLINE safeStripPrefix #-}
group :: Eq a => Slist a -> Slist (Slist a)
group :: Slist a -> Slist (Slist a)
group = (a -> a -> Bool) -> Slist a -> Slist (Slist a)
forall a. (a -> a -> Bool) -> Slist a -> Slist (Slist a)
groupBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE group #-}
groupBy :: (a -> a -> Bool) -> Slist a -> Slist (Slist a)
groupBy :: (a -> a -> Bool) -> Slist a -> Slist (Slist a)
groupBy a -> a -> Bool
p (Slist [a]
l Size
Infinity) = [Slist a] -> Slist (Slist a)
forall a. [a] -> Slist a
infiniteSlist ([Slist a] -> Slist (Slist a)) -> [Slist a] -> Slist (Slist a)
forall a b. (a -> b) -> a -> b
$ ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map [a] -> Slist a
forall a. [a] -> Slist a
slist ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
L.groupBy a -> a -> Bool
p [a]
l
groupBy a -> a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [Slist a] -> Slist (Slist a)
forall a. [a] -> Slist a
slist ([Slist a] -> Slist (Slist a)) -> [Slist a] -> Slist (Slist a)
forall a b. (a -> b) -> a -> b
$ ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map [a] -> Slist a
forall a. [a] -> Slist a
slist ([[a]] -> [Slist a]) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
L.groupBy a -> a -> Bool
p [a]
sList
{-# INLINE groupBy #-}
inits :: Slist a -> Slist (Slist a)
inits :: Slist a -> Slist (Slist a)
inits (Slist [a]
l Size
s) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [Slist a]
sList = ([a] -> Size -> Slist a) -> [[a]] -> [Size] -> [Slist a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ([a] -> [[a]]
forall a. [a] -> [[a]]
L.inits [a]
l) ([Size] -> [Slist a]) -> [Size] -> [Slist a]
forall a b. (a -> b) -> a -> b
$ Size -> [Size]
sizes Size
s
, sSize :: Size
sSize = Size
s Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1
}
{-# INLINE inits #-}
tails :: Slist a -> Slist (Slist a)
tails :: Slist a -> Slist (Slist a)
tails (Slist [a]
l Size
Infinity) = [Slist a] -> Slist (Slist a)
forall a. [a] -> Slist a
infiniteSlist ([Slist a] -> Slist (Slist a)) -> [Slist a] -> Slist (Slist a)
forall a b. (a -> b) -> a -> b
$ ([a] -> Slist a) -> [[a]] -> [Slist a]
forall a b. (a -> b) -> [a] -> [b]
P.map [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> [[a]]
forall a. [a] -> [[a]]
L.tails [a]
l)
tails (Slist [a]
l s :: Size
s@(Size Int
n)) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [Slist a]
sList = ([a] -> Int -> Slist a) -> [[a]] -> [Int] -> [Slist a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith (\[a]
li Int
i -> [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
li (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
i) ([a] -> [[a]]
forall a. [a] -> [[a]]
L.tails [a]
l) [Int
n, Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
0]
, sSize :: Size
sSize = Size
s Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1
}
{-# INLINE tails #-}
isPrefixOf :: Eq a => Slist a -> Slist a -> Bool
isPrefixOf :: Slist a -> Slist a -> Bool
isPrefixOf (Slist [a]
l1 Size
s1) (Slist [a]
l2 Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
> Size
s2 = Bool
False
| Bool
otherwise = [a]
l1 [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isPrefixOf` [a]
l2
{-# INLINE isPrefixOf #-}
safeIsPrefixOf :: Eq a => Slist a -> Slist a -> Bool
safeIsPrefixOf :: Slist a -> Slist a -> Bool
safeIsPrefixOf sl1 :: Slist a
sl1@(Slist [a]
_ Size
s1) sl2 :: Slist a
sl2@(Slist [a]
_ Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity Bool -> Bool -> Bool
&& Size
s2 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity = Bool
False
| Bool
otherwise = Slist a
sl1 Slist a -> Slist a -> Bool
forall a. Eq a => Slist a -> Slist a -> Bool
`isPrefixOf` Slist a
sl2
{-# INLINE safeIsPrefixOf #-}
isSuffixOf :: Eq a => Slist a -> Slist a -> Bool
isSuffixOf :: Slist a -> Slist a -> Bool
isSuffixOf (Slist [a]
l1 Size
s1) (Slist [a]
l2 Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
> Size
s2 = Bool
False
| Bool
otherwise = [a]
l1 [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isSuffixOf` [a]
l2
{-# INLINE isSuffixOf #-}
safeIsSuffixOf :: Eq a => Slist a -> Slist a -> Bool
safeIsSuffixOf :: Slist a -> Slist a -> Bool
safeIsSuffixOf Slist a
sl1 sl2 :: Slist a
sl2@(Slist [a]
_ Size
s2)
| Size
s2 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity = Bool
False
| Bool
otherwise = Slist a
sl1 Slist a -> Slist a -> Bool
forall a. Eq a => Slist a -> Slist a -> Bool
`isSuffixOf` Slist a
sl2
{-# INLINE safeIsSuffixOf #-}
isInfixOf :: Eq a => Slist a -> Slist a -> Bool
isInfixOf :: Slist a -> Slist a -> Bool
isInfixOf (Slist [a]
l1 Size
s1) (Slist [a]
l2 Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
> Size
s2 = Bool
False
| Bool
otherwise = [a]
l1 [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isInfixOf` [a]
l2
{-# INLINE isInfixOf #-}
safeIsInfixOf :: Eq a => Slist a -> Slist a -> Bool
safeIsInfixOf :: Slist a -> Slist a -> Bool
safeIsInfixOf sl1 :: Slist a
sl1@(Slist [a]
_ Size
s1) sl2 :: Slist a
sl2@(Slist [a]
_ Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity Bool -> Bool -> Bool
&& Size
s2 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity = Bool
False
| Bool
otherwise = Slist a
sl1 Slist a -> Slist a -> Bool
forall a. Eq a => Slist a -> Slist a -> Bool
`isInfixOf` Slist a
sl2
{-# INLINE safeIsInfixOf #-}
isSubsequenceOf :: Eq a => Slist a -> Slist a -> Bool
isSubsequenceOf :: Slist a -> Slist a -> Bool
isSubsequenceOf (Slist [a]
l1 Size
s1) (Slist [a]
l2 Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
> Size
s2 = Bool
False
| Bool
otherwise = [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isSubsequenceOf [a]
l1 [a]
l2
{-# INLINE isSubsequenceOf #-}
safeIsSubsequenceOf :: Eq a => Slist a -> Slist a -> Bool
safeIsSubsequenceOf :: Slist a -> Slist a -> Bool
safeIsSubsequenceOf sl1 :: Slist a
sl1@(Slist [a]
_ Size
s1) sl2 :: Slist a
sl2@(Slist [a]
_ Size
s2)
| Size
s1 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity Bool -> Bool -> Bool
&& Size
s2 Size -> Size -> Bool
forall a. Eq a => a -> a -> Bool
== Size
Infinity = Bool
False
| Bool
otherwise = Slist a -> Slist a -> Bool
forall a. Eq a => Slist a -> Slist a -> Bool
isSubsequenceOf Slist a
sl1 Slist a
sl2
{-# INLINE safeIsSubsequenceOf #-}
lookup :: Eq a => a -> Slist (a, b) -> Maybe b
lookup :: a -> Slist (a, b) -> Maybe b
lookup a
a = a -> [(a, b)] -> Maybe b
forall a b. Eq a => a -> [(a, b)] -> Maybe b
L.lookup a
a ([(a, b)] -> Maybe b)
-> (Slist (a, b) -> [(a, b)]) -> Slist (a, b) -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist (a, b) -> [(a, b)]
forall a. Slist a -> [a]
sList
{-# INLINE lookup #-}
filter :: forall a . (a -> Bool) -> Slist a -> Slist a
filter :: (a -> Bool) -> Slist a -> Slist a
filter a -> Bool
p (Slist [a]
l Size
Infinity) = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> [a] -> Slist a
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
L.filter a -> Bool
p [a]
l
filter a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
newS, [a]
newL) = Int -> [a] -> (Int, [a])
go Int
0 [a]
sList in
[a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
newL (Int -> Size
Size Int
newS)
where
go :: Int -> [a] -> (Int, [a])
go :: Int -> [a] -> (Int, [a])
go !Int
n [] = (Int
n, [])
go Int
n (a
x:[a]
xs) =
if a -> Bool
p a
x
then ([a] -> [a]) -> (Int, [a]) -> (Int, [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a]) -> (Int, [a])) -> (Int, [a]) -> (Int, [a])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else Int -> [a] -> (Int, [a])
go Int
n [a]
xs
{-# INLINE filter #-}
partition :: forall a . (a -> Bool) -> Slist a -> (Slist a, Slist a)
partition :: (a -> Bool) -> Slist a -> (Slist a, Slist a)
partition a -> Bool
p (Slist [a]
l Size
Infinity) = ([a] -> Slist a)
-> ([a] -> Slist a) -> ([a], [a]) -> (Slist a, Slist a)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist (([a], [a]) -> (Slist a, Slist a))
-> ([a], [a]) -> (Slist a, Slist a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
L.partition a -> Bool
p [a]
l
partition a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s1, [a]
l1, [a]
l2) = Int -> [a] -> (Int, [a], [a])
go Int
0 [a]
sList in
([a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l1 (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s1, [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l2 (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
s1)
where
go :: Int -> [a] -> (Int, [a], [a])
go :: Int -> [a] -> (Int, [a], [a])
go !Int
n [] = (Int
n, [], [])
go Int
n (a
x:[a]
xs) =
if a -> Bool
p a
x
then ([a] -> [a]) -> (Int, [a], [a]) -> (Int, [a], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a], [a]) -> (Int, [a], [a]))
-> (Int, [a], [a]) -> (Int, [a], [a])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [a], [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else ([a] -> [a]) -> (Int, [a], [a]) -> (Int, [a], [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a], [a]) -> (Int, [a], [a]))
-> (Int, [a], [a]) -> (Int, [a], [a])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [a], [a])
go Int
n [a]
xs
{-# INLINE partition #-}
partitionWith :: forall a b c . (a -> Either b c) -> Slist a -> (Slist b, Slist c)
partitionWith :: (a -> Either b c) -> Slist a -> (Slist b, Slist c)
partitionWith a -> Either b c
f (Slist [a]
l Size
Infinity) = ([b] -> Slist b)
-> ([c] -> Slist c) -> ([b], [c]) -> (Slist b, Slist c)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [b] -> Slist b
forall a. [a] -> Slist a
infiniteSlist [c] -> Slist c
forall a. [a] -> Slist a
infiniteSlist (([b], [c]) -> (Slist b, Slist c))
-> ([b], [c]) -> (Slist b, Slist c)
forall a b. (a -> b) -> a -> b
$ (a -> Either b c) -> [a] -> ([b], [c])
forall a b c. (a -> Either b c) -> [a] -> ([b], [c])
listPartitionWith a -> Either b c
f [a]
l
partitionWith a -> Either b c
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s1, [b]
l1, [c]
l2) = Int -> [a] -> (Int, [b], [c])
go Int
0 [a]
sList in
([b] -> Size -> Slist b
forall a. [a] -> Size -> Slist a
Slist [b]
l1 (Size -> Slist b) -> Size -> Slist b
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s1, [c] -> Size -> Slist c
forall a. [a] -> Size -> Slist a
Slist [c]
l2 (Size -> Slist c) -> Size -> Slist c
forall a b. (a -> b) -> a -> b
$ Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
s1)
where
go :: Int -> [a] -> (Int, [b], [c])
go :: Int -> [a] -> (Int, [b], [c])
go !Int
n [] = (Int
n, [], [])
go Int
n (a
x:[a]
xs) = case a -> Either b c
f a
x of
Left b
b -> ([b] -> [b]) -> (Int, [b], [c]) -> (Int, [b], [c])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (b
bb -> [b] -> [b]
forall a. a -> [a] -> [a]
:) ((Int, [b], [c]) -> (Int, [b], [c]))
-> (Int, [b], [c]) -> (Int, [b], [c])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [b], [c])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
Right c
c -> ([c] -> [c]) -> (Int, [b], [c]) -> (Int, [b], [c])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:) ((Int, [b], [c]) -> (Int, [b], [c]))
-> (Int, [b], [c]) -> (Int, [b], [c])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [b], [c])
go Int
n [a]
xs
{-# INLINE partitionWith #-}
listPartitionWith :: forall a b c . (a -> Either b c) -> [a] -> ([b], [c])
listPartitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
listPartitionWith a -> Either b c
f = [Either b c] -> ([b], [c])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either b c] -> ([b], [c]))
-> ([a] -> [Either b c]) -> [a] -> ([b], [c])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either b c) -> [a] -> [Either b c]
forall a b. (a -> b) -> [a] -> [b]
L.map a -> Either b c
f
{-# INLINE listPartitionWith #-}
at :: Int -> Slist a -> Maybe a
at :: Int -> Slist a -> Maybe a
at Int
n Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int -> Size
Size Int
n Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = Maybe a
forall a. Maybe a
Nothing
| Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a]
sList [a] -> Int -> a
forall a. [a] -> Int -> a
L.!! Int
n
{-# INLINE at #-}
unsafeAt :: Int -> Slist a -> a
unsafeAt :: Int -> Slist a -> a
unsafeAt Int
n Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a]
sList [a] -> Int -> a
forall a. [a] -> Int -> a
L.!! Int
n
{-# INLINE unsafeAt #-}
elemIndex :: Eq a => a -> Slist a -> Maybe Int
elemIndex :: a -> Slist a -> Maybe Int
elemIndex a
a = a -> [a] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
L.elemIndex a
a ([a] -> Maybe Int) -> (Slist a -> [a]) -> Slist a -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> [a]
forall a. Slist a -> [a]
sList
{-# INLINE elemIndex #-}
elemIndices :: Eq a => a -> Slist a -> Slist Int
elemIndices :: a -> Slist a -> Slist Int
elemIndices a
a = (a -> Bool) -> Slist a -> Slist Int
forall a. (a -> Bool) -> Slist a -> Slist Int
findIndices (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==)
{-# INLINE elemIndices #-}
findIndex :: (a -> Bool) -> Slist a -> Maybe Int
findIndex :: (a -> Bool) -> Slist a -> Maybe Int
findIndex a -> Bool
p = (a -> Bool) -> [a] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
L.findIndex a -> Bool
p ([a] -> Maybe Int) -> (Slist a -> [a]) -> Slist a -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> [a]
forall a. Slist a -> [a]
sList
{-# INLINE findIndex #-}
findIndices :: forall a . (a -> Bool) -> Slist a -> Slist Int
findIndices :: (a -> Bool) -> Slist a -> Slist Int
findIndices a -> Bool
p (Slist [a]
l Size
Infinity) = [Int] -> Slist Int
forall a. [a] -> Slist a
infiniteSlist ([Int] -> Slist Int) -> [Int] -> Slist Int
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [Int]
forall a. (a -> Bool) -> [a] -> [Int]
L.findIndices a -> Bool
p [a]
l
findIndices a -> Bool
p Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
newS, [Int]
newL) = Int -> Int -> [a] -> (Int, [Int])
go Int
0 Int
0 [a]
sList in
[Int] -> Size -> Slist Int
forall a. [a] -> Size -> Slist a
Slist [Int]
newL (Int -> Size
Size Int
newS)
where
go :: Int -> Int -> [a] -> (Int, [Int])
go :: Int -> Int -> [a] -> (Int, [Int])
go !Int
n Int
_ [] = (Int
n, [])
go Int
n !Int
cur (a
x:[a]
xs) =
if a -> Bool
p a
x
then ([Int] -> [Int]) -> (Int, [Int]) -> (Int, [Int])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (Int
curInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:) ((Int, [Int]) -> (Int, [Int])) -> (Int, [Int]) -> (Int, [Int])
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [a] -> (Int, [Int])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else Int -> Int -> [a] -> (Int, [Int])
go Int
n (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
{-# INLINE findIndices #-}
zip :: Slist a -> Slist b -> Slist (a, b)
zip :: Slist a -> Slist b -> Slist (a, b)
zip (Slist [a]
l1 Size
s1) (Slist [b]
l2 Size
s2) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [(a, b)]
sList = [a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
L.zip [a]
l1 [b]
l2
, sSize :: Size
sSize = Size -> Size -> Size
forall a. Ord a => a -> a -> a
min Size
s1 Size
s2
}
{-# INLINE zip #-}
zip3 :: Slist a -> Slist b -> Slist c -> Slist (a, b, c)
zip3 :: Slist a -> Slist b -> Slist c -> Slist (a, b, c)
zip3 (Slist [a]
l1 Size
s1) (Slist [b]
l2 Size
s2) (Slist [c]
l3 Size
s3) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [(a, b, c)]
sList = [a] -> [b] -> [c] -> [(a, b, c)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
L.zip3 [a]
l1 [b]
l2 [c]
l3
, sSize :: Size
sSize = [Size] -> Size
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Size
s1, Size
s2, Size
s3]
}
{-# INLINE zip3 #-}
zipWith :: (a -> b -> c) -> Slist a -> Slist b -> Slist c
zipWith :: (a -> b -> c) -> Slist a -> Slist b -> Slist c
zipWith a -> b -> c
f (Slist [a]
l1 Size
s1) (Slist [b]
l2 Size
s2) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [c]
sList = (a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith a -> b -> c
f [a]
l1 [b]
l2
, sSize :: Size
sSize = Size -> Size -> Size
forall a. Ord a => a -> a -> a
min Size
s1 Size
s2
}
{-# INLINE zipWith #-}
zipWith3 :: (a -> b -> c -> d) -> Slist a -> Slist b -> Slist c -> Slist d
zipWith3 :: (a -> b -> c -> d) -> Slist a -> Slist b -> Slist c -> Slist d
zipWith3 a -> b -> c -> d
f (Slist [a]
l1 Size
s1) (Slist [b]
l2 Size
s2) (Slist [c]
l3 Size
s3) = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [d]
sList = (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
L.zipWith3 a -> b -> c -> d
f [a]
l1 [b]
l2 [c]
l3
, sSize :: Size
sSize = [Size] -> Size
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Size
s1, Size
s2, Size
s3]
}
{-# INLINE zipWith3 #-}
unzip :: Slist (a, b) -> (Slist a, Slist b)
unzip :: Slist (a, b) -> (Slist a, Slist b)
unzip Slist{[(a, b)]
Size
sSize :: Size
sList :: [(a, b)]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let ([a]
as, [b]
bs) = [(a, b)] -> ([a], [b])
forall a b. [(a, b)] -> ([a], [b])
L.unzip [(a, b)]
sList in ([a] -> Slist a
forall a. [a] -> Slist a
l [a]
as, [b] -> Slist b
forall a. [a] -> Slist a
l [b]
bs)
where
l :: [x] -> Slist x
l :: [x] -> Slist x
l [x]
x = [x] -> Size -> Slist x
forall a. [a] -> Size -> Slist a
Slist [x]
x Size
sSize
{-# INLINE unzip #-}
unzip3 :: Slist (a, b, c) -> (Slist a, Slist b, Slist c)
unzip3 :: Slist (a, b, c) -> (Slist a, Slist b, Slist c)
unzip3 Slist{[(a, b, c)]
Size
sSize :: Size
sList :: [(a, b, c)]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let ([a]
as, [b]
bs, [c]
cs) = [(a, b, c)] -> ([a], [b], [c])
forall a b c. [(a, b, c)] -> ([a], [b], [c])
L.unzip3 [(a, b, c)]
sList in ([a] -> Slist a
forall a. [a] -> Slist a
l [a]
as, [b] -> Slist b
forall a. [a] -> Slist a
l [b]
bs, [c] -> Slist c
forall a. [a] -> Slist a
l [c]
cs)
where
l :: [x] -> Slist x
l :: [x] -> Slist x
l [x]
x = [x] -> Size -> Slist x
forall a. [a] -> Size -> Slist a
Slist [x]
x Size
sSize
{-# INLINE unzip3 #-}
nub :: Eq a => Slist a -> Slist a
nub :: Slist a -> Slist a
nub = (a -> a -> Bool) -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> Slist a -> Slist a
nubBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE nub #-}
nubBy :: forall a . (a -> a -> Bool) -> Slist a -> Slist a
nubBy :: (a -> a -> Bool) -> Slist a -> Slist a
nubBy a -> a -> Bool
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Int
s, [a]
l) = Int -> [a] -> [a] -> (Int, [a])
go Int
0 [] [a]
sList in case Size
sSize of
Size
Infinity -> [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist [a]
l
Size
_ -> [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s
where
go :: Int -> [a] -> [a] -> (Int, [a])
go :: Int -> [a] -> [a] -> (Int, [a])
go !Int
n [a]
res [] = (Int
n, [a]
res)
go Int
n [a]
res (a
x:[a]
xs) =
if (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (a -> a -> Bool
f a
x) [a]
res
then Int -> [a] -> [a] -> (Int, [a])
go Int
n [a]
res [a]
xs
else Int -> [a] -> [a] -> (Int, [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ([a]
res [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
x]) [a]
xs
{-# INLINE nubBy #-}
ordNub :: forall a . (Ord a) => Slist a -> Slist a
ordNub :: Slist a -> Slist a
ordNub Slist a
sl = let (Int
s, [a]
l) = Int -> Set a -> [a] -> (Int, [a])
go Int
0 Set a
forall a. Set a
Set.empty (Slist a -> [a]
forall a. Slist a -> [a]
sList Slist a
sl) in Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = [a]
l
, sSize :: Size
sSize = Int -> Size
Size Int
s
}
where
go :: Int -> Set.Set a -> [a] -> (Int, [a])
go :: Int -> Set a -> [a] -> (Int, [a])
go !Int
i Set a
_ [] = (Int
i, [])
go Int
i Set a
s (a
x:[a]
xs) =
if a
x a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set a
s
then Int -> Set a -> [a] -> (Int, [a])
go Int
i Set a
s [a]
xs
else ([a] -> [a]) -> (Int, [a]) -> (Int, [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a]) -> (Int, [a])) -> (Int, [a]) -> (Int, [a])
forall a b. (a -> b) -> a -> b
$ Int -> Set a -> [a] -> (Int, [a])
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
x Set a
s) [a]
xs
{-# INLINEABLE ordNub #-}
delete :: Eq a => a -> Slist a -> Slist a
delete :: a -> Slist a -> Slist a
delete = (a -> a -> Bool) -> a -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> a -> Slist a -> Slist a
deleteBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE delete #-}
deleteBy :: forall a . (a -> a -> Bool) -> a -> Slist a -> Slist a
deleteBy :: (a -> a -> Bool) -> a -> Slist a -> Slist a
deleteBy a -> a -> Bool
f a
a (Slist [a]
l Size
Infinity) = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> [a] -> Slist a
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> a -> [a] -> [a]
forall a. (a -> a -> Bool) -> a -> [a] -> [a]
L.deleteBy a -> a -> Bool
f a
a [a]
l
deleteBy a -> a -> Bool
f a
a Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = let (Size
del, [a]
res) = [a] -> (Size, [a])
go [a]
sList in
[a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
res (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Size
del
where
go :: [a] -> (Size, [a])
go :: [a] -> (Size, [a])
go [] = (Int -> Size
Size Int
0, [])
go (a
x:[a]
xs) = if a -> a -> Bool
f a
a a
x
then (Int -> Size
Size Int
1, [a]
xs)
else ([a] -> [a]) -> (Size, [a]) -> (Size, [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Size, [a]) -> (Size, [a])) -> (Size, [a]) -> (Size, [a])
forall a b. (a -> b) -> a -> b
$ [a] -> (Size, [a])
go [a]
xs
{-# INLINE deleteBy #-}
deleteFirstsBy :: (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
deleteFirstsBy :: (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
deleteFirstsBy a -> a -> Bool
f = (a -> Slist a -> Slist a) -> Slist a -> Slist a -> Slist a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((a -> a -> Bool) -> a -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> a -> Slist a -> Slist a
deleteBy a -> a -> Bool
f)
{-# INLINE deleteFirstsBy #-}
diff :: Eq a => Slist a -> Slist a -> Slist a
diff :: Slist a -> Slist a -> Slist a
diff = (a -> Slist a -> Slist a) -> Slist a -> Slist a -> Slist a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> Slist a -> Slist a
forall a. Eq a => a -> Slist a -> Slist a
delete
{-# INLINE diff #-}
union :: Eq a => Slist a -> Slist a -> Slist a
union :: Slist a -> Slist a -> Slist a
union = (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
unionBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE union #-}
unionBy :: (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
unionBy :: (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
unionBy a -> a -> Bool
f Slist a
xs Slist a
ys = Slist a
xs Slist a -> Slist a -> Slist a
forall a. Semigroup a => a -> a -> a
<> (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
deleteFirstsBy a -> a -> Bool
f ((a -> a -> Bool) -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> Slist a -> Slist a
nubBy a -> a -> Bool
f Slist a
ys) Slist a
xs
{-# INLINE unionBy #-}
intersect :: Eq a => Slist a -> Slist a -> Slist a
intersect :: Slist a -> Slist a -> Slist a
intersect = (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
forall a. (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
intersectBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE intersect #-}
intersectBy :: forall a . (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
intersectBy :: (a -> a -> Bool) -> Slist a -> Slist a -> Slist a
intersectBy a -> a -> Bool
_ (Slist [a]
_ (Size Int
0)) Slist a
_ = Slist a
forall a. Monoid a => a
mempty
intersectBy a -> a -> Bool
_ Slist a
_ (Slist [a]
_ (Size Int
0)) = Slist a
forall a. Monoid a => a
mempty
intersectBy a -> a -> Bool
f (Slist [a]
l1 Size
Infinity) (Slist [a]
l2 Size
_) = [a] -> Slist a
forall a. [a] -> Slist a
infiniteSlist ([a] -> Slist a) -> [a] -> Slist a
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> [a] -> [a] -> [a]
forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
L.intersectBy a -> a -> Bool
f [a]
l1 [a]
l2
intersectBy a -> a -> Bool
f (Slist [a]
l1 Size
_) (Slist [a]
l2 Size
_) =
let (Int
s, [a]
l) = Int -> [a] -> (Int, [a])
go Int
0 [a]
l1 in [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
s
where
go :: Int -> [a] -> (Int, [a])
go :: Int -> [a] -> (Int, [a])
go Int
n [] = (Int
n, [])
go Int
n (a
x:[a]
xs) =
if (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (a -> a -> Bool
f a
x) [a]
l2
then ([a] -> [a]) -> (Int, [a]) -> (Int, [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ((Int, [a]) -> (Int, [a])) -> (Int, [a]) -> (Int, [a])
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> (Int, [a])
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else Int -> [a] -> (Int, [a])
go Int
n [a]
xs
{-# INLINE intersectBy #-}
sort :: Ord a => Slist a -> Slist a
sort :: Slist a -> Slist a
sort = (a -> a -> Ordering) -> Slist a -> Slist a
forall a. (a -> a -> Ordering) -> Slist a -> Slist a
sortBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE sort #-}
sortBy :: (a -> a -> Ordering) -> Slist a -> Slist a
sortBy :: (a -> a -> Ordering) -> Slist a -> Slist a
sortBy a -> a -> Ordering
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
f [a]
sList) Size
sSize
{-# INLINE sortBy #-}
sortOn :: Ord b => (a -> b) -> Slist a -> Slist a
sortOn :: (a -> b) -> Slist a -> Slist a
sortOn a -> b
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> b) -> [a] -> [a]
forall b a. Ord b => (a -> b) -> [a] -> [a]
L.sortOn a -> b
f [a]
sList) Size
sSize
{-# INLINE sortOn #-}
sortWith :: Ord b => (a -> b) -> Slist a -> Slist a
sortWith :: (a -> b) -> Slist a -> Slist a
sortWith a -> b
f Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> b) -> [a] -> [a]
forall b a. Ord b => (a -> b) -> [a] -> [a]
Exts.sortWith a -> b
f [a]
sList) Size
sSize
{-# INLINE sortWith #-}
insert :: Ord a => a -> Slist a -> Slist a
insert :: a -> Slist a -> Slist a
insert = (a -> a -> Ordering) -> a -> Slist a -> Slist a
forall a. (a -> a -> Ordering) -> a -> Slist a -> Slist a
insertBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE insert #-}
insertBy :: (a -> a -> Ordering) -> a -> Slist a -> Slist a
insertBy :: (a -> a -> Ordering) -> a -> Slist a -> Slist a
insertBy a -> a -> Ordering
f a
a Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..} = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist ((a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
L.insertBy a -> a -> Ordering
f a
a [a]
sList) (Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1)
{-# INLINE insertBy #-}
genericLength :: Num i => Slist a -> i
genericLength :: Slist a -> i
genericLength = Int -> i
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> i) -> (Slist a -> Int) -> Slist a -> i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Slist a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINE genericLength #-}
genericTake :: Integral i => i -> Slist a -> Slist a
genericTake :: i -> Slist a -> Slist a
genericTake (i -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral -> Int
i) sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = Slist a
sl
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = Int -> [a] -> [a]
forall i a. Integral i => i -> [a] -> [a]
L.genericTake Int
i [a]
sList
, sSize :: Size
sSize = Size -> Size -> Size
forall a. Ord a => a -> a -> a
min Size
sSize (Int -> Size
Size Int
i)
}
{-# INLINE genericTake #-}
genericDrop :: Integral i => i -> Slist a -> Slist a
genericDrop :: i -> Slist a -> Slist a
genericDrop (i -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral -> Int
i) sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Slist a
sl
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = Slist :: forall a. [a] -> Size -> Slist a
Slist
{ sList :: [a]
sList = Int -> [a] -> [a]
forall i a. Integral i => i -> [a] -> [a]
L.genericDrop Int
i [a]
sList
, sSize :: Size
sSize = Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
i
}
{-# INLINE genericDrop #-}
genericSplitAt :: Integral i => i -> Slist a -> (Slist a, Slist a)
genericSplitAt :: i -> Slist a -> (Slist a, Slist a)
genericSplitAt (i -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral -> Int
i) sl :: Slist a
sl@Slist{[a]
Size
sSize :: Size
sList :: [a]
sSize :: forall a. Slist a -> Size
sList :: forall a. Slist a -> [a]
..}
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Slist a
forall a. Monoid a => a
mempty, Slist a
sl)
| Int -> Size
Size Int
i Size -> Size -> Bool
forall a. Ord a => a -> a -> Bool
>= Size
sSize = (Slist a
sl, Slist a
forall a. Monoid a => a
mempty)
| Bool
otherwise =
let ([a]
l1, [a]
l2) = Int -> [a] -> ([a], [a])
forall i a. Integral i => i -> [a] -> ([a], [a])
L.genericSplitAt Int
i [a]
sList
s2 :: Size
s2 = Size
sSize Size -> Size -> Size
forall a. Num a => a -> a -> a
- Int -> Size
Size Int
i
in ([a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l1 (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size Int
i, [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist [a]
l2 Size
s2)
{-# INLINE genericSplitAt #-}
genericAt :: Integral i => i -> Slist a -> Maybe a
genericAt :: i -> Slist a -> Maybe a
genericAt = Int -> Slist a -> Maybe a
forall a. Int -> Slist a -> Maybe a
at (Int -> Slist a -> Maybe a)
-> (i -> Int) -> i -> Slist a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE genericAt #-}
genericUnsafeAt :: Integral i => i -> Slist a -> a
genericUnsafeAt :: i -> Slist a -> a
genericUnsafeAt i
i Slist a
_ | i
i i -> i -> Bool
forall a. Ord a => a -> a -> Bool
< i
0 = [Char] -> a
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Slist.genericUnsafeAt: negative argument"
genericUnsafeAt i
i (Slist [a]
l Size
Infinity) = [a] -> i -> a
forall i a. Integral i => [a] -> i -> a
L.genericIndex [a]
l i
i
genericUnsafeAt i
i (Slist [a]
l (Size Int
n))
| i
i i -> i -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> i
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n = [Char] -> a
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Slist.genericUnsafeAt: index too large"
| Bool
otherwise = [a] -> i -> a
forall i a. Integral i => [a] -> i -> a
L.genericIndex [a]
l i
i
{-# INLINE genericUnsafeAt #-}
genericReplicate :: Integral i => i -> a -> Slist a
genericReplicate :: i -> a -> Slist a
genericReplicate i
n a
x
| i
n i -> i -> Bool
forall a. Ord a => a -> a -> Bool
<= i
0 = Slist a
forall a. Monoid a => a
mempty
| Bool
otherwise = [a] -> Size -> Slist a
forall a. [a] -> Size -> Slist a
Slist (i -> a -> [a]
forall i a. Integral i => i -> a -> [a]
L.genericReplicate i
n a
x) (Size -> Slist a) -> Size -> Slist a
forall a b. (a -> b) -> a -> b
$ Int -> Size
Size (i -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
n)
{-# INLINE genericReplicate #-}