scala programming language - uni-bielefeld.degsauthof/docs/scala.pdf · scala programming language...

37
Scala Programming Language Georg Sauthoff Introduction Features Conclusion Scala Programming Language Georg Sauthoff [email protected] Universität Bielefeld AG Praktische Informatik June 1, 2010

Upload: duongque

Post on 14-Aug-2018

257 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Scala Programming Language

Georg [email protected]

Universität BielefeldAG Praktische Informatik

June 1, 2010

Page 2: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Scala

Multi-paradigm general purpose programming language byMartin Oderskysince 2003

functionalobject orientedimperative

eager evaluation (lazy annotations available)type inference (in a limited fashion)rich operator overloadingfor the JVMintegrates well with Java modules

Page 3: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Example

1 o b j e c t Fac1 {2 de f f a c ( i : B i g I n t ) : B i g I n t = i match {3 case _ i f i == 1 => i4 case _ => i ∗ f a c ( i − 1)5 }6 de f main ( a r g s : Ar ray [ S t r i n g ] ) =7 p r i n t l n ( f a c ( I n t e g e r p a r s e I n t ( a r g s ( 0 ) ) ) )8 }

9 > module Main10 > where11 > import System ( getArgs )12

13 > f a c : : I n tege r −> I n tege r14 > f a c 1 = 115 > f a c i = i ∗ f a c ( i −1)16

17 > main = do18 > ( x :_) <− getArgs19 > putStrLn $ show $ f a c ( ( read x ) : : I n tege r )

Page 4: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Example

1 o b j e c t Fac1 {2 de f f a c ( i : B i g I n t ) : B i g I n t = i match {3 case _ i f i == 1 => i4 case _ => i ∗ f a c ( i − 1)5 }6 de f main ( a r g s : Ar ray [ S t r i n g ] ) =7 p r i n t l n ( f a c ( I n t e g e r p a r s e I n t ( a r g s ( 0 ) ) ) )8 }9 > module Main10 > where11 > import System ( getArgs )12

13 > f a c : : I n tege r −> I n tege r14 > f a c 1 = 115 > f a c i = i ∗ f a c ( i −1)16

17 > main = do18 > ( x :_) <− getArgs19 > putStrLn $ show $ f a c ( ( read x ) : : I n tege r )

Page 5: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Syntax

No offside rule;{}. in some contexts optionaltype declarations optional if type inference is possible(limited)

Page 6: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Lambda functions

1 o b j e c t Reduce {2 de f mult ( l : Ar ray [ I n t ] ) : I n t =3 l r e d u c e L e f t { _ ∗ _ }4 // l . r e d u c e L e f t ( ( i , j ) => i ∗ j )5

6 de f main ( a r g s : Ar ray [ S t r i n g ] ) =7 p r i n t l n ( mult ( a r g s map { I n t e g e r p a r s e I n t _ } ) )8 }

9 > module Main10 > where11

12 > import System ( getArgs )13

14 > mult = f o l d l (∗ ) 115

16 > main = do17 > l <− getArgs18 > putStrLn $ show $ mult $ map (\ x −> ( read x ) : : I n tege r ) l

Page 7: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Lambda functions

1 o b j e c t Reduce {2 de f mult ( l : Ar ray [ I n t ] ) : I n t =3 l r e d u c e L e f t { _ ∗ _ }4 // l . r e d u c e L e f t ( ( i , j ) => i ∗ j )5

6 de f main ( a r g s : Ar ray [ S t r i n g ] ) =7 p r i n t l n ( mult ( a r g s map { I n t e g e r p a r s e I n t _ } ) )8 }9 > module Main10 > where11

12 > import System ( getArgs )13

14 > mult = f o l d l (∗ ) 115

16 > main = do17 > l <− getArgs18 > putStrLn $ show $ mult $ map (\ x −> ( read x ) : : I n tege r ) l

Page 8: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Lambda functions

reduce, foldr, map etc. are all member functions ofcollections like array, list ...in comparison to Haskell or C++ STL not very orthogonalhowever, they are implemented only once in the traitIterable

Page 9: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Example

1 > module Main2 > where3 > import System4

5 > sep = f o l d r (\ b a −> i f b == ’ , ’ then [ ] : a6 e l s e ( b : ( head a ) ) : t a i l a ) [ [ ] ]7

8 > main = do9 > ( a : [ ] ) <− getArgs10 > putStrLn $ show $ sep a

