scala: an oo surprise

114
Scala: An OO Surprise Syropoulos Introduction What is Scala? Object Orientation Scalability Functional Features Parser Builders XML Processing XML Processing Concurrent Program- ming . . . . . . Scala: An OO Surprise Apostolos Syropoulos Xanthi, Greece 4th European Lisp Symposium March 31st - April 1st, 2011 TUHH, Hamburg University of Technology, Hamburg, Germany

Upload: apostolos-syropoulos

Post on 02-Jul-2015

54 views

Category:

Science


2 download

DESCRIPTION

A talk given at the 4th European Lisp Symposium\\March 31st - April 1st, 2011, TUHH, Hamburg University of Technology, Hamburg, Germany.

TRANSCRIPT

Page 1: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala: An OO Surprise

Apostolos SyropoulosXanthi, Greece

4th European Lisp SymposiumMarch 31st - April 1st, 2011

TUHH, Hamburg University of Technology,Hamburg, Germany

Page 2: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Outline

...1 IntroductionWhat is Scala?Object OrientationScalability

...2 Functional Features

...3 Parser Builders

...4 XML Processing

...5 XML Processing

...6 Concurrent Programming

Page 3: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 4: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 5: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 6: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 7: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 8: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 9: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 10: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 11: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 12: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 13: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 14: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A versatile programming language

A purely object-oriented language.

Runs on the Java Virtual Machine.

A scalable language.

Has features found in functional programminglanguages, but it is not a functional langauge!

Allows concurrent programing.

Includes facilities for GUI programming.

Seamless integration of XML.

Provides parser combinators.

Page 15: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Principles of Object Orientation

Abstraction (i.e., a software object is anidealization of a real world object).

Encapsulation (i.e., a data protectionmechanism).

Inheritance (i.e., an object may extend thefunctionality of another object).

Polymorphism (i.e., software modules may havedifferent instances with identical behavior).

Page 16: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Principles of Object Orientation

Abstraction (i.e., a software object is anidealization of a real world object).

Encapsulation (i.e., a data protectionmechanism).

Inheritance (i.e., an object may extend thefunctionality of another object).

Polymorphism (i.e., software modules may havedifferent instances with identical behavior).

Page 17: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Principles of Object Orientation

Abstraction (i.e., a software object is anidealization of a real world object).

Encapsulation (i.e., a data protectionmechanism).

Inheritance (i.e., an object may extend thefunctionality of another object).

Polymorphism (i.e., software modules may havedifferent instances with identical behavior).

Page 18: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Principles of Object Orientation

Abstraction (i.e., a software object is anidealization of a real world object).

Encapsulation (i.e., a data protectionmechanism).

Inheritance (i.e., an object may extend thefunctionality of another object).

Polymorphism (i.e., software modules may havedifferent instances with identical behavior).

Page 19: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Principles of Object Orientation

Abstraction (i.e., a software object is anidealization of a real world object).

Encapsulation (i.e., a data protectionmechanism).

Inheritance (i.e., an object may extend thefunctionality of another object).

Polymorphism (i.e., software modules may havedifferent instances with identical behavior).

Page 20: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 21: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 22: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 23: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 24: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 25: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What makes a programming languagescalable?

Any language that can be extended seamlesslyis scalable.

Gyu L. Steel Jr.: “[A] language design can nolonger be a thing. It must be a pattern–a patternfor growth–a pattern for growing the pattern fordefining the patterns that programmers can usefor their real work and their main goal.”

There two forms of growth: variable vocabularyor variable semantics.

Generic types, operator overloading, anduser-defined light-weight types form the kernelof every scalable language.

Page 26: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A Generic Stack

class Stack [γ] (n: Int) {private var S = new Array[γ](n)private var top = 0;def push(elem: γ) {top = top + 1S(top) = elem}

def pop () : γ = {var oldtop = toptop = top - 1S(oldtop)

}}

Page 27: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A Generic Stack

