module Data.List.HT.Private where
import Data.List as List (find, transpose, unfoldr, isPrefixOf,
findIndices, foldl', mapAccumL, )
import Data.Maybe as Maybe (fromMaybe, catMaybes, isJust, mapMaybe, )
import Data.Maybe.HT (toMaybe, )
import Control.Monad.HT ((<=<), )
import Control.Monad (guard, msum, mplus, liftM2, )
import Control.Applicative ((<$>), (<*>), )
import Data.Tuple.HT (mapPair, mapFst, mapSnd, forcePair, swap, )
import qualified Control.Functor.HT as Func
import qualified Data.List.Key.Private as Key
import qualified Data.List.Match.Private as Match
import qualified Data.List.Reverse.StrictElement as Rev
import Prelude hiding (unzip, break, span, )
inits :: [a] -> [[a]]
inits :: forall a. [a] -> [[a]]
inits = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) []
initsLazy :: [a] -> [[a]]
initsLazy :: forall a. [a] -> [[a]]
initsLazy [a]
xt =
[] forall a. a -> [a] -> [a]
:
case [a]
xt of
[] -> []
a
x:[a]
xs -> forall a b. (a -> b) -> [a] -> [b]
map (a
xforall a. a -> [a] -> [a]
:) (forall a. [a] -> [[a]]
initsLazy [a]
xs)
inits98 :: [a] -> [[a]]
inits98 :: forall a. [a] -> [[a]]
inits98 [] = [[]]
inits98 (a
x:[a]
xs) = [[]] forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (a
xforall a. a -> [a] -> [a]
:) (forall a. [a] -> [[a]]
inits98 [a]
xs)
inits98' :: [a] -> [[a]]
inits98' :: forall a. [a] -> [[a]]
inits98' =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [[a]]
prefixes -> [] forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map (a
xforall a. a -> [a] -> [a]
:) [[a]]
prefixes) [[]]
tails :: [a] -> [[a]]
tails :: forall a. [a] -> [[a]]
tails [a]
xt =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall a b. (a -> b) -> a -> b
$
case [a]
xt of
[] -> ([],[])
a
_:[a]
xs -> ([a]
xt, forall a. [a] -> [[a]]
tails [a]
xs)
tails' :: [a] -> [[a]]
tails' :: forall a. [a] -> [[a]]
tails' = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfter forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
iterate forall a. [a] -> [a]
tail
tails98 :: [a] -> [[a]]
tails98 :: forall a. [a] -> [[a]]
tails98 [] = [[]]
tails98 xxs :: [a]
xxs@(a
_:[a]
xs) = [a]
xxs forall a. a -> [a] -> [a]
: forall a. [a] -> [[a]]
tails98 [a]
xs
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy :: forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy = forall a. (a -> a -> Bool) -> [a] -> [[a]]
Key.groupBy
group :: (Eq a) => [a] -> [[a]]
group :: forall a. Eq a => [a] -> [[a]]
group = forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy forall a. Eq a => a -> a -> Bool
(==)
unzip :: [(a,b)] -> ([a],[b])
unzip :: forall a b. [(a, b)] -> ([a], [b])
unzip =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (a
x,b
y) ~([a]
xs,[b]
ys) -> (a
xforall a. a -> [a] -> [a]
:[a]
xs,b
yforall a. a -> [a] -> [a]
:[b]
ys)) ([],[])
partition :: (a -> Bool) -> [a] -> ([a], [a])
partition :: forall a. (a -> Bool) -> [a] -> ([a], [a])
partition a -> Bool
p =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\a
x ~([a]
y,[a]
z) ->
if a -> Bool
p a
x
then (a
x forall a. a -> [a] -> [a]
: [a]
y, [a]
z)
else ([a]
y, a
x forall a. a -> [a] -> [a]
: [a]
z))
([],[])
span, break :: (a -> Bool) -> [a] -> ([a],[a])
span :: forall a. (a -> Bool) -> [a] -> ([a], [a])
span a -> Bool
p =
let recourse :: [a] -> ([a], [a])
recourse [a]
xt =
forall a b. (a, b) -> (a, b)
forcePair forall a b. (a -> b) -> a -> b
$
forall a. a -> Maybe a -> a
fromMaybe ([],[a]
xt) forall a b. (a -> b) -> a -> b
$
do (a
x,[a]
xs) <- forall a. [a] -> Maybe (a, [a])
viewL [a]
xt
forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ a -> Bool
p a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ [a] -> ([a], [a])
recourse [a]
xs
in [a] -> ([a], [a])
recourse
break :: forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)
chop :: (a -> Bool) -> [a] -> [[a]]
chop :: forall a. (a -> Bool) -> [a] -> [[a]]
chop a -> Bool
p =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ a
x ~([a]
y,[[a]]
ys) -> if a -> Bool
p a
x then ([],[a]
yforall a. a -> [a] -> [a]
:[[a]]
ys) else ((a
xforall a. a -> [a] -> [a]
:[a]
y),[[a]]
ys) ) ([],[])
chop' :: (a -> Bool) -> [a] -> [[a]]
chop' :: forall a. (a -> Bool) -> [a] -> [[a]]
chop' a -> Bool
p =
let recourse :: [a] -> [[a]]
recourse =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL [] (forall a b. a -> b -> a
const [a] -> [[a]]
recourse)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p
in [a] -> [[a]]
recourse
chopAtRun :: (a -> Bool) -> [a] -> [[a]]
chopAtRun :: forall a. (a -> Bool) -> [a] -> [[a]]
chopAtRun a -> Bool
p =
let recourse :: [a] -> [[a]]
recourse [] = [[]]
recourse [a]
y =
let ([a]
z,[a]
zs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p (forall a. (a -> Bool) -> [a] -> [a]
dropWhile a -> Bool
p [a]
y)
in [a]
z forall a. a -> [a] -> [a]
: [a] -> [[a]]
recourse [a]
zs
in [a] -> [[a]]
recourse
breakAfter :: (a -> Bool) -> [a] -> ([a], [a])
breakAfter :: forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfter = forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterRec
breakAfterRec :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterRec :: forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterRec a -> Bool
p =
let recourse :: [a] -> ([a], [a])
recourse [] = ([],[])
recourse (a
x:[a]
xs) =
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$
if a -> Bool
p a
x
then ([],[a]
xs)
else [a] -> ([a], [a])
recourse [a]
xs
in forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ([a], [a])
recourse
breakAfterFoldr :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterFoldr :: forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterFoldr a -> Bool
p =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\a
x ([a], [a])
yzs -> forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ if a -> Bool
p a
x then ([], forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. [a] -> [a] -> [a]
(++) ([a], [a])
yzs) else ([a], [a])
yzs)
([],[])
breakAfterBreak :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterBreak :: forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterBreak a -> Bool
p [a]
xs =
case forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p [a]
xs of
([a]
ys, []) -> ([a]
ys, [])
([a]
ys, a
z:[a]
zs) -> ([a]
ysforall a. [a] -> [a] -> [a]
++[a
z], [a]
zs)
breakAfterTakeUntil :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterTakeUntil :: forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterTakeUntil a -> Bool
p [a]
xs =
forall a b. (a, b) -> (a, b)
forcePair forall a b. (a -> b) -> a -> b
$
(\[(a, [a])]
ys -> (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(a, [a])]
ys, forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe ([a], a)
viewR [(a, [a])]
ys)) forall a b. (a -> b) -> a -> b
$
forall a. (a -> Bool) -> [a] -> [a]
takeUntil (a -> 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
$ forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [[a]]
tails [a]
xs
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil :: forall a. (a -> Bool) -> [a] -> [a]
takeUntil a -> Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [a]
ys -> a
x forall a. a -> [a] -> [a]
: if a -> Bool
p a
x then [] else [a]
ys) []
segmentAfter :: (a -> Bool) -> [a] -> [[a]]
segmentAfter :: forall a. (a -> Bool) -> [a] -> [[a]]
segmentAfter a -> Bool
p =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\a
x ~([a]
y,[[a]]
ys) ->
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$
if a -> Bool
p a
x then ([],[a]
yforall a. a -> [a] -> [a]
:[[a]]
ys) else ([a]
y,[[a]]
ys))
([],[])
segmentAfter' :: (a -> Bool) -> [a] -> [[a]]
segmentAfter' :: forall a. (a -> Bool) -> [a] -> [[a]]
segmentAfter' a -> Bool
p =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ a
x ~yt :: [[a]]
yt@([a]
y:[[a]]
ys) -> if a -> Bool
p a
x then [a
x]forall a. a -> [a] -> [a]
:[[a]]
yt else (a
xforall a. a -> [a] -> [a]
:[a]
y)forall a. a -> [a] -> [a]
:[[a]]
ys) [[]]
segmentBefore :: (a -> Bool) -> [a] -> [[a]]
segmentBefore :: forall a. (a -> Bool) -> [a] -> [[a]]
segmentBefore a -> Bool
p =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\ a
x ~([a]
y,[[a]]
ys) ->
let xs :: [a]
xs = a
xforall a. a -> [a] -> [a]
:[a]
y
in if a -> Bool
p a
x then ([],[a]
xsforall a. a -> [a] -> [a]
:[[a]]
ys) else ([a]
xs,[[a]]
ys))
([],[])
segmentBefore' :: (a -> Bool) -> [a] -> [[a]]
segmentBefore' :: forall a. (a -> Bool) -> [a] -> [[a]]
segmentBefore' a -> Bool
p =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(\[[a]]
xst ->
forall a. a -> Maybe a -> a
fromMaybe ([],[[a]]
xst) forall a b. (a -> b) -> a -> b
$ do
((a
x:[a]
xs):[[a]]
xss) <- forall a. a -> Maybe a
Just [[a]]
xst
forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ a -> Bool
p a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (a
xforall a. a -> [a] -> [a]
:[a]
xs, [[a]]
xss)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\a
_ a
x -> Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ a -> Bool
p a
x)
segmentBefore'' :: (a -> Bool) -> [a] -> [[a]]
segmentBefore'' :: forall a. (a -> Bool) -> [a] -> [[a]]
segmentBefore'' a -> Bool
p =
(\[[a]]
xst ->
case [[a]]
xst of
~([a]
xs:[[a]]
xss) ->
forall a. [a] -> [a]
tail [a]
xs forall a. a -> [a] -> [a]
: [[a]]
xss) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\a
_ a
x -> Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ a -> Bool
p a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(forall a. HasCallStack => [Char] -> a
error [Char]
"segmentBefore: dummy element" forall a. a -> [a] -> [a]
:)
segmentBeforeJust ::
(a -> Maybe b) ->
[a] -> ([a], [(b, [a])])
segmentBeforeJust :: forall a b. (a -> Maybe b) -> [a] -> ([a], [(b, [a])])
segmentBeforeJust a -> Maybe b
f =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\ a
x ~([a]
y,[(b, [a])]
ys) ->
case a -> Maybe b
f a
x of
Just b
b -> ([],(b
b,[a]
y)forall a. a -> [a] -> [a]
:[(b, [a])]
ys)
Maybe b
Nothing -> (a
xforall a. a -> [a] -> [a]
:[a]
y,[(b, [a])]
ys))
([],[])
segmentAfterJust ::
(a -> Maybe b) ->
[a] -> ([([a], b)], [a])
segmentAfterJust :: forall a b. (a -> Maybe b) -> [a] -> ([([a], b)], [a])
segmentAfterJust a -> Maybe b
f =
forall a b. (a, b) -> (b, a)
swap forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL (\[a]
as0 (b
b,[a]
as1) -> ([a]
as1, ([a]
as0,b
b)))) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b. (a -> Maybe b) -> [a] -> ([a], [(b, [a])])
segmentBeforeJust a -> Maybe b
f
segmentBeforeRight ::
[Either a b] -> ([a], [(b, [a])])
segmentBeforeRight :: forall a b. [Either a b] -> ([a], [(b, [a])])
segmentBeforeRight =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\ Either a b
x ~([a]
y,[(b, [a])]
ys) ->
case Either a b
x of
Right b
b -> ([],(b
b,[a]
y)forall a. a -> [a] -> [a]
:[(b, [a])]
ys)
Left a
a -> (a
aforall a. a -> [a] -> [a]
:[a]
y,[(b, [a])]
ys))
([],[])
segmentAfterRight ::
[Either a b] -> ([([a], b)], [a])
segmentAfterRight :: forall a b. [Either a b] -> ([([a], b)], [a])
segmentAfterRight =
forall a b. (a, b) -> (b, a)
swap forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL (\[a]
as0 (b
b,[a]
as1) -> ([a]
as1, ([a]
as0,b
b)))) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b. [Either a b] -> ([a], [(b, [a])])
segmentBeforeRight
removeEach :: [a] -> [(a, [a])]
removeEach :: forall a. [a] -> [(a, [a])]
removeEach =
forall a b. (a -> b) -> [a] -> [b]
map (\([a]
ys, a
pivot, [a]
zs) -> (a
pivot,[a]
ysforall a. [a] -> [a] -> [a]
++[a]
zs)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [([a], a, [a])]
splitEverywhere
splitEverywhere :: [a] -> [([a], a, [a])]
splitEverywhere :: forall a. [a] -> [([a], a, [a])]
splitEverywhere [a]
xs =
forall a b. (a -> b) -> [a] -> [b]
map
(\([a]
y, [a]
zs0) ->
case [a]
zs0 of
a
z:[a]
zs -> ([a]
y,a
z,[a]
zs)
[] -> forall a. HasCallStack => [Char] -> a
error [Char]
"splitEverywhere: empty list")
(forall a. [a] -> [a]
init (forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. [a] -> [[a]]
inits [a]
xs) (forall a. [a] -> [[a]]
tails [a]
xs)))
{-# DEPRECATED splitLast "use viewR instead" #-}
splitLast :: [a] -> ([a], a)
splitLast :: forall a. [a] -> ([a], a)
splitLast [] = forall a. HasCallStack => [Char] -> a
error [Char]
"splitLast: empty list"
splitLast [a
x] = ([], a
x)
splitLast (a
x:[a]
xs) =
let ([a]
xs', a
lastx) = forall a. [a] -> ([a], a)
splitLast [a]
xs in (a
xforall a. a -> [a] -> [a]
:[a]
xs', a
lastx)
{-# INLINE viewL #-}
viewL :: [a] -> Maybe (a, [a])
viewL :: forall a. [a] -> Maybe (a, [a])
viewL (a
x:[a]
xs) = forall a. a -> Maybe a
Just (a
x,[a]
xs)
viewL [] = forall a. Maybe a
Nothing
viewR :: [a] -> Maybe ([a], a)
viewR :: forall a. [a] -> Maybe ([a], a)
viewR =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([],a
x) (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:))) forall a. Maybe a
Nothing
{-# INLINE switchL #-}
switchL :: b -> (a -> [a] -> b) -> [a] -> b
switchL :: forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL b
n a -> [a] -> b
_ [] = b
n
switchL b
_ a -> [a] -> b
j (a
x:[a]
xs) = a -> [a] -> b
j a
x [a]
xs
switchL' :: b -> (a -> [a] -> b) -> [a] -> b
switchL' :: forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL' b
n a -> [a] -> b
j =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
n (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> [a] -> b
j) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe (a, [a])
viewL
{-# INLINE switchR #-}
switchR :: b -> ([a] -> a -> b) -> [a] -> b
switchR :: forall b a. b -> ([a] -> a -> b) -> [a] -> b
switchR b
n [a] -> a -> b
j =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
n (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [a] -> a -> b
j) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe ([a], a)
viewR
takeRev :: Int -> [a] -> [a]
takeRev :: forall a. Int -> [a] -> [a]
takeRev Int
n [a]
xs = forall b a. [b] -> [a] -> [a]
Match.drop (forall a. Int -> [a] -> [a]
drop Int
n [a]
xs) [a]
xs
dropRev :: Int -> [a] -> [a]
dropRev :: forall a. Int -> [a] -> [a]
dropRev Int
n [a]
xs = forall b a. [b] -> [a] -> [a]
Match.take (forall a. Int -> [a] -> [a]
drop Int
n [a]
xs) [a]
xs
splitAtRev :: Int -> [a] -> ([a], [a])
splitAtRev :: forall a. Int -> [a] -> ([a], [a])
splitAtRev Int
n [a]
xs = forall b a. [b] -> [a] -> ([a], [a])
Match.splitAt (forall a. Int -> [a] -> [a]
drop Int
n [a]
xs) [a]
xs
maybePrefixOf :: Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf :: forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf (a
x:[a]
xs) (a
y:[a]
ys) = forall (f :: * -> *). Alternative f => Bool -> f ()
guard (a
xforall a. Eq a => a -> a -> Bool
==a
y) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf [a]
xs [a]
ys
maybePrefixOf [] [a]
ys = forall a. a -> Maybe a
Just [a]
ys
maybePrefixOf [a]
_ [] = forall a. Maybe a
Nothing
maybeSuffixOf :: Eq a => [a] -> [a] -> Maybe [a]
maybeSuffixOf :: forall a. Eq a => [a] -> [a] -> Maybe [a]
maybeSuffixOf [a]
xs [a]
ys =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf (forall a. [a] -> [a]
reverse [a]
xs) (forall a. [a] -> [a]
reverse [a]
ys)
partitionMaybe :: (a -> Maybe b) -> [a] -> ([b], [a])
partitionMaybe :: forall a b. (a -> Maybe b) -> [a] -> ([b], [a])
partitionMaybe a -> Maybe b
f =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\a
x -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (a
xforall a. a -> [a] -> [a]
:)) (\b
y -> forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (b
yforall a. a -> [a] -> [a]
:)) (a -> Maybe b
f a
x))
([],[])
takeWhileJust :: [Maybe a] -> [a]
takeWhileJust :: forall a. [Maybe a] -> [a]
takeWhileJust =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Maybe a
x [a]
acc -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (forall a. a -> [a] -> [a]
:[a]
acc) Maybe a
x) []
dropWhileNothing :: (a -> Maybe b) -> [a] -> Maybe (b, [a])
dropWhileNothing :: forall a b. (a -> Maybe b) -> [a] -> Maybe (b, [a])
dropWhileNothing a -> Maybe b
f =
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall (f :: * -> *) a c b.
Functor f =>
(a -> f c) -> (a, b) -> f (c, b)
Func.mapFst a -> Maybe b
f forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a. [a] -> Maybe (a, [a])
viewL) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [[a]]
tails
dropWhileNothingRec :: (a -> Maybe b) -> [a] -> Maybe (b, [a])
dropWhileNothingRec :: forall a b. (a -> Maybe b) -> [a] -> Maybe (b, [a])
dropWhileNothingRec a -> Maybe b
f =
let go :: [a] -> Maybe (b, [a])
go [] = forall a. Maybe a
Nothing
go (a
a:[a]
xs) = (forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) [a]
xs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe b
f a
a) forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` [a] -> Maybe (b, [a])
go [a]
xs
in [a] -> Maybe (b, [a])
go
breakJust :: (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJust :: forall a b. (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJust a -> Maybe b
f =
let go :: [a] -> ([a], Maybe (b, [a]))
go [] = ([], forall a. Maybe a
Nothing)
go (a
a:[a]
xs) =
case a -> Maybe b
f a
a of
Maybe b
Nothing -> forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
aforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ [a] -> ([a], Maybe (b, [a]))
go [a]
xs
Just b
b -> ([], forall a. a -> Maybe a
Just (b
b, [a]
xs))
in [a] -> ([a], Maybe (b, [a]))
go
breakJustRemoveEach :: (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJustRemoveEach :: forall a b. (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJustRemoveEach a -> Maybe b
f [a]
xs =
forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL ([a]
xs, forall a. Maybe a
Nothing) forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\([a]
ys,a
a,[a]
zs) -> (\b
b -> ([a]
ys, forall a. a -> Maybe a
Just (b
b,[a]
zs))) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe b
f a
a) forall a b. (a -> b) -> a -> b
$
forall a. [a] -> [([a], a, [a])]
splitEverywhere [a]
xs
breakJustPartial :: (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJustPartial :: forall a b. (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
breakJustPartial a -> Maybe b
f [a]
xs =
let ([a]
ys,[a]
zs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f) [a]
xs
in ([a]
ys,
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error [Char]
"breakJust: unexpected Nothing") forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
forall a. [a] -> Maybe (a, [a])
viewL [a]
zs)
spanJust :: (a -> Maybe b) -> [a] -> ([b], [a])
spanJust :: forall a b. (a -> Maybe b) -> [a] -> ([b], [a])
spanJust a -> Maybe b
f =
let go :: [a] -> ([b], [a])
go [] = ([], [])
go xt :: [a]
xt@(a
a:[a]
xs) =
case a -> Maybe b
f a
a of
Just b
b -> forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (b
bforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ [a] -> ([b], [a])
go [a]
xs
Maybe b
Nothing -> ([], [a]
xt)
in [a] -> ([b], [a])
go
unzipEithers :: [Either a b] -> ([a], [b])
unzipEithers :: forall a b. [Either a b] -> ([a], [b])
unzipEithers =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\a
x -> forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xforall a. a -> [a] -> [a]
:)) (\b
y -> forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (b
yforall a. a -> [a] -> [a]
:))) ([],[])
sieve, sieve', sieve'', sieve''' :: Int -> [a] -> [a]
sieve :: forall a. Int -> [a] -> [a]
sieve Int
k =
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr (\[a]
xs -> forall a. Bool -> a -> Maybe a
toMaybe (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs)) (forall a. [a] -> a
head [a]
xs, forall a. Int -> [a] -> [a]
drop Int
k [a]
xs))
sieve' :: forall a. Int -> [a] -> [a]
sieve' Int
k = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [[a]]
sliceVertical Int
k
sieve'' :: forall a. Int -> [a] -> [a]
sieve'' Int
k [a]
x = forall a b. (a -> b) -> [a] -> [b]
map ([a]
xforall a. [a] -> Int -> a
!!) [Int
0,Int
k..(forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xforall a. Num a => a -> a -> a
-Int
1)]
sieve''' :: forall a. Int -> [a] -> [a]
sieve''' Int
k = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
iterate (forall a. Int -> [a] -> [a]
drop Int
k)
sliceHorizontal, sliceHorizontal', sliceHorizontal'', sliceHorizontal''' ::
Int -> [a] -> [[a]]
sliceHorizontal :: forall a. Int -> [a] -> [[a]]
sliceHorizontal Int
n =
forall a b. (a -> b) -> [a] -> [b]
map (forall a. Int -> [a] -> [a]
sieve Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
iterate (forall a. Int -> [a] -> [a]
drop Int
1)
sliceHorizontal' :: forall a. Int -> [a] -> [[a]]
sliceHorizontal' Int
n =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [[a]]
ys -> let y :: [a]
y = forall a. [a] -> a
last [[a]]
ys in forall b a. [b] -> [a] -> [a]
Match.take [[a]]
ys ((a
xforall a. a -> [a] -> [a]
:[a]
y)forall a. a -> [a] -> [a]
:[[a]]
ys)) (forall a. Int -> a -> [a]
replicate Int
n [])
sliceHorizontal'' :: forall a. Int -> [a] -> [[a]]
sliceHorizontal'' Int
n =
forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x ~([a]
y:[[a]]
ys) -> [[a]]
ys forall a. [a] -> [a] -> [a]
++ [a
xforall a. a -> [a] -> [a]
:[a]
y]) (forall a. Int -> a -> [a]
replicate Int
n [])
sliceHorizontal''' :: forall a. Int -> [a] -> [[a]]
sliceHorizontal''' Int
n =
forall a. Int -> [a] -> [a]
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [[a]] -> [[a]]
transpose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
iterate (forall a. Int -> [a] -> [a]
drop Int
n)
sliceVertical, sliceVertical' :: Int -> [a] -> [[a]]
sliceVertical :: forall a. Int -> [a] -> [[a]]
sliceVertical Int
n =
forall a b. (a -> b) -> [a] -> [b]
map (forall a. Int -> [a] -> [a]
take Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
iterate (forall a. Int -> [a] -> [a]
drop Int
n)
sliceVertical' :: forall a. Int -> [a] -> [[a]]
sliceVertical' Int
n =
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr (\[a]
x -> forall a. Bool -> a -> Maybe a
toMaybe (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
x)) (forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
x))
search :: (Eq a) => [a] -> [a] -> [Int]
search :: forall a. Eq a => [a] -> [a] -> [Int]
search [a]
sub [a]
str = forall a. (a -> Bool) -> [a] -> [Int]
findIndices (forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
sub) (forall a. [a] -> [[a]]
tails [a]
str)
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace :: forall a. Eq a => [a] -> [a] -> [a] -> [a]
replace [a]
src [a]
dst =
let recourse :: [a] -> [a]
recourse [] = []
recourse str :: [a]
str@(a
s:[a]
ss) =
forall a. a -> Maybe a -> a
fromMaybe
(a
s forall a. a -> [a] -> [a]
: [a] -> [a]
recourse [a]
ss)
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([a]
dstforall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
recourse) forall a b. (a -> b) -> a -> b
$
forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf [a]
src [a]
str)
in [a] -> [a]
recourse
markSublists :: (Eq a) => [a] -> [a] -> [Maybe [a]]
markSublists :: forall a. Eq a => [a] -> [a] -> [Maybe [a]]
markSublists [a]
sub [a]
ys =
let ~([a]
hd', [Maybe [a]]
rest') =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
c ~([a]
hd, [Maybe [a]]
rest) ->
let xs :: [a]
xs = a
cforall a. a -> [a] -> [a]
:[a]
hd
in case forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf [a]
sub [a]
xs of
Just [a]
suffix -> ([], forall a. Maybe a
Nothing forall a. a -> [a] -> [a]
: forall a. a -> Maybe a
Just [a]
suffix forall a. a -> [a] -> [a]
: [Maybe [a]]
rest)
Maybe [a]
Nothing -> ([a]
xs, [Maybe [a]]
rest)) ([],[]) [a]
ys
in forall a. a -> Maybe a
Just [a]
hd' forall a. a -> [a] -> [a]
: [Maybe [a]]
rest'
replace' :: (Eq a) => [a] -> [a] -> [a] -> [a]
replace' :: forall a. Eq a => [a] -> [a] -> [a] -> [a]
replace' [a]
src [a]
dst [a]
xs =
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall a. a -> Maybe a -> a
fromMaybe [a]
dst) (forall a. Eq a => [a] -> [a] -> [Maybe [a]]
markSublists [a]
src [a]
xs)
replace'' :: (Eq a) => [a] -> [a] -> [a] -> [a]
replace'' :: forall a. Eq a => [a] -> [a] -> [a] -> [a]
replace'' [a]
src [a]
dst =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [a]
xs -> let y :: [a]
y=a
xforall a. a -> [a] -> [a]
:[a]
xs
in if forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
src [a]
y
then [a]
dst forall a. [a] -> [a] -> [a]
++ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
src) [a]
y
else [a]
y) []
multiReplace :: Eq a => [([a], [a])] -> [a] -> [a]
multiReplace :: forall a. Eq a => [([a], [a])] -> [a] -> [a]
multiReplace [([a], [a])]
dict =
let recourse :: [a] -> [a]
recourse [] = []
recourse str :: [a]
str@(a
s:[a]
ss) =
forall a. a -> Maybe a -> a
fromMaybe
(a
s forall a. a -> [a] -> [a]
: [a] -> [a]
recourse [a]
ss)
(forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> [a] -> [b]
map (\([a]
src,[a]
dst) ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([a]
dstforall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
recourse) forall a b. (a -> b) -> a -> b
$
forall a. Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf [a]
src [a]
str) [([a], [a])]
dict)
in [a] -> [a]
recourse
multiReplace' :: Eq a => [([a], [a])] -> [a] -> [a]
multiReplace' :: forall a. Eq a => [([a], [a])] -> [a] -> [a]
multiReplace' [([a], [a])]
dict =
let recourse :: [a] -> [a]
recourse [] = []
recourse str :: [a]
str@(a
s:[a]
ss) =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(a
s forall a. a -> [a] -> [a]
: [a] -> [a]
recourse [a]
ss)
(\([a]
src, [a]
dst) -> [a]
dst forall a. [a] -> [a] -> [a]
++ [a] -> [a]
recourse (forall b a. [b] -> [a] -> [a]
Match.drop [a]
src [a]
str))
(forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
str forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [([a], [a])]
dict)
in [a] -> [a]
recourse
shear :: [[a]] -> [[a]]
shear :: forall a. [[a]] -> [[a]]
shear =
forall a b. (a -> b) -> [a] -> [b]
map forall a. [Maybe a] -> [a]
catMaybes forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. [[a]] -> [[a]]
shearTranspose forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. [[a]] -> [[Maybe a]]
transposeFill
transposeFill :: [[a]] -> [[Maybe a]]
transposeFill :: forall a. [[a]] -> [[Maybe a]]
transposeFill =
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr (\[[a]]
xs ->
forall a. Bool -> a -> Maybe a
toMaybe (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[a]]
xs))
(forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (forall a. (a -> Bool) -> [a] -> [a]
Rev.dropWhile forall (t :: * -> *) a. Foldable t => t a -> Bool
null) forall a b. (a -> b) -> a -> b
$ forall a. [[a]] -> ([Maybe a], [[a]])
unzipCons [[a]]
xs))
unzipCons :: [[a]] -> ([Maybe a], [[a]])
unzipCons :: forall a. [[a]] -> ([Maybe a], [[a]])
unzipCons =
forall a b. [(a, b)] -> ([a], [b])
unzip forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b. (a -> b) -> [a] -> [b]
map ((\Maybe (a, [a])
my -> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst Maybe (a, [a])
my, forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] forall a b. (a, b) -> b
snd Maybe (a, [a])
my)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe (a, [a])
viewL)
unzipConsSkew :: [[a]] -> ([Maybe a], [[a]])
unzipConsSkew :: forall a. [[a]] -> ([Maybe a], [[a]])
unzipConsSkew =
let aux :: [a] -> [[a]] -> ([Maybe a], [[a]])
aux [] [] = ([],[])
aux [a]
xs [[a]]
ys = forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd ([a]
xsforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ [[a]] -> ([Maybe a], [[a]])
prep [[a]]
ys
prep :: [[a]] -> ([Maybe a], [[a]])
prep =
forall a b. (a, b) -> (a, b)
forcePair forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL ([],[])
(\[a]
y [[a]]
ys ->
let my :: Maybe (a, [a])
my = forall a. [a] -> Maybe (a, [a])
viewL [a]
y
in forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst Maybe (a, [a])
my forall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$
[a] -> [[a]] -> ([Maybe a], [[a]])
aux (forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] forall a b. (a, b) -> b
snd Maybe (a, [a])
my) [[a]]
ys)
in forall a. [[a]] -> ([Maybe a], [[a]])
prep
shear' :: [[a]] -> [[a]]
shear' :: forall a. [[a]] -> [[a]]
shear' xs :: [[a]]
xs@([a]
_:[[a]]
_) =
let ([a]
y:[[a]]
ys,[[a]]
zs) = forall a b. [(a, b)] -> ([a], [b])
unzip (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Int -> [a] -> ([a], [a])
splitAt Int
1) [[a]]
xs)
zipConc :: [[a]] -> [[a]] -> [[a]]
zipConc ([a]
a:[[a]]
as) ([a]
b:[[a]]
bs) = ([a]
aforall a. [a] -> [a] -> [a]
++[a]
b) forall a. a -> [a] -> [a]
: [[a]] -> [[a]] -> [[a]]
zipConc [[a]]
as [[a]]
bs
zipConc [] [[a]]
bs = [[a]]
bs
zipConc [[a]]
as [] = [[a]]
as
in [a]
y forall a. a -> [a] -> [a]
: forall {a}. [[a]] -> [[a]] -> [[a]]
zipConc [[a]]
ys (forall a. [[a]] -> [[a]]
shear' (forall a. (a -> Bool) -> [a] -> [a]
Rev.dropWhile forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[a]]
zs))
shear' [] = []
shearTranspose :: [[a]] -> [[a]]
shearTranspose :: forall a. [[a]] -> [[a]]
shearTranspose =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. [a] -> [[a]] -> [[a]]
zipConsSkew []
zipConsSkew :: [a] -> [[a]] -> [[a]]
zipConsSkew :: forall a. [a] -> [[a]] -> [[a]]
zipConsSkew [a]
xt [[a]]
yss =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall a b. (a -> b) -> a -> b
$
case [a]
xt of
a
x:[a]
xs -> ([a
x], forall a. [a] -> [[a]] -> [[a]]
zipCons [a]
xs [[a]]
yss)
[] -> ([], [[a]]
yss)
zipCons :: [a] -> [[a]] -> [[a]]
zipCons :: forall a. [a] -> [[a]] -> [[a]]
zipCons (a
x:[a]
xs) [[a]]
yt =
let ([a]
y,[[a]]
ys) = forall b a. b -> (a -> [a] -> b) -> [a] -> b
switchL ([],[]) (,) [[a]]
yt
in (a
xforall a. a -> [a] -> [a]
:[a]
y) forall a. a -> [a] -> [a]
: forall a. [a] -> [[a]] -> [[a]]
zipCons [a]
xs [[a]]
ys
zipCons [] [[a]]
ys = [[a]]
ys
zipCons' :: [a] -> [[a]] -> [[a]]
zipCons' :: forall a. [a] -> [[a]] -> [[a]]
zipCons' (a
x:[a]
xs) ([a]
y:[[a]]
ys) = (a
xforall a. a -> [a] -> [a]
:[a]
y) forall a. a -> [a] -> [a]
: forall a. [a] -> [[a]] -> [[a]]
zipCons' [a]
xs [[a]]
ys
zipCons' [] [[a]]
ys = [[a]]
ys
zipCons' [a]
xs [] = forall a b. (a -> b) -> [a] -> [b]
map (forall a. a -> [a] -> [a]
:[]) [a]
xs
outerProduct :: (a -> b -> c) -> [a] -> [b] -> [[c]]
outerProduct :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [[c]]
outerProduct a -> b -> c
f [a]
xs [b]
ys = forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> [a] -> [b]
map [b]
ys forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
f) [a]
xs
takeWhileMulti :: [a -> Bool] -> [a] -> [a]
takeWhileMulti :: forall a. [a -> Bool] -> [a] -> [a]
takeWhileMulti [] [a]
_ = []
takeWhileMulti [a -> Bool]
_ [] = []
takeWhileMulti aps :: [a -> Bool]
aps@(a -> Bool
p:[a -> Bool]
ps) axs :: [a]
axs@(a
x:[a]
xs) =
if a -> Bool
p a
x
then a
x forall a. a -> [a] -> [a]
: forall a. [a -> Bool] -> [a] -> [a]
takeWhileMulti [a -> Bool]
aps [a]
xs
else forall a. [a -> Bool] -> [a] -> [a]
takeWhileMulti [a -> Bool]
ps [a]
axs
takeWhileMulti' :: [a -> Bool] -> [a] -> [a]
takeWhileMulti' :: forall a. [a -> Bool] -> [a] -> [a]
takeWhileMulti' [a -> Bool]
ps [a]
xs =
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> a
fst (forall a. [a] -> [a]
tail
(forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. (a -> Bool) -> [a] -> ([a], [a])
span forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) (forall a. HasCallStack => a
undefined,[a]
xs) [a -> Bool]
ps))
foldl'r, foldl'rStrict, foldl'rNaive ::
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a,c)] -> (b,d)
foldl'r :: forall b a c d.
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> (b, d)
foldl'r b -> a -> b
f b
b0 c -> d -> d
g d
d0 =
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (forall a b. (a -> b) -> a -> b
$ b
b0) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(a
a,c
c) ~(b -> b
k,d
d) -> (\b
b -> b -> b
k forall a b. (a -> b) -> a -> b
$! b -> a -> b
f b
b a
a, c -> d -> d
g c
c d
d)) (forall a. a -> a
id,d
d0)
foldl'rStrict :: forall b a c d.
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> (b, d)
foldl'rStrict b -> a -> b
f b
b0 c -> d -> d
g d
d0 =
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (forall a b. (a -> b) -> a -> b
$ b
b0) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(a
a,c
c) ~(b -> b
k,d
d) -> ((,) forall a b. (a -> b) -> a -> b
$! (\b
b -> b -> b
k forall a b. (a -> b) -> a -> b
$! b -> a -> b
f b
b a
a)) forall a b. (a -> b) -> a -> b
$! c -> d -> d
g c
c d
d) (forall a. a -> a
id,d
d0)
foldl'rNaive :: forall b a c d.
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> (b, d)
foldl'rNaive b -> a -> b
f b
b c -> d -> d
g d
d [(a, c)]
xs =
forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' b -> a -> b
f b
b, forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr c -> d -> d
g d
d) forall a b. (a -> b) -> a -> b
$ forall a b. [(a, b)] -> ([a], [b])
unzip [(a, c)]
xs
propFoldl'r :: (Eq b, Eq d) =>
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a,c)] -> Bool
propFoldl'r :: forall b d a c.
(Eq b, Eq d) =>
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> Bool
propFoldl'r b -> a -> b
f b
b c -> d -> d
g d
d [(a, c)]
xs =
forall b a c d.
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> (b, d)
foldl'r b -> a -> b
f b
b c -> d -> d
g d
d [(a, c)]
xs forall a. Eq a => a -> a -> Bool
== forall b a c d.
(b -> a -> b) -> b -> (c -> d -> d) -> d -> [(a, c)] -> (b, d)
foldl'rNaive b -> a -> b
f b
b c -> d -> d
g d
d [(a, c)]
xs
lengthAtLeast :: Int -> [a] -> Bool
lengthAtLeast :: forall a. Int -> [a] -> Bool
lengthAtLeast Int
n =
if Int
nforall a. Ord a => a -> a -> Bool
<=Int
0
then forall a b. a -> b -> a
const Bool
True
else Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop (Int
nforall a. Num a => a -> a -> a
-Int
1)
lengthAtMost :: Int -> [a] -> Bool
lengthAtMost :: forall a. Int -> [a] -> Bool
lengthAtMost Int
n =
if Int
nforall a. Ord a => a -> a -> Bool
<Int
0
then forall a b. a -> b -> a
const Bool
False
else forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop Int
n
lengthAtMost0 :: Int -> [a] -> Bool
lengthAtMost0 :: forall a. Int -> [a] -> Bool
lengthAtMost0 Int
n = (Int
nforall a. Ord a => a -> a -> Bool
>=) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take (Int
nforall a. Num a => a -> a -> a
+Int
1)
iterateUntilCycle :: (Eq a) => (a -> a) -> a -> [a]
iterateUntilCycle :: forall a. Eq a => (a -> a) -> a -> [a]
iterateUntilCycle a -> a
f a
a =
let as :: [a]
as = forall a. (a -> a) -> a -> [a]
iterate a -> a
f a
a
in (a
aforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Eq a => a -> a -> Bool
(/=)) forall a b. (a -> b) -> a -> b
$
forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. [a] -> [a]
tail [a]
as) (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\a
ai->[a
ai,a
ai]) [a]
as)
iterateUntilCycleP :: (Eq a) => (a -> a) -> a -> [a]
iterateUntilCycleP :: forall a. Eq a => (a -> a) -> a -> [a]
iterateUntilCycleP a -> a
f a
a =
let as :: [a]
as = forall a. (a -> a) -> a -> [a]
iterate a -> a
f a
a
in forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (\(a
a1,(a
a20,a
a21)) -> a
a1forall a. Eq a => a -> a -> Bool
/=a
a20 Bool -> Bool -> Bool
&& a
a1forall a. Eq a => a -> a -> Bool
/=a
a21) forall a b. (a -> b) -> a -> b
$
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
as (forall t. [t] -> [(t, t)]
pairs (forall a. [a] -> [a]
tail [a]
as))
pairs :: [t] -> [(t, t)]
pairs :: forall t. [t] -> [(t, t)]
pairs [] = []
pairs (t
_:[]) = forall a. HasCallStack => [Char] -> a
error [Char]
"pairs: odd number of elements"
pairs (t
x0:t
x1:[t]
xs) = (t
x0,t
x1) forall a. a -> [a] -> [a]
: forall t. [t] -> [(t, t)]
pairs [t]
xs
rotate, rotate', rotate'' :: Int -> [a] -> [a]
rotate :: forall a. Int -> [a] -> [a]
rotate Int
n [a]
x =
forall b a. [b] -> [a] -> [a]
Match.take [a]
x (forall a. Int -> [a] -> [a]
drop (forall a. Integral a => a -> a -> a
mod Int
n (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
x)) (forall a. [a] -> [a]
cycle [a]
x))
rotate' :: forall a. Int -> [a] -> [a]
rotate' Int
n [a]
x =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. [a] -> [a] -> [a]
(++))
(forall a. Int -> [a] -> ([a], [a])
splitAt (forall a. Integral a => a -> a -> a
mod Int
n (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
x)) [a]
x)
rotate'' :: forall a. Int -> [a] -> [a]
rotate'' Int
n [a]
x =
forall b a. [b] -> [a] -> [a]
Match.take [a]
x (forall a. Int -> [a] -> [a]
drop Int
n (forall a. [a] -> [a]
cycle [a]
x))
mergeBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
mergeBy :: forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
mergeBy = forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
Key.mergeBy
allEqual :: Eq a => [a] -> Bool
allEqual :: forall a. Eq a => [a] -> Bool
allEqual = forall (t :: * -> *). Foldable t => t Bool -> Bool
and forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> a -> b) -> [a] -> [b]
mapAdjacent forall a. Eq a => a -> a -> Bool
(==)
isAscending :: (Ord a) => [a] -> Bool
isAscending :: forall a. Ord a => [a] -> Bool
isAscending = forall (t :: * -> *). Foldable t => t Bool -> Bool
and forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> [Bool]
isAscendingLazy
isAscendingLazy :: (Ord a) => [a] -> [Bool]
isAscendingLazy :: forall a. Ord a => [a] -> [Bool]
isAscendingLazy = forall a b. (a -> a -> b) -> [a] -> [b]
mapAdjacent forall a. Ord a => a -> a -> Bool
(<=)
mapAdjacent :: (a -> a -> b) -> [a] -> [b]
mapAdjacent :: forall a b. (a -> a -> b) -> [a] -> [b]
mapAdjacent a -> a -> b
f [a]
xs = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> a -> b
f [a]
xs (forall a. [a] -> [a]
tail [a]
xs)
mapAdjacentPointfree :: (a -> a -> b) -> [a] -> [b]
mapAdjacentPointfree :: forall a b. (a -> a -> b) -> [a] -> [b]
mapAdjacentPointfree a -> a -> b
f = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> a -> b
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. [a] -> [a]
tail
mapAdjacent1 :: (a -> a -> b -> c) -> a -> [(a,b)] -> [c]
mapAdjacent1 :: forall a b c. (a -> a -> b -> c) -> a -> [(a, b)] -> [c]
mapAdjacent1 a -> a -> b -> c
f a
a [(a, b)]
xs =
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\a
a0 (a
a1,b
b) -> a -> a -> b -> c
f a
a0 a
a1 b
b) (a
a forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(a, b)]
xs) [(a, b)]
xs
equalWith, equalWithLiftM, equalWithRec ::
(a -> b -> Bool) -> [a] -> [b] -> Bool
equalWith :: forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
equalWith a -> b -> Bool
f [a]
as [b]
bs =
forall (t :: * -> *). Foldable t => t Bool -> Bool
and forall a b. (a -> b) -> a -> b
$
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith
(\Maybe a
ma Maybe b
mb ->
case (Maybe a
ma,Maybe b
mb) of
(Just a
a, Just b
b) -> a -> b -> Bool
f a
a b
b
(Maybe a
Nothing, Maybe b
Nothing) -> Bool
True
(Maybe a, Maybe b)
_ -> Bool
False)
(forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [a]
as forall a. [a] -> [a] -> [a]
++ [forall a. Maybe a
Nothing])
(forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [b]
bs forall a. [a] -> [a] -> [a]
++ [forall a. Maybe a
Nothing])
equalWithLiftM :: forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
equalWithLiftM a -> b -> Bool
f [a]
as [b]
bs =
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. a -> Maybe a
Just Bool
True forall a. Eq a => a -> a -> Bool
==) forall a b. (a -> b) -> a -> b
$
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith
(\Maybe a
ma Maybe b
mb ->
case (Maybe a
ma,Maybe b
mb) of
(Maybe a
Nothing, Maybe b
Nothing) -> forall a. a -> Maybe a
Just Bool
True
(Maybe a, Maybe b)
_ -> forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> b -> Bool
f Maybe a
ma Maybe b
mb)
(forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [a]
as forall a. [a] -> [a] -> [a]
++ [forall a. Maybe a
Nothing])
(forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [b]
bs forall a. [a] -> [a] -> [a]
++ [forall a. Maybe a
Nothing])
equalWithRec :: forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
equalWithRec a -> b -> Bool
f =
let go :: [a] -> [b] -> Bool
go (a
a:[a]
as) (b
b:[b]
bs) = a -> b -> Bool
f a
a b
b Bool -> Bool -> Bool
&& [a] -> [b] -> Bool
go [a]
as [b]
bs
go [] [] = Bool
True
go [a]
_ [b]
_ = Bool
False
in [a] -> [b] -> Bool
go
range :: Num a => Int -> [a]
range :: forall a. Num a => Int -> [a]
range Int
n = forall a. Int -> [a] -> [a]
take Int
n (forall a. (a -> a) -> a -> [a]
iterate (forall a. Num a => a -> a -> a
+a
1) a
0)
{-# INLINE padLeft #-}
padLeft :: a -> Int -> [a] -> [a]
padLeft :: forall a. a -> Int -> [a] -> [a]
padLeft a
c Int
n [a]
xs = forall a. Int -> a -> [a]
replicate (Int
n forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) a
c forall a. [a] -> [a] -> [a]
++ [a]
xs
{-# INLINE padRight #-}
padRight, padRight1 :: a -> Int -> [a] -> [a]
padRight :: forall a. a -> Int -> [a] -> [a]
padRight a
c Int
n [a]
xs = forall a. Int -> [a] -> [a]
take Int
n forall a b. (a -> b) -> a -> b
$ [a]
xs forall a. [a] -> [a] -> [a]
++ forall a. a -> [a]
repeat a
c
padRight1 :: forall a. a -> Int -> [a] -> [a]
padRight1 a
c Int
n [a]
xs = [a]
xs forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate (Int
n forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) a
c
iterateAssociative :: (a -> a -> a) -> a -> [a]
iterateAssociative :: forall a. (a -> a -> a) -> a -> [a]
iterateAssociative a -> a -> a
op a
a =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
pow [a]
xs -> a
pow forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\a
x -> [a
x, a -> a -> a
op a
x a
pow]) [a]
xs)
forall a. HasCallStack => a
undefined (forall a. (a -> a) -> a -> [a]
iterate (\a
x -> a -> a -> a
op a
x a
x) a
a)
iterateLeaky :: (a -> a -> a) -> a -> [a]
iterateLeaky :: forall a. (a -> a -> a) -> a -> [a]
iterateLeaky a -> a -> a
op a
x =
let merge :: [a] -> [a] -> [a]
merge (a
a:[a]
as) [a]
b = a
a forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
merge [a]
b [a]
as
merge [a]
_ [a]
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"iterateLeaky: an empty list cannot occur"
sqrs :: [a]
sqrs = forall a b. (a -> b) -> [a] -> [b]
map (\a
y -> a -> a -> a
op a
y a
y) [a]
z
z :: [a]
z = a
x forall a. a -> [a] -> [a]
: forall a. [a] -> [a] -> [a]
merge [a]
sqrs (forall a b. (a -> b) -> [a] -> [b]
map (a -> a -> a
op a
x) [a]
sqrs)
in [a]
z