SlideShare a Scribd company logo
Functions rock!Harnessing the Power of Functional Part I: Programming with ScalaTUTORIALQCON 2011, LondonProf. Dr. Michael StalMichael.Stal@siemens.com
Objectives of PresentationIntroducing core concepts of Functional ProgrammingIntroducing Scala as examplePresentingthe benefits of combining OO and functional programmingIllustrating the language in a pragmatic way preferring code over theoryBut not to cover every available aspect or to cover aspects in full  detailPage 2
Whatis Functional Programming?Praise the Lambda Calculus (which is almost 80 years old) and its successors  (e.g., the typed ones)(Mathematical) FunctionsAre a means of decompositionCan be assigned to variables Can be passed as arguments to or returned from functions Can be anonymous (closures)Emphasize on Immutability: No side-effects of functions (referential transparency)Values instead of variables  Page 3
Functional ProgrammingLanguages …Are oftenhybrid such as Lisp/Clojure, F#, ScalaUse Recursion instead of IterationCan be strict (eager) or non-strict (lazy)Use  mainly the Typed Lambda Calculus and thus support Pattern MatchingSupport concepts such as Monads, ContinuationsIntegrate Comprehensions for collection types, Catamorphisms (fold), Anamorphisms (unfold)Page 4
Preconception: Functional LanguagesareSlowIsnottrueanymoredue to:HighlyefficientVMslikethe CLR, JVMStructuralSharing, no naive copyingTailCallOptimization: at least someVMsVeryefficient and powerfulLibrariesEasy leveraging of Concurrency (e.g., because of immutablity)Page 5
Object-Oriented & Functional ProgrammingObject-Oriented ProgrammingAbstraction using Classes and InterfacesRefinement using subtyping and inheritance (Remember the Liskov Substitution Principle!)Dynamics through polymorphismFunctional ProgrammingHigher Order Functions as First-Class EntitiesADTs (Abstract Data Types) following algebraic conventionsPattern matchingParametric polymorphism (generic types)Page 6SCALA
Introduction to Scala 2.8.xScala created by the team of Martin Odersky at EPFL„Scala“ means „Scalable Language“„Scalable“ means: the same language concepts for programming in the small & largeIn Scala this is achieved in a pragmatic way by combining Object Oriented Programming withFunctional Programming Scala is a statically typed language like JavaAll types are objectsPage 7Martin Odersky, Source: http://lamp.epfl.ch/~odersky/
Core Properties of Scala (seeBookProgramming in Scala)Scala is compatible: runs on JVM, interoperability with JavaScala is concise: More compact code because removal of Java boilerplate code and powerful librariesScala is high-level: Higher level of abstraction by combining OO with functional programmingScala is statically typedPage 8Scala‘s Roots: Java, C#
 Smalltalk, Ruby
 Beta, Simula, Algol
 ML
 ErlangA small Appetizer - Hello, Scala!Page 9Type InferenceImmutable valuesClassesclass HelloWorldClass (val name: String) {	def print() = println("Hello World of " + name)}object HelloWorldMain {  	def main(args: Array[String]): Unit = {val hello = new HelloWorldClass("Scala")    		hello print  	}}=> Hello World of ScalaSingletonsLook Ma, no semicolonsNo brackets required!