class Stack [γ] (n: Int) {private var S = new Array[γ](n)private var top = 0;def push(elem: γ) {top = top + 1S(top) = elem}

def pop () : γ = {var oldtop = toptop = top - 1S(oldtop)

}}

Page 28: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

User-Defined Light-Weight ComplexNumbers

class Complex(val re: Double, val im: Double) {def + (x: Complex) =new Complex(re + x.re, im + x.im)

def unary_- = new Complex(-Re, -Im). . . . . . . . . . . . . . . . . .

override def toString = if(re == 0 && im == 0) "0" …}object Complex{

def apply(re: Double)(im: Double) =new Complex(re, im)

}object i extends Complex(0.0, 1.0). . . . . . . . . . . . . . . . . . .var a = 5 + 3.0*i // a complex number!!!

Page 29: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

User-Defined Light-Weight ComplexNumbers

class Complex(val re: Double, val im: Double) {def + (x: Complex) =new Complex(re + x.re, im + x.im)

def unary_- = new Complex(-Re, -Im). . . . . . . . . . . . . . . . . .

override def toString = if(re == 0 && im == 0) "0" …}object Complex{

def apply(re: Double)(im: Double) =new Complex(re, im)

}object i extends Complex(0.0, 1.0). . . . . . . . . . . . . . . . . . .var a = 5 + 3.0*i // a complex number!!!

Page 30: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Call-By-Name or how to define newcommands

def loop(pre: => Unit)(cond: => Boolean)(post: => Unit): Unit = {

preif (!cond) {postloop(pre)(cond)(post)

}else()

}var x=6; var y=0loop {x=x-1; y=y+1

} (x == 0 ) {println(x); println(y)

}

Page 31: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Call-By-Name or how to define newcommands

def loop(pre: => Unit)(cond: => Boolean)(post: => Unit): Unit = {

preif (!cond) {postloop(pre)(cond)(post)

}else()

}var x=6; var y=0loop {x=x-1; y=y+1

} (x == 0 ) {println(x); println(y)

}

Page 32: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 33: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).

Users can define anonymous functions.var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 34: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 35: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 36: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.

var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 37: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 38: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala’s Functional Features

Functions are software modules (objects inScala’s parlance).Users can define anonymous functions.

var inc = (x:Int) => x+1

Supports closures.var mul = (x: Int) => (y:Int) => x*yvar mul3 = mul(3)

Pattern matching.

Page 39: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Data Structures with Class Hierarchies

Haskell binary tree

datatype BinTree = Empty |Node(Int, BinTree, BinTree)

Scala binary tree

abstract class BinTreecase object EmptyTree extends BinTreecase class Node(elem : Int,

left : BinTree,right: BinTree) extends BinTree

Page 40: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Data Structures with Class Hierarchies

Haskell binary tree

datatype BinTree = Empty |Node(Int, BinTree, BinTree)

Scala binary tree

abstract class BinTreecase object EmptyTree extends BinTreecase class Node(elem : Int,

left : BinTree,right: BinTree) extends BinTree

Page 41: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Data Structures with Class Hierarchies

Haskell binary tree

datatype BinTree = Empty |Node(Int, BinTree, BinTree)

Scala binary tree

abstract class BinTreecase object EmptyTree extends BinTreecase class Node(elem : Int,

left : BinTree,right: BinTree) extends BinTree

Page 42: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Data Structures with Class Hierarchies

Haskell binary tree

datatype BinTree = Empty |Node(Int, BinTree, BinTree)

Scala binary tree

abstract class BinTreecase object EmptyTree extends BinTreecase class Node(elem : Int,

left : BinTree,right: BinTree) extends BinTree

Page 43: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Data Structures with Class Hierarchies

Haskell binary tree

datatype BinTree = Empty |Node(Int, BinTree, BinTree)

Scala binary tree

abstract class BinTreecase object EmptyTree extends BinTreecase class Node(elem : Int,

left : BinTree,right: BinTree) extends BinTree

Page 44: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Pattern Matching in Action

Depth of a binary tree

