Masha Reko - Apprenticeship Blog

What we have to learn to do, we learn by doing. (Aristotle)

Day 5: Hello, world!

I love my comfort zone.

I could spend my whole life writing programs in Java. It’s like speaking your native language: I haven’t heard of anyone ever that eventually got tired of it. But as nice as the comfort zone can get, nothing ever grows there. And thus, neither can I. So it was time for me to break the Java chains and sail onto a new adventure.

I am not the one known for taking the easy path; if I decide to leave my comfort zone, I make sure to do it in the most extraordinary way possible. For my new programming language, I could have chosen any object-oriented one similar to Java (which would have made my life much easier). But no. I decided to do what I never did before: functional programming with a language that is both object-oriented and functional. I decided to do Scala.

With that said, I started to read a book titled Functional Programming in Scala by Paul Chiusano Ruinar Bjarnason. The first lesson I learned is what functional programming actually is: constructing programs using only pure functions (i.e. those that produce no side effects). Sounds easy enough, I thought at first. But then I stopped to think for a second. The fact that functions are pure, implies that they cannot modify data structures. Once I realized that, I slowly started to panic. Even writing a simple loop seemed impossible at this point. ‘Why would anyone even want to constrain himself like this?’ I asked myself. However, as if it knew what I was thinking, the book answered my question: for better modularity, testability, reusability, paralellization. Ok, that doesn’t sound bad. I guess I should give it a chance.

As I started to dive deeper into the functional programming concepts, I found that basically everything is a function and functions are everything. There are functions that take other functions as arguments. There are functions that return other functions as outputs. There are functions within functions within functions within functions. Even the loops are (recursive) functions. After a couple of hours doing (still quite simple) exercises, my mind was about to blow.

alt text

I am still not sure if I like this functional approach or not, to be honest. It’s different (a lot different!) than everything I’ve ever done, and it is still a challenge to change my way of thinking and solving problems, in order to adapt to it. Nevertheless, the small victories that come with learning a new language certainly help in keeping my spirits up. It’s been a while since I was excited about printing Hello, world! or successfully computing a factorial. So I end this day with a feeling of success, trying not to let myself think how far I am from the ultimate goal. Just like a journey of a thousand miles must begin with a single step, mastering Scala must begin with a Hello, world!

Everything else will follow with time.