Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

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.

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.

Wednesday, November 20, 2013

Data structures in Scala

How do I define my own data structures in Scala? Or does Scala does support some encapsulation mechanism?

Class similar to Java are supported in Scala. I wonder if Scala support Class then is Scala object oriented? Need to give a serious thought on this question. Any ways let me not create confusion here and first discuss the class constructs supported in Scala. Below example discuss encapsulation of rational number type and supporting various arithmetic associated.


Tuesday, November 19, 2013

Higher Order Functions

What does it mean by functions as first class values? Or, what are higher order functions?

There are languages which supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Scala is one such language which does support higher order functions, scala worksheets below explains some of these with examples:



Thursday, November 7, 2013

Evaluation strategies in Scala

What evaluation strategy does Scala follows? OR How does call by name and call by value evaluation strategies supported in Scala?

I would suggest reading wiki for understanding basics of evaluation strategies (http://en.wikipedia.org/wiki/Evaluation_strategy), specifically call by name and call by value, since both of these are supported in Scala. Below are the extracts which I found most useful:
Call by value
In call-by-value, the argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function (frequently by copying the value into a new memory region). If the function or procedure is able to assign values to its parameters, only its local copy is assigned — that is, anything passed into a function call is unchanged in the caller's scope when the function returns.
Call by name
In call-by-name evaluation, the arguments to a function are not evaluated before the function is called — rather, they are substituted directly into the function body (using capture-avoiding substitution) and then left to be evaluated whenever they appear in the function. If an argument is not used in the function body, the argument is never evaluated; if it is used several times, it is re-evaluated each time it appears.
To understand this in Scala context, let's see this using an example. I have two version of AND implementation below:

Version 1
def and(x:Boolean, y: Boolean):Boolean =  if(x) y else false

Version 2
def and(x:Boolean, y: => Boolean):Boolean =  if(x) y else false

Both of these version returns the same result when called with different boolean values for x and y. Though both of these versions are different, version 2 pass y using 'call by name' strategy, represented by '=>' symbol.
You must be wondering, so now what is the difference? Let's introduce a loop function

def loop: Boolean = loop

Analyzing the outputs of the two statements will give a better understanding of the these strategies.

and(false, loop)

Version 1 shall fall into an infinite loop since it's following a call by value strategy. While with version 2 the statement shall work fine.

Courtesy: Martin Ordersky for explaining it so beautifully, I am spreading his word here.

Tuesday, November 5, 2013

Hello Scala

I want to head-start with Scala, how can I write hello world program in Scala?

I found multiple ways of getting started with Scala, downloaded the latest scala-eclipse IDE backed by JDK7. Below are the different ways to Hello Scala:

Via Scala object:

Create below Scala object & execute this as Scala application

package learn.scala
object Hello {
  def main(args: Array[String]): Unit = {
    println("world")
  }
}

Via Scala worksheet:

Create Scala worksheet with below statement and saving the file shall do the needful

package learn.scala
object HelloWorkSheet {
    println("Hello World")
}

I plan to sync my worksheet and keep this updated on GIST. Here's the link to my GIST(https://gist.github.com/ajjain/7364919)