Tuesday, December 01, 2015


After a lot of research I kinda have a sense of why we need currying in functional languages - it turns out, there are some (don't know which) analytical techniques which can only be applied to functions with single argument.

So if we want to use these (mysterious) techniques, we need currying :)

Oh, and of course it gives us a different style of reuse

Sunday, November 15, 2015

while ((line = readLine()) != "") doesn't work in Scala

In C++ or Java, line = readLine() would return the line itself, however, in Scala, assignment to var type variable returns () i.e. Unit, so it cannot be compared to "" in a while loop.

Pure functional languages do not have loops because of this same reason, it does not return anything

Friday, November 13, 2015

Scala Class and Object

class checksumAccumulator {

private var sum = 0

def add(b: Byte): Unit = {
sum += b

def checksum(): Int = {
~(sum & 0xFF) + 1


By default sum is public, we have to explicitly make it private.
Parameter b is by default val, so we cannot re-initialize it in checksum method, there is no return keyword, in such case, last value computed by method is returned - this is the recommended style - avoid explicit and multiple return statements

class ChecksumAccumulator {
private var sum = 0
def add(b: Byte) { sum += b }
def checksum(): Int = ~(sum & 0xFF) + 1

since our methods are just single statements, they can be written without {}, also when method does not return anything, i.e. its return type is Unit, we can skip writing the return type and "=", like in the add method

all datatypes can get converted to Unit - it simply looses the data
semicolon at the end of the statement are optional

object ChecksumAccumulator {

defining object instead of class gives singleton of the class called companion object class
A class and its companion object can access each other’s private members.

To run a Scala program, you must supply the name of a standalone singleton object with a main method that takes one parameter, an Array[String], and has a result type of Unit. Any singleton object with a main method of the proper signature can be used as the entry point into an application.

object Summer {
def main(args: Array[String]) {

We can also write the following:

object FallWinterSpringSummer extends Application {

Thursday, November 12, 2015

Who uses functional programming

  • Erlang
    • Amazon
    • Yahoo
    • Facebook
    • Whatsapp
  • Haskell
    • AT&T
    • Google
    • Facebook
    • NVIDIA
    • Banks - ABN Amro, BoA, Barclay, Credit Suisse, Deutche Bank, SC
  • OCaml
    • Facebook
    • Bloomberg
  • Scala
    • LinkedIn
    • Twitter
    • Sony
    • Seimens
  • Clojure
    • Facebook
    • Walmart
    • Citi

Why functional programming matters now

  • Clock speed of processors are not increasing
  • Cores on a processor are increasing
  • In future we may have hundreds of cores
    • Execution is no longer the bottleneck
    • Memory is a bottleneck
  • Foo(f(x), f(x))
    • both f(x) might get executed on different cores
    • it will not matter if there is no state change
      • Hence functional programming

Functional Programming principles

Functional programming has 2 principles:

  • Functions are first class values
    • pass them as parameter
    • return them from a function
    • store them in variable
    • define function inside a function
    • have function literals in the code - like int lieral 42 => anon function
  • Operations should map input values to output
    • Not change data in place - like what OO does
    • Makes data immutable
    • No side-effects => referentially transparent

Attended conference on functional programming

Attended a conference (http://functionalconf.com/) on functional programming.
It gave me the push needed to take up learning a new language. I have started exploring SCALA, I will be posing my learning here.

I am the one in white shirt