My first introduction to functional programming
- Coming from an imperative background (Ruby), functional programming concepts were alien to me, I had never heard of it before.
- When I joined my company as an intern 2 years ago (2017), they had just begun exploring functional programming, and started their new projects in Purescript, a strongly typed purely-functional language that compiles to Javascript
- As it was similar to Haskell and lacking in good resources, I started off with Haskell tutorials like LYAH and RWH
- Learning FP had a steep learning curve, there were many times during this period that I felt I would learn must faster if I forgot imperative programming.
It’s impact on me
- It allowed me to approach problems in a non imperative way, not every problem requires inheritance or state
- My imperative approach to solving problems, was to define my problem statement, then think of the steps required to achieve the final goal
- With functional programming, I would define the current state & the desired state, then create functions that would transform my current state to my desired state. This approach left a much more cleaner code.
- Focusing on separating
pure
functions from functions withside effects
, helped me minimize unexpected behaviour. - Now, when coding in imperative languages, I attempt to make my code referentially transparent.
Referential transparency has helped me make my code more modular and simpler - Using higher order functions and function composition, taught me how to better separate functions for reuse.
- I now prefer writing types (if the language supports it), separating effectual and pure functions and passing state separately, even though this takes a little longer to code. I think it is worth the effort because it prevents silly runtime errors like
Cannot read property of undefined
.
The most important impact
It made me much more confident in trying new languages or frameworks,
Before starting in FP, I was used to Ruby, (& Rails). Every new problem I encountered, I would think of a way to solve it in Ruby, my thought process was, “Why should I learn X, when I could do it in Ruby?”
FP was a completely different paradigm, learning it was frustrating at times, but after I got used to it, I no longer feared learning a new language.
If I could go from Ruby (Imperative) to Purescript (Functional), then I could most definitely explore languages like Golang or Java.
After this realization:
- I attempted to make a simple compiler written in Java (Creating a JVM language and it's compiler)
- Explored mobile app frameworks like Nativescript
- Written webcrawlers, load testers and kubernetes controllers in Golang
Me in 2016, would have never done this, if something was not possible or difficult in Ruby then I would move on to another problem.
Now, I believe in focusing on the problem instead of being restricted to a specific language, and I would use whatever is the best tool for that problem.
Final thoughts
If you have never tried functional programming, I would highly recommend that you try it,
If you choose Haskell, Learn You A Haskell and Real World Haskell are some good tutorials to get started.
You don’t even have to use purely functional languages, even Javascript or Golang can incorporate functional concepts.
This link has a good list of resources for functional Javascript.