1 / 75

Scala programming language

Scala programming language. Martin Kon íček. Scala on JVM. scalac compiles Scala to Java bytecode (regular .class files) Any Java class can be used from Scala. Origin. Started at 2001 by Martin Odersky at EPFL Lausanne, Switzerland Scala 2.0 released in 2006 Current version 2.7

dalit
Download Presentation

Scala programming language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scala programming language Martin Koníček

  2. Scala on JVM • scalac compiles Scala to Java bytecode • (regular .class files) • Any Java class can be used from Scala

  3. Origin • Started at 2001 by Martin Odersky at EPFL Lausanne, Switzerland • Scala 2.0 released in 2006 • Current version 2.7 • Twitter backend runs on Scala

  4. Scala properties • Object oriented • Statically typed • Functional & imperative

  5. Static typing • Type checking done at compile time • Type associated with variable, not value • Better tools possible • More verbose code compared to dynamic language • Can’t add methods to class at runtime • No duck typing – really?

  6. Functional programming • Functions are first class citizens • Immutability • Tuples • Currying • Recursion • Monads

  7. Introduction Demo of Scala interpreter

  8. Variables & values, type inference varmsg = "Hello“// msg is mutable msg+=" world" msg = 5;// compiler error

  9. Variables & values, type inference valmsg = "Hello world“ // msg is immutable msg += " world“// compiler error valn : Int = 3// explicit type declaration varn2 : Int = 3

  10. Immutability • Why? • Immutable objects are automatically thread-safe (you don’t have to worry about object being changed by another thread) • Compiler can reason better about immutable values -> optimization • Steve Jenson from Twitter: “Start with immutability, then use mutability where you find appropriate.”

  11. Calling Java from Scala • Any Java class can be used seamlessly importjava.io._ valurl = newURL("http://www.scala-lang.org") demo

  12. Methods defmax(x : Int, y : Int) = if (x>y) xelsey // equivalent: defneg(x : Int) : Int = -x defneg(x : Int) : Int = { return –x; }

  13. Types • Int, Double, String, Char, Byte, BigInt, … • wrappers around Java types

  14. Lists • Lists are immutable (= contents cannot be changed) • List[String] contains Strings vallst = List("b", "c", "d") lst.head// “b” lst.tail// List(“c”, “d”) vallst2 = "a"::lst// cons operator

  15. Lists • Nil = synonym for empty list vall = 1 :: 2 :: 3 ::Nil • List concatenation vall2 = List(1, 2, 3) :::List(4, 5)

  16. Foreach vallist3 = List("mff", "cuni", "cz") • Following 3 calls are equivalent list.foreach((s : String) => println(s)) list.foreach(s => println(s)) list.foreach(println)

  17. For comprehensions for (s <- list) println(s) for (s <- listifs.length() == 4) println(s) • for just calls foreach

  18. Arrays • Lists are immutable, arrays are mutable vala = Array("Java", "rocks") a(0) = "Scala";

  19. Covariance • Lists are covariant, arrays are invariant // compiler error valarray : Array[Any] = Array(1, 2, 3); // ok vallist : List[Any] = List(1, 2, 3);

  20. Arrays valgreets = newArray[String](2) greets(0) = "Hello" greets(1) = "world!\n" for (i <- 0 to 1) print(greets(i))

  21. Arrays are no special type greets(i) === greets.apply(i) greets(i) = "Hi" === greets.update(i, "Hi") • Any class that defines apply / update can be used like this

  22. Every operation is a method call • “to” is not a keyword • for (i <- 0 to 2) print(greets(i)) • 0 to 2 === 0.to(2) • x – 1 === x.-(1) • map containsKey ‘a’ === map.containsKey(‘a’)

  23. Associativity • If method name ends with colon, the method is invoked on the right operand vallist = List("b", "c") "a"::list===list.::("a")

  24. Performance • Scala treats everything as objects • no primitive types, no arrays • So this comes with a cost, right? • Usually not, the scalac compiler uses Java primitive types and arrays where possible

  25. Anonymous functions vall = new List("mff", "cuni", "cz") l.filter(s => s.length == 4) vall = List[Person](newPerson(…), …) l.sort((p1, p2) => p1.lastName < p2.lastName)

  26. Currying • Function with only some arguments specified = function expecting the rest of the arguments • Common concept in functional languages

  27. Currying // Does n divide m? defnDividesM(m : Int)(n : Int) = (n%m== 0) // Currying, // isEven is of type (Int) => Boolean valisEven = nDividesM(2)_ println(isEven(4)) println(isEven(5))

  28. Tuples • Sequence of elements with different types (10, List('c', 'm'), "cache"); • type of this expression is Tuple3[Int, List[Char], String]

  29. Tuples defdivMod(x : Int, y : Int) = (x/y, x%y) valdm = divMod(10, 3) // Tuple2[Int, Int] dm._1// 3 dm._2// 1 val (d, m) = divMod(10, 3); println(d+" "+m);// 3 1

  30. Pattern matching • Like switch statement • But much more powerful

  31. Pattern matching defflatten(list: List[Any]) : List[Any] = listmatch { case (x: List[Any]) ::xs => flatten(x) :::flatten(xs) casex::xs => x::flatten(xs) caseNil => Nil } valnested = List(1, List(2, 3), 4); valflat = flatten(nested); // List(1, 2, 3, 4)

  32. Classes /** A Person class. * Constructor parameters become * public members of the class.*/ classPerson(valname: String, varage: Int){ if (age< 0) { throw … } } varp = newPerson(“Peter", 21); p.age+= 1;

  33. Objects • Scala’s way for “statics” • not quite – see next slide • (in Scala, there is no static keyword) • “Companion object” for a class • = object with same name as the class demo

  34. Objects // we declare singleton object "Person" // this is a companion object of class Person objectPerson { defdefaultName() = "nobody" } classPerson(valname: String, varage: Int) { defgetName() : String = name } // surprise, Person is really an object valsingleton : Person = Person;

  35. Case classes • Implicitely override toString, equals, hashCode • take object’s structure into account abstractclassExpr caseclassNumber(n: Int) extendsExpr caseclassSum(e1: Expr, e2: Expr) extendsExpr // true thanks to overriden equals Sum(Number(1), Number(2)) == Sum(Number(1), Number(2))

  36. Case classes • Needed if we want to pattern match on class hiearchies defeval(e: Expr): Int = ematch { caseNumber(n) => n caseSum(l, r) => eval(l) +eval(r) }

  37. Exceptions objectMain { defmain(args: Array[String]) { try { valelems = args.map(Integer.parseInt) println("Sum is: "+elems.foldRight(0) (_ + _)) } catch { casee: NumberFormatException => println("Usage: scala Main <n1> <n2> ... ") } } }

  38. Traits

  39. Traits • Like Java interfaces • But can contain implementations and fields traitPet { varage:Int = 0 defgreet(): String = { return"Hi" } }

  40. Extending traits classDogextendsPet { overridedefgreet() = "Woof" } traitExclamatoryGreeterextendsPet { overridedefgreet() = super.greet() +" !" }

  41. Traits - mixins • Traits can be “mixed in” at instation time traitExclamatoryGreeterextendsPet { overridedefgreet() = super.greet() +" !" } val pet = newDogwithExclamatoryGreeter println(pet.greet())// Woof !

  42. Traits – common use traitOrdered[A] { defcompare(that: A): Int // abstract method def< (that: A): Boolean = (thiscomparethat) < 0 def> (that: A): Boolean = (thiscomparethat) > 0 } classHealth(valvalue : Int) extendsOrdered[Health] { overridedefcompare(other : Health) = { this.value-other.value; } defisCritical() = … }

  43. Maps and Sets

  44. Map – simple example importscala.collection._ valcache = newmutable.HashMap[String,String]; cache +="foo"->"bar"; valc = cache("foo"); • The rest of Map and Set interface looks as you would expect

  45. ListBuffer • ListBuffer[T] is a mutable List • Like Java’s ArrayList<T> importscala.collection.mutable._ vallist = newListBuffer[String] list+="Vicky" list+="Christina" valstr = list(0)

  46. Option • Like“Maybe” in Haskell • Example – 3 state Boolean varsure : Option[Boolean] = Some(false); sure = Some(true); sure = None;

  47. Actors

  48. Actors • Concurrency using threads is hard • Shared state – locks, race conditions, deadlocks • Solution – message passing + no shared state • Inspired by Erlang language • Erlang used at Ericsson since 1987, open source since 1998 • Facebook chat backend runs on Erlang

  49. What is an actor • Actor is an object that receives messages • Actor has a mailbox – queue of incoming messages • Message send is by default asynchronous • Sending a message to an actor immediately returns

  50. Actors – trivial example • We define messages caseobjectMsgPing caseobjectMsgPong caseobjectMsgStop

More Related