<commit_before>{-# LANGUAGE TemplateHaskell #-}
module Snap.StarterTH where

------------------------------------------------------------------------------
import qualified Data.Foldable as F
import           Data.List
import           Language.Haskell.TH
import           System.Directory.Tree
------------------------------------------------------------------------------


------------------------------------------------------------------------------
-- Convenience types
type FileData = (String, String)
type DirData = FilePath


------------------------------------------------------------------------------
-- Gets all the directorys in a DirTree
getDirs :: [FilePath] -> DirTree a -> [FilePath]
getDirs prefix (Dir n c) = (intercalate "/" (reverse (n:prefix))) : concatMap (getDirs (n:prefix)) c
getDirs _ (File _ _) = []
getDirs _ (Failed _ _) = []


------------------------------------------------------------------------------
-- Reads a directory and returns a tuple of the list of all directories
-- encountered and a list of filenames and content strings.
readTree :: FilePath -> IO ([DirData], [FileData])
readTree dir = do
    d <- readDirectory $ dir ++ "/."
    let ps = zipPaths $ "" :/ (free d)
        fd = F.foldr (:) [] ps
        dirs = tail . getDirs [] $ free d

    return (dirs, fd)


------------------------------------------------------------------------------
-- Calls readTree and returns it's value in a quasiquote.
dirQ :: FilePath -> Q Exp
dirQ tplDir = do
    d <- runIO . readTree $ "project_template/"++tplDir
    runQ [| d |]


------------------------------------------------------------------------------
-- Creates a declaration assigning the specified name the value returned by
-- dirQ.
buildData :: String -> FilePath -> Q [Dec]
buildData dirName tplDir = do
    let dir = mkName dirName
        typeSig = SigD dir typ
        t2t = TupleT 2
        st = ConT ''String
        lst = AppT ListT st
        typ = foldl AppT t2t [lst, AppT ListT $ foldl AppT t2t [st, st]]

    v <- valD (varP dir) (normalB $ dirQ tplDir) []
    return [typeSig, v]
<commit_msg>Refactor adding type sigs in buildData<commit_after>{-# LANGUAGE TemplateHaskell #-}
module Snap.StarterTH where

------------------------------------------------------------------------------
import qualified Data.Foldable as F
import           Data.List
import           Language.Haskell.TH
import           System.Directory.Tree
------------------------------------------------------------------------------


------------------------------------------------------------------------------
-- Convenience types
type FileData = (String, String)
type DirData = FilePath


------------------------------------------------------------------------------
-- Gets all the directorys in a DirTree
getDirs :: [FilePath] -> DirTree a -> [FilePath]
getDirs prefix (Dir n c) = (intercalate "/" (reverse (n:prefix))) : concatMap (getDirs (n:prefix)) c
getDirs _ (File _ _) = []
getDirs _ (Failed _ _) = []


------------------------------------------------------------------------------
-- Reads a directory and returns a tuple of the list of all directories
-- encountered and a list of filenames and content strings.
readTree :: FilePath -> IO ([DirData], [FileData])
readTree dir = do
    d <- readDirectory $ dir ++ "/."
    let ps = zipPaths $ "" :/ (free d)
        fd = F.foldr (:) [] ps
        dirs = tail . getDirs [] $ free d

    return (dirs, fd)


------------------------------------------------------------------------------
-- Calls readTree and returns it's value in a quasiquote.
dirQ :: FilePath -> Q Exp
dirQ tplDir = do
    d <- runIO . readTree $ "project_template/" ++ tplDir
    runQ [| d |]


------------------------------------------------------------------------------
-- Creates a declaration assigning the specified name the value returned by
-- dirQ.
buildData :: String -> FilePath -> Q [Dec]
buildData dirName tplDir = do
    let dir = mkName dirName
        typeSig = SigD dir typ
        typ = tup2 (listOf str) (listOf $ tup2 str str)
        tup2 x y = foldl AppT (TupleT 2) [x, y]
        listOf = AppT ListT
        str = ConT ''String

    v <- valD (varP dir) (normalB $ dirQ tplDir) []
    return [typeSig, v]

{'fraction_non_alphanumeric': 0.3406308851224105, 'fraction_numerical': 0.0018832391713747645, 'mean_word_length': 1.1601423487544484, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2319932', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1045, 'n_words': 379}