Sunday, February 21, 2016

for loop in Scala

In Scala, the for is actually foreach, you write it as follows:

val filesHere = (new".")).listFiles
for (file < - filesHere)

for (i < - 1 to 4) //gives 1,2,3,4
for (i < - 1 until 4) //gives 1,2,3

We can not only iterate over a sequence but also filter it as follows:

for (
file < - fileshere
if file.isFile;
if file.getName.endsWith(".scala")
) println(file)

Nested loop:

for (
file < - fileshere
if file.getName.endsWith(".scala");
line < - fileLines(file)
if line.trim.matches(pattern)
) println(file +": "+ line.trim)

Creating new collection:

def scalaFiles =
for {
file < - fileshere>
if file.getName.endsWith(".scala")
} yield file

Immutable objects

  1. State does not change overtime
  2. Can pass them around freely
  3. Threads don't share an object 
  4. Can identify an object by its hash
  1. Large objects result in performance hit when they are duplicated

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