11 i n t main ( i n t argc , char ∗∗ a rgv )12 {13 vec to r <s t r i n g > v ( 1 ) ;14 f o r e a c h ( char c , a rgv [ 1 ] )15 i f ( c == ’ , ’ ) v . r e s i z e ( v . s i z e ( )+1) ;16 e l s e v . back ( ) . push_back ( c ) ;17 f o r e a c h ( s t r i n g &s , v ) cout << s << ’ ’ ;18 cout << ’ \n ’ ; re tu rn 0 ;19 }

Page 10: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Example

1 > module Main2 > where3 > import System4

5 > sep = f o l d r (\ b a −> i f b == ’ , ’ then [ ] : a6 e l s e ( b : ( head a ) ) : t a i l a ) [ [ ] ]7

8 > main = do9 > ( a : [ ] ) <− getArgs10 > putStrLn $ show $ sep a11 i n t main ( i n t argc , char ∗∗ a rgv )12 {13 vec to r <s t r i n g > v ( 1 ) ;14 f o r e a c h ( char c , a rgv [ 1 ] )15 i f ( c == ’ , ’ ) v . r e s i z e ( v . s i z e ( )+1) ;16 e l s e v . back ( ) . push_back ( c ) ;17 f o r e a c h ( s t r i n g &s , v ) cout << s << ’ ’ ;18 cout << ’ \n ’ ; re tu rn 0 ;19 }

Page 11: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Example

1 o b j e c t Sep {2

3 // S i g n a t u r e ( Cur ry s yn tax ) :4 // f o l d R i g h t : (B) ( ( In t , B) => B)B5

6 de f sep ( s : S t r i n g ) : L i s t [ S t r i n g ] =7 s . f o l d R i g h t ( L i s t ( new S t r i n g ( " " ) ) )8 { (b , a ) => i f ( b == ’ , ’ ) ( new S t r i n g ) : : a9 e l s e ( b + ( a head ) ) : : ( a t a i l ) }10

11 de f main ( a r g s : Ar ray [ S t r i n g ] ) =12 p r i n t l n ( sep ( a r g s ( 0 ) ) )13

14 }

Page 12: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Compile times

Thinkpad X200, excluding pre-heating runs, in seconds

ghc 6.10.4 -O2 scalac 2.7.5

Fac1 0.6 2Sep 0.4 2.1

Page 13: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

OOP

no multiple inheritancebut, traitsGenerics

Page 14: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Traits

don’t confuse with C++ traitslike interfaces, but may contain function bodiesno constructorsto implement unrelated/orthogonal aspectscan extend classes, traitsmay contain stateone class may ’implement’/’extend’ several traits . . .

Page 15: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Trait Example

1 t r a i t ChangeL i s t ene r { de f change }2 c l a s s Widget { }3 c l a s s Button extends Widget with ChangeL i s t ene r {4 de f change = p r i n t l n ( " changed ! " )5 }6 t r a i t ChangeSender {7 p r i v a t e va r l = L i s t [ ChangeL i s t ene r ] ( )8 de f addChangeL i s t ene r ( a : ChangeL i s t ene r ) : Un i t =9 l : := a10 de f emitChanges ( ) = l f o r e a c h ( f => f . change )11 }12 c l a s s Base { }13 c l a s s Pane l extends Base with ChangeSender { }14 o b j e c t T r a i t {15 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {16 va r p = new Pane l ; va r b = new Button17 p . addChangeL i s t ene r ( b )18 p . emitChanges19 }20 }

Page 16: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

C++ Trait

1 #i nc l ude <ios t r eam >2

3 t emp la t e <typename T>4 s t r u c t Number {5 enum { X = 0 } ;6 } ;7

8 t emp la t e <> s t r u c t Number<i n t > { enum { X = 23 } ; } ;9

10 t emp la t e <typename T> s t r u c t Number<T∗> { enum { X = 42 } ; } ;11

12 t emp la t e <typename T>13 vo id p r i n t ( const T &t ) { s td : : cout << Number<T>: :X << ’ \n ’ ; }14

15 i n t main ( ) {16 i n t ∗ i ; p r i n t ( i ) ;17 double a ; p r i n t ( a ) ;18 re tu rn 0 ;19 }

Page 17: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Verbatim XML support as language feature

1 o b j e c t Xml {2

3 v a l x = " bar "4

5 v a l doc = <xml><i s ><b loa ted >6 <e n t r y date="1">foo </ent ry >7 <e n t r y date="2">{ x }</ent ry >8 </b loa ted ></i s ></xml>9

10 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {11 p r i n t l n ( doc )12 v a l e s = doc \\ " e n t r y "13 p r i n t l n ( e s )14 v a l ds = doc \\ " e n t r y " f i n d { ( x ) =>15 ( x . a t t r i b u t e ( " date " ) != None16 && x . a t t r i b u t e ( " date " ) . ge t == "1" ) }17 p r i n t l n ( ds )18 }19 }

