Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Scala

Introduction to Scala

An updated version of my Introduction to Scala, presented for the first time at Community Days 2016 in Milan.

C2bb0454c4af1a61e7f173d54ce17b0b?s=128

Gabriele Petronella

October 07, 2016
Tweet

Transcript

  1. An introduction to Scala Gabriele Petronella Software Engineer @ buildo

  2. me, hi!

  3. stuff I do

  4. So... Scala, you said?

  5. First of all, why would you care?

  6. Why Scala? 1.Curiosity 2.A growing trend in industry (are you

    into data science?) 3.Approachable introduction to FP 4.Great learning experience that you can apply elsewhere
  7. So, what is it? Martin Odersky 2004 JVM A better

    Java? Haskell on the JVM?
  8. Talk CODE to me

  9. This is Scala abstract class Fruit { def isSeasonal(): Boolean

    } class Mango extends Fruit { def isSeasonal() = { return true } }
  10. This is also Scala def ApplicativeCompose[M[_], N[_]] (implicit ma: Applicative[M],

    na: Applicative[N]): Applicative[({type λ[α]=M[N[α]]})#λ] = new Applicative[({type λ[α]=M[N[α]]})#λ] { def ap[A, B](f: M[N[A => B]], a: M[N[A]]) = { def liftA2[X, Y, Z](f: X => Y => Z, a: M[X], b: M[Y]): M[Z] = ma.ap(ma.fmap(f, a), b) liftA2((ff: N[A => B]) => (aa: N[A]) => na.ap(ff, aa), f, a) } def point[A](a: A) = ma point (na point a) }
  11. Paradigms

  12. Object Oriented Programming

  13. Functional Programming

  14. Wait both?

  15. YES! That's kind of the gist of it. Scala sits

    in between FP and OOP. And yes, both FP and OOP eventually end up declaring war to it !
  16. Why both OOP: information hiding, modularity FP: composability OOP +

    FP = modular programming
  17. A little tour of functional programming

  18. FP in bullets 1.Functions 2.Immutability 3....no seriously that's it

  19. Functions a function f: A -> B is a subset

    f of A x B such that for all a ∈ A exists exactly one b ∈ B such that (a, b) ∈ f If A are Integers and B are Strings: f(42) -> "puppy" ✅ f(43) -> "kitten" ✅ f(42) -> "shark" ⛔ ! 42 is mapped to two values !
  20. Functions Note: » functions are pure (some would argue: functions

    are FUNCTIONS) » functions are total (they always have a return value)
  21. Functions In FP functions can be passed around as values.

    They are first-order citizens in the language. val double = (x: Int) => x * 2 List(1, 2, 3).map(double) // List(2, 4, 6)
  22. Immutability and functions val inc = (x: Int) => x

    + 1 val x = inc(42) An expression is REFERENTIALLY TRANSPARENT if this ALWAYS holds true: inc(42) == 43 So: val x = 43 // 43 or inc(42) doesn't make a difference
  23. Pretty reasonable right?

  24. Yeah, right...

  25. x = x + 1

  26. Why immutability matters var foo = 42 hey(foo) ho(foo) letsgo(foo)

    println(foo) // ???
  27. Why immutability matters val foo = 42 hey(foo) ho(foo) letsgo(foo)

    println(foo)
  28. Why immutability matters val foo = 42 hey(foo) ho(foo) letsgo(foo)

    println(foo) // 42!
  29. FP (perception)

  30. FP (reality)

  31. Immutability helps me cope with my dumbness

  32. Back to Scala

  33. Scala and FP Scala embraces FP as a default... //

    functions as values val inc = (x: Int) => x + 1 // immutable collections List(1, 2, 3).append(4) // ⛔ ERROR! // immmutable classes case class User(name: String, age: Int) val gab = User("Gabriele", 27) gab.age = 20 // ⛔ ERROR! nice try...
  34. Scala and FP ...but doesn't force you to it //

    mutable collections import scala.collection.mutable.ListBuffer val list = ListBuffer(1, 2, 3) list.append(4) println(list) // ListBuffer(1, 2, 3, 4) // mutable values case class User(name: String, var age: Int) val gab = User("Gabriele", 27) gab.age = 10 // I'm a kid again!
  35. Quite nice ✌ Want to write Java-like Scala? With minimal

    effort you can! Want to go full-force FP? No problem!* Want to sneak in some FP here and there? We got you covered! *terms and conditions may apply
  36. First lesson learned: Scala is very flexible

  37. But remember, flexibility means power

  38. and with great power... well, you know...

  39. What do you mean by "flexible"?

  40. Mixing paradigms is a form of flexibility, but not the

    only one
  41. Scala is a growable language

  42. What does it mean? “I need to design a language

    that can grow. I need to plan ways in which it might grow. But I need, too, to leave some choices so that other persons can make those choices at a later time” Growing a Language, by Guy Steele
  43. Scala as a growable language » highly-composable » very simple

    (but powerful) constructs » flexible syntax
  44. ⚠Warning⚠ SIMPLE != EASY

  45. Some features that enable growth are...

  46. Expressions Everything is an expression. And expressions compose! val animal

    = if (legs == 8) "spider" else "other" val result = try { computeQuestion(42) } catch { case e: Throwable => "How many roads must a man walk down?" }
  47. Destructuring trait Animal case class Dog(owner: Option[Person]) extends Animal case

    class Reindeer(bells: Int) extends Animal animal match { case Dog(Some(owner)) => // handle dogs with owners case Dog(None) => // handle stray dogs case Reindeer(bells) if bells > 4 => // handle reindeers with more than 4 bells }
  48. syntax Custom "operators" case class Person(name: String) { def ->>(p:

    Person) = s"$name points at ${p.name}" } val me = new Person("me") val you = new Person("you") println(me ->> you) // "me points at you"
  49. syntax Shocking truth: this is a general rule. 1.+(2) ==

    1 + 2 Every method with one argument can be used in infix form
  50. syntax Trailing arguments def runInTransaction[A](o: O)(f: (txn: Txn) => A):

    A runInTransaction(options)(txn => doStuff(txn)) runInTransaction(options) { txn => doStuff() }
  51. implicits val context = // some context doSomething(a, b, context)

    doSomethingElse(x, y, context) can we avoid passing context around? implicit val context = // some context def doSomething(a: Int, b: Int)(implicit context: Context) doSomething(a, b) // Black magic!
  52. implicits “Implicits are the source of everything that is good

    and bad in Scala” -- myself, right now
  53. implicits Remember? Simple != easy trait Eq[A] { def eq(x:

    A, y: A): Boolean } implicit object StringEq extends Eq[String] { def eq(x: String, y: String) = x == y } def isEqual[A](x: A, y: A)(implicit e: Eq[A]) = e.eq(x, y)
  54. implicits Based on a simple concept (passing an implicit value

    around) we now have libraries like: » scalaz/cats: category theory applied to Scala » shapeless: generic programming in Scala Those are CRAZY projects, that no-one could envision when designing the language
  55. Did I mention it can go crazy? object SquareRoot extends

    Baysick { def main(args:Array[String]) = { 10 PRINT "Enter a number" 20 INPUT 'n 30 PRINT "Square root of " % "'n is " % SQRT('n) 40 END RUN } }
  56. And crazier https://github.com/abbruzze/kernal64

  57. Recap

  58. A powerful and flexible language It can grow beyond imagination

  59. OOP + FP = modularity

  60. Allows you to approach FP but it lets you "escape"

  61. Resources » Functional Programming Principles in Scala (coursera) » Programming

    in Scala (M.Odersky et al.) » Functional Programming in Scala (P. Chiusano, R. Bjarnason)
  62. Thank you!