{-# LANGUAGE PatternGuards #-}
module XMonad.Util.Stack (
Zipper
, emptyZ
, singletonZ
, fromIndex
, toIndex
, fromTags
, toTags
, zipperFocusedAtFirstOf
, insertUpZ
, insertDownZ
, swapUpZ
, swapDownZ
, swapMasterZ
, focusUpZ
, focusDownZ
, focusMasterZ
, findS
, findZ
, getFocusZ
, getIZ
, sortZ
, sortByZ
, mapZ
, mapZ_
, mapZM
, mapZM_
, onFocusedZ
, onFocusedZM
, onIndexZ
, onIndexZM
, filterZ
, filterZ_
, deleteFocusedZ
, deleteIndexZ
, foldrZ
, foldlZ
, foldrZ_
, foldlZ_
, elemZ
, getI
, tagBy
, fromE
, mapE
, mapE_
, mapEM
, mapEM_
, reverseS
, reverseZ
) where
import qualified XMonad.StackSet as W
import XMonad.Prelude (guard, sortBy, (!?), (<|>))
type Zipper a = Maybe (W.Stack a)
emptyZ :: Zipper a
emptyZ :: forall a. Zipper a
emptyZ = forall a. Maybe a
Nothing
singletonZ :: a -> Zipper a
singletonZ :: forall a. a -> Zipper a
singletonZ a
a = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
a [] []
fromIndex :: [a] -> Int -> Zipper a
fromIndex :: forall a. [a] -> Int -> Zipper a
fromIndex [a]
as Int
i = forall a. [Either a a] -> Zipper a
fromTags forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a b. (a -> b) -> a -> b
($) (forall a. Int -> a -> [a]
replicate Int
i forall a b. a -> Either a b
Left forall a. [a] -> [a] -> [a]
++ [forall a b. b -> Either a b
Right] forall a. [a] -> [a] -> [a]
++ forall a. a -> [a]
repeat forall a b. a -> Either a b
Left) [a]
as
toIndex :: Zipper a -> ([a], Maybe Int)
toIndex :: forall a. Zipper a -> ([a], Maybe Int)
toIndex Maybe (Stack a)
Nothing = ([], forall a. Maybe a
Nothing)
toIndex (Just Stack a
s) = (forall a. Stack a -> [a]
W.integrate Stack a
s, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
W.up Stack a
s)
fromTags :: [Either a a] -> Zipper a
fromTags :: forall a. [Either a a] -> Zipper a
fromTags = forall {a}. ([a], Maybe a, [a]) -> Maybe (Stack a)
finalize 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}.
Either a a -> ([a], Maybe a, [a]) -> ([a], Maybe a, [a])
step ([], forall a. Maybe a
Nothing, [])
where step :: Either a a -> ([a], Maybe a, [a]) -> ([a], Maybe a, [a])
step (Right a
a) ([a]
u, Just a
f, [a]
d) = ([], forall a. a -> Maybe a
Just a
a, [a]
uforall a. [a] -> [a] -> [a]
++a
fforall a. a -> [a] -> [a]
:[a]
d)
step (Right a
a) ([a]
u, Maybe a
Nothing, [a]
d) = ([a]
u, forall a. a -> Maybe a
Just a
a, [a]
d)
step (Left a
a) ([a]
u, Just a
f, [a]
d) = (a
aforall a. a -> [a] -> [a]
:[a]
u, forall a. a -> Maybe a
Just a
f, [a]
d)
step (Left a
a) ([a]
u, Maybe a
Nothing, [a]
d) = ([a]
u, forall a. Maybe a
Nothing, a
aforall a. a -> [a] -> [a]
:[a]
d)
finalize :: ([a], Maybe a, [a]) -> Maybe (Stack a)
finalize ([a]
u, Just a
f, [a]
d) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
f (forall a. [a] -> [a]
reverse [a]
u) [a]
d
finalize ([a]
u, Maybe a
Nothing, a
a:[a]
d) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
a (forall a. [a] -> [a]
reverse [a]
u) [a]
d
finalize ([a]
_, Maybe a
Nothing, []) = forall a. Maybe a
Nothing
toTags :: Zipper a -> [Either a a]
toTags :: forall a. Zipper a -> [Either a a]
toTags Maybe (Stack a)
Nothing = []
toTags (Just Stack a
s) = forall a b. (a -> b) -> [a] -> [b]
map forall a b. a -> Either a b
Left (forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Stack a -> [a]
W.up forall a b. (a -> b) -> a -> b
$ Stack a
s) forall a. [a] -> [a] -> [a]
++ [forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Stack a -> a
W.focus forall a b. (a -> b) -> a -> b
$ Stack a
s]
forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. a -> Either a b
Left (forall a. Stack a -> [a]
W.down Stack a
s)
zipperFocusedAtFirstOf :: Eq q => [q] -> [q] -> Zipper q
zipperFocusedAtFirstOf :: forall q. Eq q => [q] -> [q] -> Zipper q
zipperFocusedAtFirstOf [] [q]
xs = forall a. [a] -> Maybe (Stack a)
W.differentiate [q]
xs
zipperFocusedAtFirstOf (q
z : [q]
zs) [q]
xs
| q
z forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [q]
xs = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
W.Stack { focus :: q
W.focus = q
z
, up :: [q]
W.up = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= q
z) [q]
xs
, down :: [q]
W.down = forall a. Int -> [a] -> [a]
drop Int
1 forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
/= q
z) [q]
xs
}
| Bool
otherwise = forall q. Eq q => [q] -> [q] -> Zipper q
zipperFocusedAtFirstOf [q]
zs [q]
xs
insertUpZ :: a -> Zipper a -> Zipper a
insertUpZ :: forall a. a -> Zipper a -> Zipper a
insertUpZ a
a Maybe (Stack a)
Nothing = forall a. [a] -> Maybe (Stack a)
W.differentiate [a
a]
insertUpZ a
a (Just Stack a
s) = forall a. a -> Maybe a
Just Stack a
s { focus :: a
W.focus = a
a , down :: [a]
W.down = forall a. Stack a -> a
W.focus Stack a
s forall a. a -> [a] -> [a]
: forall a. Stack a -> [a]
W.down Stack a
s }
insertDownZ :: a -> Zipper a -> Zipper a
insertDownZ :: forall a. a -> Zipper a -> Zipper a
insertDownZ a
a Maybe (Stack a)
Nothing = forall a. [a] -> Maybe (Stack a)
W.differentiate [a
a]
insertDownZ a
a (Just Stack a
s) = forall a. a -> Maybe a
Just Stack a
s { focus :: a
W.focus = a
a, up :: [a]
W.up = forall a. Stack a -> a
W.focus Stack a
s forall a. a -> [a] -> [a]
: forall a. Stack a -> [a]
W.up Stack a
s }
swapUpZ :: Zipper a -> Zipper a
swapUpZ :: forall a. Zipper a -> Zipper a
swapUpZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
swapUpZ (Just Stack a
s) | a
u:[a]
up <- forall a. Stack a -> [a]
W.up Stack a
s = forall a. a -> Maybe a
Just Stack a
s { up :: [a]
W.up = [a]
up, down :: [a]
W.down = a
uforall a. a -> [a] -> [a]
:forall a. Stack a -> [a]
W.down Stack a
s}
swapUpZ (Just Stack a
s) = forall a. a -> Maybe a
Just Stack a
s { up :: [a]
W.up = forall a. [a] -> [a]
reverse (forall a. Stack a -> [a]
W.down Stack a
s), down :: [a]
W.down = [] }
swapDownZ :: Zipper a -> Zipper a
swapDownZ :: forall a. Zipper a -> Zipper a
swapDownZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
swapDownZ (Just Stack a
s) | a
d:[a]
down <- forall a. Stack a -> [a]
W.down Stack a
s = forall a. a -> Maybe a
Just Stack a
s { down :: [a]
W.down = [a]
down, up :: [a]
W.up = a
dforall a. a -> [a] -> [a]
:forall a. Stack a -> [a]
W.up Stack a
s }
swapDownZ (Just Stack a
s) = forall a. a -> Maybe a
Just Stack a
s { up :: [a]
W.up = [], down :: [a]
W.down = forall a. [a] -> [a]
reverse (forall a. Stack a -> [a]
W.up Stack a
s) }
swapMasterZ :: Zipper a -> Zipper a
swapMasterZ :: forall a. Zipper a -> Zipper a
swapMasterZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
swapMasterZ (Just (W.Stack a
f [a]
up [a]
down)) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
f [] (forall a. [a] -> [a]
reverse [a]
up forall a. [a] -> [a] -> [a]
++ [a]
down)
focusUpZ :: Zipper a -> Zipper a
focusUpZ :: forall a. Zipper a -> Zipper a
focusUpZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
focusUpZ (Just Stack a
s) | a
u:[a]
up <- forall a. Stack a -> [a]
W.up Stack a
s = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
u [a]
up (forall a. Stack a -> a
W.focus Stack a
sforall a. a -> [a] -> [a]
:forall a. Stack a -> [a]
W.down Stack a
s)
focusUpZ (Just Stack a
s) | forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
W.down Stack a
s = forall a. a -> Maybe a
Just Stack a
s
focusUpZ (Just (W.Stack a
f [a]
_ [a]
down)) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack (forall a. [a] -> a
last [a]
down) (forall a. Int -> [a] -> [a]
drop Int
1 (forall a. [a] -> [a]
reverse [a]
down) forall a. [a] -> [a] -> [a]
++ [a
f]) []
focusDownZ :: Zipper a -> Zipper a
focusDownZ :: forall a. Zipper a -> Zipper a
focusDownZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
focusDownZ (Just Stack a
s) | a
d:[a]
down <- forall a. Stack a -> [a]
W.down Stack a
s = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
d (forall a. Stack a -> a
W.focus Stack a
sforall a. a -> [a] -> [a]
:forall a. Stack a -> [a]
W.up Stack a
s) [a]
down
focusDownZ (Just Stack a
s) | forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
W.up Stack a
s = forall a. a -> Maybe a
Just Stack a
s
focusDownZ (Just (W.Stack a
f [a]
up [a]
_)) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack (forall a. [a] -> a
last [a]
up) [] (forall a. Int -> [a] -> [a]
drop Int
1 (forall a. [a] -> [a]
reverse [a]
up) forall a. [a] -> [a] -> [a]
++ [a
f])
focusMasterZ :: Zipper a -> Zipper a
focusMasterZ :: forall a. Zipper a -> Zipper a
focusMasterZ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
focusMasterZ (Just (W.Stack a
f [a]
up [a]
down)) | Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
up
= forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack (forall a. [a] -> a
last [a]
up) [] (forall a. Int -> [a] -> [a]
drop Int
1 (forall a. [a] -> [a]
reverse [a]
up) forall a. [a] -> [a] -> [a]
++ [a
f] forall a. [a] -> [a] -> [a]
++ [a]
down)
focusMasterZ (Just Stack a
s) = forall a. a -> Maybe a
Just Stack a
s
findS :: (a -> Bool) -> W.Stack a -> Maybe (W.Stack a)
findS :: forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
findS a -> Bool
p Stack a
st = Stack a
st forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Stack a -> a
W.focus) Stack a
st forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Stack a -> Maybe (Stack a)
findUp Stack a
st forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Stack a -> Maybe (Stack a)
findDown Stack a
st
where findDown :: Stack a -> Maybe (Stack a)
findDown = forall a. Zipper a -> Zipper a
reverseZ forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack a -> Maybe (Stack a)
findUp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Stack a -> Stack a
reverseS
findUp :: Stack a -> Maybe (Stack a)
findUp Stack a
s | a
u:[a]
ups <- forall a. Stack a -> [a]
W.up Stack a
s = (if a -> Bool
p a
u then forall a. a -> Maybe a
Just else Stack a -> Maybe (Stack a)
findUp)
forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
u [a]
ups (forall a. Stack a -> a
W.focus Stack a
s forall a. a -> [a] -> [a]
: forall a. Stack a -> [a]
W.down Stack a
s)
| Bool
otherwise = forall a. Maybe a
Nothing
findZ :: (a -> Bool) -> Zipper a -> Zipper a
findZ :: forall a. (a -> Bool) -> Zipper a -> Zipper a
findZ a -> Bool
_ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
findZ a -> Bool
p (Just Stack a
st) = forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
findS a -> Bool
p Stack a
st
getFocusZ :: Zipper a -> Maybe a
getFocusZ :: forall a. Zipper a -> Maybe a
getFocusZ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Stack a -> a
W.focus
getIZ :: Int -> Zipper a -> Maybe a
getIZ :: forall a. Int -> Zipper a -> Maybe a
getIZ Int
i = forall a. Int -> [a] -> Maybe a
getI Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe (Stack a) -> [a]
W.integrate'
sortZ :: Ord a => Zipper a -> Zipper a
sortZ :: forall a. Ord a => Zipper a -> Zipper a
sortZ = forall a. (a -> a -> Ordering) -> Zipper a -> Zipper a
sortByZ forall a. Ord a => a -> a -> Ordering
compare
sortByZ :: (a -> a -> Ordering) -> Zipper a -> Zipper a
sortByZ :: forall a. (a -> a -> Ordering) -> Zipper a -> Zipper a
sortByZ a -> a -> Ordering
f = forall a. [Either a a] -> Zipper a
fromTags forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall {t} {t} {t}. (t -> t -> t) -> Either t t -> Either t t -> t
adapt a -> a -> Ordering
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Zipper a -> [Either a a]
toTags
where adapt :: (t -> t -> t) -> Either t t -> Either t t -> t
adapt t -> t -> t
g Either t t
e1 Either t t
e2 = t -> t -> t
g (forall a. Either a a -> a
fromE Either t t
e1) (forall a. Either a a -> a
fromE Either t t
e2)
mapZ :: (Bool -> a -> b) -> Zipper a -> Zipper b
mapZ :: forall a b. (Bool -> a -> b) -> Zipper a -> Zipper b
mapZ Bool -> a -> b
f = forall a. [Either a a] -> Zipper a
fromTags forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (Bool -> a -> b) -> Either a a -> Either b b
mapE Bool -> a -> b
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Zipper a -> [Either a a]
toTags
mapZ_ :: (a -> b) -> Zipper a -> Zipper b
mapZ_ :: forall a b. (a -> b) -> Zipper a -> Zipper b
mapZ_ = forall a b. (Bool -> a -> b) -> Zipper a -> Zipper b
mapZ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
mapZM :: Monad m => (Bool -> a -> m b) -> Zipper a -> m (Zipper b)
mapZM :: forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Zipper a -> m (Zipper b)
mapZM Bool -> a -> m b
f Zipper a
as = forall a. [Either a a] -> Zipper a
fromTags forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM Bool -> a -> m b
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Zipper a -> [Either a a]
toTags) Zipper a
as
mapZM_ :: Monad m => (a -> m b) -> Zipper a -> m (Zipper b)
mapZM_ :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Zipper a -> m (Zipper b)
mapZM_ = forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Zipper a -> m (Zipper b)
mapZM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
onFocusedZ :: (a -> a) -> Zipper a -> Zipper a
onFocusedZ :: forall a. (a -> a) -> Zipper a -> Zipper a
onFocusedZ a -> a
f = forall a b. (Bool -> a -> b) -> Zipper a -> Zipper b
mapZ forall a b. (a -> b) -> a -> b
$ \Bool
b a
a -> if Bool
b then a -> a
f a
a else a
a
onFocusedZM :: Monad m => (a -> m a) -> Zipper a -> m (Zipper a)
onFocusedZM :: forall (m :: * -> *) a.
Monad m =>
(a -> m a) -> Zipper a -> m (Zipper a)
onFocusedZM a -> m a
f = forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Zipper a -> m (Zipper b)
mapZM forall a b. (a -> b) -> a -> b
$ \Bool
b a
a -> if Bool
b then a -> m a
f a
a else forall (m :: * -> *) a. Monad m => a -> m a
return a
a
onIndexZ :: Int -> (a -> a) -> Zipper a -> Zipper a
onIndexZ :: forall a. Int -> (a -> a) -> Zipper a -> Zipper a
onIndexZ Int
i a -> a
_ Zipper a
as | Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = Zipper a
as
onIndexZ Int
i a -> a
f Zipper a
as = case forall a. Int -> [a] -> ([a], [a])
splitAt Int
i forall a b. (a -> b) -> a -> b
$ forall a. Zipper a -> [Either a a]
toTags Zipper a
as of
([Either a a]
before, []) -> forall a. [Either a a] -> Zipper a
fromTags [Either a a]
before
([Either a a]
before, Either a a
a:[Either a a]
after) -> forall a. [Either a a] -> Zipper a
fromTags forall a b. (a -> b) -> a -> b
$ [Either a a]
before forall a. [a] -> [a] -> [a]
++ forall a b. (Bool -> a -> b) -> Either a a -> Either b b
mapE (forall a b. a -> b -> a
const a -> a
f) Either a a
a forall a. a -> [a] -> [a]
: [Either a a]
after
onIndexZM :: Monad m => Int -> (a -> m a) -> Zipper a -> m (Zipper a)
onIndexZM :: forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> Zipper a -> m (Zipper a)
onIndexZM Int
i a -> m a
f Zipper a
as = case forall a. Int -> [a] -> ([a], [a])
splitAt Int
i forall a b. (a -> b) -> a -> b
$ forall a. Zipper a -> [Either a a]
toTags Zipper a
as of
([Either a a]
before, []) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [Either a a] -> Zipper a
fromTags [Either a a]
before
([Either a a]
before, Either a a
a:[Either a a]
after) -> do Either a a
a' <- forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM (forall a b. a -> b -> a
const a -> m a
f) Either a a
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [Either a a] -> Zipper a
fromTags forall a b. (a -> b) -> a -> b
$ [Either a a]
before forall a. [a] -> [a] -> [a]
++ Either a a
a' forall a. a -> [a] -> [a]
: [Either a a]
after
filterZ :: (Bool -> a -> Bool) -> Zipper a -> Zipper a
filterZ :: forall a. (Bool -> a -> Bool) -> Zipper a -> Zipper a
filterZ Bool -> a -> Bool
_ Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
filterZ Bool -> a -> Bool
p (Just Stack a
s) = case ( Bool -> a -> Bool
p Bool
True (forall a. Stack a -> a
W.focus Stack a
s)
, forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> a -> Bool
p Bool
False) (forall a. Stack a -> [a]
W.up Stack a
s)
, forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> a -> Bool
p Bool
False) (forall a. Stack a -> [a]
W.down Stack a
s) ) of
(Bool
True, [a]
up', [a]
down') -> forall a. a -> Maybe a
Just Stack a
s { up :: [a]
W.up = [a]
up', down :: [a]
W.down = [a]
down' }
(Bool
False, [], []) -> forall a. Maybe a
Nothing
(Bool
False, a
f:[a]
up', []) -> forall a. a -> Maybe a
Just Stack a
s { focus :: a
W.focus = a
f, up :: [a]
W.up = [a]
up', down :: [a]
W.down = [] }
(Bool
False, [a]
up', a
f:[a]
down') -> forall a. a -> Maybe a
Just Stack a
s { focus :: a
W.focus = a
f
, up :: [a]
W.up = [a]
up'
, down :: [a]
W.down = [a]
down' }
filterZ_ :: (a -> Bool) -> Zipper a -> Zipper a
filterZ_ :: forall a. (a -> Bool) -> Zipper a -> Zipper a
filterZ_ = forall a. (Bool -> a -> Bool) -> Zipper a -> Zipper a
filterZ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
deleteFocusedZ :: Zipper a -> Zipper a
deleteFocusedZ :: forall a. Zipper a -> Zipper a
deleteFocusedZ = forall a. (Bool -> a -> Bool) -> Zipper a -> Zipper a
filterZ (\Bool
b a
_ -> Bool -> Bool
not Bool
b)
deleteIndexZ :: Int -> Zipper a -> Zipper a
deleteIndexZ :: forall a. Int -> Zipper a -> Zipper a
deleteIndexZ Int
i Zipper a
z = let numbered :: Zipper (Int, a)
numbered = (forall a. [Either a a] -> Zipper a
fromTags forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall {a} {b}. a -> Either b b -> Either (a, b) (a, b)
number [Int
0..] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Zipper a -> [Either a a]
toTags) Zipper a
z
number :: a -> Either b b -> Either (a, b) (a, b)
number a
j = forall a b. (Bool -> a -> b) -> Either a a -> Either b b
mapE (\Bool
_ b
a -> (a
j,b
a))
in forall a b. (a -> b) -> Zipper a -> Zipper b
mapZ_ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> Zipper a -> Zipper a
filterZ_ ((forall a. Eq a => a -> a -> Bool
/=Int
i) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) Zipper (Int, a)
numbered
foldrZ :: (Bool -> a -> b -> b) -> b -> Zipper a -> b
foldrZ :: forall a b. (Bool -> a -> b -> b) -> b -> Zipper a -> b
foldrZ Bool -> a -> b -> b
_ b
b Maybe (Stack a)
Nothing = b
b
foldrZ Bool -> a -> b -> b
f b
b (Just Stack a
s) = let b1 :: b
b1 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> a -> b -> b
f Bool
False) b
b (forall a. Stack a -> [a]
W.down Stack a
s)
b2 :: b
b2 = Bool -> a -> b -> b
f Bool
True (forall a. Stack a -> a
W.focus Stack a
s) b
b1
b3 :: b
b3 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
$ Bool -> a -> b -> b
f Bool
False) b
b2 (forall a. Stack a -> [a]
W.up Stack a
s)
in b
b3
foldlZ :: (Bool -> b -> a -> b) -> b -> Zipper a -> b
foldlZ :: forall b a. (Bool -> b -> a -> b) -> b -> Zipper a -> b
foldlZ Bool -> b -> a -> b
_ b
b Maybe (Stack a)
Nothing = b
b
foldlZ Bool -> b -> a -> b
f b
b (Just Stack a
s) = let b1 :: b
b1 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
$ Bool -> b -> a -> b
f Bool
False) b
b (forall a. Stack a -> [a]
W.up Stack a
s)
b2 :: b
b2 = Bool -> b -> a -> b
f Bool
True b
b1 (forall a. Stack a -> a
W.focus Stack a
s)
b3 :: b
b3 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (Bool -> b -> a -> b
f Bool
False) b
b2 (forall a. Stack a -> [a]
W.down Stack a
s)
in b
b3
foldrZ_ :: (a -> b -> b) -> b -> Zipper a -> b
foldrZ_ :: forall a b. (a -> b -> b) -> b -> Zipper a -> b
foldrZ_ = forall a b. (Bool -> a -> b -> b) -> b -> Zipper a -> b
foldrZ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
foldlZ_ :: (b -> a -> b) -> b -> Zipper a -> b
foldlZ_ :: forall b a. (b -> a -> b) -> b -> Zipper a -> b
foldlZ_ = forall b a. (Bool -> b -> a -> b) -> b -> Zipper a -> b
foldlZ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
elemZ :: Eq a => a -> Zipper a -> Bool
elemZ :: forall a. Eq a => a -> Zipper a -> Bool
elemZ a
a = forall b a. (b -> a -> b) -> b -> Zipper a -> b
foldlZ_ Bool -> a -> Bool
step Bool
False
where step :: Bool -> a -> Bool
step Bool
True a
_ = Bool
True
step Bool
False a
a' = a
a' forall a. Eq a => a -> a -> Bool
== a
a
getI :: Int -> [a] -> Maybe a
getI :: forall a. Int -> [a] -> Maybe a
getI Int
i [a]
xs = [a]
xs forall a. [a] -> Int -> Maybe a
!? Int
i
{-# DEPRECATED getI "Use XMonad.Prelude.(!?) instead." #-}
mapE :: (Bool -> a -> b) -> Either a a -> Either b b
mapE :: forall a b. (Bool -> a -> b) -> Either a a -> Either b b
mapE Bool -> a -> b
f (Left a
a) = forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Bool -> a -> b
f Bool
False a
a
mapE Bool -> a -> b
f (Right a
a) = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Bool -> a -> b
f Bool
True a
a
mapE_ :: (a -> b) -> Either a a -> Either b b
mapE_ :: forall a b. (a -> b) -> Either a a -> Either b b
mapE_ = forall a b. (Bool -> a -> b) -> Either a a -> Either b b
mapE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
mapEM :: Monad m => (Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM :: forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM Bool -> a -> m b
f (Left a
a) = forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> a -> m b
f Bool
False a
a
mapEM Bool -> a -> m b
f (Right a
a) = forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> a -> m b
f Bool
True a
a
mapEM_ :: Monad m => (a -> m b) -> Either a a -> m (Either b b)
mapEM_ :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Either a a -> m (Either b b)
mapEM_ = forall (m :: * -> *) a b.
Monad m =>
(Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
fromE :: Either a a -> a
fromE :: forall a. Either a a -> a
fromE (Right a
a) = a
a
fromE (Left a
a) = a
a
tagBy :: (a -> Bool) -> a -> Either a a
tagBy :: forall a. (a -> Bool) -> a -> Either a a
tagBy a -> Bool
p a
a = if a -> Bool
p a
a then forall a b. b -> Either a b
Right a
a else forall a b. a -> Either a b
Left a
a
reverseS :: W.Stack a -> W.Stack a
reverseS :: forall a. Stack a -> Stack a
reverseS (W.Stack a
foc [a]
ups [a]
downs) = forall a. a -> [a] -> [a] -> Stack a
W.Stack a
foc [a]
downs [a]
ups
reverseZ :: Zipper a -> Zipper a
reverseZ :: forall a. Zipper a -> Zipper a
reverseZ = (forall a. Stack a -> Stack a
reverseS forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)