Scala–Methods vs Functions

An excellent explanation on Scala Methods and Functions:

Apparently, Methods in Scala are different from Functions. From what I understand, a method is similar to a method in Java – it takes input and produces an output. But a Function is an object. Also takes an input and produces an output, but is an object. Here’s how they’re different:

def f1(x:Int):Int = x*2     //Method that doubles input

def f2 = (x:Int)=>x*2      //Function that doubles input

//f1.toString                    //Cannot be done!

f2.toString                      //Is perfectly valid

For more details, I’d suggest you take a look at Jim McBeath’s explanation.

Check if Binary tree is balanced or not

A quick interview exercise is to write code that checks whether a binary tree is balanced or not. Since a binary tree just begs for recursion, I decided to try it out in Scala. Takes less number of lines than I thought it would and it works. Logic works like this:

  1. If left node and right node are empty, tree is balanced
  2. If neither node is empty, just check that both left and right nodes are balanced
  3. If left node is empty, then right node can go down at most one more level
  4. If right node is empty, then left node can go down at most one more level

Here’s the code for it:

class Tree(val left: Tree, val right: Tree)

def isBalanced(tree: Tree): Boolean = {
  if (tree.left == null && tree.right == null)
  else if (tree.left != null && tree.right != null) {
    isBalanced(tree.left) && isBalanced(tree.right)
  } else if (tree.left != null && tree.right == null) {
    tree.left.left == null && tree.left.right == null
  } else {
    tree.right.left == null && tree.right.right == null



Working with Functions in Scala – Part 1

Scala treats functions as first class citizens. I keep forgetting the syntax for it, so putting it here as a handy reference. This post deals with the following features of Scala:

  1. Anonymous Functions
  2. Assigning Functions to variables
  3. Composite Functions
    Please note that the term ‘function’ here is used to refer to both mathematical functions and programmatic functions. The key strength of Functional Programming languages is the ability to unify both.

Continue reading

An Imperative Programmer beginning with Functional Programming

Couple of months ago, a friend of mine drew my attention to Functional Programming in Scala, a course on Coursera. Having worked for years with the likes of PHP, C, C++, Java and not to mention BASIC before that, I didn’t think much of Functional Programming. Truth be told, when someone mentioned Functional Programming to me, I thought he was talking about C or rather non-OOP programming which didn’t involve Objects(Yes! I admit it! I was a brash naïve 22 year old fresh with a non-CS degree!). As a matter of fact, that was my perception until I took the course (Okay, there’s honesty and then there’s beating a dead horse!).

So, for the benefit of the uninitiated who happened to accidentally stumble upon my obscure and randomly technical blog, I’ll give the lowdown on Functional Programming … in my own words. Apart from the regular differences between Functional and Imperative programming, here’s my experience with it:

Continue reading