Page 18: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Case classes

1 a b s t r a c t c l a s s Tree2

3 case c l a s s Lea f ( s : S t r i n g ) extends Tree4 case c l a s s Br ( l : Tree , r : Tree ) extends Tree5

6 o b j e c t Case {7

8 de f depth ( a : Tree ) : I n t =9 a match {10 case Lea f ( s ) => 111 case Br ( l , r ) =>12 1 + ( Math max( depth ( l ) , depth ( r ) ) )13 }14

15 de f main ( a r g s : Ar ray [ S t r i n g ] ) =16 p r i n t l n ( depth (17 Br ( Br ( Lea f ( " a " ) , Br ( Lea f ( "b" ) ,18 Lea f ( " c " ) ) ) , Lea f ( "d" ) ) ) )19 }

Page 19: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Generics

1 a b s t r a c t c l a s s Tree [T]2 case c l a s s Lea f [T ] ( s : T) extends Tree [T]3 case c l a s s Br [T ] ( l : Tree [T] , r : Tree [T ] ) extends Tree [T]4

5 c l a s s He lpe r [T] {6 de f depth ( a : Tree [T ] ) : I n t =7 a match {8 case Lea f ( s ) => 19 case Br ( l , r ) =>10 1 + ( Math max( depth ( l ) , depth ( r ) ) )11 }12 }13 o b j e c t Gen {14 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {15 v a l h = new He lpe r [ S t r i n g ]16 p r i n t l n ( h . depth (17 Br ( Br ( Lea f ( " a " ) , Br ( Lea f ( "b" ) ,18 Lea f ( " c " ) ) ) , Lea f ( "d" ) ) ) )19 }20 }

Page 20: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Inheritance vs. Pattern Matching

1 a b s t r a c t c l a s s Tree { de f depth : I n t }2

3 case c l a s s Lea f ( s : S t r i n g ) extends Tree {4 de f depth : I n t = 15 }6 case c l a s s Br ( l : Tree , r : Tree ) extends Tree {7 de f depth : I n t = 1 + ( Math max( l . depth , r . depth ) )8 }9

10 o b j e c t Depth {11

12 de f main ( a r g s : Ar ray [ S t r i n g ] ) =13 p r i n t l n ( (14 Br ( Br ( Lea f ( " a " ) , Br ( Lea f ( "b" ) ,15 Lea f ( " c " ) ) ) , Lea f ( "d" ) ) )16 . depth )17 }

Page 21: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Companion Classes

1 a b s t r a c t c l a s s Tree { de f depth : I n t }2

3 c l a s s Lea f ( s : S t r i n g ) extends Tree {4 de f depth : I n t = 15 }6 o b j e c t Lea f { de f app l y ( s : S t r i n g ) = new Lea f ( s ) }7 c l a s s Br ( l : Tree , r : Tree ) extends Tree {8 de f depth : I n t = 1 + ( Math max( l . depth , r . depth ) )9 }10 o b j e c t Br { de f app l y ( l : Tree , r : Tree ) = new Br ( l , r ) }11

12 o b j e c t Depth2 {13

14 de f main ( a r g s : Ar ray [ S t r i n g ] ) =15 p r i n t l n ( (16 Br ( Br ( Lea f ( " a " ) , Br ( Lea f ( "b" ) ,17 Lea f ( " c " ) ) ) , Lea f ( "d" ) ) )18 . depth )19 }

Page 22: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Companion Classes and Pattern Matching

1 o b j e c t Even { de f unapp ly ( x : I n t ) : Boolean = x % 2 == 0 }2 o b j e c t Odd { de f unapp ly ( x : I n t ) : Boolean = x % 2 != 0 }3 o b j e c t Nega t i v e {4 de f unapp ly ( x : I n t ) : Opt ion [ ( I n t , I n t ) ] =5 i f ( x<0) Some (42 , 23) e l s e None6 }7

8 o b j e c t Companion {9

10 de f main ( a r g s : Ar ray [ S t r i n g ] ) =11 {12 v a l x : I n t = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )13 x match {14 case Negat i v e ( a , b ) => p r i n t l n ( " Answer i s : " + a )15 case Even ( ) => p r i n t l n ( " foo " )16 case Odd ( ) => p r i n t l n ( " bar " )17 }18 }19 }

