Introduce unzipping and use it

This commit is contained in:
Niels G. W. Serup 2024-09-25 23:06:53 +02:00
parent 5e73358df7
commit 056c8d278b
No known key found for this signature in database
GPG Key ID: 38EEEBCE67324F19
4 changed files with 32 additions and 0 deletions

View File

@ -9,8 +9,12 @@ module DependencyGenerator
, mapDepGenM , mapDepGenM
, mapDepGenM_ , mapDepGenM_
, filterDepGenM , filterDepGenM
, unzipFstDepGenM
, unzipSndDepGenM
, unzipDepGenM
, joinPaths , joinPaths
, fileComponents
, isImageFilename , isImageFilename
, convertedImageFilename , convertedImageFilename
, applyTemplate , applyTemplate
@ -98,6 +102,27 @@ filterDepGenM f input = do
conds <- mapDepGenM f input conds <- mapDepGenM f input
genDependency (makeDependency (TupleToken (input, conds)) FilterComp) genDependency (makeDependency (TupleToken (input, conds)) FilterComp)
unzipFstDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [a])
unzipFstDepGenM t = do
t' <- toToken t
case t' of
ZipToken (a, _) -> pure a
Token _ -> genDependency (makeDependency t' UnzipFst)
unzipSndDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [b])
unzipSndDepGenM t = do
t' <- toToken t
case t' of
ZipToken (_, b) -> pure b
Token _ -> genDependency (makeDependency t' UnzipSnd)
unzipDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [a], Token [b])
unzipDepGenM t = do
t' <- toToken t
a <- unzipFstDepGenM t'
b <- unzipSndDepGenM t'
pure (a, b)
class TokenableTo t s | s -> t where class TokenableTo t s | s -> t where
toToken :: s -> DepGenM' t toToken :: s -> DepGenM' t
@ -113,6 +138,9 @@ joinPaths (a, b) = do
b' <- toToken b b' <- toToken b
runFunction JoinPaths $ TupleToken (a', b') runFunction JoinPaths $ TupleToken (a', b')
fileComponents :: TokenableTo FilePath a => a -> DepGenM' (String, String)
fileComponents a = runFunction FileComponents =<< toToken a
isImageFilename :: TokenableTo FilePath a => a -> DepGenM' Bool isImageFilename :: TokenableTo FilePath a => a -> DepGenM' Bool
isImageFilename a = runFunction IsImageFilename =<< toToken a isImageFilename a = runFunction IsImageFilename =<< toToken a

View File

@ -6,6 +6,7 @@ import DependencyGenerator
handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> DepGenM () handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> DepGenM ()
handleRecipeDir outputDir template dir = do handleRecipeDir outputDir template dir = do
dirContents <- listDirectory dir dirContents <- listDirectory dir
(bases, exts) <- unzipDepGenM (mapDepGenM fileComponents dirContents)
imageFilenames <- filterDepGenM isImageFilename dirContents imageFilenames <- filterDepGenM isImageFilename dirContents
convertedImageFilenames <- mapDepGenM convertedImageFilename imageFilenames convertedImageFilenames <- mapDepGenM convertedImageFilename imageFilenames
mapDepGenM_ mapDepGenM_

View File

@ -23,6 +23,8 @@ data Action = Function Function
| FilterComp | FilterComp
| GetListElem | GetListElem
| SetListElem | SetListElem
| UnzipFst
| UnzipSnd
| MapComp [Dependency] | MapComp [Dependency]
deriving (Show, Lift) deriving (Show, Lift)

View File

@ -5,6 +5,7 @@ module Types.Function
import Language.Haskell.TH.Syntax (Lift) import Language.Haskell.TH.Syntax (Lift)
data Function = JoinPaths data Function = JoinPaths
| FileComponents
| IsImageFilename | IsImageFilename
| ConvertedImageFilename | ConvertedImageFilename
| ApplyTemplate | ApplyTemplate