Source code for this blog post can be found here.
One of my favorite ways of learning a programming language is to just create little projects for myself that I want to learn. The process of doing the research to learn how to achieve a particular programming task I often find more enjoyable than trying to understand all of the in’s and out’s of a language. That said, one project I embarked on to get better at Haskell was reading a yaml config file. Pretty much all programming projects I have worked on have had some sort of configuration so I felt that this little project would be well worth the effort.
As always, the first place we start looking is for libraries. Out of
the gate we find the yaml
libraries. The more general
yaml library seems to specifically deal
with yaml data. On the otherhand, the
yaml-config library seems to
go a step further and deals with loading a config file from
disk. Looking at it’s
.cabal file it seems that unsurprisingly
yaml-config uses the
yaml library behind the scenes. I always like
to work at the highest level of abstraction if possible and it seems
yaml-config may do exactly what we are looking for so lets
We’ll start off by creating a basic project with stack:
We can see that executing the basic stub program with
someFunc. This means our scaffolded
program is ready for us to write some code.
The next thing we’ll want to do is go ahead and add the
library dependency to our
Then we rebuild the project with
stack build to acquire the dependencies.
Next we create a basic
yaml config file:
Now lets write some code:
This library seems to have a dependency on OverloadedStrings so I have
added that from the example on the github page. The code is all pretty
straight forward. We load up the file, grab the “config” key and get
the collection of “items”.
lookupDefault is used here as an example
of how a default can be specified if a key is not found in a config