Page 23: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Abstract Types

1 c l a s s Base [U] {2 // type U3 type T = { de f ge t :U }4

5 de f p r i n t ( x :T) = { p r i n t l n ( x . ge t ) }6 }7

8 c l a s s He lpe r { de f ge t = " foo " ; d e f bar = " bar " }9

10 o b j e c t A b s t r a c t {11 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {12 // v a l x = new Base { type U = S t r i n g }13 v a l x = new Base [ S t r i n g ]14 x . p r i n t ( new He lpe r )15 }16 }

Page 24: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

List comprehensions

1 > module Main2 > where3 > impor t System ( getArgs )4

5 > permute x = [ ( i , j ) | i <− [ 0 . . x ] , j <− [ i . . x ] ]6

7 > main = do8 > ( x :_) <− getArgs9 > putSt rLn $ show $ permute $ ( ( r ead x ) : : I n t )

10 o b j e c t Comprehend {11 de f permute ( n : I n t ) =12 f o r ( i <− 0 u n t i l n+1; j <− i u n t i l n+1)13 y i e l d P a i r ( i , j )14

15 de f main ( a r g s : Ar ray [ S t r i n g ] ) =16 {17 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )18 p r i n t l n ( permute ( x ) )19 }20 }

Page 25: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

List comprehensions

1 > module Main2 > where3 > impor t System ( getArgs )4

5 > permute x = [ ( i , j ) | i <− [ 0 . . x ] , j <− [ i . . x ] ]6

7 > main = do8 > ( x :_) <− getArgs9 > putSt rLn $ show $ permute $ ( ( r ead x ) : : I n t )10 o b j e c t Comprehend {11 de f permute ( n : I n t ) =12 f o r ( i <− 0 u n t i l n+1; j <− i u n t i l n+1)13 y i e l d P a i r ( i , j )14

15 de f main ( a r g s : Ar ray [ S t r i n g ] ) =16 {17 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )18 p r i n t l n ( permute ( x ) )19 }20 }

Page 26: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

List comprehensions

1 > module Main2 > where3 > impor t System ( getArgs )4

5 > permute x = [ ( i , j ) | i <− [ 0 . . x ] , j <− [ i . . x ] ,6 ( i+j ) ‘mod ‘ 2 == 0 ]7

8 > main = do9 > ( x :_) <− getArgs10 > putSt rLn $ show $ permute $ ( ( r ead x ) : : I n t )

11 o b j e c t Comprehend2 {12 de f permute ( n : I n t ) =13 f o r ( i <− 0 u n t i l n+1; j <− i u n t i l n+1 i f ( i+j )%2==0)14 y i e l d P a i r ( i , j )15

16 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {17 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )18 p r i n t l n ( permute ( x ) )19 }20 }

Page 27: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

List comprehensions

1 > module Main2 > where3 > impor t System ( getArgs )4

5 > permute x = [ ( i , j ) | i <− [ 0 . . x ] , j <− [ i . . x ] ,6 ( i+j ) ‘mod ‘ 2 == 0 ]7

8 > main = do9 > ( x :_) <− getArgs10 > putSt rLn $ show $ permute $ ( ( r ead x ) : : I n t )11 o b j e c t Comprehend2 {12 de f permute ( n : I n t ) =13 f o r ( i <− 0 u n t i l n+1; j <− i u n t i l n+1 i f ( i+j )%2==0)14 y i e l d P a i r ( i , j )15

16 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {17 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )18 p r i n t l n ( permute ( x ) )19 }20 }

Page 28: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Lazy evaluation

1 o b j e c t Lazy {2

3 de f bottom : I n t = bottom4

5 de f f 1 ( x : I n t , y : ( ) => I n t ) : I n t = x ∗ 26

7 de f f 2 ( x : I n t , y : => I n t ) : I n t = x ∗ 28

9 de f g ( x : I n t , y : I n t ) : I n t = x ∗ 210

11 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {12 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )13 p r i n t l n ( f 1 ( x , ( ) => bottom ) )14 p r i n t l n ( f 2 ( x , bottom ) )15 }16 }

Page 29: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Lazy evaluation

1 c l a s s Foo ( x : I n t ) {2 p r i v a t e de f e x p e n s i v e = x∗x∗x∗x∗x∗x3 p r i v a t e de f compute = {}4

5 l a z y v a l bar = { compute ; e x p e n s i v e }6 }7

8 o b j e c t Lazy2 {9

10 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {11 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )12 v a l f = new Foo ( x )13 i f ( x >42)14 p r i n t l n ( f . bar )15 }16 }

