Skip to main content


Showing posts from May, 2017

Performance and scalability cheatsheet

Performance tips! Boxing and unboxing  cost might be high especially when in a loop (can create many heap allocations). Tail recursion  If you find yourself in a situation where you are doing a recursive call (where you should first find for already existing library function to do the operation for you instead of recursive call), then do whatever possible for the recursive call to be  tail recursive . A function is  tail recursive  if the recursive call is the last instruction performed which means only first call is in  stack . A hint for many tail recursive call is that you pass some accumulator inside the recursive call, which means you are doing the calculation in the next step in the recursion as opposed to holding the accumulator outside the tail recursive call. def tailrecSum ( l : List [ Int ]) : Int = { def loop ( list : List [ Int ], acc : Int ) : Int = list match { case Nil => acc case x :: xs => loop ( xs , acc + x ) /

Scala's for comprehension transformation flatMap, map for the resting developer

I recently published this in stackoverflow, enjoy Our plan I'm not a scala mega mind so feel free to correct me, but this is how I explain the  flatMap/map/for-comprehension  saga to myself! To understand  for comprehension  and it's translation to  scala's map / flatMap  we must take small steps and understand the composing parts -  map  and  flatMap . But isn't  scala's flatMap just  map  with  flatten  you ask thyself! if so why do so many developers find it so hard to get the grasp of it or of  for-comprehension / flatMap / map . Well, if you just look at scala's  map  and  flatMap  signature you see they return the same return type  M[B]  and they work on the same input argument  A  (at least the first part to the function they take) if that's so what makes a difference? Our plan Understand scala's  map . Understand scala's  flatMap . Understand scala's  for comprehension .` Scala's map scala map signature: map [ B ](