Вы находитесь на странице: 1из 50

Scala

Scala
Scala
Scala
Scala
Scala

Scala

Epam Systems.

, 2011

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala
lazy
view

Scala
implicit

Scala

Scala
traits & mixins

Scala
Scala Fork/Join


Scala

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view


-
,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view


-
,

case class Employee(id: Int, name: String, managerId: Int) {


lazy val manager: Employee = Db.get(managerId)
lazy val team: List [ Employee] = Db.team(id)
}

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

view

view
view

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

view

view
view

,
,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

view

view
view

,
,


Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

collection . view.map((_210).toString) // Lets discuss this computation


// If it is distributed amond multiple function you could have
collection . map(_2).map(_10).map(_.toString) // Two hidden temporary collections
// This one is distributed , but works efficiently
collection . view.map(_2).map(_10).map(_.toString).force

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

// The eager one


collection . map(_10).filter(_>73).map(_.toString)
// The lazy one
collection . view.map(_10).filter(_>73).map(_.toString)

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

// The eager one


collection . map(_10).filter(_>73).map(_.toString)
// The lazy one
collection . view.map(_10).filter(_>73).map(_.toString)

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

// The eager one


collection . map(_10).filter(_>73).map(_.toString)
// The lazy one
collection . view.map(_10).filter(_>73).map(_.toString)







,
...

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

...

,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

...

,
?

take(2) -

Scala

Scala
Scala
Scala
Scala
Scala
Scala

lazy
view

...

,
?

take(2) -


. .

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

Scala - implicits

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

implicit view (implicit )


,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

implicit view (implicit )


,

Scala ?
implicit (scope)

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

implicit view (implicit )


,

Scala ?
implicit (scope)



implicit

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

implicit view (implicit )


,

Scala ?
implicit (scope)



implicit

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

class SayMe(s:String){
def say = println(s)
}
implicit def str2sayer (value:String) = new SayMe(value)
"hello".say

Scala

Scala
Scala
Scala
Scala
Scala
Scala

implicit

class SayMe(s:String){
def say = println(s)
}
implicit def str2sayer (value:String) = new SayMe(value)
"hello".say

class Value{
var i : Int=0
def inc{
i +=1
}
}
class Adder(value:Value){
def add(other:Int){
for ( i <0 until other)
value. inc
}
def +(other:Int) = add(other)
}
implicit def value2adder(value:Value) = new Adder(value)
var v = new Value
v.add(10)
v + 10

Scala

Scala
Scala
Scala
Scala
Scala
Scala


message passing (Command pattern)

-
- enum
== -

Scala

Scala
Scala
Scala
Scala
Scala
Scala


message passing (Command pattern)

-
- enum
== -

mySymbolName
Symbol

mySymbolName == mySymbolName

Scala

Scala
Scala
Scala
Scala
Scala
Scala


message passing (Command pattern)

-
- enum
== -

mySymbolName
Symbol

mySymbolName == mySymbolName

- stop, stop

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait



Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait




traits
i trait -
interface

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait




traits
i trait -
interface

?
!

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait




traits
i trait -
interface

?
!
Scala diamond-problem
,
...

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait
trait MyOddEven{
def isOdd:Boolean
def isEven = !isOdd
}
case class Number(i:Int){
def isOdd = i%2!=0
}
case class SuperNumber(i:Int) extends Number(i) with MyOddEven // mixin class
var a = new Number(10) with MyOddEven // mixin when creating the object
var b = new SuperNumber(10)
println (a.isEven+" "+b.isEven)



,
isOdd, isEven

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait
trait MyOddEven{
def isOdd:Boolean
def isEven = !isOdd
}
case class Number(i:Int){
def isOdd = i%2!=0
}
case class SuperNumber(i:Int) extends Number(i) with MyOddEven // mixin class
var a = new Number(10) with MyOddEven // mixin when creating the object
var b = new SuperNumber(10)
println (a.isEven+" "+b.isEven)



,
isOdd, isEven
mixins

Scala

Scala
Scala
Scala
Scala
Scala
Scala

traits & mixins

trait Greeter{
def hello {}
}
class NamedGreeter(s:String) extends Greeter{
override def hello = println("Hello, "+s)
}
class WelcomeGreeter(s:String) extends NamedGreeter(s){
override def hello = println("Welcome, "+s)
}
trait LoggedGreeter extends Greeter{
override def hello {
println ("About to greet ... ")
super. hello
println ("Done. And dont you use AOP instrumentation ever")
}
}
// mixin
class LoggedWelcomeGreeter(s:String) extends WelcomeGreeter(s:String) with LoggedGreeter
def main(args: Array[String]) : Unit = {
val a = new LoggedWelcomeGreeter("User!")
val b = new WelcomeGreeter("User!") with LoggedGreeter // mixin
a. hello
b. hello
}

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join


Scala Fork/Join

,
Scala
Reasonable defaults -

-
,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join



spawn

spawn{
println ("Hey, I am in another thread");
}
println ("I am still here")

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join



spawn

spawn{
println ("Hey, I am in another thread");
}
println ("I am still here")

future

var a = future{ 10 20 }
var b = future{ 12 99 }
a.get b.get

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join



spawn

spawn{
println ("Hey, I am in another thread");
}
println ("I am still here")

future

var a = future{ 10 20 }
var b = future{ 12 99 }
a.get b.get

replicate
start end
replicate (10,100){x=>
println ("Hello" + x)
}

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join


par

par
,

map, forAll, count -
foldLeft - by design

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join


foldLeft - ?

foldLeft -



- aggregate

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Scala Fork/Join


foldLeft - ?

foldLeft -



- aggregate

-

,


aggregate

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Actor -

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Actor -


,

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Actor -


,


Scala

Scala
Scala
Scala
Scala
Scala
Scala

PROFIT???

- 10000 JVM?

Scala

Scala
Scala
Scala
Scala
Scala
Scala

PROFIT???

- 10000 JVM?
EIP

Scala

Scala
Scala
Scala
Scala
Scala
Scala

PROFIT???

- 10000 JVM?
EIP
Scala Fork/Join

Scala

Scala
Scala
Scala
Scala
Scala
Scala

PROFIT???

- 10000 JVM?
EIP
Scala Fork/Join
. ?

Scala

Scala
Scala
Scala
Scala
Scala
Scala

PROFIT???

- 10000 JVM?
EIP
Scala Fork/Join
. ?
Shared nothing architecture

Scala

Scala
Scala
Scala
Scala
Scala
Scala

Shared nothing architecture

Scala

Scala
Scala
Scala
Scala
Scala
Scala

class Ping extends Actor{


var pong:Pong = _

class Pong extends Actor{


var ping:Ping = _

def act{
loop{
react{
case msg:String=>{
println ("Ping "+msg)
pong ! msg
}
case stop => exit
}
}
}

def act{
loop{
react{
case msg:String=>{
println ("Pong "+msg)
ping ! msg
}
case stop => exit
}
}
}

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


var ping = new Ping
var pong = new Pong
ping.pong = pong;
pong.ping = ping;
ping. start ; pong.start
ping ! "Ball"
Thread.sleep(1000)
ping ! stop
pong ! stop
}

Scala