Page 30: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Currying

different signature of curried functions (vs. e.g. Haskell)it is possible to convert between curry and non-curryfunctions

Example1 o b j e c t Cur ry {2 de f p l u s ( x : I n t , y : I n t ) = x + y3 de f p l u s 2 ( x : I n t ) ( y : I n t ) = x + y4 de f p l u s 3 ( x : I n t ) = ( y : I n t ) => x+y5

6 v a l p l u s 4 = Funct i on . c u r r i e d ( p l u s _)7

8 v a l f 2 = p l u s 2 (2 ) _9 v a l f 3 = p l u s 3 (2 )10 v a l f 4 = p l u s 4 (2 )11

12 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {13 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )14 p r i n t l n ( f 2 ( x ) ) ; p r i n t l n ( f 3 ( x ) ) ; p r i n t l n ( f 4 ( x ) )15 }16 }

Page 31: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Currying

different signature of curried functions (vs. e.g. Haskell)it is possible to convert between curry and non-curryfunctions

Example1 o b j e c t Cur ry {2 de f p l u s ( x : I n t , y : I n t ) = x + y3 de f p l u s 2 ( x : I n t ) ( y : I n t ) = x + y4 de f p l u s 3 ( x : I n t ) = ( y : I n t ) => x+y5

6 v a l p l u s 4 = Funct i on . c u r r i e d ( p l u s _)7

8 v a l f 2 = p l u s 2 (2 ) _9 v a l f 3 = p l u s 3 (2 )10 v a l f 4 = p l u s 4 (2 )11

12 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {13 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )14 p r i n t l n ( f 2 ( x ) ) ; p r i n t l n ( f 3 ( x ) ) ; p r i n t l n ( f 4 ( x ) )15 }16 }

Page 32: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Uniform access

to avoid the getter/setter boilerplate hell

Example1 c l a s s FooOld { va r s t a t e : I n t = 0 }2 c l a s s Foo {3 p r i v a t e va r s : I n t = 04 de f s t a t e_ =(x : I n t ) = s = x∗25 de f s t a t e = s6 }7 c l a s s Bar { de f s t a t e : I n t = 23 }8 c l a s s Baz extends Bar { o v e r r i d e v a l s t a t e : I n t = 42 }9

10 o b j e c t Uniform {11 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {12 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )13 va r f = new Foo ; f . s t a t e = x14 p r i n t l n ( f . s t a t e )15 v a l g = new Baz16 p r i n t l n ( g . s t a t e )17 }18 }

Page 33: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Uniform access

to avoid the getter/setter boilerplate hell

Example1 c l a s s FooOld { va r s t a t e : I n t = 0 }2 c l a s s Foo {3 p r i v a t e va r s : I n t = 04 de f s t a t e_ =(x : I n t ) = s = x∗25 de f s t a t e = s6 }7 c l a s s Bar { de f s t a t e : I n t = 23 }8 c l a s s Baz extends Bar { o v e r r i d e v a l s t a t e : I n t = 42 }9

10 o b j e c t Uniform {11 de f main ( a r g s : Ar ray [ S t r i n g ] ) = {12 v a l x = I n t e g e r p a r s e I n t ( a r g s ( 0 ) )13 va r f = new Foo ; f . s t a t e = x14 p r i n t l n ( f . s t a t e )15 v a l g = new Baz16 p r i n t l n ( g . s t a t e )17 }18 }

Page 34: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Actors

Message passing with fancy operatorsSyntax similar to Erlang message passing constructsnot part of the language, a lib with some operators

Page 35: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Conclusion

Scala fits well into the JVM world→ easy to mix Scala and Java modules

Notation not as elegant as Haskell’sType system limits type inferenceIn comparison to Java many sources oftedious boilerplate are eliminated

Page 36: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Introduction

Features

Conclusion

Discussion

Thanks! && Do you have questions?

Page 37: Scala Programming Language - uni-bielefeld.degsauthof/docs/scala.pdf · Scala Programming Language ... Scala Multi-paradigmgeneralpurposeprogramminglanguageby ... OOP nomultipleinheritance

ScalaProgrammingLanguage

GeorgSauthoff

Photo images in this document are fromhttp://commons.wikimedia.org/. I.e. they are licensedunder the GNU Free Documentation License (GFDL) and/or aCreative Commons license (e.g. CC-by-sa). If you need moreinformation about the specific license or URL of a single photoimage, don’t hesitate to write an e-mail [email protected].