{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, NoImplicitPrelude #-}
module Data.InsertLeft (
InsertLeft(..)
, subG
, takeFromEndG
, reverseTakeFromEndG
, dropFromEndG
, reverseDropFromEndG
, takeWhile
, dropWhile
, span
, splitAtEndG
, preAppend
, safeHeadG
, safeInitG
, safeLastG
, mapG
, filterG
, partitionG
, reverseTakeG
, takeG
, reverseDropG
, dropG
, splitAtG
, safeTailG
) where
import GHC.Base
import GHC.Num
import GHC.Real
import Data.Tuple
import qualified Data.Foldable as F
import Data.Monoid
infixr 1 %@, %^
class (F.Foldable t, Eq a, Eq (t a)) => InsertLeft t a where
(%@) :: a -> t a -> t a
(%^) :: t a -> t (t a) -> t (t a)
instance (Eq a) => InsertLeft [] a where
%@ :: a -> [a] -> [a]
(%@) = (:)
%^ :: [a] -> [[a]] -> [[a]]
(%^) = (:)
subG :: (InsertLeft t a, Monoid (t a), Monoid (t (t a))) => t a -> t a -> t (t a)
subG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a), Monoid (t (t a))) =>
t a -> t a -> t (t a)
subG t a
whspss t a
xs = if t a -> Bool
forall a. t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
F.null t a
ts then t (t a)
forall a. Monoid a => a
mempty else t a
w t a -> t (t a) -> t (t a)
forall (t :: * -> *) a. InsertLeft t a => t a -> t (t a) -> t (t a)
%^ t a -> t a -> t (t a)
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a), Monoid (t (t a))) =>
t a -> t a -> t (t a)
subG t a
whspss t a
s''
where ts :: t a
ts = (a -> Bool) -> t a -> t a
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> t a
dropWhile (a -> t a -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`F.elem` t a
whspss) t a
xs
(t a
w, t a
s'') = (a -> Bool) -> t a -> (t a, t a)
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
span (a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`F.notElem` t a
whspss) t a
ts
{-# SPECIALIZE subG :: String -> String -> [String] #-}
dropWhile' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)
dropWhile' :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
dropWhile' a -> Bool
p = (a -> (t a, t a) -> (t a, t a)) -> (t a, t a) -> t a -> (t a, t a)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, t a) -> (t a, t a)
forall {t :: * -> *}.
InsertLeft t a =>
a -> (t a, t a) -> (t a, t a)
f (t a, t a)
v
where f :: a -> (t a, t a) -> (t a, t a)
f a
x (t a
ys, t a
xs) = (if a -> Bool
p a
x then t a
ys else a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs, a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs)
v :: (t a, t a)
v = (t a
forall a. Monoid a => a
mempty,t a
forall a. Monoid a => a
mempty)
dropWhile :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> t a
dropWhile :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> t a
dropWhile a -> Bool
p = (t a, t a) -> t a
forall a b. (a, b) -> a
fst ((t a, t a) -> t a) -> (t a -> (t a, t a)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> t a -> (t a, t a)
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
dropWhile' a -> Bool
p
span :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)
span :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
span a -> Bool
p = (\(t a
x, t a
y, t a
_) -> (t a
x, t a
y)) ((t a, t a, t a) -> (t a, t a))
-> (t a -> (t a, t a, t a)) -> t a -> (t a, t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> t a -> (t a, t a, t a)
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a, t a)
span' a -> Bool
p
span' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a, t a)
span' :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a, t a)
span' a -> Bool
p = (a -> (t a, t a, t a) -> (t a, t a, t a))
-> (t a, t a, t a) -> t a -> (t a, t a, t a)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, t a, t a) -> (t a, t a, t a)
forall {t :: * -> *} {t :: * -> *}.
(InsertLeft t a, InsertLeft t a, Monoid (t a)) =>
a -> (t a, t a, t a) -> (t a, t a, t a)
f (t a, t a, t a)
v
where f :: a -> (t a, t a, t a) -> (t a, t a, t a)
f a
x (t a
ys, t a
zs, t a
xs)
| a -> Bool
p a
x = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
ys, t a
zs, a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs)
| Bool
otherwise = (t a
forall a. Monoid a => a
mempty,a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs, a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs)
v :: (t a, t a, t a)
v = (t a
forall a. Monoid a => a
mempty, t a
forall a. Monoid a => a
mempty, t a
forall a. Monoid a => a
mempty)
takeWhile :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> t a
takeWhile :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> t a
takeWhile a -> Bool
p = (t a, t a) -> t a
forall a b. (a, b) -> a
fst ((t a, t a) -> t a) -> (t a -> (t a, t a)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> t a -> (t a, t a)
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
takeWhile' a -> Bool
p
takeWhile' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)
takeWhile' :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
takeWhile' a -> Bool
p = (a -> (t a, t a) -> (t a, t a)) -> (t a, t a) -> t a -> (t a, t a)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, t a) -> (t a, t a)
forall {t :: * -> *} {t :: * -> *}.
(Monoid (t a), InsertLeft t a, InsertLeft t a) =>
a -> (t a, t a) -> (t a, t a)
f (t a, t a)
v
where f :: a -> (t a, t a) -> (t a, t a)
f a
x (t a
ys,t a
xs) = (if a -> Bool
p a
x then a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
ys else t a
forall a. Monoid a => a
mempty, a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
xs)
v :: (t a, t a)
v = (t a
forall a. Monoid a => a
mempty,t a
forall a. Monoid a => a
mempty)
preAppend :: (InsertLeft t a, Monoid (t (t a))) => t a -> t (t a) -> t (t a) -> t (t a)
preAppend :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t (t a))) =>
t a -> t (t a) -> t (t a) -> t (t a)
preAppend t a
ts t (t a)
uss t (t a)
tss = [t (t a)] -> t (t a)
forall a. Monoid a => [a] -> a
mconcat [t a
ts t a -> t (t a) -> t (t a)
forall (t :: * -> *) a. InsertLeft t a => t a -> t (t a) -> t (t a)
%^ t (t a)
tss, t (t a)
uss]
{-# INLINE preAppend #-}
{-# SPECIALIZE preAppend :: String -> [String] -> [String] -> [String] #-}
takeFromEndG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
takeFromEndG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
takeFromEndG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (t a, b) -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, b) -> (t a, b)
forall {t :: * -> *} {a}.
InsertLeft t a =>
a -> (t a, b) -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: a -> (t a, b) -> (t a, b)
f a
x (t a
zs,b
k)
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,b
k)
{-# SPECIALIZE takeFromEndG :: Int -> String -> String #-}
reverseTakeFromEndG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
reverseTakeFromEndG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
reverseTakeFromEndG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (t a, b) -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, b) -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
a -> (t a, b) -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: a -> (t a, b) -> (t a, b)
f a
x (t a
zs,b
k)
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
zs t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,b
k)
{-# SPECIALIZE reverseTakeFromEndG :: Int -> String -> String #-}
reverseTakeG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
reverseTakeG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
reverseTakeG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t a, b) -> a -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (t a, b) -> a -> (t a, b)
forall {t :: * -> *} {a}.
InsertLeft t a =>
(t a, b) -> a -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: (t a, b) -> a -> (t a, b)
f (t a
zs,b
k) a
x
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,b
k)
{-# SPECIALIZE reverseTakeG :: Int -> String -> String #-}
takeG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
takeG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
takeG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t a, b) -> a -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (t a, b) -> a -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
(t a, b) -> a -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: (t a, b) -> a -> (t a, b)
f (t a
zs,b
k) a
x
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
zs t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,b
k)
{-# SPECIALIZE takeG :: Int -> String -> String #-}
reverseDropG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
reverseDropG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
reverseDropG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t a, b) -> a -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (t a, b) -> a -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
(t a, b) -> a -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: (t a, b) -> a -> (t a, b)
f (t a
zs,b
k) a
x
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs,b
k)
{-# SPECIALIZE reverseDropG :: Int -> String -> String #-}
dropFromEndG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
dropFromEndG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
dropFromEndG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (t a, b) -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, b) -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
a -> (t a, b) -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: a -> (t a, b) -> (t a, b)
f a
x (t a
zs,b
k)
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs,b
k)
{-# SPECIALIZE dropFromEndG :: Int -> String -> String #-}
reverseDropFromEndG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
reverseDropFromEndG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
reverseDropFromEndG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (t a, b) -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, b) -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
a -> (t a, b) -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: a -> (t a, b) -> (t a, b)
f a
x (t a
zs,b
k)
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),b
k)
{-# SPECIALIZE reverseDropFromEndG :: Int -> String -> String #-}
dropG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> t a
dropG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
dropG b
n = (\(t a
xs,b
_) -> t a
xs) ((t a, b) -> t a) -> (t a -> (t a, b)) -> t a -> t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t a, b) -> a -> (t a, b)) -> (t a, b) -> t a -> (t a, b)
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (t a, b) -> a -> (t a, b)
forall {t :: * -> *} {a}.
(Monoid (t a), InsertLeft t a) =>
(t a, b) -> a -> (t a, b)
f (t a, b)
v
where v :: (t a, b)
v = (t a
forall a. Monoid a => a
mempty,b
0)
f :: (t a, b) -> a -> (t a, b)
f (t a
zs,b
k) a
x
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),b
k)
{-# SPECIALIZE dropG :: Int -> String -> String #-}
splitAtG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> (t a, t a)
splitAtG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtG b
n = (\(t a
x,t a
y,b
_) -> (t a
x,t a
y)) ((t a, t a, b) -> (t a, t a))
-> (t a -> (t a, t a, b)) -> t a -> (t a, t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t a, t a, b) -> a -> (t a, t a, b))
-> (t a, t a, b) -> t a -> (t a, t a, b)
forall b a. (b -> a -> b) -> b -> t a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (t a, t a, b) -> a -> (t a, t a, b)
forall {t :: * -> *} {a} {t :: * -> *}.
(Monoid (t a), Monoid (t a), InsertLeft t a, InsertLeft t a) =>
(t a, t a, b) -> a -> (t a, t a, b)
f (t a, t a, b)
v
where v :: (t a, t a, b)
v = (t a
forall a. Monoid a => a
mempty,t a
forall a. Monoid a => a
mempty,b
0)
f :: (t a, t a, b) -> a -> (t a, t a, b)
f (t a
zs,t a
ts,b
k) a
x
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (t a
zs t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,t a
ts t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
forall a. Monoid a => a
mempty),b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
{-# SPECIALIZE splitAtG :: Int -> String -> (String,String) #-}
{-# SPECIALIZE splitAtG :: (Eq a) => Int -> [a] -> ([a],[a]) #-}
splitAtEndG :: (Integral b, InsertLeft t a, Monoid (t a)) => b -> t a -> (t a, t a)
splitAtEndG :: forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG b
n = (\(t a
x,t a
y,b
_) -> (t a
y,t a
x)) ((t a, t a, b) -> (t a, t a))
-> (t a -> (t a, t a, b)) -> t a -> (t a, t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (t a, t a, b) -> (t a, t a, b))
-> (t a, t a, b) -> t a -> (t a, t a, b)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (t a, t a, b) -> (t a, t a, b)
forall {t :: * -> *} {a} {t :: * -> *}.
(Monoid (t a), InsertLeft t a, InsertLeft t a) =>
a -> (t a, t a, b) -> (t a, t a, b)
f (t a, t a, b)
v
where v :: (t a, t a, b)
v = (t a
forall a. Monoid a => a
mempty,t a
forall a. Monoid a => a
mempty,b
0)
f :: a -> (t a, t a, b) -> (t a, t a, b)
f a
x (t a
zs,t a
ts,b
k)
| b
k b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
n = (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs,t a
forall a. Monoid a => a
mempty,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
| Bool
otherwise = (t a
zs,a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
ts,b
k b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
{-# SPECIALIZE splitAtEndG :: Int -> String -> (String,String) #-}
{-# SPECIALIZE splitAtEndG :: (Eq a) => Int -> [a] -> ([a],[a]) #-}
safeHeadG :: (F.Foldable t) => t a -> Maybe a
safeHeadG :: forall (t :: * -> *) a. Foldable t => t a -> Maybe a
safeHeadG = (a -> Bool) -> t a -> Maybe a
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
F.find (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
True)
{-# SPECIALIZE safeHeadG :: [a] -> Maybe a #-}
safeTailG :: (InsertLeft t a, Monoid (t a)) => t a -> t a
safeTailG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
t a -> t a
safeTailG = Integer -> t a -> t a
forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
dropG Integer
1
{-# SPECIALIZE safeTailG :: (Eq a) => [a] -> [a] #-}
safeInitG :: (InsertLeft t a, Monoid (t a)) => t a -> t a
safeInitG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
t a -> t a
safeInitG = Integer -> t a -> t a
forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
dropFromEndG Integer
1
{-# SPECIALIZE safeInitG :: (Eq a) => [a] -> [a] #-}
safeLastG :: (InsertLeft t a, Monoid (t a)) => t a -> Maybe a
safeLastG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
t a -> Maybe a
safeLastG = (a -> Bool) -> t a -> Maybe a
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
F.find (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
True) (t a -> Maybe a) -> (t a -> t a) -> t a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> t a -> t a
forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> t a
takeFromEndG Integer
1
{-# SPECIALIZE safeLastG :: (Eq a) => [a] -> Maybe a #-}
mapG :: (InsertLeft t b, Monoid (t b)) => (a -> b) -> t a -> t b
mapG :: forall (t :: * -> *) b a.
(InsertLeft t b, Monoid (t b)) =>
(a -> b) -> t a -> t b
mapG a -> b
f = (a -> t b -> t b) -> t b -> t a -> t b
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr (\a
x t b
ys -> a -> b
f a
x b -> t b -> t b
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t b
ys) t b
forall a. Monoid a => a
mempty
{-# INLINE mapG #-}
{-# SPECIALIZE mapG :: (Eq b) => (a -> b) -> [a] -> [b] #-}
filterG :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> t a
filterG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> t a
filterG a -> Bool
p = (a -> t a -> t a) -> t a -> t a -> t a
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr (\a
x t a
ys -> if a -> Bool
p a
x then a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
ys else t a
ys) t a
forall a. Monoid a => a
mempty
{-# INLINE filterG #-}
{-# SPECIALIZE filterG :: (Eq a) => (a -> Bool) -> [a] -> [a] #-}
partitionG :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)
partitionG :: forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a)) =>
(a -> Bool) -> t a -> (t a, t a)
partitionG a -> Bool
p = (a -> (t a, t a) -> (t a, t a)) -> (t a, t a) -> t a -> (t a, t a)
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr (\a
x (t a
ys,t a
zs) -> if a -> Bool
p a
x then (a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
ys,t a
zs) else (t a
ys,a
x a -> t a -> t a
forall (t :: * -> *) a. InsertLeft t a => a -> t a -> t a
%@ t a
zs)) (t a
forall a. Monoid a => a
mempty,t a
forall a. Monoid a => a
mempty)
{-# INLINE partitionG #-}
{-# SPECIALIZE partitionG :: (Eq a) => (a -> Bool) -> [a] -> ([a],[a]) #-}