Scala for Jedi

Download Scala for Jedi

Post on 22-Jun-2015

992 views

Category:

Technology

1 download

Embed Size (px)

DESCRIPTION

" ", . http://bit.ly/mainstreamless , , .

TRANSCRIPT

<ul><li> 1. SCALA vladimir.parnenko@gmail.com @cypok</li></ul> <p> 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) =&gt; println(s"Found $elem!")case None =&gt; 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 &amp; UPPER BOUNDS class List[+A] { def ++[B &gt;: 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 &amp; CONCURRENCY 18. PARALLEL COLLECTIONSval nums = (1 to 10).parnums foreach { x =&gt; 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 =&gt;for (post println("An error has occured: " + t.getMessage)} 21. FORval files: Seq[File]for (file p.tryComplete(x)}g onSuccess {case x =&gt; p.tryComplete(x)}p.future} 29. 2.10ACTORS &amp; AKKA 30. ACTORSclass MyActor extends Actor {def receive = {case "test" =&gt; log("received test")case _=&gt; 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 =&gt; sender ! (x * x)case x: Double =&gt; sender ! (x * x)}}class Mathematician extends Actor {val squarer = context.actorOf(Props[Squarer], name = "squarer")def receive = {case x: Int =&gt;(squarer ? x) onComplete {case Success(x) =&gt; log(x)case Failure =&gt; fail()}}} 32. OTHER FEATURES 33. {on[ A] = tin)) : Opeatrait =&gt;BoolProvi , p : (Adef sder { recLis t[A] } mth: Int@ tail A]( xs: f ind[p) deftch {ne (ta il,x s ma l = &gt; No=&gt; 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: =&gt; 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!</p>