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:



Sunday, November 10, 2013

First Class Citizen

What are First Class Citizens?

'First Class Citizen', I have gone through this sentence many times but never bothered on what does this really mean.
But yersterday while going through basics of 'Functional Programming', another programming paradigm, I stumbled upon 'Functions as First Class Citizens'. I recalled it was Objects and now it's Functions, How come? Are functions promoted? J.
In programming language design, a first-class citizen (also object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as a parameter, returned from a function, and assigned to a variable. 
Another similar definition, 'First Class Citizens' of a programming language can be:

  1. can be stored in variables and data structures
  2. can be passed as a parameter to a subroutine
  3. can be returned as the result of a subroutine
  4. can be constructed at run-time
  5. has intrinsic identity (independent of any given name)
Java FCC are Objects: True
Scala FCC are Functions: ? (I may have to evaluate Scala to check the validity of 4 & 5).

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)