Top Banner
Introduction to Scala Suraj Atreya [email protected] @suraj2585
21

Scala

Nov 15, 2014

Download

Technology

suraj_atreya

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scala

Introduction to Scala

Suraj [email protected]

@suraj2585

Page 2: Scala

First bits

• Scalable language pronounced as “Sca-lah”

• Developed by Martin Odersky in 2001

• He is also famous for developing javac

• Currently owns his own company called Typesafe

Page 3: Scala

Motivation

Java Code

public class DemoAdd {

public static void main(String[] args) {

int sum = 0;

for(int i = 1; i <= 1000; ++i){sum = sum + i;

} System.out.println(sum); }}

Scala Code

object DemoAdd {

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

println((1 to 1000).reduceLeft( _ + _ ))

}}

Page 4: Scala

Scala Code

object DemoList {

def main(args:Array[String]):Unit = { val ls = List("One", "Two", "Three", "Four") for(xs <- ls) println(xs) }}

Java Code

public class DemoList {

public static void main(String[] args) {

List<String> ls = new ArrayList<String>();

ls.add(“One");ls.add(“Two");ls.add(“Three");

Iterator it = ls.iterator();

while(it.hasNext()){ String value=(String)it.next();

System.out.println(value); } }}

Motivation

Page 5: Scala

Motivation

• Code size is much smaller compared to Java• Less verbose• Functional Programming(FP) + Object oriented• Statically typed• JVM based• .NET (CLR) support• Scalable – from small to large distributed

systems

Page 6: Scala

Motivation

• Seamless integration with Java• Hot language • Facebook, Twitter, LinkedIn, Foursquare

Page 7: Scala

IDE

• http://www.scala-lang.org/• Eclipse plugin (http://www.eclipse.org)• IntelliJ (http://www.jetbrains.com/idea/)

Page 8: Scala

Classes and Objects

class DemoClass {

var name:String = "" }

object Demo{ def main (args:Array[String]):Unit = { val hello = new DemoClass hello name = "Hello" println(hello name) }}

Page 9: Scala

Classes and Objectsclass Person(age: Int, name: String) { override def toString = age + " " + name}

object DemoConstr {

def main(args: Array[String]): Unit = { val person = new Person(10, "John") println(person) }}

public class Person{

private int m_age = 0; private String m_name = "";

Person(int age, String name){ m_age = age; m_name = name; }

public static void main(String[] args) {

Person person = new Person(10, "John"); System.out.println(person.m_age + " " +

person.m_name); }}

Page 10: Scala

Functional Programming (FP)

• Learning FP is challenging• Functions are first-class citizens• In FP, constraint is “no state” and “immutable”– Other words FP does not allow “side effects”– Reassigning a variable– Modifying DS in place

• FP allows programming using “pure functions”

Page 11: Scala

FP Pure Functions

val x = "Hello, World"x: java.lang.String = Hello, World

val r1 = x.reverser1: String = dlroW ,olleH

val r2 = x.reverser2: String = dlroW ,olleH

Substitute “x” with its value scala> val r1 = "Hello, World".reverse

r1: String = dlroW ,olleHval r2 = "Hello, World".reverse

r2: String = dlroW ,olleH

x is referentially transparent

After substitution, the outputremains same – “no side effects”

Page 12: Scala

FP Pure Functions

val x = new StringBuilder("Hello")x: java.lang.StringBuilder = Hello

val y = x.append(", World")y: java.lang.StringBuilder = Hello, World

val r1 = y.toStringr1: java.lang.String = Hello, World

val r2 = y.toStringr2: java.lang.String = Hello, World

Page 13: Scala

FP Pure Functions

val x = new StringBuilder("Hello")x: java.lang.StringBuilder = Hello

val r1 = x.append(", World").toStringr1: java.lang.String = Hello, World

val r2 = x.append(", World").toStringr2: java.lang.String = Hello, World, World

x is not referentially transparent

After substitution, the outputIs different– “side effects”

Page 14: Scala

FP

object NumOfElems{

def length[A](ls: List[A]): Int = ls match { case Nil => 0 case _ :: tail => 1 + length(tail) case _ => throw new NoSuchElementException }

def main(args: Array[String]): Unit = { println(List(1, 2, 4, 5, 98, 23, 53).length) println(length(List(1, 2, 4, 5, 98, 23, 53))) }

}

Page 15: Scala

FP 1 + length(List(2, 4, 5, 98, 23, 53))

1 + (1 + length(List(4, 5, 98, 23, 53)))

1 + (1 + (1 + length(List(5, 98, 23, 53))))

1 + (1 + (1 + (1 + length(List(98, 23, 53)))))

1 + (1 + (1 + (1 + (1 + length(List(23, 53))))))

1 + (1 + (1 + (1 + (1 + (1 + length(List(53)))))))

1 + (1 + (1 + (1 + (1 + (1 + (1 + length(List())))))))

1 + (1 + (1 + (1 + (1 + (1 + (1 + 0)))))))

1 + (1 + (1 + (1 + (1 + (1 + 1)))))

1 + (1 + (1 + (1 + (1 + 2))))

1 + (1 + (1 + (1 + 3)))

1 + (1 + (1 + 4))

1 + (1 + 5)

1 + 6

7

Page 16: Scala

FP• This works for small lists, but what if you have a very large list ?!

• Don’t give up on “Recursion”, we have a solution for that

• Remember, “Recursion is the bread and butter of FP”

Page 17: Scala

FP - Tail call optimisation

def lengthTR[A](ls: List[A]):Int = { def loop(ls:List[A], acc:Int):Int = ls match{ case Nil => acc case _ :: tail => loop(tail, acc + 1) } loop(ls,0) }

def length[A](ls: List[A]): Int = ls match { case Nil => 0 case _ :: tail => 1 + length(tail) case _ => throw new NoSuchElementException }

Page 18: Scala

FP - Tail call optimisationloop(List(2, 4, 5, 98, 23, 53), 0 + 1)

loop(List(4, 5, 98, 23, 53), 1 + 1)

loop(List(5, 98, 23, 53), 2 + 1)

loop(List(98, 23, 53), 3 + 1)

loop(List(23, 53), 4 + 1)

loop(List(53), 5 + 1)

loop(List(), 6 + 1)

7

def lengthTR[A](ls: List[A]):Int = { def loop(ls:List[A], acc:Int):Int = ls match{ case Nil => acc case _ :: tail => loop(tail, acc + 1) } loop(ls,0) }

Page 19: Scala

Pattern Matchingobject DemoRegEx {

def main(args:Array[String]):Unit = { val Date = """(\d\d)/(\d\d)/(\d\d\d\d)""".r val date = "01/02/2013" val Date(day, month, year) = date println(day) println(month) println(year) } }

Page 20: Scala

Final thoughts

• Scala is great for general purpose computing• Scales well and has an elegant syntax• Neat integration with Java• Great RegEx support• Actors replaces Threads in Java for

concurrency

Page 21: Scala

E0F