Expression evaluation mode in haskell for scripting
Regarding modules: ghc -e
uses your ~/.ghci
file, so in this case, you'd add :m +Data.List
to it (import Data.List(nub)
is also supported since GHC 7 or so).
Regarding packages: You can use ghc-pkg hide somepackage
and ghc-pkg expose somepackage
to define the default set of visible packages (packages are exposed by default though; maybe I misunderstand your question).
You might find eddie useful.
In addition to the standard configuration per the .ghci file (apart from your global ~/.ghci, you can have specialised .ghci files in some directories, which will then be read before that), you can also pass a Haskell source file as command line argument, ghc -e "expression" path/to/Source.hs
. expression
will then be evaluated in the context of module Source
.
Like FunctorSalad said, ghc -e
loads the ~/.ghci
so you can use it to add any boilerplate that you might use in your script. In your case you could add
let script f = getContents >>= return fimport Data.List
to you ~/.ghci
file and then run:
git diff ... | ghc -e "script.nub.map(fst.break(=='/').head.words).lines"