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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s