Functional Programming / Object Oriented Programming
I’ve started studying Functional Programming in Elixir as I’m interested in learning more about what it offers for concurrent programming and scalability.
I’ve encountered a number of articles about object oriented programmers needing to change their frame of mind in order to understand functional programming. For example, Avdi Grimm has an excelent series of posts describing his experiences working through Elixir using the exercises in Function Programming for the Object Oriented Programmer. But, the more I read about the differences between functional programming and object oriented programming the more I realize that I’m not an object oriented Programmer.
I may not be a object oriented programmer but I haven’t been a functional programmer before either. I’ve spent my professional life programming C on very large procedural style projects. Within these projects I have felt the pain of functions with side effects.
In my spare time I’ve been studing object oriented design in ruby in the hopes of learning new ways of coping with state changes. But now I’m intrigued by functional programming’s treatment state. I’m interested in seeing how fp can help me make state changes more explicit and controlled so that my programs are easier to reason about and change.
When I’ve programmed in ruby I’ve often used reek to guide my object oriented design. I find that over and over again I write methods that take their input from their parameters and don’t depend on any object state. Reek reports a code smell, describing my method as “a utility function”. From an object oriented perspective this is a problem because the method in question has no cohesion with the rest of the class. But, from a functional programming perspective my method is a perfectly good, referentially transparent, function. Maybe I have already been leaning towards the functional style all this time.
Functional Programming in Elixir
As I mentioned last week I’ve started studying functional programming in Elixir. And I’ve encountered a number of blog posts and other information. So far, the best resources I’ve found so far are the books:
- Functional Programming in Scala by Paul Chiusano and Rúnar Bjarnason.
- Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas.
I’m focusing on Elixir but Functional Programming in Scala is an excellent and highly recommended introduction to functional programming. The book strongly recommends working through the exercises and so far I’ve been enjoying them. I’ve also been using exercism.io to practice. In an interesting bout of synchronicity I found that I was reading about lists in Chapter 3 for Functional Programming in Scala at the same I was working through the list-ops exercise in Elixir on exercism.io. There was quite a bit of overlap between the two sets of exercises but the exercises in the book go on a bit further.
It wasn’t until this point that I realized I could, like Avdi, adapt exercises in another language to Elixir. So, I’ll be working through FP in Scala’s exercises using Elixir. Furthermore, I will confirm my results and drive my development by continuing with the test-driven style I’d be using with exercism.io.
Over the next week I want to start doing this. I’ll continue where the list-ops exercise left off and will write tests describing the FP in Scala exercises and then will write up my solutions. I’m planning on chronicling the whole experience here on this blog.
Of course I’ll still have to learn and practice enough Scala to keep up with the examples and discussion in the text.
Check back next week to see how this goes.