Scala for Jedi

  • View
    1.001

  • Download
    1

Embed Size (px)

DESCRIPTION

Второе выступление в рамках спецкурса "Немейнстримовые технологии разработки", читаемого в НГУ. http://bit.ly/mainstreamless Аудио дорожка работает, но нужно иметь некоторое терпение, так как грузится она не моментально.

Text of Scala for Jedi

  • 1. SCALA vladimir.parnenko@gmail.com @cypok

2. THE LIGHT SIDE 3. THE DARK SIDE 4. TYPES 5. OPTIONval set = Set(42, null, "Yoda")val found = set find { _ == null }if (found != null) {// huh, weve found it or not?println(s"Found $found!")} else {println("Not found.")} 6. OPTIONval set = Set(42, null, "Yoda")val found = set find { _ == null }found match {case Some(elem) => println(s"Found $elem!")case None => println("Not found.")}olation2 .10string interp 7. TYPES ON STEROIDSsealed abstract class Option[+A] {def get: A}final case class Some[+A](x: A) extends Option[A] {def get = x}case object None extends Option[Nothing] {def get = throw new NoSuchElementException("None.get")} 8. TYPE COVARIANCE not im2 .10 plemeclass Cell[T](x: T) {nte ddef get: T = ???def set(x: T) { ??? }}val c1 = new Cell[String]("Anakin")val c2: Cell[Any] = c1 // type mismatch 9. TYPE COVARIANCEclass Cell[+T](x: T) {def get: T = ???}val c1 = new Cell[String]("Anakin")val c2: Cell[Any] = c1val jedi = c2.get 10. TYPE COVARIANCEclass Cell[+T](x: T) {def get: T = ???def set(x: T) { ??? } // compilation error}val c1 = new Cell[String]("Anakin")val c2: Cell[Any] = c1c2.set(37) 11. TYPE COVARIANCEclass Cell[-T](x: T) {def get: T = ??? // compilation errordef set(x: T) { ??? }}val c1 = new Cell[Any](37)val c2: Cell[String] = c1val jedi = c2.get 12. TYPE COVARIANCEclass Cell[-T](x: T) {def set(x: T) { ??? }}val c1 = new Cell[Any](Anakin)val c2: Cell[String] = c1c2.set("Darth Vader") 13. LOWER & UPPER BOUNDS class List[+A] { def ++[B >: A](that: List[B]): List[B] = ??? } val strs = List("Leia", "Organa") val syms = List(Anakin, Skywalker) val all = strs ++ syms // List[java.io.Serializable] def max[A : A](implicit cmp: Ordering[B]): A = ???def max[A : Ordering]: A = ???} 15. TYPES HIERARCHYAnyAnyVal AnyRef/Object ScalaObjectValue classesPrimitives Java classes2.10Scala classesNullNothing 16. TYPES ON STEROIDSsealed abstract class Option[+A] {def get: A}final case class Some[+A](x: A) extends Option[A] {def get = x}case object None extends Option[Nothing] {def get = throw new NoSuchElementException("None.get")} 17. PARALLELISM & CONCURRENCY 18. PARALLEL COLLECTIONSval nums = (1 to 10).parnums foreach { x => print(x + " ") }// 1 2 8 9 10 7 5 6 3 4nums reduce { _ - _ }// -15nums reduce { _ - _ }// 5nums reduce { _ + _ }// 55val word = Seq("G", "r", "i", "e", "v", "o", "u", "s")word.par reduce { _ ++ _ }// Grievous 19. 2.10FUTURES , 20. FUTURESval f: Future[List[String]] = future {session.getRecentPosts}f onSuccess {case posts =>for (post println("An error has occured: " + t.getMessage)} 21. FORval files: Seq[File]for (file p.tryComplete(x)}g onSuccess {case x => p.tryComplete(x)}p.future} 29. 2.10ACTORS & AKKA 30. ACTORSclass MyActor extends Actor {def receive = {case "test" => log("received test")case _=> log("received unknown message")}}object Main extends App {val system = ActorSystem("MySystem")val myActor = system.actorOf(Props[MyActor], name = "myactor")myActor ! "test"} 31. ACTORSclass Squarer extends Actor {def receive = {case x: Int => sender ! (x * x)case x: Double => sender ! (x * x)}}class Mathematician extends Actor {val squarer = context.actorOf(Props[Squarer], name = "squarer")def receive = {case x: Int =>(squarer ? x) onComplete {case Success(x) => log(x)case Failure => fail()}}} 32. OTHER FEATURES 33. {on[ A] = tin)) : Opeatrait =>BoolProvi , p : (Adef sder { recLis t[A] } mth: Int@ tail A]( xs: f ind[p) deftch {ne (ta il,x s ma l = > No=> find classe NitaillseLazyO c as::hea d) e lazy ne ex tendshead) S ome( val s Provi caseead)??? mth = der { p(h {if ( }}}}assert(assertion: Boolean, message: => Any): Unitassert(true, { ??? })assert(false, { ??? })?my t = ??2.10 age: In?packo o {v al x t= ?? implicit class Rocla ss F e[this] y : In = ???ckStringvatval ntdef rock() = ??? (str: String) { prie[ Foo] al z: I}pr ivat [my] ve"foo".ropr ivat ck() } val xml = More!