class: center, middle # Quick Tour of Scala 2.13 Ross A. Baker • `@rossabaker` ??? - notes here - see https://remarkjs.com/#1 --- # flatMaps Haskell ```haskell (>>=) :: forall a b . Monad m => m a -> (a -> m b) -> m b ``` -- Java ```java public
Stream
flatMap( Function super T,? extends Stream extends R>> mapper) ``` -- Kotlin ```kotlin inline fun
Iterable
.flatMap( transform: (T) -> Iterable
): List
``` -- Scala 2.12 ```scala final def flatMap[B, That](f: (A) ⇒ GenTraversableOnce[B])( implicit bf: CanBuildFrom[List[A], B, That]): That ``` --- # Without `CanBuildFrom` ```scala def flatMap[B](f: (A) => IterableOnce[B]): LazyList[B] ``` -- * `LazyList[A]` extends `IterableOps[+A, CC[_], +C]` * `A` is the `element type` * `CC` is the type constructor of the collection (`LazyList`) * `C` is the type of the collection (`LazyList[A]`) -- * Ops that return a collection of a different type `B` return `CC[B]` * Ops that return a collection of the same type return `C` * What this doesn't cover is covered by simple overloading. --- # And where did `GenTraversableOnce` go? -- * `Traversable` is deprecated in favor of `Iterable` -- * Parallel collections exiled to module, so `Gen*` traits gone. --- # And what's a `LazyList`? -- * It replaces `Stream`. -- * Both head and tail are lazy. ```scala // Memoizes 10 elements LazyList.from(0).drop(10000000).take(10).toList // Memoizes 10000010 elements Stream.from(0).drop(10000000).take(10).toList ``` --- # `collection.Seq` ```scala scala> val v = collection.mutable.Queue.fill(3)(0) v: scala.collection.mutable.Queue[Int] = Queue(0, 0, 0) scala> val v = collection.mutable.Buffer.fill(3)(0) v: scala.collection.mutable.Buffer[Int] = ArrayBuffer(0, 0, 0) scala> val seq: collection.Seq[Int] = v seq: scala.collection.Seq[Int] = ArrayBuffer(0, 0, 0) scala> v(1) = 40 scala> seq res4: scala.collection.Seq[Int] = ArrayBuffer(0, 40, 0) ``` --- # collection.immutable.Seq ```scala scala> val v = collection.mutable.Queue.fill(3)(0) v: scala.collection.mutable.Queue[Int] = Queue(0, 0, 0) scala> val seq: collection.immutable.Seq[Int] = v ^ error: type mismatch; found : scala.collection.mutable.Queue[Int] required: Seq[Int] ``` * `collection.immutable.Seq` is now the default `Seq` * This is a good thing. * But be explicit in cross-built libraries! --- # On the subject of cross-building [scala-collection-compat](https://github.com/scala/scala-collection-compat) is: * a set of shims for libraries that crossbuild across the 2.12/2.13 divide * a set of scalafixes to help migrate your code --- # Parasitic execution context -- * Runs on the calling thread -- * Saves a context switch for _fast_, _non-blocking_ operations -- * Stack safe -- * Deadlocks when you mess up --- # Fun additions * String interpolator extractors: `val s"$day-$month-$year" = "11-June-2019"` -- * More concise upcasting of `Left` and `Right`: `Left(3).withRight[String]` -- * `@unused` annotation to suppress warnings --- # Addition I don't like ```scala val lines: Seq[String] = Using.resource(new BufferedReader(new FileReader("file.txt"))) { reader => Iterator.unfold(())(_ => Option(reader.readLine()).map(_ -> ())).toList } ``` * Uncontrolled side effects * No cancellation * Bewildering exception suppression logic * If you want this, you're ready for cats-effect --- # Subtraction I love * `any2StringAdd` is deprecated! ```scala scala> 2 + "3" ^ warning: method + in class Int is deprecated (since 2.13.0): Adding a number and a String is deprecated. Use the string interpolation `s"$num$str"` res0: String = 23 ``` --- # Literal types ```scala scala> def answer(i: Int) = println(s"The answer is $i") answer: (i: Int)Unit scala> answer(43) The answer is 43 scala> def answer(i: 42) = println(s"The answer is $i") answer: (i: 42)Unit scala> answer(43) ^ error: type mismatch; found : Int(43) required: 42 scala> answer(42) The answer is 42 ``` --- # Literal types: Y, tho? * Shapeless records ```scala val book = ("author" ->> "Benjamin Pierce") :: ("title" ->> "Types and Programming Languages") :: ("id" ->> 262162091) :: ("price" ->> 44.11) :: HNil ``` -- * Refined types: type level predicates! -- * Dimensional analysis: typelevel metric system! --- class: center, middle # Thanks! Code and slides at `rossabaker/scala213` on GitHub [Scala release notes](https://github.com/scala/scala/releases/tag/v2.13.0) ## Questions?