Introduction to Scala

Post on 28-Nov-2014

527 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

A Short presentation on Scala for beginners with Introduction to Functional Programming, Basics of Scala, Use-casses and Examples

Transcript

Introduction to ScalaAugust 2014 Meetup

Rahul Jain

@rahuldausa

2

Agenda

• Introduction to Functional Programming

• Basics of Scala

• Scala Use-casses

• Examples

• Code Samples/Walk-through

Function Programming

• programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state.

• Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function.

• Being first-class also means that it is possible to define and manipulate functions from within other functions.

About Scala

• Scala, short for Scalable Language, is a hybrid functional programming language

• created by Martin Odersky and it was first released in 2003.

• features of object-oriented and functional languages

• run on the Java Virtual Machine• Not a statically typed language• www.scala-lang.org

Scala IDE

• Eclipse:– http://scala-ide.org/

• IntelliJIdea :– http://

www.jetbrains.com/idea/features/scala.html

Scala in the Enterprise

• The Scala programming language is used by many companies to develop commercial software and production systems

• For e.g. :– LinkedIn, EDFT,Twitter, Novell, the Guardian, Xebia, Xerox

, FourSquare, Sony, Siemens, Thatcham, OPower, GridGain, AppJet, Reaktorand many others.

• http://www.scala-lang.org/old/node/1658.html• http://

www.quora.com/Startups/What-startups-or-tech-companies-are-using-Scala

Credit: http://alvinalexander.com/scala/whos-using-scala-akka-play-framework

Popular Frameworks built on Scala

• Akka• Play framework• Lift web• Apache Kafka• Scalding (from twitter)• Gizzard (from twitter)• Kestrel• and many more…

Data TypesByte : 8 bit signed value. Range from -128 to 127Short: 16 bit signed value. Range -32768 to 32767Int : 32 bit signed value. Range -2147483648 to 2147483647Long : 64 bit signed value. -9223372036854775808 to 9223372036854775807Float : 32 bit IEEE 754 single-precision floatDouble : 64 bit IEEE 754 double-precision floatChar : 16 bit unsigned Unicode character. Range from U+0000 to U+FFFFString : A sequence of CharsBoolean : Either the literal true or the literal falseUnit: Corresponds to no value : voidNull: null or empty referenceNothing : The subtype of every other type; includes no valuesAny: The supertype of any type; any object is of type Any : Java's Object classAnyRef: The supertype of any reference type

How to Install

Setting up the development Environment

• http://www.scala-lang.org/download/

• C:\>scala -versionScala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL

• C:\>scalaWelcome to Scala version 2.10.4 (Java HotSpot(TM) Client VM, Java 1.7.0_51).Type in expressions to have them evaluated.Type :help for more information.

• scala> println("Hello, Scala!");Hello, Scala!

Compile and Run

• Compile– scalac HelloWorld.scala– C:\> scalac HelloWorld.scala

• Run– C:\> scala HelloWorld– Hello, World!

Sample Program

object Test {

def main(args: Array[String]){ println("hello world"); } def Hello(args:Array[String]){ println("hello scala"); } this.Hello(null);}

Functionobject <name> extends App {

def <function_name>(var_name: <var_type>, var_name: <var_type>): <return_type>= { }}

object Sum extends App {

def sumInt(x: Int, y: Int): Int = { x + y //return the sum of two values }

println("Sum of 1 and 2: " + sumInt(1, 2))}

Sum

object Sum extends App {

def sumInt(x: Int, y: Int): Int = { x + y //return the sum of two values }

println("Sum of 1 and 2: " + sumInt(1, 2))}

Unit’s Exampleobject UnitTest extends App {

def greetMe(): Unit = { println("Greetings !!!") }

def greetMeByName(name: String): Unit = { println("Hey " + name) }

greetMe // print Greetings !!! greetMeByName("John") // print Hey John

}

object & class

• class– A class is a definition, a description. It defines a type in terms of

methods and composition of other types.• Object

– An object is a singleton -- an instance of a class which is guaranteed to be unique. For every object in the code, an anonymous class is created, which inherits from whatever classes you declared object to implement. This class cannot be seen from Scala source code -- though you can get at it through reflection.

• You can think of the "object" keyword creating a Singleton object of a class, that is defined implicitly.

object & class e.g.

• object A extends B with C– This will declare an anonymous class which

extends B with the trait C and create a single instance of this class named A.

• http://stackoverflow.com/questions/1755345/scala-difference-between-object-and-class

Scala Keywords (Reserved)

• Can not be used as constant or variable or any other identifier names.

abstract case catch classdef do else extendsfalse final finally forforSomeif implicit importlazy match new nullobject overridepackage privateprotected return sealed superthis throw trait trytrue type val varwhile with yield - : = =><- <: <% >:# @

Scala packages import

• import scala.xml._– imports the contents of the scala.xml package

• import scala.collection.mutable.HashMap– You can import a single class and object, for example, HashMap

from the scala.collection.mutable package

• import scala.collection.immutable.{TreeMap, TreeSet}– You can import more than one class or object from a single

package, for example, TreeMap and TreeSet from the scala.collection.immutable package:

Multi line Strings

• multi-line string literal is a sequence of characters enclosed in triple quotes """ ... ""“

• For e.g. :– """the present string

spans threelines."""

var vs val

• var refers to a variable that can change value– mutable variable– var myVar : String = "Foo"

• val refers to a variable that can not change value– Immutable variable– val myVal : String = "Foo“– Equivalent to Java’s final

Scala’s Null

• null value is of type scala.Null • compatible with every reference type. • denotes a reference value which refers to a

special "null" object.

Scala vs Java• All types are objects.

• Type inference.

• Nested Functions.

• Functions are objects.

• Domain specific language (DSL) support.

• Traits.

• Closures.

• Concurrency support inspired by Erlang.

26

Questions ?

27

Thanks!@rahuldausa on twitter and slideshare

http://www.linkedin.com/in/rahuldausa

Interested in Search/Information Retrieval ?

Join us @ http://www.meetup.com/Hyderabad-Apache-Solr-Lucene-Group/

top related