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

Scala: A 10 minutes introduction

Scala: A 10 minutes introduction

Firas Bessadok

December 10, 2013
Tweet

Other Decks in Programming

Transcript

  1. Scala • Object Oriented • Imperative/Functionnal Programming • Statically Typed

    • Compiles to bytecode • Executed on a JVM • Java Ecosystem !4
  2. Functions def function(param: Type): ReturnType = { blablabla blablabla }

    ! def function(param: Type): ReturnType = blabla ! (p1: Type, p2: Type): Type => blabla !7
  3. def add(a: Int, b: Int): Int = { val c

    = a + b c } ! def move(dx: Int, dy: Int) { x = x + dx y = y + dy } !8 Functions
  4. Tuple public Integer[] biskebiskerelle() { blablabla return tab; // contains

    exactly 2 integers } ! public Object[] biskebiskerage() { blablabla return tab; // contains exactly 1 integer, // 1 string and 1 date } !9
  5. def biskebiskerelle(): (Int, Int) = { blablabla (3, 5) //

    contains exactly 2 integers } ! def biskebiskerage(): (Int, String, Date) = { blablabla (height, name, birthday) } !10 Tuple
  6. Classes !13 class Car { private int year; private double

    km; ! public Car(int year) { this. year = year; km = 0; } public Car(int year, double km) { this.year = year; this.km = km; } ! public int getYear() { return year; } public double getKm() { return km; } public void setKm(double km) { this.km = km; } } class Car(val year: Int, var km: Double = 0)
  7. Classes !15 class Car { private int year; private double

    km; ! public Car(int year) { this.year = year; km = 0; } public Car(int year, double km) { this. year = year; this.km = km; } ! public int getYear() { return year; } public double getKm() { return km; } public void setKm(double km) { this.km = km; } } class Car(val year: Int, var km: Double = 0) ! val v1 = new Car(2013) val v2 = new Car(2013, 130000) ! ! ! class Car(val year: Int, var km: Double = 0) { ! val variable1: Type ! def function1(param: Type): Type = { blablabla } }
  8. def add(a: Int, b: Int): Int = { val c

    = a + b c } ! def add(a: Int, b: Int): Int = { a + b } !18
  9. def add(a: Int, b: Int): Int = { val c

    = a + b c } ! def add(a: Int, b: Int): Int = { a + b } ! def add(a: Int, b: Int): Int = a + b !19
  10. def add(a: Int, b: Int): Int = a + b

    ! def min(a: Int, b: Int): Int = a - b ! def mul(a: Int, b: Int): Int = a * b !21
  11. def add(a: Int, b: Int): Int = a + b

    ! def min(a: Int, b: Int): Int = a - b ! def mul(a: Int, b: Int): Int = a * b ! (Int, Int) => Int !22
  12. def add(a: Int, b: Int): Int = a + b

    ! def min(a: Int, b: Int): Int = a - b ! def mul(a: Int, b: Int): Int = a * b ! def maths(a: Int, b: Int, f: (Int, Int) => Int): Int = f(a,b) !23
  13. def maths(a: Int, b: Int, f: (Int, Int) => Int):

    Int = f(a,b) ! maths(5, 6, add) ! maths(25, 26, min) ! maths(45, 46, mul) ! maths(65, 66, (a: Int, b, Int) => a+b+10) !25
  14. maths(65, 66, (a: Int, b, Int) => a+b+10) ! maths(65,

    66, _ + _ + 10) ! maths(5, 6, _ + _) ! maths(25, 26, _ - _) ! maths(45, 46, _ * _) !28
  15. Pattern Matching x match { case 1 => « one

    » case 2 => « two » case _ => « many » } !29
  16. Pattern Matching x match { case 1 => « one

    » case 2 => « two » case a: Int if(a > 2) => « many » } !30
  17. Pattern Matching x match { case a: Int => «

    integer » case a: Double => « double » case a: String => « string » } !31
  18. Pattern Matching x match { case Car(a, km) => «

    car. year » + a case Number(n) => « number » + n case a: String => « string » } !32
  19. Pattern Matching x match { case Car(a, km) if (km

    > 0) => « car. year » + a case Car(a, km) if (km == 0) => « new is always better » } !33
  20. Lists val myList = List(6, 1, 3 ,2, 9) !

    myList.min!! ! // 1 myList.max! ! // 9 myList.sorted!! // List(1, 2, 3, 6, 9) myList.head! ! // 6 myList.tail! ! ! // List(1, 3, 2, 9) myList.last!! ! // 9 myList.init! ! ! // List(6, 1, 3, 2) !34
  21. Lists val myList = List(6, 1, 3 ,2, 9) !

    myList.map(x => x*2)! // List(12,2,6,4,18) !35
  22. Lists val myList = List(6, 1, 3 ,2, 9) !

    myList.map(x => x*2)! // List(12,2,6,4,18) ! val myCoord = List((48.861013, 2.335871), (l1,l2)) ! myCoord.map((lat,lng) => gmap.getAddress(lat, lng)) !36
  23. Lists val myList = List(6, 1, 3 ,2, 9) !

    myList.map(_*2)!! // List(12,2,6,4,18) myList.filter(_>3)! // List(6,9) ! 4 :: myList // List(4,6,1,3,2,9) 4 +: myList // List(4,6,1,3,2,9) myList :+ 4 // List(6,1,3,2,9,4) !37
  24. Lists & Pattern matching myList match { case List() =>

    « empty » case x :: List() => « one element » case x :: y :: List() => « two elements » case x :: xs => « many elements » } !38