def depth(t: BinTree): Int = {t match {case EmptyTree => 0case Node(_,EmptyTree,r) => 1 + depth(r)case Node(_,l,EmptyTree) => 1 + depth(l)case Node(_,l,r) => Math.max(depth(l),

depth(r)) + 1}

}

Page 45: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Pattern Matching in Action

Depth of a binary tree

def depth(t: BinTree): Int = {t match {case EmptyTree => 0case Node(_,EmptyTree,r) => 1 + depth(r)case Node(_,l,EmptyTree) => 1 + depth(l)case Node(_,l,r) => Math.max(depth(l),

depth(r)) + 1}

}

Page 46: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Pattern Matching in Action

Depth of a binary tree

def depth(t: BinTree): Int = {t match {case EmptyTree => 0case Node(_,EmptyTree,r) => 1 + depth(r)case Node(_,l,EmptyTree) => 1 + depth(l)case Node(_,l,r) => Math.max(depth(l),

depth(r)) + 1}

}

Page 47: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Constructing Parsers with Parser Builders

Tools that can be combined to build real parsers.

A toy parser that accepts binary numerals.

import scala.util.parsing.combinator._class BinDigit extends JavaTokenParsers {def D = "0" | "1"def B = D~rep(D)def parse(text : String) = parseAll(B,text)

}var P = new BinDigitprintln("input : "+args(0))println(P.parse(args(0)))

Page 48: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Constructing Parsers with Parser Builders

Tools that can be combined to build real parsers.

A toy parser that accepts binary numerals.

import scala.util.parsing.combinator._class BinDigit extends JavaTokenParsers {def D = "0" | "1"def B = D~rep(D)def parse(text : String) = parseAll(B,text)

}var P = new BinDigitprintln("input : "+args(0))println(P.parse(args(0)))

Page 49: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Constructing Parsers with Parser Builders

Tools that can be combined to build real parsers.

A toy parser that accepts binary numerals.

import scala.util.parsing.combinator._class BinDigit extends JavaTokenParsers {def D = "0" | "1"def B = D~rep(D)def parse(text : String) = parseAll(B,text)

}var P = new BinDigitprintln("input : "+args(0))println(P.parse(args(0)))

Page 50: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Constructing Parsers with Parser Builders

Tools that can be combined to build real parsers.

A toy parser that accepts binary numerals.

import scala.util.parsing.combinator._class BinDigit extends JavaTokenParsers {def D = "0" | "1"def B = D~rep(D)def parse(text : String) = parseAll(B,text)

}var P = new BinDigitprintln("input : "+args(0))println(P.parse(args(0)))

Page 51: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Variables can be assigned XML content.

var poem =<poem> <title> Magic Everywhere </title><poet realname="yes"> Tom Jones </poet>

<stanza> <verse> magic everywhere </verse></stanza> </poem>

println(poem)

Or they can be assigned XML content stored in files.

val x = XML.loadFile("listdir.html")

Page 52: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Variables can be assigned XML content.

var poem =<poem> <title> Magic Everywhere </title><poet realname="yes"> Tom Jones </poet>

<stanza> <verse> magic everywhere </verse></stanza> </poem>

println(poem)

Or they can be assigned XML content stored in files.

val x = XML.loadFile("listdir.html")

Page 53: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Variables can be assigned XML content.

var poem =<poem> <title> Magic Everywhere </title><poet realname="yes"> Tom Jones </poet>

<stanza> <verse> magic everywhere </verse></stanza> </poem>

println(poem)

Or they can be assigned XML content stored in files.

val x = XML.loadFile("listdir.html")

Page 54: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Variables can be assigned XML content.

var poem =<poem> <title> Magic Everywhere </title><poet realname="yes"> Tom Jones </poet>

<stanza> <verse> magic everywhere </verse></stanza> </poem>

println(poem)

Or they can be assigned XML content stored in files.

val x = XML.loadFile("listdir.html")

Page 55: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Variables can be assigned XML content.

var poem =<poem> <title> Magic Everywhere </title><poet realname="yes"> Tom Jones </poet>

