A calculator to add, subtract, multiply and divide rational numbers.
I enjoyed thinking my way through this project. Union types, data abstraction with modules and elm-test all helped to make implementing the calculator's logic manageable.
I had two major goals I wanted to accomplish when building this application.
- To have a clean separation between the UI and the application logic.
- To have an example where tests (in spite of types) are helpful in catching bugs.
I think I accomplished both but you can be the judge.
With respect to the first goal I did it by starting with the right abstraction. If you follow the commits from when I added addition and subtraction to when I finally made decimal input work you'd see the evolution of the abstraction and the minor changes I needed to make to the UI.
With respect to the second goal I don't think I could have maintained my sanity without the tests. Don't get me wrong, types helped a ton but they couldn't guarantee the correctness of some of the algorithms I needed to write.
For e.g. in my rational to decimal representation logic I had a bug where
-1/3 was being represented as
0.(3) when it should have been
test caught that, see here.
And, I was able to fix
it soon afterwards.
Types are great but tests are still needed for complex application logic or algorithms. There's a myth in the community that once it compiles it works. But I think that that's a bad expectation to set.
Union types are really great for domain modeling.
You can learn a lot with a well chosen project.
"In science, one can learn the most by studying what seems the least." ~ Marvin Minsky, Society of Mind
That's all folks!
Check out the demo.