Stairway to Heaven – First Steps using Scala This source code file may be compiled using scalac und run using scala:scalac HelloWorldMain.scalascala HelloWorldMain Note: the source file must be named like the main object to be executed You may run an interpreter by using the following command line insteadscala HelloWorldMain.scala In this case you may also use plain Scala scripts (no classes or objects required)Or, even better, you might use an IDE like Idea, Netbeans, or EclipsePage 10
Scala Type HierarchyPage 11Scala uses a pure object-oriented type systemEvery value is an objectTwo types: values and referencesAny is parent class of all classes, Nothing subclass of all classesBasictypes like inJavaSource: Scala Reference Manual
First Class ScalaPage 12born and id will bepublic fieldsMain constructorClasses in Scala contain fields, methods, types, constructorsVisibility is public per defaultclass CatID(val id : Int)  //that's a whole classclass Cat(val born: Int, val id: CatID) {private var miceEaten: Int = 0   def digested() = miceEatendef hunt(miceCaught: Int)              { miceEaten +=  miceCaught }}object ScalaClasses {   def main(args: Array[String]) {     val id = new CatID(42)     val tom = new Cat(2010, id)     tom.hunt(3)     tom hunt 2     println(“cat was born in “ + tom.born)      println(tom.digested)   }} // => 5 <\n> Tom was born in 2010 definitionof methodsNo bracketsrequired
Class ConstructorsPage 13class Complex (r : Double, i: Double) {  println("Constructing a complex number")  val re = r  val im = i  def this(r : Double) = this(r,0)  override def toString =         re + (if (im < 0) "-" + (-im)              else "+" + im) + "*i"  ...}Belongs toPrimaryconstructorAuxilliaryconstructorsmust callprimary
Immutable and Mutable ObjectsScala provides immutable objects (functional programming) but also mutable objectsImmutability has many benefitsReduction of race conditions in concurrent programsProtection against unwanted modificationScala provides mutable & immutable versions of collection typesMutable objects are important to address objects that are supposed to change their state, but use them with carePage 14class Person(var name: String)object ImmutabilityDemo { def main(args:Array[String]) = {val s = "Michael"   s = "Bill" // errorvar t = "Michael“ // t variable   t = "Bill" // ok   val c = new Person("Bill")   c = new Person("Tom") // error// ok - c itself unchanged:   c.name = "Tom" }}
InheritanceIn Scala classes can be derived from at most one base classClasses may be abstractYou need to indicate whether you override inherited methodsPage 15abstractclass Shape {type Identifier = Int // defining types  def getID() : Identifier = 42  def draw() : String   // abstract method}class Circle (val cx: Double, val cy: Double, val r: Double) extends Shape {  val id : Identifier = getID()override def draw() : String = "I am a Circle"}
Companion Objects and Standalone ObjectsWe already saw standalone objectsObjects are singletonsIf you need static fields or methods for a class, introduce a companion class with the same nameConvention: apply() methods may be provided as factory methodsPage 16class Cat private (val born : Int, val id: CatID) {   ...private def this(id: CatID) = this(2010, id)   ...} // all constructors are privateobject Cat { // this is the companion object of Catdef apply(born: Int, id: CatID) =  new Cat(born, id)   def apply(id: CatID) = new Cat(id) // factory method   def whatCatsDo() = "Sleep, eat, play"	}object ScalaClasses { // Standalone Object   def main(args: Array[String]) {     val id = new CatID(43)val pussy = Cat(id) // no new required     println("Pussy was born in " + pussy.born)     println(Cat.whatCatsDo)  // like static in Java     }}
Application Base ClassFor experimenting with Scala use the base class ApplicationJust compile this with: scalac ObjDemo.scalaAnd run it with: scala ObjDemoUseful abbreviation if you do not need to deal with command line argumentsPage 17  Inheritanceobject ObjDemoextends Application {val s: String = "Michael"println(s) // => Michael}
TraitsClasses and instances may mix-in additional functionality using traitsTraits represent an abstraction between interfaces and classesUsing traits we can easily live with the single-inheritance restrictionYou may use also traits via anonymous classes:	val x = new Identity{}  	x.name = "UFO"  	println(x.whoAmI)Page 18trait   Identity  {   var name: String=""   def whoAmI() : String = name	}class  Person(var name: String)         extends Identity class Animalobject TraitDemo { def main(args:Array[String]) = {   val p = new Person   p.name = "Michael"   println(p.whoAmI)val a = new Animal with Identity   a.name = "Kittie"   println(a.whoAmI) }} // => Michael <\n> Kittie
Traits and Virtual Super: Inheritance LinearizationPage 19abstract class Processor { def process() }trait Compressor extends Processor { // trait only applicable to Processor subclassabstract override def process() =  { println("I am compressing"); super.process }}trait Encryptor extends Processor { // only applicable to Processor subclassabstract override def process() = { println("I am encrypting"); super.process}}class SampleProcessor extends Processor { // subclass of Processor    override def process() = println("I am a Sample Processor")}object traitsample2 {    def main(args:Array[String]) = { // mixing in a trait to an object:val proc1 = new SampleProcessor with Compressor with Encryptorproc1.process// Encryptor.process=>Compressor.process     }                        //                                   => SampleProcessor.process}Note: abstract override for a trait method means the actual subclass of Processor will provide a concrete implementation of that method!
Scala Basics: if StatementsIf statements are expressions themselves, i.e. they have valuesPage 20import java.util._if (1 + 1 == 3) println(“strange world”) else {println(“everything’s ok”)}val res = if ((new Random().nextInt(6) + 1) == 6) 		"You win!" 	    else             "You lose!"
Scala Basics: for Comprehensions (1)A for comprehension is like a for loop. It lets you traverse a collection, return every object in a temporary variable which is then passed to an expression. You may also specify nested iterations:You can specify filters for the collection elementsPage 21val aList  = List(1,2,3,4,5,6)for (i <- aList) println(i) // => 1 <\n> 2 ...val dogs = Set("Lassie", "Lucy", "Rex", "Prince");for (a <- dogs if a.contains("L")) println(a)// => “Lassie” <\n> “Lucy”
Scala Basics: for Comprehensions (2)Yield allows to create new collections in a for comprehension:You can specify filters for the collection elementsPage 22var newSet = for {                   a <- dogs                   if a.startsWith("L")             } yield aprintln(newSet)  // Set(Lassie, Lucy)for {     i <- List(1,2,3,4,5,6)    j = i * 2 // new variable j defined} println(j) // => 2 <\n> 4 <\n> 6 ...
Scala Basics: Other loopsScala supports while and do-while loopsBut generator expressions such as (1 to 6)  incl. 6 or (1 until 6) excl. 6 together with for make this much easierNote: The reason this works is a conversion to RichInt where to is defined as a method that returns an object of type Range.Inclusive, an inner class of Range implementing for comprehensionsPage 23var i = 1while (i <= 6)  {    println(i)    i  += 1} // = 1 <\n> 2 <\n> 3 ... for (i <- 1 to 6) println(i)
Scala Basics: Exception handlingtry-catch-finally available in Scala but throws isn‘tcatching checked exceptions is optional!catch-order important as in Java, C++ or C#Page 24def temperature(f: Double) {  if (f < 0) throw new IllegalArgumentException()}try {   println("acquiring resources")   temperature(-5)}catch {   case ex:  IllegalArgumentException => println("temperatur < 0!")   case _ => println("unexpected problem")}finally {   println("releasing resources")}
Inner ClassesYou may define inner classes as in JavaSpecial notation (<name> =>)  for referring to outer class this from an inner class: you might also use <outerclass>.this insteadPage 25class Element (val id: String){ elem =>class Properties { // inner classtype KV = Tuple2[String, Any]    var props: List[KV] = Nil    def add(entry: KV) { props = entry :: props }    override def toString = {      var s: String = ""      for (p <- properties.props) s = s + p +"\n"      s    }}  override def toString = "ID = " + id + "\n" + properties  val properties = new Properties  }object InnerClassDemo extends Application {  val e = new Element("Window")  e.properties.add("Color", "Red")  e.properties.add("Version", 42)  println(e.toString)}
Imports and PackagesWe can partition Scala definitions into packages:A package is a special object which defines a set of member classes, objects and packages. Unlike other objects, packages are not introduced by a definitionImporting packages works via import statements – almost like in JavaPackages scala, java.lang, scala.Predefare automatically importedPage 26package MyPackage1 {   class Person(val name: String)   class Animal(val name: String)}import MyPackage1._object PacDemo extends Application {   val person = new Person("Michael")   println(person name)} import p._              all members of p (this is analogous                                    to import p.* in Java).  import p.x              the member x of p.
 import p.{x => a}   the member x of p renamed as a.
 import p.{x, y}       the members x and y of p.
 import p1.p2.z       the member z of p2, itself member of p1.Advanced Types: ListsCollection Type: ListPage 27object ListDemo { def main(args:Array[String]) {   val l1 : List[Int] = Nil // empty List   val l2 = List[Int](1,2,3,4,5) // list with 5 elements   val l3 = 0 :: l2 // add 0 to the list   val l4 = List[Int](0,1,2) :::  List[Int](3,4,5) // concat 2 lists   println("Top Element:  " + l4.head) // => 0   println("Rest of list: " + l4.tail) // => List(1,2,3,4,5)   println("Last Element: " + l4.last) // => 5l4.foreach { i => println(i) } // => 1 <\n> 2 <\n> 3 ... }}Foreach traverses a list and passes each element found to the closure passed as argument
Advanced Types: SetsCollection Type: SetPage 28object SetDemo { def main(args:Array[String]) {val s1 = Set[Int](1,2,3,4,5) // we could also use = Set(1,2,3,4,5)val s2 = Set[Int](2,3,5,7)println(s2.contains(3))  // => trueval s3 = s1 ++ s2 // unionprintln(s3) // => Set(5,7,3,1,4,2)vals4 = s1 & s2 // intersection: in earlier versions **println(s4) // Set(5,3,2)  }}
Advanced Types: MapsCollection Type: MapPage 29object MapDemo { def main(args:Array[String]) {val m1 = Map[Int, String](1 -> "Scala", 2->"Java", 3->"Clojure")valm2 = m1 + (4 -> "C#") // add entryprintln(m2 + " has size " + m2.size)                            //=> Map(1->Scala,…) has size 4println(m1(1)) // => Scalaval m3 = m2 filter { element => val (key, value) = element                    (value contains "a") }  println(m3) // => Map(1->Scala, w->Java) }}
Advanced Types: Optionsobject DayOfWeek extends Enumeration {  val Monday    = Value("Monday") //argument optional   val Sunday    = Value("Sunday") //argument optional }import DayOfWeek._ object OptionDemo { def whatIDo(day: DayOfWeek.Value) : Option[String] =     {    day match {      case Monday  => Some("Working hard")      case Sunday  => None    } } def main(args:Array[String]) {   println(whatIDo(DayOfWeek.Monday))    println(whatIDo(DayOfWeek.Sunday))  } //=> Some(„Working Hard“) <\n> None}The Option type helps dealing with optional valuesFor instance, a search operation might return  a result or nothingWe are also introducing EnumerationtypesPage 30
Advanced Types: Regular ExpressionsType: Regex introduces well-known regular expressionsPage 31With this syntax strings remain formatted as specified and escape sequences are not requiredobject RegDemo { def main(args:Array[String]) {val pattern = """\d\d\.\d\d\.\d\d\d\d""".rval sentence = “X was born on 01.01.2000 ?"println (pattern findFirstIn sentence) // => Some(01.01.2000) }}Note: the „r“ in “““<string>“““.r means: regular expression
Advanced Types: TuplesTuples combine fixed number of Elements of various typesThus, you are freed from creating heavy-weight classes for  simple aggregatesPage 32println( (1, "Douglas Adams", true) )def goodBook = {("Douglas Adams", 42, "Hitchhiker's Guide")}        println ( goodBook._3 ) // get third element// => (1, Douglas Adams, true)// => Hitchhiker‘s Guide
Advanced Types: ArraysArrays hold sequences of elementsAccess very efficientPage 33val a1 = new Array[Int](5) // initialized with zeros val a2 = Array(1,2,3,4,5) // initialized with 1,2,3,4,5println( a2(1) ) // => 2a2(1) = 1 // => Array (1,1,3,4,5)Note:In Scala the assignment operator = does not return a reference to the left variable (e.g., in a = b).Thus, the following is allowed in Java but not in Scala: a = b = c
Smooth OperatorIn Scala operator symbols are just plain method names For instance 1 + 2 stands for 1.+(2)Precedence rules:All letters|^&<  >=  !:+  -*  /  %Page 34class Complex(val re:Double, val im:Double) {def +(that: Complex) : Complex = {     new Complex(this.re + that.re,                  this.im + that.im)   }   override def toString() : String = {     re + (if (im < 0) "" else "+") + im +"i"   }}object Operators {  def main(args: Array[String]) {        val c1 = new Complex(1.0, 1.0)        val c2 = new Complex(2.0, 1.0)        println(c1+c2)  }} // => (3.0+2.0i)
ConversionsImplicit converters allow Scala to automatically convert data typesSuppose, you‘d like to introduce a mathematical notatation such as 10! Using implicit type converters you can easily achieve thisPage 35object Factorial {  def fac(n: Int): BigInt =    if (n == 0) 1 else fac(n-1) * n  class Factorizer(n: Int) {def ! = fac(n)  }implicit def int2fac(n: Int) = new Factorizer(n)}import Factorial._object ConvDemo extends Application {println("8! = " + (8!)) // 8 will be implicitly converted} // => 40320
Parameterized Types in ScalaClasses, Traits, Functions may be parameterized with typesIn contrast to Java no wildcards permitted – parameter types must have namesVariance specification allow to specify covariance and contravariancePage 36trait MyTrait[S,T] {  def print(s:S, t:T) : String = "(" + s  + "," + t + ")"}class MyPair[S,T] (val s : S, val t : T) extends MyTrait [S,T] {  override def toString() : String = print(s,t)	}object Generics {  def main(args: Array[String]) {	val m = new MyPair[Int,Int](1,1)	printf(m.toString())  }} // => (1,1)
Small Detour to Variance and Covariance / Type BoundsIf X[T] is a parameterized type and T an immutable type:X[T] is covariant in T if:	S subTypeOf T => X[S] subTypeOf  X[T]X[T] is contravariant in T if:  	S subTypeOf T => X[S] superTypeOf X[T]In Scala covariance is expressed as X[+T] and contravariance with X[-T]Covariance is not always what you want: Intuitively we could assign a set of apples to a set of fruits. However, to a set of fruits we can add an orange.  The original set of apples gets „corrupted“ this wayExample List[+T]: Covariance means a List[Int] can be assigned to a List[Any] because Int is subtype of AnyUpper/Lower Bounds may be specified:In the following example D must be supertype of S:	def copy[S, D>:S](src: Array[S], dst: Array[D]) = { ...Page 37
Functional Aspects: Functions and ClosuresIn Scala Functions are First-Class CitizensThey can be passed as argumentsassigned to variables:           val closure={i:Int => i+42}Nested functions are also supportedPage 38object scalafunctions {  def add(left:Int,right:Int, code:Int=>Int)=  {var res = 0     for (i<-left to right) res += code(i)     res  }  def main(args: Array[String]) {println(add(0,10, i => i))println(add(10,20, i => i % 2  ))  }}=>555
Functional Aspects: Call-by-NameIf a parameterless closure is passed as an argument to a function, Scala will evaluate the argument when the argument is actually usedThis is in contrast to call-by-value argumentsA similar effect can be achieved using lazy (value) evaluation:     lazy val = <expr>       Page 39import java.util._object CbNDemo {def fun(v: => Int) : Int = v  // v is a Call-by-Name Parameterdef v() : Int = new Random().nextInt(1000) def main(args:Array[String]) {   println( fun(v) )   println( fun(v) )   }} // => 123 <\n> 243
Functional Aspects: Currying (1)Currying means to transform a function with multiple arguments to a nested call of functions with one (or more) argument(s)def fun(i:Int)(j:Int) {}   (Int)=>(Int)=>Unit=<function1>Page 40object scalafunctions {   def fun1(i:Int, j:Int) : Int = i + jdef fun2(i:Int)(j:Int) : Int = i + j     def main(args: Array[String]) {	println(fun1(2,3))	println(fun2(2){3})	println(fun2{2}{3} )   }}  // => 5 5 5
Functional Aspects: Currying (2)Currying helps increase readabilityTake foldleft as an examplePage 41FoldLeft Operatorval x =  (0 /: (1 to 10)) { (sum, elem) => sum + elem } // 55 Carryover value for next iterationFunction argumentsCarryover valueCollectionFor each iteration, foldleft passes the carry over value and the current collection element. We need to provide the operation to be appliedThis is collection which we iterate overThis is the value that is updated in each iterationThink how this would be implemented in Java!
Positional ParametersIf you use a parameter only once, you can use positional notation of parameters with _ (underscore) insteadPage 42object scalafunctions { def main(args:Array[String]) {	val seq= (1 to 10)	println( (0 /: seq) { (sum, elem) => sum + elem } )	println( (0 /: seq) { _ + _ } )  }	}
Using the features we can build new DSLs and additional features easilyThe following loop-unless example is from the Scala tutorial Page 43object TargetTest2 extends Application {def loop(body: => Unit): LoopUnlessCond =    new LoopUnlessCond(body)protected class LoopUnlessCond(body: => Unit) {    def unless(cond: => Boolean) {      body      if (!cond) unless(cond)    }  }  var i = 10   loop {    println("i = " + i)    i -= 1  } unless (i == 0)}We are calling loopwith this body ...and invoking unlesson the result
Functional Aspect: Partially Applied FunctionsIf you only provide a subset of arguments to a function call, you actually retrieve a partially defined functionOnly the passed arguments are bound, all others are notIn a call to a partially applied function you need to pass the unbound argumentsAll this is useful to leverage the DRY principle when passing the same arguments again and againPage 44object scalafunctions {  def fun(a : Int, b : Int, c:Int) = a+b+c  def main(args: Array[String]) {val partialFun = fun(1,2,_:Int)	  println( partialFun(3) ) // 6	  println( partialFun(4) ) // 7	}}
Functions Are Objects Function: S => T trait Function1[-S,+T] {     def apply(x:S):T   }Example:  (x: Int) => x * 2-> new Function1[Int,Int] {     def apply(X:Int):Int = x * 2   }In Scala all function values are objectsBasically, each function is identical to a class with an apply methodThus, you can even derive subclasses from functionsArray is an example for this: class Array [T] (length: Int ) extends (Int => T)  {def length: Int = ...Page 45
Functional Aspects: Pattern MatchingPattern matching allows to make a  pragmatic choice between various optionsPage 46valaNumber = new Random().nextInt(6) + 1;aNumbermatch {case 6 => println("You got a 6")case 1 => println("You got a 1");caseotherNumber => println("It is a " + otherNumber)}
Functional Aspects: Matching on TypesIt is also possible to differentiate by type:Page 47object TypeCase {   def matcher(a: Any) {a match {	   case i : Int if (i == 42) => println("42")	   case j : Int => println("Another int")	   case s : String => println(s)	   case _  => println("Something else")        }   }   def main(args: Array[String]) {	matcher(42)	matcher(1)	matcher("OOP")	matcher(1.3)	   }} // => 41 <\n> 1  <\n> OOP <\n> Something else
Functional Aspects: Matching on ListsLists can be easily used with Pattern Matching:Page 48object ListCase {   def matcher(l: List[Int]) {l match {	   case List(1,2,3,5,7) => println("Primes")	   case List(_,_,_3,_) => println("3 on 3");	   case 1::rest => println("List with starting 1");	   case List(_*) => println("Other List");        }   }   def main(args: Array[String]) {	matcher(List(1,2,3,5,7))	matcher(List(5,4,3,2))	matcher(List(1,4,5,6,7,8));	matcher(List(42))   }} => Primes <\n> 3 on 3 <\n> List with starting 1 <\n> Other List
Functional Aspects: Matching on TuplesSo do Tuples:Page 49object TupleCase {   def matcher(t : Tuple2[String,String]) {t match {case („QCON",s) => println(„QCON " + s)  		case ("Scala", s) => println("Scala " + s)	   	case _ => println("Other Tuple")        }   }   def main(args: Array[String]) {matcher(„QCON", "2011")	matcher("Scala", "rocks");	matcher("A","B")   }} => QCON 2011 <\n> Scala rocks >cr> Other Tuple
Functional Aspects: Matching on Case ClassesCase Classes are immutable classes for which the compiler generates additional functionality to enable pattern matching, e.g., an apply() method:Page 50sealed abstract class Shape // sealed => subclasses only in this source filecase class Circle(val center: Point, val radius: Double) extends Shapecase class Line(val pt1: Point, val pt2: Point) extends Shapecase class Point (val x:Double, val y:Double){   override def toString() = "(" + x +"," + y + ")" }object CaseClasses {   def matcher(s : Shape) {s match {	   case Circle(c, r) => println(“Circle“ :  + c +  “ “ + r)	   case Line(p1, p2) => println("Line " + p1 + " : " + p2) 	   case _ => println("Unknown shape")        }   }   def main(args: Array[String]) {	matcher(Circle(Point(1.0, 1.0), 2.0))	matcher(Line(Point(1.0, 1.0), Point(2.0, 2.0)))   }}There are also case objects  !!!
Functional Aspect: ExtractorsExtractors are objects with an unapply method used to match a value and partition it into constituents – an optional apply is used for synthesisPage 51object EMail {  def apply(prefix: String, domain: String) = prefix + "@" + domaindef unapply(s: String): Option[(String,String)] = {   val parts = s split "@"   if (parts.length == 2) Some(parts(0), parts(1)) else None  } }object scalafunctions { def main(args:Array[String]) {   val s = "michael.stal@siemens.com"   s match {      case EMail(user, domain) => println(user + " AT " + domain)     case _ => println("Invalid e-mail")   }  }	}
Partial Functions Partial Functions are not defined for all domain valuesCan be asked with isDefinedAt whether a domain value is acceptedExample: Blocks of Pattern Matching CasesPage 52trait PartialFunction[-D, +T] extends (D => T) {  def isDefinedAt(x: D): Boolean}
Actors by ExamplePage 53!Note: react & receive have cousins with timeout arguments:   receiveWithin and reactWithinimport scala.actors._import Actor._object Calculator extends Actor {  def fib(n: Int) : Int = { require(n >= 0) // this is a precondition if (n <= 1) n else fib(n-2) + fib(n-1) }  def act() {loop {        react { // or receive if thread must preserve call-stack	       case i:Int => actor {println("Fibonacci of "+i+" is "+fib(i))}	       case s:String if (s == „exit")  => {println(„exit!"); exit}	       case _ => println("received unknown message")         }     }  }}object ActorDemo extends Application {   Calculator.start // start Actor   for (i <- 0 to 30) Calculator ! i // here we send a msg to the actorCalculator ! "exit"}
Processing XML in ScalaScala can directly handle XMLWith package scala.xml we can read, parse, create and store XML documentsXPath like query syntaxPage 54import scala.xml._ // in our example not requiredobject XMLDemo extends Application {   val x : scala.xml.Elem = <conferences>     <conference name=„QCON"> <year> 2011 </year> </conference>     <conference name=„GOTO"> <year> 2011 </year> </conference>   </conferences>   var conferenceNodes = x \ "conference„ // get all conference nodes   for (c <- conferenceNodes) println( c\"@name“ ) // get attribute} // => 	OOP <\n> SET
Accessing the Web with ScalaYou may use a mixture of Java and Scala code to access the WebSuppose, you‘d like to read a Web PageHere is an example how this might workPage 55import java.net._object WebDemo {  def main(args: Array[String]) {    require(args.length == 1)// we assume an URL was passed at the    // command line:val url = new URL(args(0)) // make URL// read web page stream and convert    // result to a string:    val page =   io.Source.fromURL(url).mkString    println(page)  // display result     }}
Combinator ParsingScala allows to implement DSL parsers (LL(1))For basic elements such as symbols, floats, strings etc. lexers are providedDevelopers can provide their own Let us use a simple example: a language that defines polygons as lists of points such as[(-1,0)(0,1)(+1,0)] which represents a triangleThe grammar is pretty simple:poly	::= coord *coord	::= “(“ number “,“ number “)“ number aka floatingPointNumber is predefinedPage 56
Build the Parser in Scala -  Part IPage 57import scala.util.parsing.combinator._class PolygonParserClass1 extends JavaTokenParsers {  def poly  : Parser[Any] = "["~rep(point)~"]"  def point : Parser[Any] = "("~floatingPointNumber~","~floatingPointNumber~")"}object PolygonParser1 extends PolygonParserClass1 {  def parse (input: String) = parseAll(poly, input)}object ParserDemo extends Application {println(PolygonParser1.parse("[(1,2)(3,7)]"))}=> parsed: (([~List((((((~1)~,)~2)~)), (((((~3)~,)~7)~))))~])~ 	: sequence		~>	: ignore left side of productionrep( )	: repetition		<~	: ignore right side of productionopt()	: option|	; alternative
Build the Parser in Scala -  Part 2Page 58Now, we would like to produce some code instead of just syntax checkingWe can add actions using ^^case class Point(x : Float, y : Float)class PolygonParserClass2 extends JavaTokenParsers {  def poly : Parser[List[Point]] = "["~>rep(coor)<~"]" ^^                                       { List[Point]() ++ _ }   def coor : Parser[Point] =        "("~floatingPointNumber~","~floatingPointNumber~")" ^^        { case "("~x~","~y~")"  => Point(x.toFloat,y.toFloat) }}object PolygonParser2 extends PolygonParserClass2 {  def parse (input: String) = parseAll(poly, input)} object ParserDemo extends Application {  println(PolygonParser2.parse("[(1,2)(3,7)]"))}=> parsed: List(Point(1.0,2.0), Point(3.0,7.0))
New in Version 2.8: Packrat ParsersPage 59With Packrat parsers you can handle left recursionIn addition parsing is possible in constant time but unlimited lookaheadimport scala.util.parsing.combinator._import scala.util.parsing.input.CharArrayReadercase class Point(x : Float, y : Float)object PackratParserDemo extends JavaTokenParsers with PackratParsers {  lazy val poly : Parser[List[Point]] = "["~>rep(coor)<~"]" ^^         { List[Point]() ++ _ }   lazy val coor : Parser[Point] =             "("~floatingPointNumber~","~floatingPointNumber~")" ^^         { case "("~x~","~y~")"  => Point(x.toFloat,y.toFloat) }}object ParserDemo extends Application {  println(PackratParserDemo.phrase(PackratParserDemo.poly)       (new CharArrayReader("[(1,2)(4,5)]".toCharArray)))}=> parsed: List(Point(1.0,2.0), Point(4.0,5.0))
What is new in Scala 2.8.xSo far we covered basically Scala 2.7In the meantime, the new Scala version 2.8 is available with some improvements, e.g.:collection library has been reorganized and optimizedtools improvements such as for REPLperformance improvementsFor details around Scala 2.8 refer to http://www.scala-lang.org/node/198Let me provide main changes of Scala version 2.8 in a Nutshellsee also Dean Wampler‘s blog: http://blog.objectmentor.com/articles/2009/06/05/bay-area-scala-enthusiasts-base-meeting-whats-new-in-scala-2-8Page 60
Scala 2.8 in a Nutshell - Default argumentsDefault arguments get a default valuePage 61object O {  def charge(amount: Double, Currency : String = “GBP"){    // do whatever necessary to get the moneyprintln(amount + " " + Currency)}def main(args : Array[String]) : Unit = {	  charge (200.0)	  charge (120.67, "EUR")  }}
Scala 2.8 in a Nutshell - Named argumentsNamed arguments help to call methods without being constrained by order of arguments Page 62object O {def welcome(firstName: String, lastName : String){println("Hello " + firstName + " " + lastName)  }def main(args : Array[String]) : Unit = {welcome(lastName= “Duck“, firstName = “Donald")  }}
Scala 2.8 in a Nutshell - AnnotationsAnnotations can be nested:@specialized annotation: Scala generics are fully specified at declaration place with a uniform implementation. This reduced performance for „primitive“ types (i.e., those derived from AnyVal). The annotation forces the compiler to generate an optimized version of the Generic for such types:Page 63@ExcellentClass(arg= @AuthorDetails)def m[@specialized T](x: T, f: T => T) = f(x)m(2, (x:Int) => x * 2)
Scala 2.8 in a Nutshell  -  ContinuationsContinuations are the most difficult to understand feature ( for an explanation I recommend http://www.slideshare.net/league/monadologie )Provided as a separate plug-inLook for continuations.jar in subfolder <scala2.8InstallDir>\misc\scala-devel\pluginsPage 64defdos = reset {println("look here")valx = 2 + cont* 4println(x)}defcont = shift {   k: (Int => Unit) => 	      k(2)println("ok")      k(3)        }                //  => look here <cr> 10 <cr> 14Use-P:continuations:enableas Compiler argument !!!
Scala 2.8 in a Nutshell – Package ObjectsWhat is the problem?If you move entities of an existing package to another one, your imports are brokenThis can be prevented by package objects... which represent collections of aliasesPage 65package object scala {// reference to class    type List[+A] = scala.collection.immutable.List[A]// reference to companion objectval List =scala.collection.immutable.List   …}
Scala 2.8 in a Nutshell – Case Classes Case classes were refinedFor example, a copy method is now availableIs helpful if you need copies of objects (maybe with minor changes)Page 66case class Circle(val center: Point, val radius: Double)// now you can use:val c_orig = Circle(1.0,2.0)val c_clon = c_orig.copy(radius = 2.2)
Page 67Scala Future
STM (Software Transactional Memory)Dealing with locking and thread-management is hardAgents are not a good solution for applications that share dataThis holds especially when combining multiple actions In database management systems, transactions come to our rescueSame can be done in memory using STMScalaSTM offers a good solution based on Clojure Page 68See also: http://www.scala-lang.org/node/8359
STM – The IdeaIdea: Separate object from identityIf object is going to be modified in a thread, let reference (identity) refer to new objectObjects themselves remain unchangedIf another thread has changed the object in the meantimeroll back changeotherwise commit changePage 69RefObjectval xObject‘
Example Code without STMPage 70object TestCounter {  var ctr = 0 // variable with no lock!  class IncrementerThread(howOften : Int) extends Thread {    override def run() {      for (m <- 0 until howOften) {          ctr += 1      }    }}  def runAll(nThreads : Int, howOften : Int) {    val pThreads = Array.tabulate(nThreads){_ => new                                     IncrementerThread(howOften) }    for (t <- pThreads) t.start()    for (t <- pThreads) t.join()  }  def main(args: Array[String]) { runAll(100, 100)     println("RESULT  " + ctr)  }}  // result will be usually less than 10000 due to raise conditions
package scala.concurrent.stmobject TestCounter {val ctr = Ref(0)  class IncrementerThread(howOften : Int) extends Thread {    override def run() {      for (m <- 0 until howOften) {atomic { implicit txn =>          ctr += 1        }      }    }  }  def runAll(nThreads : Int, howOften : Int) {    val pThreads = Array.tabulate(nThreads){_ => new                                IncrementerThread(howOften) }    for (t <- pThreads) t.start()    for (t <- pThreads) t.join()  }  def main(args: Array[String]) {  runAll(100, 100)    println("RESULT  " + ctr.single())}  // will always result in 10000} Example Code with STMScalaSTM libraryDefining a refAccess to refs only in atomic transaction blocksAllow single optransactionPage 71
STM Additional Featuresretry in atomic block allows to roll back and wait until input conditions change:Waiting for multiple events: if upper block retries, control is transferred to lower block:Page 72def maybeRemoveFirst(): Option[Int] = {atomic { implicit txn =>      Some(removeFirst())    } orAtomic { implicit txn =>      None    }  }// note: header.next and// header.prev are refsdef removeFirst(): Int = atomic { implicit txn =>    val n = header.next()    if (n == header)retry    val nn = n.next()    header.next() = nn    nn.prev() = header    n.elem  }
Homework for testing your Scala capabilitiesTry to read through the following example and figure out what it does and how it does what it doesPage 73
A more advanced Scala example (1)  (from Venkat Subramanian‘s book Programming Scala, pp.5)Page 74import scala.actors._import Actor._val symbols = List( "AAPL", "GOOG", "IBM", "JAVA", "MSFT")val receiver = selfval year = 2009symbols.foreach {   symbol =>  actor { receiver ! getYearEndClosing(symbol, year) }}val (topStock, highestPrice) = getTopStock(symbols.length)printf("Top stock of %d is %s closing at price %f\n", year, topStock, highestPrice)def getYearEndClosing(symbol : String, year : Int) = {    val url = new       java.net.URL("http://ichart.finance.yahoo.com/table.csv?s=" +    symbol + "&a=11&b=01&c=" + year + "&d=11&e=31&f=" + year + "&g=m")        val data = io.Source.fromURL(url).mkString    val price = data.split("\n")(1).split(",")(4).toDouble      (symbol, price)} // .. to be continued
A more advanced Scala example (2)(from Venkat Subramanian‘s book Programming Scala, pp.5)Run this within interpreter mode scala TopScala.scalaAfter the end of the talk return to this example and check whether you better understand itPage 75// continued ...def getTopStock(count : Int) : (String, Double) = {     (1 to count).foldLeft("", 0.0) { (previousHigh, index) =>         receiveWithin(10000) {             case (symbol : String, price : Double) =>                   if (price > previousHigh._2) (symbol, price)            else previousHigh         }     }} // will result in =>// Top stock of 2009 is GOOG closing at price 619,980000
Scala Installation & UseDownload distribution from http://www.scala-lang.orgYou may useScala Compilers: scalac and fscEclipse, JetBrains, NetBeans Plug-InREPL (Read-Eval-Print-Loop) shell: scalaI have tested these on Windows {XP, Vista, 7} as well as Mac OS X (Snow Leopard)Or a Web site for evaluating Scala scripts: http://www.simplyscala.com/If you are interested in a Web Framework based on Scala use Lift: http://liftweb.net/Page 76
Ad

More Related Content

What's hot (19)

C# in depth
C# in depthC# in depth
C# in depth
Arnon Axelrod
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
DIWE - Fundamentals of PHP
DIWE - Fundamentals of PHPDIWE - Fundamentals of PHP
DIWE - Fundamentals of PHP
Rasan Samarasinghe
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
jeffz
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
ParaSail
ParaSail  ParaSail
ParaSail
AdaCore
 
Modern C++
Modern C++Modern C++
Modern C++
Richard Thomson
 
DITEC - Programming with Java
DITEC - Programming with JavaDITEC - Programming with Java
DITEC - Programming with Java
Rasan Samarasinghe
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
Jack Nutting
 
C# / Java Language Comparison
C# / Java Language ComparisonC# / Java Language Comparison
C# / Java Language Comparison
Robert Bachmann
 
Scala
ScalaScala
Scala
Zhiwen Guo
 
Java8
Java8Java8
Java8
Felipe Mamud
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
Envision Computer Training Institute
 
Python Programming
Python ProgrammingPython Programming
Python Programming
Sreedhar Chowdam
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 

Similar to Qcon2011 functions rockpresentation_scala (20)

Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
Łukasz Wójcik
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
Venkateswaran Kandasamy
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
PrasannaKumar Sathyanarayanan
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Xebia IT Architects
 
Scala idioms
Scala idiomsScala idioms
Scala idioms
Knoldus Inc.
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
Isaias Barroso
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
Riccardo Cardin
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
Shai Yallin
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
Alf Kristian Støyle
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
Bert Van Vreckem
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
Miles Sabin
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgramming
Meir Maor
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Yardena Meymann
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
Oregon FIRST Robotics
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
Isaias Barroso
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
Riccardo Cardin
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
Shai Yallin
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
Miles Sabin
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgramming
Meir Maor
 
Ad

Recently uploaded (20)

P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Ad

Qcon2011 functions rockpresentation_scala

  • 1. Functions rock!Harnessing the Power of Functional Part I: Programming with ScalaTUTORIALQCON 2011, LondonProf. Dr. Michael [email protected]
  • 2. Objectives of PresentationIntroducing core concepts of Functional ProgrammingIntroducing Scala as examplePresentingthe benefits of combining OO and functional programmingIllustrating the language in a pragmatic way preferring code over theoryBut not to cover every available aspect or to cover aspects in full detailPage 2
  • 3. Whatis Functional Programming?Praise the Lambda Calculus (which is almost 80 years old) and its successors (e.g., the typed ones)(Mathematical) FunctionsAre a means of decompositionCan be assigned to variables Can be passed as arguments to or returned from functions Can be anonymous (closures)Emphasize on Immutability: No side-effects of functions (referential transparency)Values instead of variables Page 3
  • 4. Functional ProgrammingLanguages …Are oftenhybrid such as Lisp/Clojure, F#, ScalaUse Recursion instead of IterationCan be strict (eager) or non-strict (lazy)Use mainly the Typed Lambda Calculus and thus support Pattern MatchingSupport concepts such as Monads, ContinuationsIntegrate Comprehensions for collection types, Catamorphisms (fold), Anamorphisms (unfold)Page 4
  • 5. Preconception: Functional LanguagesareSlowIsnottrueanymoredue to:HighlyefficientVMslikethe CLR, JVMStructuralSharing, no naive copyingTailCallOptimization: at least someVMsVeryefficient and powerfulLibrariesEasy leveraging of Concurrency (e.g., because of immutablity)Page 5
  • 6. Object-Oriented & Functional ProgrammingObject-Oriented ProgrammingAbstraction using Classes and InterfacesRefinement using subtyping and inheritance (Remember the Liskov Substitution Principle!)Dynamics through polymorphismFunctional ProgrammingHigher Order Functions as First-Class EntitiesADTs (Abstract Data Types) following algebraic conventionsPattern matchingParametric polymorphism (generic types)Page 6SCALA
  • 7. Introduction to Scala 2.8.xScala created by the team of Martin Odersky at EPFL„Scala“ means „Scalable Language“„Scalable“ means: the same language concepts for programming in the small & largeIn Scala this is achieved in a pragmatic way by combining Object Oriented Programming withFunctional Programming Scala is a statically typed language like JavaAll types are objectsPage 7Martin Odersky, Source: http://lamp.epfl.ch/~odersky/
  • 8. Core Properties of Scala (seeBookProgramming in Scala)Scala is compatible: runs on JVM, interoperability with JavaScala is concise: More compact code because removal of Java boilerplate code and powerful librariesScala is high-level: Higher level of abstraction by combining OO with functional programmingScala is statically typedPage 8Scala‘s Roots: Java, C#
  • 11. ML
  • 12. ErlangA small Appetizer - Hello, Scala!Page 9Type InferenceImmutable valuesClassesclass HelloWorldClass (val name: String) { def print() = println("Hello World of " + name)}object HelloWorldMain { def main(args: Array[String]): Unit = {val hello = new HelloWorldClass("Scala") hello print }}=> Hello World of ScalaSingletonsLook Ma, no semicolonsNo brackets required!
  • 13. Stairway to Heaven – First Steps using Scala This source code file may be compiled using scalac und run using scala:scalac HelloWorldMain.scalascala HelloWorldMain Note: the source file must be named like the main object to be executed You may run an interpreter by using the following command line insteadscala HelloWorldMain.scala In this case you may also use plain Scala scripts (no classes or objects required)Or, even better, you might use an IDE like Idea, Netbeans, or EclipsePage 10
  • 14. Scala Type HierarchyPage 11Scala uses a pure object-oriented type systemEvery value is an objectTwo types: values and referencesAny is parent class of all classes, Nothing subclass of all classesBasictypes like inJavaSource: Scala Reference Manual
  • 15. First Class ScalaPage 12born and id will bepublic fieldsMain constructorClasses in Scala contain fields, methods, types, constructorsVisibility is public per defaultclass CatID(val id : Int) //that's a whole classclass Cat(val born: Int, val id: CatID) {private var miceEaten: Int = 0 def digested() = miceEatendef hunt(miceCaught: Int) { miceEaten += miceCaught }}object ScalaClasses { def main(args: Array[String]) { val id = new CatID(42) val tom = new Cat(2010, id) tom.hunt(3) tom hunt 2 println(“cat was born in “ + tom.born) println(tom.digested) }} // => 5 <\n> Tom was born in 2010 definitionof methodsNo bracketsrequired
  • 16. Class ConstructorsPage 13class Complex (r : Double, i: Double) { println("Constructing a complex number") val re = r val im = i def this(r : Double) = this(r,0) override def toString = re + (if (im < 0) "-" + (-im) else "+" + im) + "*i" ...}Belongs toPrimaryconstructorAuxilliaryconstructorsmust callprimary
  • 17. Immutable and Mutable ObjectsScala provides immutable objects (functional programming) but also mutable objectsImmutability has many benefitsReduction of race conditions in concurrent programsProtection against unwanted modificationScala provides mutable & immutable versions of collection typesMutable objects are important to address objects that are supposed to change their state, but use them with carePage 14class Person(var name: String)object ImmutabilityDemo { def main(args:Array[String]) = {val s = "Michael" s = "Bill" // errorvar t = "Michael“ // t variable t = "Bill" // ok val c = new Person("Bill") c = new Person("Tom") // error// ok - c itself unchanged: c.name = "Tom" }}
  • 18. InheritanceIn Scala classes can be derived from at most one base classClasses may be abstractYou need to indicate whether you override inherited methodsPage 15abstractclass Shape {type Identifier = Int // defining types def getID() : Identifier = 42 def draw() : String // abstract method}class Circle (val cx: Double, val cy: Double, val r: Double) extends Shape { val id : Identifier = getID()override def draw() : String = "I am a Circle"}
  • 19. Companion Objects and Standalone ObjectsWe already saw standalone objectsObjects are singletonsIf you need static fields or methods for a class, introduce a companion class with the same nameConvention: apply() methods may be provided as factory methodsPage 16class Cat private (val born : Int, val id: CatID) { ...private def this(id: CatID) = this(2010, id) ...} // all constructors are privateobject Cat { // this is the companion object of Catdef apply(born: Int, id: CatID) = new Cat(born, id) def apply(id: CatID) = new Cat(id) // factory method def whatCatsDo() = "Sleep, eat, play" }object ScalaClasses { // Standalone Object def main(args: Array[String]) { val id = new CatID(43)val pussy = Cat(id) // no new required println("Pussy was born in " + pussy.born) println(Cat.whatCatsDo) // like static in Java }}
  • 20. Application Base ClassFor experimenting with Scala use the base class ApplicationJust compile this with: scalac ObjDemo.scalaAnd run it with: scala ObjDemoUseful abbreviation if you do not need to deal with command line argumentsPage 17 Inheritanceobject ObjDemoextends Application {val s: String = "Michael"println(s) // => Michael}
  • 21. TraitsClasses and instances may mix-in additional functionality using traitsTraits represent an abstraction between interfaces and classesUsing traits we can easily live with the single-inheritance restrictionYou may use also traits via anonymous classes: val x = new Identity{} x.name = "UFO" println(x.whoAmI)Page 18trait Identity { var name: String="" def whoAmI() : String = name }class Person(var name: String) extends Identity class Animalobject TraitDemo { def main(args:Array[String]) = { val p = new Person p.name = "Michael" println(p.whoAmI)val a = new Animal with Identity a.name = "Kittie" println(a.whoAmI) }} // => Michael <\n> Kittie
  • 22. Traits and Virtual Super: Inheritance LinearizationPage 19abstract class Processor { def process() }trait Compressor extends Processor { // trait only applicable to Processor subclassabstract override def process() = { println("I am compressing"); super.process }}trait Encryptor extends Processor { // only applicable to Processor subclassabstract override def process() = { println("I am encrypting"); super.process}}class SampleProcessor extends Processor { // subclass of Processor override def process() = println("I am a Sample Processor")}object traitsample2 { def main(args:Array[String]) = { // mixing in a trait to an object:val proc1 = new SampleProcessor with Compressor with Encryptorproc1.process// Encryptor.process=>Compressor.process } // => SampleProcessor.process}Note: abstract override for a trait method means the actual subclass of Processor will provide a concrete implementation of that method!
  • 23. Scala Basics: if StatementsIf statements are expressions themselves, i.e. they have valuesPage 20import java.util._if (1 + 1 == 3) println(“strange world”) else {println(“everything’s ok”)}val res = if ((new Random().nextInt(6) + 1) == 6) "You win!" else "You lose!"
  • 24. Scala Basics: for Comprehensions (1)A for comprehension is like a for loop. It lets you traverse a collection, return every object in a temporary variable which is then passed to an expression. You may also specify nested iterations:You can specify filters for the collection elementsPage 21val aList = List(1,2,3,4,5,6)for (i <- aList) println(i) // => 1 <\n> 2 ...val dogs = Set("Lassie", "Lucy", "Rex", "Prince");for (a <- dogs if a.contains("L")) println(a)// => “Lassie” <\n> “Lucy”
  • 25. Scala Basics: for Comprehensions (2)Yield allows to create new collections in a for comprehension:You can specify filters for the collection elementsPage 22var newSet = for { a <- dogs if a.startsWith("L") } yield aprintln(newSet) // Set(Lassie, Lucy)for { i <- List(1,2,3,4,5,6) j = i * 2 // new variable j defined} println(j) // => 2 <\n> 4 <\n> 6 ...
  • 26. Scala Basics: Other loopsScala supports while and do-while loopsBut generator expressions such as (1 to 6) incl. 6 or (1 until 6) excl. 6 together with for make this much easierNote: The reason this works is a conversion to RichInt where to is defined as a method that returns an object of type Range.Inclusive, an inner class of Range implementing for comprehensionsPage 23var i = 1while (i <= 6) { println(i) i += 1} // = 1 <\n> 2 <\n> 3 ... for (i <- 1 to 6) println(i)
  • 27. Scala Basics: Exception handlingtry-catch-finally available in Scala but throws isn‘tcatching checked exceptions is optional!catch-order important as in Java, C++ or C#Page 24def temperature(f: Double) { if (f < 0) throw new IllegalArgumentException()}try { println("acquiring resources") temperature(-5)}catch { case ex: IllegalArgumentException => println("temperatur < 0!") case _ => println("unexpected problem")}finally { println("releasing resources")}
  • 28. Inner ClassesYou may define inner classes as in JavaSpecial notation (<name> =>) for referring to outer class this from an inner class: you might also use <outerclass>.this insteadPage 25class Element (val id: String){ elem =>class Properties { // inner classtype KV = Tuple2[String, Any] var props: List[KV] = Nil def add(entry: KV) { props = entry :: props } override def toString = { var s: String = "" for (p <- properties.props) s = s + p +"\n" s }} override def toString = "ID = " + id + "\n" + properties val properties = new Properties }object InnerClassDemo extends Application { val e = new Element("Window") e.properties.add("Color", "Red") e.properties.add("Version", 42) println(e.toString)}
  • 29. Imports and PackagesWe can partition Scala definitions into packages:A package is a special object which defines a set of member classes, objects and packages. Unlike other objects, packages are not introduced by a definitionImporting packages works via import statements – almost like in JavaPackages scala, java.lang, scala.Predefare automatically importedPage 26package MyPackage1 { class Person(val name: String) class Animal(val name: String)}import MyPackage1._object PacDemo extends Application { val person = new Person("Michael") println(person name)} import p._ all members of p (this is analogous to import p.* in Java). import p.x the member x of p.
  • 30. import p.{x => a} the member x of p renamed as a.
  • 31. import p.{x, y} the members x and y of p.
  • 32. import p1.p2.z the member z of p2, itself member of p1.Advanced Types: ListsCollection Type: ListPage 27object ListDemo { def main(args:Array[String]) { val l1 : List[Int] = Nil // empty List val l2 = List[Int](1,2,3,4,5) // list with 5 elements val l3 = 0 :: l2 // add 0 to the list val l4 = List[Int](0,1,2) ::: List[Int](3,4,5) // concat 2 lists println("Top Element: " + l4.head) // => 0 println("Rest of list: " + l4.tail) // => List(1,2,3,4,5) println("Last Element: " + l4.last) // => 5l4.foreach { i => println(i) } // => 1 <\n> 2 <\n> 3 ... }}Foreach traverses a list and passes each element found to the closure passed as argument
  • 33. Advanced Types: SetsCollection Type: SetPage 28object SetDemo { def main(args:Array[String]) {val s1 = Set[Int](1,2,3,4,5) // we could also use = Set(1,2,3,4,5)val s2 = Set[Int](2,3,5,7)println(s2.contains(3)) // => trueval s3 = s1 ++ s2 // unionprintln(s3) // => Set(5,7,3,1,4,2)vals4 = s1 & s2 // intersection: in earlier versions **println(s4) // Set(5,3,2) }}
  • 34. Advanced Types: MapsCollection Type: MapPage 29object MapDemo { def main(args:Array[String]) {val m1 = Map[Int, String](1 -> "Scala", 2->"Java", 3->"Clojure")valm2 = m1 + (4 -> "C#") // add entryprintln(m2 + " has size " + m2.size) //=> Map(1->Scala,…) has size 4println(m1(1)) // => Scalaval m3 = m2 filter { element => val (key, value) = element (value contains "a") } println(m3) // => Map(1->Scala, w->Java) }}
  • 35. Advanced Types: Optionsobject DayOfWeek extends Enumeration { val Monday = Value("Monday") //argument optional val Sunday = Value("Sunday") //argument optional }import DayOfWeek._ object OptionDemo { def whatIDo(day: DayOfWeek.Value) : Option[String] = { day match { case Monday => Some("Working hard") case Sunday => None } } def main(args:Array[String]) { println(whatIDo(DayOfWeek.Monday)) println(whatIDo(DayOfWeek.Sunday)) } //=> Some(„Working Hard“) <\n> None}The Option type helps dealing with optional valuesFor instance, a search operation might return a result or nothingWe are also introducing EnumerationtypesPage 30
  • 36. Advanced Types: Regular ExpressionsType: Regex introduces well-known regular expressionsPage 31With this syntax strings remain formatted as specified and escape sequences are not requiredobject RegDemo { def main(args:Array[String]) {val pattern = """\d\d\.\d\d\.\d\d\d\d""".rval sentence = “X was born on 01.01.2000 ?"println (pattern findFirstIn sentence) // => Some(01.01.2000) }}Note: the „r“ in “““<string>“““.r means: regular expression
  • 37. Advanced Types: TuplesTuples combine fixed number of Elements of various typesThus, you are freed from creating heavy-weight classes for simple aggregatesPage 32println( (1, "Douglas Adams", true) )def goodBook = {("Douglas Adams", 42, "Hitchhiker's Guide")} println ( goodBook._3 ) // get third element// => (1, Douglas Adams, true)// => Hitchhiker‘s Guide
  • 38. Advanced Types: ArraysArrays hold sequences of elementsAccess very efficientPage 33val a1 = new Array[Int](5) // initialized with zeros val a2 = Array(1,2,3,4,5) // initialized with 1,2,3,4,5println( a2(1) ) // => 2a2(1) = 1 // => Array (1,1,3,4,5)Note:In Scala the assignment operator = does not return a reference to the left variable (e.g., in a = b).Thus, the following is allowed in Java but not in Scala: a = b = c
  • 39. Smooth OperatorIn Scala operator symbols are just plain method names For instance 1 + 2 stands for 1.+(2)Precedence rules:All letters|^&< >= !:+ -* / %Page 34class Complex(val re:Double, val im:Double) {def +(that: Complex) : Complex = { new Complex(this.re + that.re, this.im + that.im) } override def toString() : String = { re + (if (im < 0) "" else "+") + im +"i" }}object Operators { def main(args: Array[String]) { val c1 = new Complex(1.0, 1.0) val c2 = new Complex(2.0, 1.0) println(c1+c2) }} // => (3.0+2.0i)
  • 40. ConversionsImplicit converters allow Scala to automatically convert data typesSuppose, you‘d like to introduce a mathematical notatation such as 10! Using implicit type converters you can easily achieve thisPage 35object Factorial { def fac(n: Int): BigInt = if (n == 0) 1 else fac(n-1) * n class Factorizer(n: Int) {def ! = fac(n) }implicit def int2fac(n: Int) = new Factorizer(n)}import Factorial._object ConvDemo extends Application {println("8! = " + (8!)) // 8 will be implicitly converted} // => 40320
  • 41. Parameterized Types in ScalaClasses, Traits, Functions may be parameterized with typesIn contrast to Java no wildcards permitted – parameter types must have namesVariance specification allow to specify covariance and contravariancePage 36trait MyTrait[S,T] { def print(s:S, t:T) : String = "(" + s + "," + t + ")"}class MyPair[S,T] (val s : S, val t : T) extends MyTrait [S,T] { override def toString() : String = print(s,t) }object Generics { def main(args: Array[String]) { val m = new MyPair[Int,Int](1,1) printf(m.toString()) }} // => (1,1)
  • 42. Small Detour to Variance and Covariance / Type BoundsIf X[T] is a parameterized type and T an immutable type:X[T] is covariant in T if: S subTypeOf T => X[S] subTypeOf X[T]X[T] is contravariant in T if: S subTypeOf T => X[S] superTypeOf X[T]In Scala covariance is expressed as X[+T] and contravariance with X[-T]Covariance is not always what you want: Intuitively we could assign a set of apples to a set of fruits. However, to a set of fruits we can add an orange. The original set of apples gets „corrupted“ this wayExample List[+T]: Covariance means a List[Int] can be assigned to a List[Any] because Int is subtype of AnyUpper/Lower Bounds may be specified:In the following example D must be supertype of S: def copy[S, D>:S](src: Array[S], dst: Array[D]) = { ...Page 37
  • 43. Functional Aspects: Functions and ClosuresIn Scala Functions are First-Class CitizensThey can be passed as argumentsassigned to variables: val closure={i:Int => i+42}Nested functions are also supportedPage 38object scalafunctions { def add(left:Int,right:Int, code:Int=>Int)= {var res = 0 for (i<-left to right) res += code(i) res } def main(args: Array[String]) {println(add(0,10, i => i))println(add(10,20, i => i % 2 )) }}=>555
  • 44. Functional Aspects: Call-by-NameIf a parameterless closure is passed as an argument to a function, Scala will evaluate the argument when the argument is actually usedThis is in contrast to call-by-value argumentsA similar effect can be achieved using lazy (value) evaluation: lazy val = <expr> Page 39import java.util._object CbNDemo {def fun(v: => Int) : Int = v // v is a Call-by-Name Parameterdef v() : Int = new Random().nextInt(1000) def main(args:Array[String]) { println( fun(v) ) println( fun(v) ) }} // => 123 <\n> 243
  • 45. Functional Aspects: Currying (1)Currying means to transform a function with multiple arguments to a nested call of functions with one (or more) argument(s)def fun(i:Int)(j:Int) {} (Int)=>(Int)=>Unit=<function1>Page 40object scalafunctions { def fun1(i:Int, j:Int) : Int = i + jdef fun2(i:Int)(j:Int) : Int = i + j def main(args: Array[String]) { println(fun1(2,3)) println(fun2(2){3}) println(fun2{2}{3} ) }} // => 5 5 5
  • 46. Functional Aspects: Currying (2)Currying helps increase readabilityTake foldleft as an examplePage 41FoldLeft Operatorval x = (0 /: (1 to 10)) { (sum, elem) => sum + elem } // 55 Carryover value for next iterationFunction argumentsCarryover valueCollectionFor each iteration, foldleft passes the carry over value and the current collection element. We need to provide the operation to be appliedThis is collection which we iterate overThis is the value that is updated in each iterationThink how this would be implemented in Java!
  • 47. Positional ParametersIf you use a parameter only once, you can use positional notation of parameters with _ (underscore) insteadPage 42object scalafunctions { def main(args:Array[String]) { val seq= (1 to 10) println( (0 /: seq) { (sum, elem) => sum + elem } ) println( (0 /: seq) { _ + _ } ) } }
  • 48. Using the features we can build new DSLs and additional features easilyThe following loop-unless example is from the Scala tutorial Page 43object TargetTest2 extends Application {def loop(body: => Unit): LoopUnlessCond = new LoopUnlessCond(body)protected class LoopUnlessCond(body: => Unit) { def unless(cond: => Boolean) { body if (!cond) unless(cond) } } var i = 10 loop { println("i = " + i) i -= 1 } unless (i == 0)}We are calling loopwith this body ...and invoking unlesson the result
  • 49. Functional Aspect: Partially Applied FunctionsIf you only provide a subset of arguments to a function call, you actually retrieve a partially defined functionOnly the passed arguments are bound, all others are notIn a call to a partially applied function you need to pass the unbound argumentsAll this is useful to leverage the DRY principle when passing the same arguments again and againPage 44object scalafunctions { def fun(a : Int, b : Int, c:Int) = a+b+c def main(args: Array[String]) {val partialFun = fun(1,2,_:Int) println( partialFun(3) ) // 6 println( partialFun(4) ) // 7 }}
  • 50. Functions Are Objects Function: S => T trait Function1[-S,+T] { def apply(x:S):T }Example: (x: Int) => x * 2-> new Function1[Int,Int] { def apply(X:Int):Int = x * 2 }In Scala all function values are objectsBasically, each function is identical to a class with an apply methodThus, you can even derive subclasses from functionsArray is an example for this: class Array [T] (length: Int ) extends (Int => T) {def length: Int = ...Page 45
  • 51. Functional Aspects: Pattern MatchingPattern matching allows to make a pragmatic choice between various optionsPage 46valaNumber = new Random().nextInt(6) + 1;aNumbermatch {case 6 => println("You got a 6")case 1 => println("You got a 1");caseotherNumber => println("It is a " + otherNumber)}
  • 52. Functional Aspects: Matching on TypesIt is also possible to differentiate by type:Page 47object TypeCase { def matcher(a: Any) {a match { case i : Int if (i == 42) => println("42") case j : Int => println("Another int") case s : String => println(s) case _ => println("Something else") } } def main(args: Array[String]) { matcher(42) matcher(1) matcher("OOP") matcher(1.3) }} // => 41 <\n> 1 <\n> OOP <\n> Something else
  • 53. Functional Aspects: Matching on ListsLists can be easily used with Pattern Matching:Page 48object ListCase { def matcher(l: List[Int]) {l match { case List(1,2,3,5,7) => println("Primes") case List(_,_,_3,_) => println("3 on 3"); case 1::rest => println("List with starting 1"); case List(_*) => println("Other List"); } } def main(args: Array[String]) { matcher(List(1,2,3,5,7)) matcher(List(5,4,3,2)) matcher(List(1,4,5,6,7,8)); matcher(List(42)) }} => Primes <\n> 3 on 3 <\n> List with starting 1 <\n> Other List
  • 54. Functional Aspects: Matching on TuplesSo do Tuples:Page 49object TupleCase { def matcher(t : Tuple2[String,String]) {t match {case („QCON",s) => println(„QCON " + s) case ("Scala", s) => println("Scala " + s) case _ => println("Other Tuple") } } def main(args: Array[String]) {matcher(„QCON", "2011") matcher("Scala", "rocks"); matcher("A","B") }} => QCON 2011 <\n> Scala rocks >cr> Other Tuple
  • 55. Functional Aspects: Matching on Case ClassesCase Classes are immutable classes for which the compiler generates additional functionality to enable pattern matching, e.g., an apply() method:Page 50sealed abstract class Shape // sealed => subclasses only in this source filecase class Circle(val center: Point, val radius: Double) extends Shapecase class Line(val pt1: Point, val pt2: Point) extends Shapecase class Point (val x:Double, val y:Double){ override def toString() = "(" + x +"," + y + ")" }object CaseClasses { def matcher(s : Shape) {s match { case Circle(c, r) => println(“Circle“ : + c + “ “ + r) case Line(p1, p2) => println("Line " + p1 + " : " + p2) case _ => println("Unknown shape") } } def main(args: Array[String]) { matcher(Circle(Point(1.0, 1.0), 2.0)) matcher(Line(Point(1.0, 1.0), Point(2.0, 2.0))) }}There are also case objects !!!
  • 56. Functional Aspect: ExtractorsExtractors are objects with an unapply method used to match a value and partition it into constituents – an optional apply is used for synthesisPage 51object EMail { def apply(prefix: String, domain: String) = prefix + "@" + domaindef unapply(s: String): Option[(String,String)] = { val parts = s split "@" if (parts.length == 2) Some(parts(0), parts(1)) else None } }object scalafunctions { def main(args:Array[String]) { val s = "[email protected]" s match { case EMail(user, domain) => println(user + " AT " + domain) case _ => println("Invalid e-mail") } } }
  • 57. Partial Functions Partial Functions are not defined for all domain valuesCan be asked with isDefinedAt whether a domain value is acceptedExample: Blocks of Pattern Matching CasesPage 52trait PartialFunction[-D, +T] extends (D => T) { def isDefinedAt(x: D): Boolean}
  • 58. Actors by ExamplePage 53!Note: react & receive have cousins with timeout arguments: receiveWithin and reactWithinimport scala.actors._import Actor._object Calculator extends Actor { def fib(n: Int) : Int = { require(n >= 0) // this is a precondition if (n <= 1) n else fib(n-2) + fib(n-1) } def act() {loop { react { // or receive if thread must preserve call-stack case i:Int => actor {println("Fibonacci of "+i+" is "+fib(i))} case s:String if (s == „exit") => {println(„exit!"); exit} case _ => println("received unknown message") } } }}object ActorDemo extends Application { Calculator.start // start Actor for (i <- 0 to 30) Calculator ! i // here we send a msg to the actorCalculator ! "exit"}
  • 59. Processing XML in ScalaScala can directly handle XMLWith package scala.xml we can read, parse, create and store XML documentsXPath like query syntaxPage 54import scala.xml._ // in our example not requiredobject XMLDemo extends Application { val x : scala.xml.Elem = <conferences> <conference name=„QCON"> <year> 2011 </year> </conference> <conference name=„GOTO"> <year> 2011 </year> </conference> </conferences> var conferenceNodes = x \ "conference„ // get all conference nodes for (c <- conferenceNodes) println( c\"@name“ ) // get attribute} // => OOP <\n> SET
  • 60. Accessing the Web with ScalaYou may use a mixture of Java and Scala code to access the WebSuppose, you‘d like to read a Web PageHere is an example how this might workPage 55import java.net._object WebDemo { def main(args: Array[String]) { require(args.length == 1)// we assume an URL was passed at the // command line:val url = new URL(args(0)) // make URL// read web page stream and convert // result to a string: val page = io.Source.fromURL(url).mkString println(page) // display result }}
  • 61. Combinator ParsingScala allows to implement DSL parsers (LL(1))For basic elements such as symbols, floats, strings etc. lexers are providedDevelopers can provide their own Let us use a simple example: a language that defines polygons as lists of points such as[(-1,0)(0,1)(+1,0)] which represents a triangleThe grammar is pretty simple:poly ::= coord *coord ::= “(“ number “,“ number “)“ number aka floatingPointNumber is predefinedPage 56
  • 62. Build the Parser in Scala - Part IPage 57import scala.util.parsing.combinator._class PolygonParserClass1 extends JavaTokenParsers { def poly : Parser[Any] = "["~rep(point)~"]" def point : Parser[Any] = "("~floatingPointNumber~","~floatingPointNumber~")"}object PolygonParser1 extends PolygonParserClass1 { def parse (input: String) = parseAll(poly, input)}object ParserDemo extends Application {println(PolygonParser1.parse("[(1,2)(3,7)]"))}=> parsed: (([~List((((((~1)~,)~2)~)), (((((~3)~,)~7)~))))~])~ : sequence ~> : ignore left side of productionrep( ) : repetition <~ : ignore right side of productionopt() : option| ; alternative
  • 63. Build the Parser in Scala - Part 2Page 58Now, we would like to produce some code instead of just syntax checkingWe can add actions using ^^case class Point(x : Float, y : Float)class PolygonParserClass2 extends JavaTokenParsers { def poly : Parser[List[Point]] = "["~>rep(coor)<~"]" ^^ { List[Point]() ++ _ } def coor : Parser[Point] = "("~floatingPointNumber~","~floatingPointNumber~")" ^^ { case "("~x~","~y~")" => Point(x.toFloat,y.toFloat) }}object PolygonParser2 extends PolygonParserClass2 { def parse (input: String) = parseAll(poly, input)} object ParserDemo extends Application { println(PolygonParser2.parse("[(1,2)(3,7)]"))}=> parsed: List(Point(1.0,2.0), Point(3.0,7.0))
  • 64. New in Version 2.8: Packrat ParsersPage 59With Packrat parsers you can handle left recursionIn addition parsing is possible in constant time but unlimited lookaheadimport scala.util.parsing.combinator._import scala.util.parsing.input.CharArrayReadercase class Point(x : Float, y : Float)object PackratParserDemo extends JavaTokenParsers with PackratParsers { lazy val poly : Parser[List[Point]] = "["~>rep(coor)<~"]" ^^ { List[Point]() ++ _ } lazy val coor : Parser[Point] = "("~floatingPointNumber~","~floatingPointNumber~")" ^^ { case "("~x~","~y~")" => Point(x.toFloat,y.toFloat) }}object ParserDemo extends Application { println(PackratParserDemo.phrase(PackratParserDemo.poly) (new CharArrayReader("[(1,2)(4,5)]".toCharArray)))}=> parsed: List(Point(1.0,2.0), Point(4.0,5.0))
  • 65. What is new in Scala 2.8.xSo far we covered basically Scala 2.7In the meantime, the new Scala version 2.8 is available with some improvements, e.g.:collection library has been reorganized and optimizedtools improvements such as for REPLperformance improvementsFor details around Scala 2.8 refer to http://www.scala-lang.org/node/198Let me provide main changes of Scala version 2.8 in a Nutshellsee also Dean Wampler‘s blog: http://blog.objectmentor.com/articles/2009/06/05/bay-area-scala-enthusiasts-base-meeting-whats-new-in-scala-2-8Page 60
  • 66. Scala 2.8 in a Nutshell - Default argumentsDefault arguments get a default valuePage 61object O { def charge(amount: Double, Currency : String = “GBP"){ // do whatever necessary to get the moneyprintln(amount + " " + Currency)}def main(args : Array[String]) : Unit = { charge (200.0) charge (120.67, "EUR") }}
  • 67. Scala 2.8 in a Nutshell - Named argumentsNamed arguments help to call methods without being constrained by order of arguments Page 62object O {def welcome(firstName: String, lastName : String){println("Hello " + firstName + " " + lastName) }def main(args : Array[String]) : Unit = {welcome(lastName= “Duck“, firstName = “Donald") }}
  • 68. Scala 2.8 in a Nutshell - AnnotationsAnnotations can be nested:@specialized annotation: Scala generics are fully specified at declaration place with a uniform implementation. This reduced performance for „primitive“ types (i.e., those derived from AnyVal). The annotation forces the compiler to generate an optimized version of the Generic for such types:Page 63@ExcellentClass(arg= @AuthorDetails)def m[@specialized T](x: T, f: T => T) = f(x)m(2, (x:Int) => x * 2)
  • 69. Scala 2.8 in a Nutshell - ContinuationsContinuations are the most difficult to understand feature ( for an explanation I recommend http://www.slideshare.net/league/monadologie )Provided as a separate plug-inLook for continuations.jar in subfolder <scala2.8InstallDir>\misc\scala-devel\pluginsPage 64defdos = reset {println("look here")valx = 2 + cont* 4println(x)}defcont = shift { k: (Int => Unit) => k(2)println("ok") k(3) } // => look here <cr> 10 <cr> 14Use-P:continuations:enableas Compiler argument !!!
  • 70. Scala 2.8 in a Nutshell – Package ObjectsWhat is the problem?If you move entities of an existing package to another one, your imports are brokenThis can be prevented by package objects... which represent collections of aliasesPage 65package object scala {// reference to class type List[+A] = scala.collection.immutable.List[A]// reference to companion objectval List =scala.collection.immutable.List …}
  • 71. Scala 2.8 in a Nutshell – Case Classes Case classes were refinedFor example, a copy method is now availableIs helpful if you need copies of objects (maybe with minor changes)Page 66case class Circle(val center: Point, val radius: Double)// now you can use:val c_orig = Circle(1.0,2.0)val c_clon = c_orig.copy(radius = 2.2)
  • 73. STM (Software Transactional Memory)Dealing with locking and thread-management is hardAgents are not a good solution for applications that share dataThis holds especially when combining multiple actions In database management systems, transactions come to our rescueSame can be done in memory using STMScalaSTM offers a good solution based on Clojure Page 68See also: http://www.scala-lang.org/node/8359
  • 74. STM – The IdeaIdea: Separate object from identityIf object is going to be modified in a thread, let reference (identity) refer to new objectObjects themselves remain unchangedIf another thread has changed the object in the meantimeroll back changeotherwise commit changePage 69RefObjectval xObject‘
  • 75. Example Code without STMPage 70object TestCounter { var ctr = 0 // variable with no lock! class IncrementerThread(howOften : Int) extends Thread { override def run() { for (m <- 0 until howOften) { ctr += 1 } }} def runAll(nThreads : Int, howOften : Int) { val pThreads = Array.tabulate(nThreads){_ => new IncrementerThread(howOften) } for (t <- pThreads) t.start() for (t <- pThreads) t.join() } def main(args: Array[String]) { runAll(100, 100) println("RESULT " + ctr) }} // result will be usually less than 10000 due to raise conditions
  • 76. package scala.concurrent.stmobject TestCounter {val ctr = Ref(0) class IncrementerThread(howOften : Int) extends Thread { override def run() { for (m <- 0 until howOften) {atomic { implicit txn => ctr += 1 } } } } def runAll(nThreads : Int, howOften : Int) { val pThreads = Array.tabulate(nThreads){_ => new IncrementerThread(howOften) } for (t <- pThreads) t.start() for (t <- pThreads) t.join() } def main(args: Array[String]) { runAll(100, 100) println("RESULT " + ctr.single())} // will always result in 10000} Example Code with STMScalaSTM libraryDefining a refAccess to refs only in atomic transaction blocksAllow single optransactionPage 71
  • 77. STM Additional Featuresretry in atomic block allows to roll back and wait until input conditions change:Waiting for multiple events: if upper block retries, control is transferred to lower block:Page 72def maybeRemoveFirst(): Option[Int] = {atomic { implicit txn => Some(removeFirst()) } orAtomic { implicit txn => None } }// note: header.next and// header.prev are refsdef removeFirst(): Int = atomic { implicit txn => val n = header.next() if (n == header)retry val nn = n.next() header.next() = nn nn.prev() = header n.elem }
  • 78. Homework for testing your Scala capabilitiesTry to read through the following example and figure out what it does and how it does what it doesPage 73
  • 79. A more advanced Scala example (1) (from Venkat Subramanian‘s book Programming Scala, pp.5)Page 74import scala.actors._import Actor._val symbols = List( "AAPL", "GOOG", "IBM", "JAVA", "MSFT")val receiver = selfval year = 2009symbols.foreach { symbol => actor { receiver ! getYearEndClosing(symbol, year) }}val (topStock, highestPrice) = getTopStock(symbols.length)printf("Top stock of %d is %s closing at price %f\n", year, topStock, highestPrice)def getYearEndClosing(symbol : String, year : Int) = { val url = new java.net.URL("http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&a=11&b=01&c=" + year + "&d=11&e=31&f=" + year + "&g=m") val data = io.Source.fromURL(url).mkString val price = data.split("\n")(1).split(",")(4).toDouble (symbol, price)} // .. to be continued
  • 80. A more advanced Scala example (2)(from Venkat Subramanian‘s book Programming Scala, pp.5)Run this within interpreter mode scala TopScala.scalaAfter the end of the talk return to this example and check whether you better understand itPage 75// continued ...def getTopStock(count : Int) : (String, Double) = { (1 to count).foldLeft("", 0.0) { (previousHigh, index) => receiveWithin(10000) { case (symbol : String, price : Double) => if (price > previousHigh._2) (symbol, price) else previousHigh } }} // will result in =>// Top stock of 2009 is GOOG closing at price 619,980000
  • 81. Scala Installation & UseDownload distribution from http://www.scala-lang.orgYou may useScala Compilers: scalac and fscEclipse, JetBrains, NetBeans Plug-InREPL (Read-Eval-Print-Loop) shell: scalaI have tested these on Windows {XP, Vista, 7} as well as Mac OS X (Snow Leopard)Or a Web site for evaluating Scala scripts: http://www.simplyscala.com/If you are interested in a Web Framework based on Scala use Lift: http://liftweb.net/Page 76
  • 82. Scala TestScala Test offers a test framework for ScalaFacilitates different flavors of testing: TestNG, JUnit4, JUnit3Leverages the conciseness of Scala Official site: http://www.scalatest.org/Latest version 1.2Good presentation webcast with Bill Venners: http://www.parleys.com/#id=1552&st=5Page 77
  • 83. specsprovides BDD (Business-Driven Development) for ScalaMain site: http://code.google.com/p/specs/Good introductory presentation available in http://www.slideshare.net/etorreborre/oscon-2010-specs-talkPage 78
  • 84. Tools, tools, toolsA lot of excellent tools and frameworks available:If you are interested in a Web Framework based on Scala use Lift: http://liftweb.net/Use the sbt (simple-build-tool) as the ant-tool for Scala: http://code.google.com/p/simple-build-tool/Use sbaz (Scala Bazaar) which is a package installation and maintenance system: it is already available in your Scala bin directory!Page 79
  • 85. SummaryScala combines the best of two worlds: OO and Functional ProgrammingIt runs on the JVM offering Java interopActor library helps dealing with complexity of concurrent programming Scala programs are compact and concise => big productivity boost possiblev2.8.x offers additional benefits such as named & default arguments, continuationsMore to come in v.2.9++ such as STM, Parallel CollectionsScala is no island - many further tools and frameworks (e.g., Lift) availableCoding with Scala is fun - Try it yourself!Page 80
  • 86. Books: My RecommendationsM. Odersky, L. Spoon, B. Venners: Programming in Scala: A Comprehensive Step-by-step Guide (Paperback), Artima Inc; 1st edition (November 26, 2008) – The Language Reference! 2nd edition soon!V. Subramanian: Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine (Pragmatic Programmers) (Paperback), Pragmatic Bookshelf (July 15, 2009) D. Wampler, A. Paine: Programming Scala: Scalability = Functional Programming + Objects (Animal Guide) (Paperback), O'Reilly Media; 1st edition (September 25, 2009)A lot of additional books available in the meantime. Page 81