Scala for Simpletons — brief articles explaining functional programming in Scala. Ep.1) What is Functional Programming?
First off, welcome to Scala for simpletons, I'm not sure I will be able to write everything in a way that even I would be able to understand, but we will see. This is the first of a series of articles I will be putting out to help solidify my own understanding of Scala and the FP paradigm. Hope you enjoy and find value.
So what functional programming?
To answer this we are going to start with what is Object-Oriented Programming; Understanding how data is being handled in one will really help understand how functional programming is different. Im going to write my own brief synopsis of OOP and then ill go find a professional answer.
Object-oriented programming is a programming style based on objects, obviously, you create objects, give these objects fields and methods, and to change the state of your program, use those methods to change the state of the objects. (Am I being concise?), this is based on how hardware is currently built in a computer, but that is an article in itself; look up Imperative programming and John Von Neuman…
With OOP, there is a mathematical flaw in the idea that x + y is not guaranteed to equal z, because you can change the fields(state) of these objects it allows for a lot of possible error, Functional programming fixes that along with a few other problems. OOP and Functional programming have their place, Scala pulls out features of both.
above you will see a ObjectOriented client, main is the entry point into our application, inside of ObjectOriented i have nested a couple additional classes to illustrate a flaw in mutable state. As you can see we create two objects, one holds the value 1 and the other 5. We created a Divider which takes in the Objects with values 1 and 5, and we divide them to get .25.
Now, two lines down we call the same method, and BOOM, catastrophic failure, the world is burning, we now have to spend a million hours changing the entire structure of our code base to fix an issue that laid dorment for months. it seemed to work fine, now it doesn’t.
In Pure Functional programming there is Zero state change in the application, all inputes have the same output, there in no flow control, looping is done with recursive operations, an everything is self contained.
We will get into recursion and looping, call by name vs call by values in a future article. This is a very basic illustration of how we think declarativly vs imperatively.
So in short, once you create a “definition” or “value” you do not need to worry about the functionality of that value changing. Functional programming also favors much more modular thinking and composition through much smaller functions which make for code that is easier to debug and reason with.
Next week I will go into Functions and evaluations in Scala.
Awesome Resources for FP and Scala:
— Scala-lang is extremely well documented, here is a cheat sheet to the Scalal language
scalaexamples is a blog spot that goes into great depth about different features of Scala
Functional Programming in Scala, amazing course on coursera.