Top Banner
Presented by Mohana Rao INTRODUCTION
29
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

Presented by Mohana Rao

INTRODUCTION

Page 2: Scala introduction

SCALA - BACKGROUND

Page 3: Scala introduction

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.”

Page 4: Scala introduction

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

Page 5: Scala introduction

Feb 5, 2015:Scala.JS 0.6 released

No longer experimental!

Fast

Great interop with Javascript libraries

SCALA.JS

Page 6: Scala introduction

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

REDMONK RANKING

Page 7: Scala introduction

SCALA JOB TRENDS

Page 8: Scala introduction

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

Page 9: Scala introduction

Statically Typed.

Functional.

Object Oriented.

Expressive.

DSL-friendly

SCALABLE LANGUAGE

Page 10: Scala introduction

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

Page 11: Scala introduction

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

Page 12: Scala introduction

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

Page 13: Scala introduction

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

Page 14: Scala introduction

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

Page 15: Scala introduction

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

Page 16: Scala introduction

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

Page 17: Scala introduction

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

Page 18: Scala introduction

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

Page 19: Scala introduction

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

Page 20: Scala introduction

DSL’S

Page 21: Scala introduction

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

Page 22: Scala introduction

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

Page 23: Scala introduction

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

Page 24: Scala introduction

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

Page 25: Scala introduction

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”

Page 26: Scala introduction

PATTERN MATCHING

Page 27: Scala introduction

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

Page 28: Scala introduction

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

IF I HAD MORE TIME

Page 29: Scala introduction