27
Introduction to Scala August 2014 Meetup Rahul Jain @rahuldausa

Introduction to Scala

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Introduction to Scala

Introduction to ScalaAugust 2014 Meetup

Rahul Jain

@rahuldausa

Page 2: Introduction to Scala

2

Agenda

• Introduction to Functional Programming

• Basics of Scala

• Scala Use-casses

• Examples

• Code Samples/Walk-through

Page 3: Introduction to Scala

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.

Page 4: Introduction to Scala

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

Page 5: Introduction to Scala

Scala IDE

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

• IntelliJIdea :– http://

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

Page 6: Introduction to Scala

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

Page 7: Introduction to Scala

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

Page 8: Introduction to Scala

Popular Frameworks built on Scala

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

Page 9: Introduction to Scala

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

Page 10: Introduction to Scala

How to Install

Page 11: Introduction to Scala

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!

Page 12: Introduction to Scala

Compile and Run

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

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

Page 13: Introduction to Scala

Sample Program

object Test {

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

Page 14: Introduction to Scala

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))}

Page 15: Introduction to Scala

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))}

Page 16: Introduction to Scala

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

}

Page 17: Introduction to Scala

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.

Page 18: Introduction to Scala

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

Page 19: Introduction to Scala

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 - : = =><- <: <% >:# @

Page 20: Introduction to Scala

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:

Page 21: Introduction to Scala

Multi line Strings

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

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

spans threelines."""

Page 22: Introduction to Scala

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

Page 23: Introduction to Scala

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.

Page 24: Introduction to Scala

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.

Page 26: Introduction to Scala

26

Questions ?

Page 27: Introduction to Scala

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/