Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Saturday, April 7, 2012

Scala Basics - Simple Class

Simple class definition
scala> class User {
   val userName:String ="Steve Kent"
   def getUserName(firstName: String, lastName:String): String = firstName + " " + lastName
 }  
 defined class User

Define object of the Class
scala> scala> var user = new User
user: User = User@1fce2f2

Access Class members through Object
scala> scala> scala> user.getUserName("sam","anderson")
res1: String = sam anderson

Tuesday, April 3, 2012

Scala Basics - Anonymous Functions

Simple way to create anonymous functions on Scala

scala> (result: String) => "Hello " + result
res0: String => java.lang.String = <function1>
It adds "Hello" to an String result, so we call this function like this
scala> res0("Scala")
res2: java.lang.String = Hello Scala

Pass anonymous functions into vals
scala> val welcome = (result: String) => "Hello " + result
welcome: String => java.lang.String = <function1>

scala> welcome("Scala")
res3: java.lang.String = Hello Scala

Expressions - {}
You can define expressions like this
scala> def welcome(message: String): String = {
    "Hello " + message
}
welcome: (message: String)String

scala> welcome("world")
res4: String = Hello world


scala> def addition(i: Int): Int = {
     i + 10
}

Real anonymous function
scala> { message: String =>
       "Hello " + message
 }
res5: String => java.lang.String = <function1>
scala> res5("Anonymous function")
res6: String = Hello Anonymous function

Thursday, March 29, 2012

Scala Basics - Functions

You can define function with "def"

scala> def pageVisitCount(session: Int): Int = session + 1 pageVisitCount: (session: Int)Int scala> var totalPageCount = pageVisitCount(1) totalPageCount: Int = 2 scala> def message() = "Hello Scala" message: ()java.lang.String scala> message() res0: java.lang.String = Hello Scala scala> message res1: java.lang.String = Hello Scala
Even you can able to call function without parenthesis if it doesn't have arguments.

Scala Basics - Variables


You can define variable by "var" keyword

scala> var message="Hello Scala" message: java.lang.String = Hello Scala scala> var count=7 count: Int = 7
These two example we have created variables of type String and Int.Remember you can able to change message and count binding, since it's declared by "var".

Scala Basics - Simple Expression

Here is simple expression in Scala.

Scala> 7 + 7 res0: Int = 14 Scala> 4.5+9.3 res0: Double = 13.8
Scala interpreter automatically created value name(res0) for result of the expression. Last expression, res0 has Double type and stores 13.8 value.

You can define you're own name for a expression through "val".

scala> val result = 4*5 result: Int = 20
You cann't change the binding to a val.Almost everything in Scala is an expression.

Wednesday, March 28, 2012

Scala Basics - Interpreter for testing/learning!

Once you're machine installed with scala, then starting scala interpreter is easy

Note: refer this post for Scala install instructions for Ubuntu

$ scala

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

Now you're machine is ready for testing Scala code!