Scala - A Scalable Language
Upcoming SlideShare
Loading in...5
×
 

Scala - A Scalable Language

on

  • 7,246 views

Introduction to Scala

Introduction to Scala

Statistics

Views

Total Views
7,246
Views on SlideShare
7,212
Embed Views
34

Actions

Likes
17
Downloads
290
Comments
0

4 Embeds 34

http://www.slideshare.net 17
http://cloudscale.sinaapp.com 10
http://dschool.co 4
http://www.techgig.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scala - A Scalable Language Scala - A Scalable Language Presentation Transcript

  • Scala ... a Scalable Language 06.10.2009 XPUG Rhein Main Mario Gleichmann Mario Gleichmann XPUG Rhein/Main
  • Introduction Mario Gleichmann site: www.mg-informatik.de blog: 'brain driven development' gleichmann.wordpress.com mail: mario.gleichmann@mg-informatik.de Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Scala vs. Java 9 • Functional programming for the imperative mind • Discover the (new) possibilities ... Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... “If Java programmers want to • Scala vs. Java 9 use features that aren't present • Functional programming forthe language, I think they're in the imperative mind probably best off using another • Discover the (new) possibilities ... language that targets the JVM, such as Scala and Groovy“ Joshua Bloch Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... ● Functions & Closures • Scala vs. Java 9 ● Extended Type System • Functional programming for the imperative mind ● Extended Module System • ● Properties Discover the (new) possibilities ... ● Essence over Ceremony ● Extended Control Structs Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Scala vs. Java 9 “If i were to pick a language • Functional programming foruse today other than Java, to the imperative mind it would be Scala“ • Discover the (new) possibilities ... James Gosling Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Imparative programming is a programming paradigm that describes computation in terms of statements that change a programs state Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Functional programming is a programming paradigm that describes computation as the evaluation of mathematical functions avoiding state and mutable data Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Lazy Evaluation Continuations Monads Recursion Higher Order Functions Closures Currying Immutable Datatypes Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind ... • Discover the (new) possibilities ... Control Structure Abstraction Composition Traits Pattern Matching Type Variance Modularity Type Extentions / Conversions Mario Gleichmann XPUG Rhein/Main
  • Motivation Open your mind !!! • “Scala taught me to program and reason about programming differently. I stopped thinking in terms of allocating buffers, structs and objects and of changing those pieces in memory. Instead I learned to think about most of my programs as transforming input to output. This change in thinking has lead to lower defect rates, more modular code, and more testable code“ David Pollak Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Martin Odersky (EPFL Switzerland) • Functional • Pizza, Fummel & Co. Statically Typed Java Generic • • • javac Reference Compiler • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed ''No primitive Types'' 123.hashCode • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed ''No primitive Types'' 123.hashCode • Functional ''Operations are method calls'' • Runs on the JVM actor ! msg <=> actor.!( msg ) Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • public BigInteger factorial( BigInteger n ){ Functional Jav a if( n.equals( BigInteger.ZERO ) return BigInteger.ONE else • Statically Typed return n.multiply( factorial( n.subtract( BigInteger.ONE ) ) ); • Runs on the JVM } Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functionalintroduction Method Result Type Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed Parameter list Type declaration Definition • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed ● (almost) everything is an expression • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed ● (almost) everything is an expression BigInt 'integrates' like a Built-In type (but it's not!) ● • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented Introduction of class definition • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... class value parameter • Pure Object Oriented (primary constructor) • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented Single Inheritance • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented Mandatory ! • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs ordinary Method on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } RunsClass Instantiation on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Self reference Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional • Statically Typed • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional Empty Stack : Non Empty Stack : [] • Statically Typed element: Int [ 7 ] Rest: Stack : element: Int [ 23 ] • Runs on the JVM Rest: Stack : [] Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional Empty Stack : Non Empty Stack : [] element: Int [ 10 ] Rest: Stack : • Statically Typed element: Int [ 7 ] Rest: Stack : element: Int [ 23 ] • Runs on the JVM Rest: Stack : [] Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented abstract class IntStack { def push(x: Int): IntStack = new NonEmptyIntStack( x, this ) • Functional def isEmpty: Boolean • Statically Typed Int def top: def pop: IntStack } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Abstract class Object Oriented abstract class IntStack { def push(x: Int): IntStack = new NonEmptyIntStack( x, this ) • Functional def isEmpty: Boolean Abstract method • Statically Typed Int def top: Abstract method def pop: IntStack Abstract method } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented Inheritance class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } Throwing a 'checked' Exception • Runs on the JVM ... but catching is optional Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack • Runs on the JVM var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack 'Immutable' value • Runs on the JVM var noInts = new EmptyIntStack 'mutable' variable Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackExceptionNo need def pop } for multiple val zeroInts = new EmptyIntStack Instances of • Runs on the JVM EmptyIntStack var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented object EmptyIntStack extends IntStack { • Functional def isEmpty = true def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } • Runs on the Singleton Object ● JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented object EmptyIntStack extends IntStack { • Functional def isEmpty = true def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack • Runs on the JVM var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class NonEmptyIntStack( elem: Int, rest: IntStack ) extends IntStack { • Functional def isEmpty = false def top = elem • Statically Typed def pop = rest } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class NonEmptyIntStack( elem: Int, rest: IntStack ) extends IntStack { • Functional def isEmpty = false def top = elem • Statically Typed def pop = rest } • Runs on the JVM var s = EmptyIntStack push 23 push 7 push 10 Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● Using Stack with other types than Int ● Concept of stacking elements is generic • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● Using Stack with other types than Int ● Concept of stacking elements is generic • Statically Typed => Generic Types • Functional (Type Parameterization) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented abstract class Stack[A] { Type Paramter def push(x: A): Stack[A] = • Statically Typed new NonEmptyStack[A]( x, this ) def isEmpty: Boolean • Functional def top: A def pop: Stack[A] • Runs on the JVM } Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented class NonEmptyStack[A]( elem: A, rest: Stack[A] ) extends Stack[A] { • Statically Typed def isEmpty = false • Functional def top = elem def pop = rest } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top => 2 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top => 2 Parameterized Type • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top Mandatory => 2 ( no Raw Types ! ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional abstract class Stack[A <: Number]{ ... } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional abstract class Stack[A <: Number]{ ... } Defined by the implementor, • Runs on the JVM not by the user ! Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional Compile Error: ''Type mismatch: cannot convert • Runs on the JVM List<String> to List<Object>'' Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional Generic Types are • Runs on the JVM INVARIANT Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional Arrays are • Runs on the JVM COVARIANT Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional oArray[0] = BigDecimal.ONE; • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional oArray[0] = BigDecimal.ONE; ArrayStoreException • Runs on the JVM at Runtime ! Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Covariant subtyping Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Covariant subtyping Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } Compile Error: ''covariant type A occurs in • Runs on the JVM contravariant position in type A of value x'' Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } Lower Type Bound ''Parameter B is restricted to range • Runs on the JVM only over supertypes of type A'' Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } val s1 = new EmptyStack[Int] push 1 push 2 • Runs on the JVM val s2 = s1 push "x" Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } Stack[Int] val s1 = new EmptyStack[Int] push 1 push 2 • Runs on the JVM val s2 = s1 push "x" Stack[Any] Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional def add( a: Int, b: Int ): Int = a + b • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional def add( a: Int, b: Int ): Int = a + b def add( a: Int, b: Int ) = a + b • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' => Sc l ble L ngu ge • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional new StringExtension( “Scala“ ).-( 'a' ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional implicit def toStringExtension( s: String ) = new StringExtension( s ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' Implicit conversion to StringExtension • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional implicit def toStringExtension( s: String ) = new StringExtension( s ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional Argument list Definition • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 succ( 7 ) => 8 • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 succ( 7 ) => 8 • Runs on the JVM type of succ: ( Int ) => Int Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional => List( 2, 14, 11 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 List[Int] (Type inference) • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional ... accepting a function which => List( 2, 14, 11 )and results to boolean accepts an Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 free variable • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional bound variable 'open term' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 capturing • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional ● bound within lexical scope of function • Runs on the JVM => open term is closed Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional barrier = 21 val usMinors = ages.filter( minors ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) 'dynamic' bound • Functional barrier = 21 val usMinors = ages.filter( minors ) • Runs on the JVM => 2, 20, 14, 19, 11 Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed A function ... accepting two Args • Functional ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed type of function add: ( Int, Int ) => Int • Functional 'resulting in ...' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed Quiz: ''transform into a function which is accepting • Functional only one single Argument after another'' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed A function ... accepting one Arg • Functional ... resulting in another function • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed ... accepting one Arg • Functional ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed type of function add: (Int) => (Int) => Int • Functional 'resulting in ...' 'resulting in ...' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed val succ = add( 1 ) succ( 7 ) => 8 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b A function ... val succ = add( 1 ) ... accepting one Arg • Statically Typed ... resulting in another function succ( 7 ) => 8 • Functional ... accepting one Arg ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed multiple parameter lists • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed multiple parameter lists • Functional Signature: mult ( Int ) ( Int ) : Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed Partially applied 2nd Arg unapplied • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed • Functional Coercion into a function of type ( Int ) => Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed double( 6 ) => 12 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList List[Int] def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional expects function of type ( Int ) => Boolean • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional curried to function of type ( Int ) => Boolean • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) => List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) => List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 ) • Functional hours.filter( modulo( 4 ) ) • Runs on the JVM => List( 0, 2, 4, 8, 12, …, 16, 20 ) Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = ( x: Int ) => x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } succ( 7 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } succ.apply( 7 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Compiles to Bytecode • Seamless Java Interoperability • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Compiles to Bytecode • Seamless Java Interoperability • Runs on the JVM • Performance on par with Java Mario Gleichmann XPUG Rhein/Main
  • What is Scala ? A programming language ... “I can honestly say if someone • Pure Object Oriented had shown me the Programming in Scala book ... back in 2003 • Statically Typed I'd probably have never created Groovy“ • Functional James Strachan • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible Can you spot the intention ? • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Results into a Tuple2[List[Person],List[Person]] Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Results into a Tuple2[List[Person],List[Person]] Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible Pattern Matching: ''Split Persons into minors and adults by their age'' bound to single elements of a Tuple2 • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' adults => List(Person(Hugo,19), Person(Heinz,38)) • Pragmatic minors => List(Person(Hans,11), Person(Helga,16)) Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) Implicit Conversion into a Tuple2 • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) Implicit Conversion into a Currency • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, Compagnion Object • High Level object Map{ “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) def apply[A, B]( elems: (A, B)* ) : Map[A, B] = ... ... • Concise } bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, Map instance • High Level “Systems Thinking“ -> 30 EUR, class <xxx>Map[A, B] extends Map[A, B] { “Code Complete“ -> 25 USD ) override def apply(key: A): B = ... ... • Concise } bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Upper Case in given name ? Jav • Expressive a boolean hasUpperCase = false; • High Level for( int i=0; i < name.length; i++ ){ if( Character.isUpperCase( name.charAt( i ) { • Concise hasUpperCase = true; break; } • Extensible } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function val hasUpperCase = • Extensible name.exists( c => c isUpperCase ) • Pragmatic Type inference Mario Gleichmann XPUG Rhein/Main
  • Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function val hasUpperCase = • Extensible name.exists( _ isUpperCase ) • Pragmatic parameter shortcut Mario Gleichmann XPUG Rhein/Main
  • Characteristics Find maximal Distance Jav • Expressive a List<Integer> distances = new ArrayList<Integer>(); • High Level distances.add( 12 ); distances.add( 17 ); ... • Concise Integer maxDistance = 0; • Extensible for( Integer distance : distances ){ if( distance > maxDistance ){ • Pragmatic } maxDistance = distance } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Find maximal Distance Scal • Expressive a val distances = List( 12, 17, 14, 21, ...) • High Level val maxDistance = distances.foldLeft( 0 ){ Math.max } • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Find maximal Distance Scal • Expressive a val distances = List( 12, 17, 14, 21, ...) • High Level val maxDistance = distances.foldLeft( 0 ){ Math.max } • Concise 'Higher Order Method' • Extensible 1st Param: Seed • Pragmatic 2nd Param: Function Mario Gleichmann XPUG Rhein/Main
  • Characteristics Find maximal Distance Scal • Expressive a val distances = List( 12, 17, 14, 21, ...) • High Level val maxDistance = distances.foldLeft( 0 ){ Math.max } • Concise • Extensible (x: Int, y: Int ) => Math.max( x, y ) • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Find maximal Distance Scal • Expressive a val distances = List( 12, 17, 14, 21, ...) • High Level val maxDistance = distances.foldLeft( 0 ){ Math.max } • Concise 12 0 • Extensible 17 12 14 17 • Pragmatic 21 17 21 Mario Gleichmann XPUG Rhein/Main
  • Characteristics Declarative style ! Scal • Expressive a Check for prime number def isPrime( candidate: Int ) = { • High Level (2 to candidate/2 ) .forall( number => candidate % number != 0 ) • Concise } • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Declarative style ! Scal • Expressive a Check for prime number def isPrime( candidate: Int ) = { • High Level (2 to candidate/2 ) Range .forall( number => candidate % number != 0 ) • Concise } Predicate (Function) • Extensible • Pragmatic Higher Order ('Quantor') Method Mario Gleichmann XPUG Rhein/Main
  • Characteristics Declarative style ! Scal • Expressive a Check for prime number def isPrime( candidate: Int ) = { • High Level (2 to candidate/2 ) .forall( number => candidate % number != 0 ) • Concise } • Extensible ● Remember: Everything is an Expression • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Declarative style ! Scal • Expressive a Check for prime number def isPrime( candidate: Int ) = { • High Level (2 to candidate/2 ) .forall( number => candidate % number != 0 ) • Concise } • Extensible ● Remember: Everything is an Expression • Pragmatic ● No Assignments Mario Gleichmann XPUG Rhein/Main
  • Characteristics Declarative style ! Scal • Expressive a Check for prime number def isPrime( candidate: Int ) = { • High Level (2 to candidate/2 ) .forall( number => candidate % number != 0 ) • Concise } • Extensible ● Remember: Everything is an Expression • Pragmatic ● No Assignments => almost Functional Style Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics class Person{ Jav • Expressive private String name; a private int age; public Person( String name ){ • High Level this.name = name } public String getName(){ • Concise return this.name; } • Extensible public int getAge(){ return this.age; } • Pragmatic public void setAge( int age ){ this.age = age; } } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a class Person( val name: String ){ • High Level var age: Int } • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a class Person( val name: String ){ • High Level var age: Int } • Concise ● Class value parameter(s) ● Default Visibility: private • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a class Person( val name: String ){ • High Level var age: Int } • Concise ● read-only and public • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a class Person( val name: String ){ • High Level var age: Int } • Concise ● Class property • Extensible ● Default visibility: public readable and writeable variable Pragmatic ● • Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a class Person( val name: String ){ • High Level var age: Int } • Concise val friend = new Person( “Joe“ ) friend.age = 30 • Extensible println( friend.name ) • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Jav • Expressive a Resource Control • High Level Reader reader = new BufferedReader( ... ); try{ System.out.println( reader.readLine() ); • Concise } finally{ reader.close(); • Extensible } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Jav • Expressive a Resource Control • High Level Reader reader = new BufferedReader( ... ); try{ System.out.println( reader.readLine() ); • Concise } finally{ reader.close(); • Extensible } • Pragmatic Resource control ! Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level using ( new BufferedReader( ... ) ) { reader => println( reader.readLine() ); } • Concise '' Loan Pattern '' • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive 1st Parameter: Resource under control a Adding new Control Structures Resource Control • High Level using ( new BufferedReader( ... ) ) { reader => println( reader.readLine() ); } • Concise 2nd Parameter: Function, using Resource • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive 1st Parameter: Resource under control a Adding new Control Structures Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } nd 2 Parameter: Function, using Reader • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } calling the function, passing the reader • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } • Resource control completely separated • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } • Resource control completely separated • Pragmatic • Reusable with any Reader Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using( reader: Reader ) ( block: Reader => Unit ) { try{ • Concise } block( reader ) finally{ reader.close • Extensible } } • Resource control completely separated • Pragmatic • But there's still a more generic way ! Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using [ T <: { def close() } ] ( resource: T ) ( block: T => Unit ) { • Concise try{ block( resource ) } • Extensible finally{ resource.close() } } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control Structural Type • High Level def using [ T <: { def close() } ] Any Type ( resource: T ) which offers ( block: T => Unit ) { a close() method • Concise try{ block( resource ) } • Extensible finally{ resource.close() } } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Resource Control • High Level def using [ T <: { def close() } ] ... statically typed ( resource: T ) 'Duck Typing' ( block: T => Unit ) { • Concise try{ block( resource ) } • Extensible finally{ resource.close() } } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Write your own 'Loop – Unless' • High Level def loop( body: => Unit ): LoopUnlessCond = new LoopUnlessCond( body ) • Concise protected class LoopUnlessCond( body: => Unit ) { • Extensible def unless( cond: => Boolean ) { body if ( !cond ) unless( cond ) • Pragmatic } } Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Write your own 'Loop – Unless' By-name parameter (Function without Arg) • High Level def loop( body: => Unit ): LoopUnlessCond = new LoopUnlessCond( body ) • Concise Function as class parameter protected class LoopUnlessCond( body: => Unit ) { • Extensible def unless( cond: => Boolean ) { body calling the Function if ( !cond ) unless( cond ) • Pragmatic } } calling the Function (evaluating the condition) Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Write your own 'Loop – Unless' • High Level var i = 10 loop { • Concise println("i = " + i) i -= 1 } unless ( i == 0 ) • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Write your own 'Loop – Unless' • High Level var i = 10 By-name parameter loop { instead of • Concise println("i = " + i) i -= 1 loop { () => ... } unless ( i == 0 ) • Extensible } unless( .. ) • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Adding new Control Structures Scal • Expressive a Write your own 'Loop – Unless' • High Level var i = 10 loop { • Concise println("i = " + i) By-name parameter i -= 1 } unless ( i == 0 ) instead of • Extensible unless( () => ... ) • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a Adding new Control Structures • High Level def using( reader: Reader ) is ( block: Reader => Unit ) { rd try{ block( resource ) Concise o ! • } w y y r finally{ Extensible } e a resource.close k r r lib • } u o y • Pragmatic Y m Mario Gleichmann XPUG Rhein/Main
  • Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a def booksAsXml = • High Level <books> <book category=“IT“> • Concise <isbn>{ book.isbn }</isbn> <author>{ book.author }</author> ... • Extensible </book> ... </books> • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive Parameterless Method a def booksAsXml = Direct XML Generation • High Level <books> <book category=“IT“> • Concise <isbn>{ book.isbn }</isbn> <author>{ book.author }</author> ... • Extensible </book> ... Embedding </books> • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • Characteristics Scal • Expressive a Pattern Matching • High Level def printAuthors { booksAsXml match case <books>{ books @ _* }</books> => • Concise for( book <- books ) println( “Author:“ + ( book “author“ ).text ) • Extensible } XPath – like Method • Pragmatic Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition • Pattern Matching • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin • Composable Types • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Singer{ trait Flyer{ def sing = … def fly = … } } • Composable Types • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Singer{ trait Flyer{ def sing = … def fly = … } } • Composable Types Separation of independent facets • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Singer{ trait Flyer{ def sing = … def fly = … } } • Composable Types ...can be mixedof independent facets Separation into any type independently • Enrichment class Bird extends Flyer with Singer {…} val myBird = new Bird • Stackable Behaviour myBird.sing myBird.fly Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Singer{ trait Flyer{ def sing = … def fly = … } } • Composable Types Separation of independently to any orthogonal / independent facets type hierarchy • Enrichment class Plane extends Flyer {…} trait Superstar extends Human • Stackable with Singer Behaviour with Dancer with ... ... Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin abstract class Spaceship{ def engage } • Composable Types • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin abstract class Spaceship{ def engage } • Composable abstract Method (without definition) Types • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin abstract class Spaceship{ def engage } trait CommandoBridge{ • Composable def engage { for( _ <- 1 to 3 ){ speedUp } } Types def speedUp } • Enrichment abstract Method (without definition) • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Spaceship{ def engage } trait CommandoBridge{ • Composable def engage { for( _ <- 1 to 3 ){ speedUp } } Types def speedUp } • Enrichment trait PulseEngine{ val maxPulse: Int abstract value • Stackable var currentPulse = 0; Behaviour def speedUp { if( currentPulse < maxPulse ) currentPulse += 1 } } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class StarCruiser extends Spacecraft with CommandoBridge with PulseEngine{ • Composable Types val maxPulse = 200 } • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class StarCruiser extends Spacecraft with CommandoBridge with PulseEngine{ • Composable Types val maxPulse = 200 } • Enrichment class Shuttle extends Spacecraft with ControlCabin with PulseEngine{ • Stackable val maxPulse = 50 Behaviour def increaseSpeed = speedUp } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class StarCruiser extends Spacecraft with CommandoBridge with PulseEngine{ • Composable trait PulseEngine{ 200 trait ControlCabin{ Types val maxPulse = } def speedUp = ... def increaseSpeed } } • Enrichment class Shuttle extends Spacecraft with ControlCabin with PulseEngine{ • Stackable val maxPulse = 50 'wiring' Behaviour def increaseSpeed = speedUp } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait WarpEngine{ val maxWarp: Int • Composable var currentWarp = 0; Types def toWarp( x: Int ) { if( x < maxWarp ) currentWarp = x } } • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait WarpEngine{ val maxWarp: Int • Composable var currentWarp = 0; Types def toWarp( x: Int ) { if( x < maxWarp ) currentWarp = x } } • Enrichment class Explorer extends Spacecraft with CommandoBridge • Stackable with WarpEngine{ Behaviour val maxWarp = 10 def speedUp = toWarp( currentWarp + 1 ) } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait WarpEngine{ val maxWarp: Int • Composable var currentWarp = 0; Types def toWarp( x: Int ) { trait CommandoBridge { if( x < maxWarp ) currentWarp = x } def speedUp } } • Enrichment class Explorer extends Spacecraft with CommandoBridge • Stackable with WarpEngine{ Behaviour val maxWarp = 10 'wiring' def speedUp = toWarp( currentWarp + 1 ) } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Jet extends Airplane with WarpEngine{ val maxWarp = 4 • Composable ... } Types • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Jet extends Airplane with WarpEngine{ val maxWarp = 4 • Composable ... } Types WarpEngine is meant to be used only within Spaceships !!! • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Jet extends Airplane with WarpEngine{ val maxWarp = 4 • Composable ... } Types trait WarpEngine{ this: Spacecraft => • Enrichment ... } • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Jet extends Airplane with WarpEngine{ val maxWarp = 4 • Composable ... } Types trait WarpEngine{ this: Spacecraft => • Enrichment ... } selftype declaration : • Stackable ''can only be mixed into something Behaviour which is at least of type Spacecraft'' Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin class Jet extends Airplane with WarpEngine{ val maxWarp = 4 • Composable ... } Types trait WarpEngine{ this: Spacecraft => • Enrichment ... } Compiler Error: • Stackable ''illegal inheritance: Jet does not conform Behaviour to WarpEngine's selftype WarpEngine with Spacecraft'' Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin def inspection( craft: ControlCabin with PulseEngine ) { • Composable craft.increaseSpeed Types assert( craft.currentPulse > 0 ) } • Enrichment • Stackable Assert that ControlCabin Behaviour is wired with PulseEngine Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin def inspection( craft: ControlCabin with PulseEngine ) { • Composable craft.increaseSpeed Mixed in by ControlCabin Types assert( craft.currentPulse > 0 ) } • Enrichment Mixed in by PulseEngine • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin def inspection( craft: ControlCabin with PulseEngine ) { • Composable craft.increaseSpeed Types assert( craft.currentPulse > 0 ) } • Enrichment • Stackable 'Compound type' Behaviour Intersection of object types Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait DBProvider { def mydatabase : ObjectContainer • Composable } Types class CafeDAO{ self: DBProvider => • Enrichment val db = mydatabase def findByName(..) ... • Stackable } Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait DBProvider { def mydatabase : ObjectContainer • Composable } Types Self type class CafeDAO{ Can only be self: DBProvider => instantiated with a • Enrichment val db = mydatabase mixed in DBProvider def findByName(..) ... • Stackable } Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait DBProvider { def mydatabase : ObjectContainer • Composable } Types Self type class CafeDAO{ Can only be self: DBProvider => instantiated with a • Enrichment val db = mydatabase mixed in DBProvider def findByName(..) ... Get the database • Stackable } from the mixed in Behaviour DBProvider Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait ProdDatabase extends DBProvider{ def mydatabase = Db4o openFile "prodCafe.yap" • Composable } Types trait TestDatabase extends DBProvider{ def mydatabase = Db4o openFile "testCafe.yap" } • Enrichment • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait ProdDatabase extends DBProvider{ def mydatabase = Db4o openFile "prodCafe.yap" • Composable } Types trait TestDatabase extends DBProvider{ def mydatabase = Db4o openFile "testCafe.yap" } • Enrichment ... val cafeDaoTestee = • Stackable new CafeDAO with TestDatabase Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition ''Dependency Injection'' • Feature Mixin trait ProdDatabase extends DBProvider{ def mydatabase = Db4o openFile "prodCafe.yap" • Composable } Types trait TestDatabase extends DBProvider{ def mydatabase = Db4o openFile "testCafe.yap" } • Enrichment ... val cafeDaoTestee = • Stackable new CafeDAO with TestDatabase Behaviour 'Dynamic Mixin' Single instance gets TestDatabase mixed in Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait RichCollection[+T] { def foreach( f: T => Unit ) • Composable def exist ( predicate: T => Boolean ): Boolean = { Types foreach{ elem => if( predicate(elem) ) return true } false } • Enrichment def foldLeft[B]( seed: B)( f: (B,T) => B ) = { var res = seed foreach{ elem => res = f(res, elem) } • Stackable res Behaviour } ... } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait RichCollection[+T] { def foreach( f: T => Unit ) 'contract' • Composable def exist ( predicate: T => Boolean ): Boolean = { Types foreach{ elem => if( predicate(elem) ) return true } false } • Enrichment def foldLeft[B]( seed: B)( f: (B,T) => B ) = { var res = seed foreach{ elem => res = f(res, elem) } • Stackable res Behaviour } ... } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait RichCollection[+T] { def foreach( f: T => Unit ) 'contract' • Composable def exist ( predicate: T => Boolean ): Boolean = { Types foreach{ elem => if( predicate(elem) ) return true } false } • Enrichment def foldLeft[B]( seed: B)( f: (B,T) => B ) = { var res = seed foreach{ elem => res = f(res, elem) } • Stackable res Behaviour } ... forall, filter, partition, size, ... } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait RichCollection[+T] { Implement def foreach( f: T => Unit ) one ... • Composable def exist ( predicate: T => Boolean ): Boolean = { Types foreach{ elem => if( predicate(elem) ) return true } false } • Enrichment def foldLeft[B]( seed: B)( f: (B,T) => B ) = { var res = seed foreach{ elem => res = f(res, elem) } • Stackable res Behaviour } ... ... receive many } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin abstract class Stack[+A] extends Object with RichCollection[A] { def push[B >: A](x: B): Stack[B] = ... • Composable def isEmpty: Boolean Types def top: A def pop: Stack[A] • Enrichment def foreach( f: A => Unit ) { if( ! isEmpty ){ • Stackable f( top ) pop.foreach( f ) Behaviour } } } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin val s = new EmptyStack[Int] push 1 push 2 push 3 • Composable Types s.exist( _ >= 2 ) => true s.foldLeft(0)( _ + _ ) => 6 • Enrichment s.filter( _ >= 2 ) => List( 2, 3 ) • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin val jSet = new java.util.HashSet[Int] with RichCollection[Int] { • Composable def foreach( f: Int => Unit ) { Types val elems = iterator while( elems.hasNext ){ f( elems.next ) } } • Enrichment } • Stackable jSet.exist( _ >= 2 ) => true Behaviour jSet.foldLeft(0)( _ + _ ) => 6 jSet.filter( _ >= 2 ) => List( 2, 3 ) Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Logging[A] extends java.util.Set[A]{ abstract override def add(x: A) = { • Composable println( "adding "+ x ) super.add( x ) Types } } trait Doubling extends java.util.Set[Int]{ • Enrichment abstract override def add(x: Int) = super.add( x * 2 ) } • Stackable trait Incrementing extends java.util.Set[Int]{ Behaviour abstract override def add(x: Int) = super.add( x + 1 ) } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin trait Logging[A] extends java.util.Set[A]{ abstract override def add(x: A) = { • Composable println( "adding "+ x ) super.add( x ) Types } 'decorating' java.util.Set.add } trait Doubling extends java.util.Set[Int]{ • Enrichment abstract override def add(x: Int) = super.add( x * 2 ) } • Stackable trait Incrementing extends java.util.Set[Int]{ Behaviour abstract override def add(x: Int) = super.add( x + 1 ) } Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin val jSet = new java.util.HashSet[Int] with Logging[Int] • Composable with Incrementing with Doubling Types jSet add 1 jSet add 2 jSet add 3 • Enrichment => adding 3 adding 5 adding 7 • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin val jSet = new java.util.HashSet[Int] with Logging[Int] • Composable with Doubling with Incrementing Types jSet add 1 jSet add 2 jSet add 3 • Enrichment => adding 4 adding 6 adding 8 • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • Composition • Feature Mixin val jSet = new java.util.HashSet[Int] with Logging[Int] • Composable with Doubling with Incrementing Types jSet add 1 jSet add 2 jSet add 3 • Enrichment Lineariation => adding 4 adding 6 adding 8 • Stackable Behaviour Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition • Pattern Matching • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition EXPRESSION := NUMBER | BINARY_OP • Pattern BINARY_OP := ADD | SUB | MULT Matching ADD := Add( EXPRESSION, EXPRESSION ) • Modules SUB := Sub( EXPRESSION, EXPRESSION ) MULT := Mult( EXPRESSION, EXPRESSION ) • Monads NUMBER := Number( Int ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition EXPRESSION := NUMBER | BINARY_OP • Pattern BINARY_OP := ADD | SUB | MULT Matching ADD := Add( EXPRESSION, EXPRESSION ) • Modules SUB := Sub( EXPRESSION, EXPRESSION ) MULT := Mult( EXPRESSION, EXPRESSION ) • Monads NUMBER := Number( Int ) Sub( Mult( Number( 3 ), Number( 4 ) ), Number( 5 ) ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition • Pattern Sub Matching Mult Number(5) • Modules Number(3) Number(4) • Monads Sub( Mult( Number( 3 ), Number( 4 ) ), Number( 5 ) ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition abstract class Expression • Pattern case class Number( num: Int ) extends Expression Matching case class BinaryOperator ( opCode: String, left: Expression, right: Expression ) extends Expression • Modules case class Add( s1: Expression, s2: Expression ) extends BinaryOperator( "+", s1, s2 ) • Monads case class Sub( s1: Expression, s2: Expression ) extends BinaryOperator( "-", s1, s2 ) case class Mult( m1: Expression, m2: Expression ) extends BinaryOperator( "*", m1, m2 ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition Case class abstract class Expression • Pattern case class Number( num: Int ) extends Expression Matching case class BinaryOperator ( opCode: String, left: Expression, right: Expression ) extends Expression • Modules case class Add( s1: Expression, s2: Expression ) extends BinaryOperator( "+", s1, s2 ) • Monads case class Sub( s1: Expression, s2: Expression ) extends BinaryOperator( "-", s1, s2 ) case class Mult( m1: Expression, m2: Expression ) extends BinaryOperator( "-", m1, m2 ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition Case class abstract class Expression • Pattern case class Number( num: Int ) extends Expression Matching case class BinaryOperator ( opCode: String, left: Expression, right: Expression ) extends Expression • Modules case class Serve Super Constructor ! Add( s1: Expression, s2: Expression ) extends BinaryOperator( "+", s1, s2 ) • Monads case class Sub( s1: Expression, s2: Expression ) extends BinaryOperator( "-", s1, s2 ) case class Mult( m1: Expression, m2: Expression ) extends BinaryOperator( "-", m1, m2 ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { • Pattern expr match { case Number( x ) => print( x ) Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { case Number( x ) => print( x ) Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { case Number( x ) => print( x ) Matching case BinaryOperator( opCode,Number( Int )) ? { expr matches case class expr1, expr2 => print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { case Number( x ) => print( x ) Matching Bindcase BinaryOperator( opCode, expr1, expr2 to x { class' value parameter in Number( Int ) ) => print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { expr case Number( x ) => class x ) matches any case print( BinaryOperator(..) ? Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { caseBind class' )value parameters ... Number( x => print( x ) Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { case Number( x ) => print( x ) Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules Block instead of prettyPrint( expr1 ) a single expression print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def prettyPrint( expr: Expression ) { 'Patterns' Match expr agains • Pattern expr match { case Number( x ) => print( x ) Matching case BinaryOperator( opCode, expr1, expr2 ) => { print( "(" ) • Modules prettyPrint( expr1 ) print( opCode ) prettyPrint( expr2 ) • Monads print( ")" ) } case _ => print( "unknown" ) } } Matches against 'everything' Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = Sub( Mult( Number( 3 ), Number( 4 ) ), Number( 5 ) ) • Pattern Matching prettyPrint( expr ) => ( ( 3 * 4 ) - 5 ) • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = Sub( Mult( Number( 3 ), Number( 4 ) ), Number( 5 ) ) • Pattern Matching prettyPrint(Compagnion object.apply() expr ) for every case class provided => ( ( 3 * 4 ) - 5 ) (no instantiation using new necessary) • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition simplify( expr: Expression ): Expression = { def expr match { • Pattern case Add( e, Number( 0 ) ) => e Matching case Add( Number( 0 ), e ) => e case Mult( e, Number( 0 ) ) => Number( 0 ) case Mult( Number( 0 ), e ) => Number( 0 ) case Mult( e, Number( 1 ) ) => e • Modules case Mult( Number( 1 ), e ) => e case Sub( Number( x ), Number( y ) ) if x == y => Number( 0 ) case Sub( Add( e, Number( x ) ), Number( y ) ) if x == y => e • Monads case Mult( e1, e2 ) => Mult( simplify( e1 ), simplify( e2 ) ) case _ => expr } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition simplify( expr: Expression ): Expression = { def expr match { • Pattern case Add( e, Number( 0 ) ) => e Matching case Add( Number( 0 ), e ) => e case Mult( e, Number( 0 ) ) => Number( 0 ) case Mult( Number( 0 ), e ) => Number( 0 ) ) Matches only against Number( 0 case Mult( e, Number( 1 ) ) => e • Modules case Mult( Number( 1 ), e ) => e case Sub( Number( x ), Number( y ) ) if x == y => Number( 0 ) case Sub( Add( e, Number( x ) ), Number( y ) ) if x == y => e • Monads case Mult( e1, e2 ) => Mult( simplify( e1 ), simplify( e2 ) ) case _ => expr } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition simplify( expr: Expression ): Expression = { def expr match { • Pattern case Add( e, Number( 0 ) ) => e Matching case Add( Number( 0 ), e ) => e case Mult( e, Number( 0 ) ) => Number( 0 ) case Mult( Number( 0 ), e ) => Number( 0 ) case Mult( e, Number( 1 ) ) => e • Modules case Mult( Number( 1only) if bound x equals Guard: Matches ), e => e bound y case Sub( Number( x ), Number( y ) ) if x == y => Number( 0 ) case Sub( Add( e, Number( x ) ), Number( y ) ) if x == y => e • Monads case Mult( e1, e2 ) => Mult( simplify( e1 ), simplify( e2 ) ) case _ => expr } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = • Pattern Mult( Sub( Add( Number( 1 ), Number( 4 ) ), Number( 4 ) ), Sub( Number(3), Number(2) ) ) Matching prettyPrint( expr ) => ( ( ( 1 + 4 ) - 4 ) * ( 3 - 2 ) ) • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = • Pattern Mult( Sub( Add( Number( 1 ), Number( 4 ) ), Number( 4 ) ), Sub( Number(3), Number(2) ) ) Matching prettyPrint( expr ) => ( ( ( 1 + 4 ) - 4 ) * ( 3 - 2 ) ) • Modules val sExpr = simplify( expr ) • Monads prettyPrint( sExpr ) => ( 1 * ( 3 - 2 ) ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = • Pattern Mult( Sub( Add( Number( 1 ), Number( 4 ) ), Number( 4 ) ), Sub( Number(3), Number(2) ) ) Matching prettyPrint( expr ) => ( ( ( 1 + 4 ) - 4 ) * ( 3 - 2 ) ) • Modules val sExpr = simplify( expr ) • Monads prettyPrint( sExpr ) => ( 1 * ( 3 - 2 ) ) Mult( Number(1), expr) should be expr !!! Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def simplify( expr: Expression ): Expression = { • Pattern expr match { Matching case ... case Mult( e1, e2 ) => { • Modules val se1 = simplify( e1 ) val se2 = simplify( e2 ) • Monads if( se1 != e1 || se2 != e2 ) simplify( Mult( se1, se2 ) ) else Mult( se1, se2 ) } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition def simplify( expr: Expression ): Expression = { • Pattern expr match { Matching case ... case Mult( e1, e2 ) => { • Modules val se1 = simplify( e1 ) val se2 = simplify( e2 ) • Monads if( se1 != e1 || se2 != e2 ) simplify( Mult( se1, se2 ) ) else Mult( se1, se2 ) } } (not) equals() on every case class provided ! Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A little 'Expression Language' • Composition val expr = • Pattern Mult( Sub( Add( Number( 1 ), Number( 4 ) ), Number( 4 ) ), Sub( Number(3), Number(2) ) ) Matching val sExpr = simplify( expr ) • Modules prettyPrint( sExpr ) => ( 3 - 2 ) • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features Some Pattern 'types' • Composition def matchAny( a: Any ) : Any { • Pattern a match { Matching case 1 => “one“ case “two“ => 2 case i: Int => “scala.Int“ • Modules case <tag>{ t }</tag> => t case head::tail => head • Monads case ( x, y ) => “tuple“ case _ => “anything else“ } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition • Pattern Matching • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition client • Pattern Module Matching public interface • Modules • Monads internal implementation Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition client Jav a • Pattern Matching package interface • Modules • Monads package internal Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ Module • Composition object interface{ public interface import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ internal • Modules import service.interface.TheService impl. private object ServiceHelper{ def print( it: String ) = println( it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ • Composition object interface{ Service interface import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ • Modules import service.interface.TheService private object ServiceHelper{ def print( it: String ) = println( it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ • Composition object interface{ import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ Nested Package • Modules import service.interface.TheService private object ServiceHelper{ def print( it: String ) = println( it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ • Composition object interface{ import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ • Modules import service.interface.TheService Local import private object ServiceHelper{ def print( it: String ) = println( it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ • Composition object interface{ import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ Only visible within • Modules import service.interface.TheService package this private object ServiceHelper{ def print( it: String ) = println( it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features package service{ • Composition object interface{ import service.internal._ • Pattern trait TheService{ def doIt( in: String ) } Matching } val getService: TheService = new ServiceImpl package internal{ Only visible within • Modules import service.interface.TheService package Only visible within this private object ServiceHelper{ this package, up to def print( it: String ) = println(package service it ) • Monads } private[service] class ServiceImpl extends TheService{ def doIt( in: String ) = ServiceHelper.print( in ) } } } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition package client{ import service.interface._ • Pattern object TheClient{ Matching val theService: TheService = getService theService.doIt( "hello" ); } • Modules } • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition package client{ importing all members import service.interface._ of the public • Pattern object TheClient{ interface object Matching val theService: TheService = getService theService.doIt( "hello" ); } • Modules } • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition package client{ import service.interface._ • Pattern import service.internal._ Matching object TheClient{ val theService: TheService = getService val theService = new ServiceImpl • Modules theService.doIt( "hello" ); } • Monads } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition package client{ import service.interface._ • Pattern import service.internal._ Matching object TheClient{ val theService: TheService = getService val theService = new ServiceImpl • Modules theService.doIt( "hello" ); } • Monads } Compile Error: ''class ServiceImpl cannot be accessed in package service.internal'' Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition • Pattern Matching • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition << abstract >> • Pattern Option[+A] Matching • Modules Some[+A] None • Monads presence absence Handling the or of something Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition class CustomerDAO{ • Pattern def findCustomer( custId: Long ) : Option<Customer> = { ... Matching if( found( customer ) ) Some( customer ) else None } } • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition class CustomerDAO{ • Pattern def findCustomer( custId: Long ) : Option<Customer> = { ... Matching if( found( customer ) ) Some( customer ) else None } } • Modules Explicit Notion, that there may be 'none' result • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val customerHit = customerDAO.findCustomer( 123 ); ... • Pattern customerHit match { Matching case Some( customer ) => println( customer.name ) case None => println( “not found“ ) } • Modules • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val customerHit = customerDAO.findCustomer( 123 ); ... • Pattern customerHit match { Matching case Some( customer ) => println( customer.name ) case None => println( “not found“ ) } • Modules Explicit Handling the absensce of a result • Monads Forces 'Awareness' Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val customerHit = customerDAO.findCustomer( 123 ); ... • Pattern customerHit match { Matching case Some( customer ) => println( customer.name ) case None => println( “not found“ ) } • Modules Explicit Handling the absensce of a result • Monads Forces 'Awareness' ... beside from that ... what's the deal ??? Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val customerHit = customerDAO.findCustomer( 123 ); ... • Pattern customerHit match { Matching case Some( customer ) => println( customer.name ) case None => println( “not found“ ) } • Modules Explicit Handling the absensce of a result • Monads Forces 'Awareness' ... beside from that ... what's the deal ??? 'Combination' !!! Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val projects = Map( "Jan" -> "IKT", "Joe" -> "TensE", • Pattern "Luca" -> "InTA" ) Matching val customers = Map( "IKT" -> "Hanso GmbH", "InTA" -> "RAIA Duo" ) • Modules val cities = Map( "Hanso GmbH" -> "Stuttgart", "Mogno" -> "Mailand" ) • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val projects = Map( "Jan" -> "IKT", "Joe" -> "TensE", • Pattern "Luca" -> "InTA" ) Matching val customers = Map( "IKT" -> "Hanso GmbH", "InTA" -> "RAIA Duo" ) • Modules val cities = Map( "Hanso GmbH" -> "Stuttgart", "Mogno" -> "Mailand" ) • Monads Where is Jan ? Jan -> IKT -> Hanso GmbH -> Stuttgart Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition val projects = Map( "Jan" -> "IKT", "Joe" -> "TensE", • Pattern "Luca" -> "InTA" ) Matching val customers = Map( "IKT" -> "Hanso GmbH", "InTA" -> "RAIA Duo" ) • Modules val cities = Map( "Hanso GmbH" -> "Stuttgart", "Mogno" -> "Mailand" ) • Monads Where is Luca ? Luca -> InTA -> RAIA Duo -> ??? ( 'unknown' ) Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Jav public String whereIs( String name ){ a String project = projects.get( name ); • Pattern if( project != null ){ Matching String customer = customers.get( project ); if( customer != null ){ • Modules String city = cities.get( customer ) if( city != null ) return city; • Monads else return “unknown“; } else return ''unknown''; } else return ''unknown''; } Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Results in Option[String] Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Results in Option[String] Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads Option[A].map( ( A ) => B ) => Option[B] Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Results in Option[String] Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads Option[A].map( ( A ) => B ) => Option[B] B -> Option[B] ) => Option[Option[B] Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Results in Option[String] Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads Option[A].map( ( A ) => B ) => Option[B] B -> Option[B] ) => Option[Option[B] ...flatmap( ( A ) => Option[B] ) => Option[B] Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } Alternative (else) if None • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Matching .flatMap( project => customers get project ) .flatMap( customer => cities get customer ) .getOrElse( "Unknown!" ) • Modules } • Monads ● No tests of absence during 'combination' of Maps projects, customers and cities necessary ● Option monad provides safe 'binding' of operations Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern projects.get( name ) Matching .flatMap( customers get ) .flatMap( cities get ) .getOrElse( "Unknown!" ) • Modules } shortcut for ( project => customers get project ) • Monads Mario Gleichmann XPUG Rhein/Main
  • (Some) Features A simple Monad: Option • Composition Scal a def whereIs( name: String ) = { • Pattern ( for( project <- projects get name; Matching customer <- customers get project; city <- cities get customer ) yield city • Modules ).getOrElse( "Unknown!" ) } • Monads ● Combination of Operations on Maps written as for-comprehension Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition • Pattern Matching • Modules • Monads • Any many more ... Mario Gleichmann XPUG Rhein/Main
  • (Some) Features • Composition Continuations (2.8) • Pattern View Bounds Named Parameters (2.8) Matching Nested Methods Extractor Objects • Modules Implicit Parameters • Monads (abstract) Type members • And many more ... Combinator Parsing Mario Gleichmann XPUG Rhein/Main
  • Summary Scala is ... • Object Oriented • Functional • Pragmatic • Scalable Mario Gleichmann XPUG Rhein/Main
  • Summary Thank you ! Mario Gleichmann XPUG Rhein/Main
  • Reference M.Odersky, L.Spoon, B.Venners 'Programming in Scala' (Artima Inc) Scala Home http://www.scala-lang.org Scala By Example http://www.scala-lang.org/docu/files/ScalaByExample.pdf Jonas Boner 'Pragmatic Real World Scala' http://www.infoq.com/presentations/Scala-Jonas-Boner D.Wampler, A.Payne 'Programming Scala' (O'Reilly) http://programming-scala.labs.oreilly.com/index.html James Strachan Scala as the long term replacement for java/javac? http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html Mario Gleichmann XPUG Rhein/Main