Skip to content

Commit

Permalink
Add Foldable, Functor, and Traversable instances for Stack
Browse files Browse the repository at this point in the history
`Functor` is provided by DeriveFunctor.
`Foldable` uses `integrate` (`Stack`'s `toList`).
`Traversable` uses the `Reverse` Applicative to traverse the `up` list in
reverse order.
  • Loading branch information
wygulmage authored and liskin committed Apr 3, 2021
1 parent 5cdf428 commit 2c91ea1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/XMonad/StackSet.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE DeriveFunctor #-}

-----------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -52,6 +53,8 @@ module XMonad.StackSet (
) where

import Prelude hiding (filter)
import Control.Applicative.Backwards (Backwards (Backwards, forwards))
import Data.Foldable (foldr, toList)
import Data.Maybe (listToMaybe,isJust,fromMaybe)
import qualified Data.List as L (deleteBy,find,splitAt,filter,nub)
import Data.List ( (\\) )
Expand Down Expand Up @@ -175,8 +178,19 @@ data RationalRect = RationalRect !Rational !Rational !Rational !Rational
data Stack a = Stack { focus :: !a -- focused thing in this set
, up :: [a] -- clowns to the left
, down :: [a] } -- jokers to the right
deriving (Show, Read, Eq)

deriving (Show, Read, Eq, Functor)

instance Foldable Stack where
toList = integrate
foldr f z = foldr f z . toList

instance Traversable Stack where
traverse f s =
flip Stack
-- 'Backwards' applies the Applicative in reverse order.
<$> forwards (traverse (Backwards . f) (up s))
<*> f (focus s)
<*> traverse f (down s)

-- | this function indicates to catch that an error is expected
abort :: String -> a
Expand Down
1 change: 1 addition & 0 deletions xmonad.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ library
, mtl
, process
, setlocale
, transformers >= 0.3
, unix
, utf8-string >= 0.3 && < 1.1
ghc-options: -funbox-strict-fields -Wall -fno-warn-unused-do-bind
Expand Down

0 comments on commit 2c91ea1

Please sign in to comment.