Introduction to Scala

35

Transcript of Introduction to Scala

Page 1: Introduction to Scala

JDevDay - Introduction to Scala

Saleem Ansari

10th March 2013

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 1 / 35

Page 2: Introduction to Scala

Outline

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 2 / 35

Page 3: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 3 / 35

Page 4: Introduction to Scala

What is Scala?

What is Scala? The name Scala stands for �scalable language�. Thelanguage is so named because it was designed to grow with thedemands of its users.

What makes Scala a Scalable Language ?

Statically typed language with OOP + FP both.Fast and expressive, and peasant to use.Excellent type inference.Runs on JVM.

Whats more?

Scala is compatible with JavaScala is conciseScala is high levelScala is statically typed ( vs dynamically typed )

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 4 / 35

Page 5: Introduction to Scala

What can you do with Scala today?

Write web applications

Play Web Framework http://www.playframework.com/Lift Web Framework http://liftweb.net/

Write code that scales to huge amounts of data

Spark Project http://spark-project.org/Scalding https://github.com/twitter/scalding

Process huge number of concurrent tasks

Akka http://akka.io/

Natural Language Processing and Machine Learning

ScalaNLP http://www.scalanlp.org/

And anything could do in Java, now more concisely :-)

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 5 / 35

Page 6: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 6 / 35

Page 7: Introduction to Scala

Install the Scala Complier

yum install scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 7 / 35

Page 8: Introduction to Scala

Install your faviourite Editor / IDE

yum install emacs

OR install Eclipse ( ScalaIDE )

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 8 / 35

Page 9: Introduction to Scala

Scala compiler is

scalac ( just like javac command )

scala ( just like java command )

fsc ( fast scala compiler )

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 9 / 35

Page 10: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 10 / 35

Page 11: Introduction to Scala

Hello Scala World

helloworld.scala

object HelloWorld {

def main(args: Array[String]) = {

println("Hello Scala World!")

}

}

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 11 / 35

Page 12: Introduction to Scala

Compile and run Hello Scala World

Output

$ scalac helloworld.scala

$ ls

helloworld.scala

HelloWorld.class

HelloWorld$.class

$ scala HelloWorld

Hello Scala World!

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 12 / 35

Page 13: Introduction to Scala

Values and Variables

An example in Ruby ( or maybe Python ) a dynamically typedlanguage

counter = Counter.newcounter = AtomicCounter.newcounter = File.new # this works here!

Scala's static type system, avoids runtime overhead of dynamictypes. The method dispatch is fast in a statically typedlanguage.

var counter = new Counter()counter = new AtomicCounter() // this has to be a Countercounter = new File() // this doesn't work in Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 13 / 35

Page 14: Introduction to Scala

Data Types

Almost everything is same as Java

Basic Data Types: ( all integers are signed two's complement )

Integers: Byte (8bit), Short (16bit), Int (32bit), Long (64bit)Char (16 bit unicode character), String (squence of Chars)Reals: Float (32bit), Double (64bit)Boolean: true / false

Literal

basic data types i.e. 1, 0.123, 12L, `a', �String�symbol literal: `identi�er

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 14 / 35

Page 15: Introduction to Scala

More Concepts

Data Containers

ArrayListSetMapTuple

Programming Abstraction Tools

ClassObjectScala AppPackage

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 15 / 35

Page 16: Introduction to Scala

Expressions

Every thing is an expression

Basic expression: 1+2An assignment is an expressionA function is an expression

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 16 / 35

Page 17: Introduction to Scala

Control Constructs

if-else

while

do-while

for

match-case

try-catch-�nally

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 17 / 35

Page 18: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 18 / 35

Page 19: Introduction to Scala

Matematical Logic

Lambda Calculus ( seeWikipedia )

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 19 / 35

Page 20: Introduction to Scala

Factorial Function

Expressed as mathematical logic

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 20 / 35

Page 21: Introduction to Scala

FP is guided by two main ideas:

Functions are �st-class values

Functions have no side e�ects i.e. they can be replaced withtheir values

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 21 / 35

Page 22: Introduction to Scala

Hallmarks of Functional Programming

mapping

�ltering

folding

reducing

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 22 / 35

Page 23: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 23 / 35

Page 24: Introduction to Scala

Object Oriented

Decompose the problem into entities and interactions amongentities

Each entity and their interaction is represented usingclass/object

internal state is the member variablesinteractions are the member functions

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 24 / 35

Page 25: Introduction to Scala

Functions

factorial.scala

def factorial(n:Int): Int =

if(n<=0) 1 else n*factorial(n-1)

Placeholder syntax

Partially applied functions

Closures

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 25 / 35

Page 26: Introduction to Scala

Traits

traits.scala

trait PartTime {

// trait definition

}

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 26 / 35

Page 27: Introduction to Scala

Classes

classes.scala

class Employee(name: String, age: Int) {

override def toString = name + ", " + age

}

class Supervisor(name: String, age: Int

) extends Employee(name, age) with PartTime

{

override def toString = name + ", " + age

}

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 27 / 35

Page 28: Introduction to Scala

Objects

objects.scala

object Employee {

override def toString = name + ", " + age

}

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 28 / 35

Page 29: Introduction to Scala

Packages

package-example.scala

package in.tuxdna.scala

class Employee(name: String, age: Int) {

override def toString = name + ", " + age

}

object Main extends App {

val emp1 = new Employee("Tom", 21)

println("Employee 1: "+emp1)

}

// $ scalac pacakge-example.scala

// Employee 1: Tom, 21

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 29 / 35

Page 30: Introduction to Scala

Features to be convered later

XML Processing

Actors

Case Classes

Properties

Extistential Types

Implicits

Lazy Evaluation

Parser Combinations

Monads

Annotations

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 30 / 35

Page 31: Introduction to Scala

Using Scala as a scripting language

scala scriptname.scala

employee.scala

class Employee(name: String, age: Int) {

override def toString = name + ", " + age

}

val emp1 = new Employee("Tom", 21)

println("Employee 1: "+emp1)

// $ scala employee.scala

// Employee 1: Tom, 21

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 31 / 35

Page 32: Introduction to Scala

Topic

1 Introduction to Scala

2 Setting up Scala

3 Absolute Scala basics

4 Functional Paradigm in Scala

5 Object Oriented Programming in Scala

6 Where to learn more Scala

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 32 / 35

Page 33: Introduction to Scala

Books

Scala for the Impatient (free) http://blog.typesafe.com/free-pdf-from-typesafe-scala-for-the-impatien-64715

Programming Scala (free)http://ofps.oreilly.com/titles/9780596155957

Programming in Scala 2nd Ed.

http://www.amazon.com/Programming-Scala-Comprehensive-Step-Step/dp/0981531644

Functional Programming Principles in Scala ( free onlinecourse )

https://www.coursera.org/course/progfun

Blogs

Forums

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 33 / 35

Page 34: Introduction to Scala

Questions?

tuxdna (at) gmail (dot) com

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 34 / 35

Page 35: Introduction to Scala

Thank you!

twitter.com/tuxdna

Saleem Ansari () JDevDay - Introduction to Scala 10th March 2013 35 / 35