scala introduction

29
Presented by Mohana Rao INTRODUCTION

Upload: mohan-rao-pusarla

Post on 14-Aug-2015

98 views

Category:

Software


0 download

TRANSCRIPT

Presented by Mohana Rao

INTRODUCTION

SCALA - BACKGROUND

SCALA - BACKGROUND

Groovy’s Creator , James Strachan wrote on his blog in 2009 about scala

“I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably have never created Groovy.”

Scala

Akka

Play

Slick

Spark

JVM

Kafka

Mesos

Chisel

scalaz

cats

scalding

ScalaCheck

scodec Specssqueryl

Samza

Finagle

ScalaTest

shapelesssbt

Lift

BlueEyesscalatra

JS

ADAM

SCALA ECOSYSTEM

Feb 5, 2015:Scala.JS 0.6 released

No longer experimental!

Fast

Great interop with Javascript libraries

SCALA.JS

Scala ranked as #14 in RedMonk Language Rankings of January 2015

REDMONK RANKING

SCALA JOB TRENDS

Scala – “Scalable Language” : A language which grows with user demands

Designed by Martin Odersky at EPFL

Compiles to java byte code and runs on JVM

Adopted at companies like Twitter, LinkedIn and many

Used in development of well renowned frameworks like Play, Spark, Akka, Kafka, Spray and many

Completed 10+ years. Announced in 2004

SCALA - BACKGROUND

Statically Typed.

Functional.

Object Oriented.

Expressive.

DSL-friendly

SCALABLE LANGUAGE

JAVA int square(int x) {

return x * x;

}

have to define method parameters to be final to have immutability

Integer intVariable = 3

final Integer intConstant = 3.17

SCALA def square(x:Int):Int = {

x * x

}

function/method parameters are by default final

var intVariable = 3

val intConstant = 3.17

IMMUTABILITY

JAVA String companyName = “Synerzip”;

int square(int x) {

return x * x;

}

Integer result = square(2)

SCALA val companyName = “Synerzip”

def square(x:Int) = {

x * x

}

val result = square(2)

or

val result:Int = square(2)

STATICALLY TYPED WITH TYPE INFERENCE

JAVA List<String> cities = new

ArrayList<String>();

cities.add(“Pune”);

cities.add(“Mumbai”);

Map<String,List<Integer>> citiesMap= new

Map<String,List<Integer>();

List<Integer> pincodes = new ArrayList<Integer>();

pincodes.add(411027);

pincodes.add(411038);

citiesMap.put(“Pune”,pincodes);

SCALA

val cities = List(“Pune”,”Mumbai”)

val citiesMap = Map(“Pune” -> List(410027,411038))

STATICALLY TYPED WITH TYPE INFERENCE

JAVA List<String> cities = new

ArrayList<String>();

cities.add(“Pune”);

cities.add(“Mumbai”);

Map<String,List<Integer>> citiesMap= new

Map<String,List<Integer>();

List<Integer> pincodes = new ArrayList<Integer>();

pincodes.add(411027);

pincodes.add(411038);

citiesMap.put(“Pune”,pincodes);

SCALA

val cities: List[String] = List(“Pune”,”Mumbai”)

val citiesMap :Map[String,List[Int]] = Map(“Pune” -> List(411027,411038))

STATICALLY TYPED WITH TYPE INFERENCE

Lambda’s Anonymous functions

(x:Int) = x * 10

Defining Functions def multiplyByTen(x:Int) = x * 10

Functions assigning to variables/Functions as values val addTen:Int=>Int = (x => x + 10)

Clousures Allow closures

val message = “Hello “; def greet(name:String) = message + name + “!”;

FUNCTIONS ARE FIRST-CLASS CITIZENS

High Order Functions

def poly[A,B,C](f:A=>B, g:B=>C) = f andThen g

def composedFunction = poly(multiplyByTen , addTen)

composedFunction(6) results to 70

(addTen compose multiplyByTen)(6) result to 70

HIGH ORDER FUNCTIONS

Default Parameter values def greet(greeting:String = “Hello ” , person:String = “Synerzipian”) = {

println(greeting + “ “ + person)

}

greet() Hello Synerzipian

greet(“Hey”) Hey Synerzipian

greet(“Welcome”, “to Scala”) Welcome to Scala

FUNCTIONAL FUN

Named Parameter values def greet(greeting:String = “Hello ” , person:String = “Synerzipian”) = {

println(greeting + “ “ + person)

}

greet(person=“World”) Hello World

greet(greeting=“Welcome”) Welcome Synerzipian

greet(person=“to Scala”,greeting=“Welcome”) Welcome to Scala

FUNCTIONAL FUN

def factorial(n: Int) = {

def fact(n: Int, acc: Int): Int = {

if (n == 0) acc

else fact(n - 1, n * acc)

}

fact(n, 1)

}

TAIL RECURSION

Call By Name (Lazy Evaluation of parameters) def greet(greeting:String = “Hello ” , person: =>String) = {

println(“Greet Called”);

println(greeting + “ “ + person)

}

def getPerson() = {

println(“Generated Person Name”)

“Synerzipian”

}

greet(person=getPerson())

Greet Called

Generated Person Name

Hello Synerzipian

FUNCTIONAL FUN

DSL’S

var x = 0

object Do {

def apply(body: => Unit) = new DoBody(body)

}

class DoBody(body: => Unit) {

def until(cond: =>Boolean): Unit = {

body;

while(cond)

body

}

}

Do { x = x + 1 } until (x < 10)

DSL’S

JAVA public class Employee {

private String name;

private Integer age;

public Employee(String name, Integer age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setAge(Integer age){

this.age = age;

}

public Integer getAge() {

return age;

}

}

SCALA class Employee(val name:String, var

age:Int)

or

case class Employee(val name:String, var age:Int)

SCALA CLASSES

JAVA public class Employee {

private String name;

private Integer age;

public Employee(String name, Integer age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setAge(Integer age){

this.age = age;

}

public Integer getAge() {

return age;

}

}

SCALA class Employee(val name:String, var

age:Int)

or

case class Employee(val name:String, var age:Int)

SCALA CLASSES

Traits are Stackable

They are fundamental unit for code reuse in Scala

A Trait encapsulates method and field definitions, which can be reused by mixing them in classes

Unlike class inheritance , in which class must inherit from just one

superclass, a class may mix in any number of Traits

Unlike Interfaces they can have concrete methods

TRAITS

t ra i t Language {

val name:Str ing

overr ide def toStr ing = name

}

t ra i t JVM {

overr ide def toStr ing = super. toStr ing + “ runs on JVM”

}

t ra i t Stat ic {

overr ide def toStr ing = super. toStr ing + “ is stat ic”

}

class Scala extends Language wi th JVM with Stat ic {

val name = “Scala”

}

pr int ln(new Scala()) “Scala runs on JVM is stat ic”

PATTERN MATCHING

All default Scala collections are immutableEvery collection type has lot of higher order functions

val xs = List(1 to 10)

xs.filter(x => x%2 ==0)

xs.map(_*2)

COLLECTIONS

For ComprehensionsFuturesImplicitStructural TypingCurryingPartially Applied FunctionsCovariant/Contravariant/Nonvariant Type ParamatersAnd much more…

IF I HAD MORE TIME