Tuesday, December 24, 2013

Functions as Objects

How are functions treated in Scala?
Are functions objects in Scala?

Functions values are treated as objects in Scala.
Any function definition of type A => B is equivalent to type Function1[A,B] and this can be expanded with below definition

trait Function1[A,B]{
   def apply(x: A): B
}

So such traits actually supported by Scala. Function1, Function2,... Function21 in scala package. With such support Scala does support Function definition with 22 parameters.

Anonymous Functions

Expanding the above conversation to see how Anonymous Functions are supported.
For example:

(x: Int) => x * x

when expanded per the above definition below is the structure:

new Function1[Int,Int]{
  def apply(x: Int): Int = x * x  
}

The above definition suggest similar to anonymous class structure provided by Java.

Function Calls

Above discussion provides an understanding of function definition treatment in Scala, but how about function calls. Funtion calls maps to apply method as describe in the structure above.

Lets expand this discussion in terms of below definition which declares an anonymous function and further a call is made to the function:

val f = (x:Int) => x*x
f(2)

val f = new Function1[Int,Int]{
  def apply(x: Int): Int = x * x  
}
f.apply(2)


def f(x: Int): Int = f(x)
out of this (x: Int) => f(x) is treated as anonymous function and expanded by Scala in a similar way discussed above. This translation of a name f is termed as 'eta - expansion' in lamda calculas.

Wednesday, December 18, 2013

Polymorphism in Scala

Is Scala polymorphic? Incase yes, what are the different ways with which this is supported?

Scala being a hybrid of object & functional programming paradigmn, supports polymorphism. Scala support two polymorphism forms:

Subtyping: 

Instances of subclass can be passed to functions instead of declared base class.

Generics:

Functions and classed driven via type parameterization.
  1. Function can be applied to arguments of many types (via generics support at method level)
  2. The type can have instance of many types (via generics support at class level)
Field definition in Scala are special cases of method overriding. They can implement abstract method overriding and traits.

Below are a few highlights in the example:
  1. Cons(val head: Int, val tail: IntList) declares both parameter and field of the class. By declaring this it automatically overrides the abstract definition for head and tail methods.
  2. Generic Type parameter using [T] similar to type definition in Java via <T>
  3. Type parameters are not just limited to classes, but they can be declared at method level as well.
Since Nothing is a subtype of any other type and this is true for T as well. Therefore below syntax is valid and there by this method can be overrided with below definition
def head: Nothing = throw new NoSuchElementException("head.nil")
def tail: Nothing = throw new NoSuchElementException("tail.nil")

Saturday, December 14, 2013

Good Story

Thanks Stephen

Mutter Paneer


This one is not about Scala ;)

Being a weekend thought of preparing something special today. And the very moment heard Mutter wala selling mutter (peas) out side. This reminded me of Mutter-Paneer which SVJ brought yesterday for his evening meal.

Bingo, Why not try Mutter Paneer today!! 

Googled the recipe and stumble upon Nisha Madhulika's site, this details the recipe and that too a Jain version. Followed the steps suggested by her and here it is (on the left) !! 

Comments:
Mumma: She liked it but found it a bit sweet. I haven't added any sugar, looks like peas were sweet.
Shrut: She too find it a less spicy one and bit sweet. But overall it was a nice preparation.
Aryaa: She's the critic, never eat what I prepare.. :(
Me: I liked it, it was delicious. Honest comments.
After eating went to for a nap for 2 hours. Made my day !!

From this experiment, I now believe that such meals can be prepared at home and with better taste then how it is served at restaurants. I am fond of cooking & this experiment has encouraged me.
Now my next goal is to get some Mexican dish prepared at home. I have tasted one such dish at Chipotle and since then I loved it.

Tuesday, December 3, 2013

Operator Overloading in Scala

Does Scala support Operator Overloading?

If I go by below definition then I would say yes it does.
In object-oriented programming, operator overloading—less commonly known as operator ad hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by the language, the programmer, or both.
Below is an example which elaborates on how is this supported. I am extending  binary <, unary pre negation (-) and unary post ++ operator for a custom datatype Rational.