Functional programming and TDD
I’m studying the online course Functional Programming Principles in Scala by Martin Odersky. At the moment the course is still in week 2. While doing this week’s assignment I noticed how TDD helps me understand functional programming better.
When composing Scala functions I find myself making a series of assertions that are very much like assertions I’d make in a unit test. For example, the following function defines a singleton set:
Using singletonSet
it’s easy to define union
:
In my mind when I wrote union
I had in mind a series of assertions that will make the function work - exactly the way I’d think in TDD except that in TDD I’m doing it from the outside of the function whereas in functional programming I’m actually asserting the body of the function. In TDD, using Java for instance, I’d then have to implement the method body in imperative style by creating a new set and adding the two sets passed in as arguments to the new set and returning it.
Obviously the comparison isn’t perfect but I find it a helpful guide at the moment. I’m eager to see how the course pans out but so far my gut tells me it’s not an easy course. Quite a bit of reading outside the supplied course material to really make progress.