on master help sign in
Foldable/Traversable instance for Item
commit99200aef5d99a9ffe1c9c98fc9d31db4a78d2e26 authorJasper Van der Jeugt <m@jaspervdj.be> dateWed 26 Dec 2012, 15:00 (14 years ago) parentf61c5a7
3 files changed+28−10
src/Hakyll/Core/Item.hs+24−6 src/Hakyll/Core/UnixFilter.hs+3−3 tests/Hakyll/Core/UnixFilter/Tests.hs+1−1
src/Hakyll/Core/Item.hs+24−6
@@ -5,17 +5,21 @@
55module Hakyll.Core.Item
66 ( Item (..)
77 , itemSetBody
8− , itemM
8+ , withItemBody
99 ) where
1010
1111
1212--------------------------------------------------------------------------------
13−import Control.Applicative ((<$>), (<*>))
14−import Data.Binary (Binary (..))
15−import Data.Typeable (Typeable)
13+import Control.Applicative (Applicative, (<$>), (<*>))
14+import Data.Binary (Binary (..))
15+import Data.Foldable (Foldable (..))
16+import Data.Traversable (Traversable (..))
17+import Data.Typeable (Typeable)
18+import Prelude hiding (foldr)
1619
1720
1821--------------------------------------------------------------------------------
22+import Hakyll.Core.Compiler.Internal
1923import Hakyll.Core.Identifier
2024
2125
@@ -31,6 +35,16 @@ instance Functor Item where
3135 fmap f (Item i x) = Item i (f x)
3236
3337
38+--------------------------------------------------------------------------------
39+instance Foldable Item where
40+ foldr f z (Item _ x) = f x z
41+
42+
43+--------------------------------------------------------------------------------
44+instance Traversable Item where
45+ traverse f (Item i x) = Item i <$> f x
46+
47+
3448--------------------------------------------------------------------------------
3549instance Binary a => Binary (Item a) where
3650 put (Item i x) = put i >> put x
@@ -43,5 +57,9 @@ itemSetBody x (Item i _) = Item i x
4357
4458
4559--------------------------------------------------------------------------------
46−itemM :: Monad m => (a -> m b) -> Item a -> m (Item b)
47−itemM f (Item i b) = f b >>= \b' -> return (Item i b')
60+-- | Perform a compiler action on the item body. This is the same as 'traverse',
61+-- but looks less intimidating.
62+--
63+-- > withItemBody = traverse
64+withItemBody :: (a -> Compiler b) -> Item a -> Compiler (Item b)
65+withItemBody = traverse
src/Hakyll/Core/UnixFilter.hs+3−3
@@ -26,7 +26,7 @@ import Hakyll.Core.Compiler
2626-- as a compiler.
2727--
2828-- > rev :: Compiler String
29−-- > rev = getResourceString >>= itemM (unixFilter "rev" [])
29+-- > rev = getResourceString >>= withItemBody (unixFilter "rev" [])
3030--
3131-- A more realistic example: one can use this to call, for example, the sass
3232-- compiler on CSS files. More information about sass can be found here:
@@ -38,7 +38,7 @@ import Hakyll.Core.Compiler
3838-- > match "style.scss" $ do
3939-- > route $ setExtension "css"
4040-- > compile $ getResourceString >>=
41−-- > itemM (unixFilter "sass" ["-s", "--scss"]) >>=
41+-- > withItemBody (unixFilter "sass" ["-s", "--scss"]) >>=
4242-- > compressCssCompiler
4343unixFilter :: String -- ^ Program name
4444 -> [String] -- ^ Program args
@@ -59,7 +59,7 @@ unixFilter = unixFilterWith writer reader
5959--
6060-- > match "music.wav" $ do
6161-- > route $ setExtension "ogg"
62−-- > compile $ getResourceLBS >>= unixFilter "oggenc" ["-"]
62+-- > compile $ getResourceLBS >>= withItemBody (unixFilter "oggenc" ["-"])
6363unixFilterLBS :: String -- ^ Program name
6464 -> [String] -- ^ Program args
6565 -> ByteString -- ^ Program input
tests/Hakyll/Core/UnixFilter/Tests.hs+1−1
@@ -33,5 +33,5 @@ unixFilterRev = withTestStore $ \store -> do
3333 expected <- testCompilerDone store provider "russian.md" getResourceString
3434 H.assert $ rev (itemBody expected) == lines (itemBody output)
3535 where
36− compiler = getResourceString >>= itemM (unixFilter "rev" [])
36+ compiler = getResourceString >>= withItemBody (unixFilter "rev" [])
3737 rev = map reverse . lines