by DanWaterworth · 14y ago · 0 comments
Haskell turned out to be a fantastic language to write this in; I especially liked how I was able to use the type system to separate out the levels of abstraction in the code base. I'm no stranger to implementing database like things and of the languages I've used in the past, I wouldn't favour any of them over Haskell in this setting.
I'll get right on to reorganization, it was on my todo list, but it's always more fun to add features instead. Thanks for taking the time to look at Siege.
I completely understand not cleaning your code up--for some of my old projects in various languages, that's still on the todo list :) I just wanted to make sure you knew about that article--had I known it when starting some of my earlier projects, they would have been less of a mess initially. (Admittedly my last project was for a hackathon and was an unholy amalgamation of Haskell, Scala, JavaScript and a bit of Scheme; nothing could make that neat. :()
Essentially, I start with basic code and possibly use some constants in the beginning. Eventually, I need the constants to be loaded from a config file or command line and now a huge swath of my code, which was perfectly pure, needs to be dragged into the IO Monad and the refactoring effort involved is huge, as no function that is even indirectly touching the so-called constant is now pure. This ends up being extremely painful. I have asked around and 2 suggestions I received, dump all my refactored functions inside a giant let clause, or wait for parameterized modules. How did you deal with this issue?
The simplest case is:
do_work :: Config -> Result
do_work config = ...
read_config :: IO Config
main = do
config <- read_config
print . do_work $ config
But you can also put the configuration in a Reader, and avoid the step of manually passing the config to each function that needs it. Instead of: my_program :: Config -> Result
step1 :: Config -> Arg -> OneResult
step2 :: Config -> Arg -> SecondResult
my_program config = step2 config . step1 config
You'll write: my_program = runReader config $ do
x <- step1 42
return . step2 $ x
So let clauses and parameterized modules are not even in the running. Whenever you have a problem in Haskell, the best way to solve it is to ask yourself how you'd solve it in some other language. Then do that.(Nobody writes software in any language where every function is responsible for reading a config file; that functionality is delegated to some common instance that is passed around as needed. So do that in Haskell, too.)
my_program = do
x <- step1 42
step2 x
or
my_program = step2 =<< step1 42(I use =<< instead of >>= so that nonadic composition reads like normal composition (.).)
Anyway, then do:
runReader my_program config