Transition from java OOPs to scala FP -1

Sanjeev Ranjan
3 min readMay 25, 2020

My journey has been a roller-coaster ride and is worth mentioning here for the programmers who are looking how to gain that extra bit of confidence in this ever-changing habitat and perform remarkably well .From a guy who started writing pretty much verbose code in start of the career to writing very much neat and pleasant ones , the journey has been prodigious .I will write the whole transition of mine in a series of articles demonstrating Einsteins famous quote which encourages all.

“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”

To start with i want to explicate that this article is not a debate between languages or proving out any specific point but in support to general theory to evolve for survival of the fittest . In these series of articles , i would basically guide you through the complete pathway of scala and advanced . I myself started up with very basic building blocks and now i can understand almost any type of scala code . So to start with let’s have a glimpse of comparison which i started off .

Snippet of early days in java .. the evergreen quick-sort algorithm in java :

class QuickSortJava {

int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

return i + 1;
}

void sort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}

static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String args[]) {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;
QuickSortJava ob = new QuickSortJava();
ob.sort(arr, 0, n - 1);
System.out.println("sorted array");
printArray(arr);
}
}

Below is the scala version of the same quick-sort algorithm . Doesn’t it looks more like English which is easily readable and understandable ?

object QuickSort extends App{
println(quicksort(List(6, 9, 23, 1, 2, 3)))

def quicksort(xs: List[Int]): List[Int] = {
xs match {
case Nil => Nil
case head :: tail => {
val (low, high) = tail.partition(_ < head)
quicksort(low) ::: head :: quicksort(high)
}
}
}
}

it is not only pearls of code in both the JVM languages , but also it gives you a lot many things to ponder upon while solving a computer problem .Following are some :

  1. How efficient is my code ?
  2. How neat is my code ?
  3. How maintainable is my code ?
  4. what is the effort required to do a task in your habitat?
  5. What is the learning curve for the environment and the language ?

So, next i would be starting up with a contrast discussion of scala with respect to java . I will make sure you understand every component of FP in scala and finally demystify some of the abstraction level libraries like cats , scalaz and others . To have an objective in mind , this series is for someone who feels confused reading about monads ,functors ,state monads , IO and other advanced scala topics , who doesn’t has much confidence in writing code like

def updateHealth (delta: Int): Game[Int] =
StateT[IO, GameState, Int] {
(s: GameState) =>
val newHealth = s.player.health + delta
IO ((s.copy (player =
s.player.copy (health = newHealth) ), newHealth) )
}

So , stay tuned for more …..

Link for part 2 :

https://medium.com/@coolsanjeev1606/transition-from-java-oops-to-scala-fp-2-29e11e0e19c1

--

--

Sanjeev Ranjan

Software Engineer || Programming Evangelist || Scala & Java