The Explore Scala Series – Overview

I thought of reading up scala and come up with a simple series of scala tutorials, at the same time, help myself familarise with the language. The idea of functional language really attracts me, of course in java 1.6, there’s support for scripting language,s

Features

  • Object-Oriented
    • Everything is an object
    • Numbers are objects also (have methods)
  • Functional
    • Every function is a value
    • Supports higher order functions
  • Statically typed
  • Can ineract with java directly

Everything is an object

As mentioned above, number is also an object, i.e they have methods and behave like what we know for a typically object. So instead of

object A{
   def main(args: Array[String]){
      println(1 + 1);
   }
}

We could have

object A{
   def main(args: Array[String]){
      println(1.+(1));
   }
}

where in here + is a method of Number.

Functional Example

object A{
 def add(num1: int, num2: int): int = {
 	num1 + num2
 }

	def sub(num1: int, num2: int): int = {
 	num1 - num2
 }

	def op(f: (int,int) => int, para1: int, para2: int): int= {
 	f(para1,para2)
 }

	def main(args: Array[String]){
 	println(op(add, 1,2)) //takes in an add function and 2 para
 	println(op(sub, 1,2)) //takes in a sub function and 2 para
 }
}

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to FurlAdd to Newsvine

Advertisements