A small Haskell-module for extracting source code powered by Template Haskell.
functionExtractor :: String -> ExpQ -- ListE
testFunctions = $(functionExtractor "^test")
fooFunctionsInFile = $(functionExtractor "^foo")
fooBar = "hej"
fooGoo = "nej"
is the same as
fooFunctionsInFile = [("fooBar", fooBar),("fooGoo", fooGoo)]
fooBar = "hej"
fooGoo = "nej"
functionExtractorMap works like functionsExtractor but applies a function over all function-pairs.
This functions is useful if the common return type of the functions is a type class.
functionExtractorMap :: String -> ExpQ -> ExpQ
secondTypeclassTest =
do let expected = ["45", "88.8", "\"hej\""]
actual = $(functionExtractorMap "^tc" [|\n f -> show f|] )
expected @=? actual
tcInt :: Integer
tcInt = 45
tcDouble :: Double
tcDouble = 88.8
tcString :: String
tcString = "hej"
locationModule :: ExpQ -- LitE
name = $(locationModule)
module Foo where
name = $(locationModule)
is the same as
module Foo where
name = "Foo"
Useful with splice ([|..|]
).