<stanza> <verse> magic everywhere </verse></stanza> </poem>

println(poem)

Or they can be assigned XML content stored in files.

val x = XML.loadFile("listdir.html")

Page 56: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Saving XML content.

var z = <person><name>Blaise</name> <surname>Pascal</surname></person>XML.save("persons.xml",z)

Example: Find all poems that have been publishedafter 1960 and print the title and the poet of eachsuch poem.

for ( val poem <- poems \ "poem" ) {if ( (poem \ "year").text.trim.toInt > 1960 ) {

var poet = (poem \ "poet").textvar title = (poem \ "title").textprintln("\""+title+"\" by "+poet)

}

Page 57: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Saving XML content.

var z = <person><name>Blaise</name> <surname>Pascal</surname></person>XML.save("persons.xml",z)

Example: Find all poems that have been publishedafter 1960 and print the title and the poet of eachsuch poem.

for ( val poem <- poems \ "poem" ) {if ( (poem \ "year").text.trim.toInt > 1960 ) {

var poet = (poem \ "poet").textvar title = (poem \ "title").textprintln("\""+title+"\" by "+poet)

}

Page 58: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Saving XML content.

var z = <person><name>Blaise</name> <surname>Pascal</surname></person>XML.save("persons.xml",z)

Example: Find all poems that have been publishedafter 1960 and print the title and the poet of eachsuch poem.

for ( val poem <- poems \ "poem" ) {if ( (poem \ "year").text.trim.toInt > 1960 ) {

var poet = (poem \ "poet").textvar title = (poem \ "title").textprintln("\""+title+"\" by "+poet)

}

Page 59: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Saving XML content.

var z = <person><name>Blaise</name> <surname>Pascal</surname></person>XML.save("persons.xml",z)

Example: Find all poems that have been publishedafter 1960 and print the title and the poet of eachsuch poem.

for ( val poem <- poems \ "poem" ) {if ( (poem \ "year").text.trim.toInt > 1960 ) {

var poet = (poem \ "poet").textvar title = (poem \ "title").textprintln("\""+title+"\" by "+poet)

}

Page 60: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

XML Processing

Saving XML content.

var z = <person><name>Blaise</name> <surname>Pascal</surname></person>XML.save("persons.xml",z)

Example: Find all poems that have been publishedafter 1960 and print the title and the poet of eachsuch poem.

for ( val poem <- poems \ "poem" ) {if ( (poem \ "year").text.trim.toInt > 1960 ) {

var poet = (poem \ "poet").textvar title = (poem \ "title").textprintln("\""+title+"\" by "+poet)

}

Page 61: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 62: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 63: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 64: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 65: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 66: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Truths

EETimes Europe(http://www.electronics-eetimes.com): “AsMoore’s Law runs out of steam and computinggoes mobile, technologists are searching forways to make the leap to new parallelprogramming frameworks that can leveragelow-power multicore architectures.”

Concurrent programming is not only the nextlogical step in software development, but thenext necessary step.

All modern programming languages mustprovide constructs and libraries that will easethe construction of concurrent programs

Page 67: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 68: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 69: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 70: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 71: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 72: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Scala and Concurrency

Scala programmers can code concurrent structureswith…

threads (i.e., Java’s legacy to Scala)

mailboxes (something like actors)

actors

Page 73: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Creating threaded applications byextending class Thread

class PrintProgressMark(val mark: Char,val delay :Int) extends Thread {

private var i = 0override def run(): Unit =try {

while (i <= 100) {print(mark); i += 1; Thread.sleep(delay);

}} catch { case ex : InterruptedException => return }

}object threadExample {def main(args: Array[String]) {new PrintProgressMark('+', 40).startnew PrintProgressMark('*', 100).start

}}

Page 74: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Creating threaded applications byextending class Thread

class PrintProgressMark(val mark: Char,val delay :Int) extends Thread {

private var i = 0override def run(): Unit =try {

while (i <= 100) {print(mark); i += 1; Thread.sleep(delay);

}} catch { case ex : InterruptedException => return }

}object threadExample {def main(args: Array[String]) {new PrintProgressMark('+', 40).startnew PrintProgressMark('*', 100).start

}}

Page 75: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Creating threaded applications by usingtrait Runnable

class PrintProgressMark2(val mark: Char,val delay :Int) extends Runnable {

private var i = 0override def run(): Unit =try {

while (i <= 100) {print(mark); i += 1; Thread.sleep(delay_)

}} catch { case ex : InterruptedException => return }

}object threadExample2 {def main(args: Array[String]) {var plus = new PrintProgressMark2('+', 40)var ast = new PrintProgressMark2('*', 100)

new Thread(plus).start(); new Thread(ast).start()} }

Page 76: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Creating threaded applications by usingtrait Runnable

class PrintProgressMark2(val mark: Char,val delay :Int) extends Runnable {

private var i = 0override def run(): Unit =try {

while (i <= 100) {print(mark); i += 1; Thread.sleep(delay_)

}} catch { case ex : InterruptedException => return }

}object threadExample2 {def main(args: Array[String]) {var plus = new PrintProgressMark2('+', 40)var ast = new PrintProgressMark2('*', 100)

new Thread(plus).start(); new Thread(ast).start()} }

Page 77: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 78: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 79: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 80: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 81: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 82: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 83: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 84: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 85: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 86: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 87: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 88: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 89: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

What is an actor?

An actor is a computational agent which maps eachincoming communication to a triple consisting of…

a finite set of communications sent to otheractors;

a new behavior that will govern the response tothe next communication processed; and,

a finite set of new actors created.

Thus,

behaviors are history sensitive;

there is no presumed sequentiality in the actionsan actor performs; and,

actor creation is an integral of the computationalmodel.

Page 90: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A first example in actor programming

import scala.actors.Actor._object actorsExample {def main(args: Array[String]) {var PrintProgressPlus = actor {

var i = 0while (i <= max) {print('+'); i += 1; Thread.sleep(40)

} }var PrintProgressTimes = actor {

var i = 0while (i <= max) {print('*'); i += 1; Thread.sleep(100)

} }1 // <--- block must end in result expression,

} } // not in definition

Page 91: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

A first example in actor programming

import scala.actors.Actor._object actorsExample {def main(args: Array[String]) {var PrintProgressPlus = actor {

var i = 0while (i <= max) {print('+'); i += 1; Thread.sleep(40)

} }var PrintProgressTimes = actor {

var i = 0while (i <= max) {print('*'); i += 1; Thread.sleep(100)

} }1 // <--- block must end in result expression,

} } // not in definition

Page 92: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Defining a class that subclasses traitActor

import scala.actors._class PrintProgressMark(val mark: Char,

val delay :Int) extends Actor {private var i = 0def act: Unit = {while (i <= 100) {

print(mark); i += 1; Thread.sleep(delay_)}

}}object actorsExample {def main(args: Array[String]) {new PrintProgressMark('+', 40).startnew PrintProgressMark('*', 100).start

}}

Page 93: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Defining a class that subclasses traitActor

import scala.actors._class PrintProgressMark(val mark: Char,

val delay :Int) extends Actor {private var i = 0def act: Unit = {while (i <= 100) {

print(mark); i += 1; Thread.sleep(delay_)}

}}object actorsExample {def main(args: Array[String]) {new PrintProgressMark('+', 40).startnew PrintProgressMark('*', 100).start

}}

Page 94: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 95: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 96: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 97: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 98: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 99: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Carl Hewitt in “Viewing Cotrol Structures as Patternsof Passing Messages” (Artificial Intelligence 8, 3(1977), 323–364)

“present[s] an approach to modelling intelligence interms of a society of communicatingknowledge-based problem solving experts”;

“each of the experts can be viewed as a society thatcan be further decomposed in the same way until theprimitive actors of the system are reached”;

he was “investigating the nature of thecommunication mechanisms needed for effectiveproblem-solving by a society of experts and theconventions of civilized discourse that make thispossible.”

Page 100: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:

...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 101: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:

...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 102: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:

...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 103: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 104: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 105: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Computing Factorials

Hewitt demonstrates these “principles” byshowing how to compute the factorial functionwith actors:...1 using a resursive procedure, that is, a procedurethat uses replication and

...2 an iterative procudure, that is, a procedure thatrelies on message passing to deliver a result.

Page 106: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Actor Replication

case class Val(a: Actor, n: Int)case class Res(n: Int)class Fact extends Actor {def act =react {

case Val(a,0) => a ! Res(1)case Val(a,n) =>var p = new Factp.startp ! Val(self,n-1)react { case Val(_, m) => a ! Val(a, n*m)

case Res(m) => a ! Val(a, n*m)}}

}

Page 107: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Actor Replication

case class Val(a: Actor, n: Int)case class Res(n: Int)class Fact extends Actor {def act =react {

case Val(a,0) => a ! Res(1)case Val(a,n) =>var p = new Factp.startp ! Val(self,n-1)react { case Val(_, m) => a ! Val(a, n*m)

case Res(m) => a ! Val(a, n*m)}}

}

Page 108: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Using Class Fact

object actorsExample {def main(args: Array[String]) {var factorial = actor {

react {case Val(a,n) =>var q = new Factq.startq ! Val(self,n)react { case Val(_,f) => a ! Val(a,f) }

}}factorial ! Val(self,6)react { case Val(_, n) => println(n) }

}}

Page 109: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Using Class Fact

object actorsExample {def main(args: Array[String]) {var factorial = actor {

react {case Val(a,n) =>var q = new Factq.startq ! Val(self,n)react { case Val(_,f) => a ! Val(a,f) }

}}factorial ! Val(self,6)react { case Val(_, n) => println(n) }

}}

Page 110: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Iterative Actor

case class InitVal(a: Actor, n: Int)case class Val(a: Actor, n: Int, m: Int)object actorsExample {def main(args: Array[String]) {val loopActor = actor {

var ResActor : Actor = nullloop{react {case Val(_,acc,1) =>

ResActor ! acc; exit("done")case Val(a,1,count) =>

ResActor = aself ! Val(self,count,count-1)

case Val(a,acc,count) =>self ! Val(self,acc*count,count-1)

}}

}

Page 111: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Iterative Actor

case class InitVal(a: Actor, n: Int)case class Val(a: Actor, n: Int, m: Int)object actorsExample {def main(args: Array[String]) {val loopActor = actor {

var ResActor : Actor = nullloop{react {case Val(_,acc,1) =>

ResActor ! acc; exit("done")case Val(a,1,count) =>

ResActor = aself ! Val(self,count,count-1)

case Val(a,acc,count) =>self ! Val(self,acc*count,count-1)

}}

}

Page 112: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Using Class Fact

var factorial = actor {react {case InitVal(a,0) => a ! InitVal(a, 1)case InitVal(a,1) => a ! InitVal(a, 1)case InitVal(a,n) =>loopActor ! Val(self,1,n)react { case f: Int => a ! InitVal(a,f) }

}}factorial ! InitVal(self,6)receive { case InitVal(_, n) => println(n) }

}}

Page 113: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

Using Class Fact

var factorial = actor {react {case InitVal(a,0) => a ! InitVal(a, 1)case InitVal(a,1) => a ! InitVal(a, 1)case InitVal(a,n) =>loopActor ! Val(self,1,n)react { case f: Int => a ! InitVal(a,f) }

}}factorial ! InitVal(self,6)receive { case InitVal(_, n) => println(n) }

}}

Page 114: Scala: An OO Surprise

Scala: AnOO Surprise

Syropoulos

IntroductionWhat is Scala?

Object Orientation

Scalability

FunctionalFeatures

ParserBuilders

XMLProcessing

XMLProcessing

ConcurrentProgram-ming

. . . . . .

The end!

Thank you so much for your attention!