on master help sign in
Define all require functions in arrows
9b3e524Jasper Van der Jeugt
src/Hakyll/Core/Compiler.hs+30−19
@@ -8,8 +8,10 @@ module Hakyll.Core.Compiler
8 , getRoute
8 , getRoute
9 , getResourceString
9 , getResourceString
10 , fromDependency
10 , fromDependency
11+ , require_
11 , require
12 , require
12 , requireA
13 , requireA
14+ , requireAll_
13 , requireAll
15 , requireAll
14 , requireAllA
16 , requireAllA
15 , cached
17 , cached
@@ -17,11 +19,11 @@ module Hakyll.Core.Compiler
17 ) where
19 ) where
18
20
19import Prelude hiding ((.), id)
21import Prelude hiding ((.), id)
20−import Control.Arrow ((>>>))
22+import Control.Arrow ((>>>), (&&&), arr)
21import Control.Applicative ((<$>))
23import Control.Applicative ((<$>))
22import Control.Monad.Reader (ask)
24import Control.Monad.Reader (ask)
23import Control.Monad.Trans (liftIO)
25import Control.Monad.Trans (liftIO)
24−import Control.Category (Category, (.))
26+import Control.Category (Category, (.), id)
25import Data.Maybe (fromMaybe)
27import Data.Maybe (fromMaybe)
26
28
27import Data.Binary (Binary)
29import Data.Binary (Binary)
@@ -85,7 +87,7 @@ getResourceString = getIdentifier >>> getResourceString'
85-- | Auxiliary: get a dependency
87-- | Auxiliary: get a dependency
86--
88--
87getDependency :: (Binary a, Writable a, Typeable a)
89getDependency :: (Binary a, Writable a, Typeable a)
88− => Identifier -> CompilerM a
90+ => Identifier -> CompilerM a
89getDependency identifier = CompilerM $ do
91getDependency identifier = CompilerM $ do
90 store <- compilerStore <$> ask
92 store <- compilerStore <$> ask
91 fmap (fromMaybe error') $ liftIO $
93 fmap (fromMaybe error') $ liftIO $
@@ -96,6 +98,15 @@ getDependency identifier = CompilerM $ do
96 ++ " not found in the cache, the cache might be corrupted or"
98 ++ " not found in the cache, the cache might be corrupted or"
97 ++ " the item you are referring to might not exist"
99 ++ " the item you are referring to might not exist"
98
100
101+
102+-- | Variant of 'require' which drops the current value
103+--
104+require_ :: (Binary a, Typeable a, Writable a)
105+ => Identifier
106+ -> Compiler b a
107+require_ identifier =
108+ fromDependency identifier >>> fromJob (const $ getDependency identifier)
109+
99-- | Require another target. Using this function ensures automatic handling of
110-- | Require another target. Using this function ensures automatic handling of
100-- dependencies
111-- dependencies
101--
112--
@@ -103,12 +114,7 @@ require :: (Binary a, Typeable a, Writable a)
103 => Identifier
114 => Identifier
104 -> (b -> a -> c)
115 -> (b -> a -> c)
105 -> Compiler b c
116 -> Compiler b c
106−require identifier f =
117+require identifier = requireA identifier . arr . uncurry
107− fromDependency identifier >>> fromJob require'
108− where
109− require' x = do
110− y <- getDependency identifier
111− return $ f x y
112
118
113-- | Arrow-based variant of 'require'
119-- | Arrow-based variant of 'require'
114--
120--
@@ -116,7 +122,19 @@ requireA :: (Binary a, Typeable a, Writable a)
116 => Identifier
122 => Identifier
117 -> Compiler (b, a) c
123 -> Compiler (b, a) c
118 -> Compiler b c
124 -> Compiler b c
119−requireA identifier = (require identifier (,) >>>)
125+requireA identifier = (id &&& require_ identifier >>>)
126+
127+-- | Variant of 'requireAll' which drops the current value
128+--
129+requireAll_ :: (Binary a, Typeable a, Writable a)
130+ => Pattern
131+ -> Compiler b [a]
132+requireAll_ pattern = fromDependencies getDeps >>> fromJob requireAll_'
133+ where
134+ getDeps = matches pattern . resourceList
135+ requireAll_' = const $ CompilerM $ do
136+ deps <- getDeps . compilerResourceProvider <$> ask
137+ mapM (unCompilerM . getDependency) deps
120
138
121-- | Require a number of targets. Using this function ensures automatic handling
139-- | Require a number of targets. Using this function ensures automatic handling
122-- of dependencies
140-- of dependencies
@@ -125,14 +143,7 @@ requireAll :: (Binary a, Typeable a, Writable a)
125 => Pattern
143 => Pattern
126 -> (b -> [a] -> c)
144 -> (b -> [a] -> c)
127 -> Compiler b c
145 -> Compiler b c
128−requireAll pattern f =
146+requireAll pattern = requireAllA pattern . arr . uncurry
129− fromDependencies getDeps >>> fromJob requireAll'
130− where
131− getDeps = matches pattern . resourceList
132− requireAll' x = CompilerM $ do
133− deps <- getDeps . compilerResourceProvider <$> ask
134− items <- mapM (unCompilerM . getDependency) deps
135− return $ f x items
136
147
137-- | Arrow-based variant of 'requireAll'
148-- | Arrow-based variant of 'requireAll'
138--
149--
@@ -140,7 +151,7 @@ requireAllA :: (Binary a, Typeable a, Writable a)
140 => Pattern
151 => Pattern
141 -> Compiler (b, [a]) c
152 -> Compiler (b, [a]) c
142 -> Compiler b c
153 -> Compiler b c
143−requireAllA pattern = (requireAll pattern (,) >>>)
154+requireAllA pattern = (id &&& requireAll_ pattern >>>)
144
155
145cached :: (Binary a, Typeable a, Writable a)
156cached :: (Binary a, Typeable a, Writable a)
146 => String
157 => String