Scala vs Java 8 in a Java 8 World

download Scala vs Java 8 in a Java 8 World

of 38

  • date post

    21-Aug-2015
  • Category

    Technology

  • view

    84
  • download

    2

Embed Size (px)

Transcript of Scala vs Java 8 in a Java 8 World

  1. 1. Scala in a Java 8 World
  2. 2. Takeaways Why Scala? Scala still relevant in Java 8 world?
  3. 3. What do we need in a language?
  4. 4. Developers should start thinking about tens, hundreds, and thousands of cores now - Intel
  5. 5. Parallel processing is hard
  6. 6. Mutable State public void setX(int x) { this.x = x; } setX(0) async { setX(getX()) + 1) } async { setX(getX()) * 2) }
  7. 7. Scala can help
  8. 8. Functional
  9. 9. Object Oriented
  10. 10. JVM Based
  11. 11. All logos owned by respective companies.
  12. 12. Isnt Java 8 enough?
  13. 13. Its all about developer productivity
  14. 14. classPoint{ privateintx; privateinty; publicPoint(intx,inty){ setX(x); setY(y); } publicintgetX(){returnx;} publicvoidsetX(intx){this.x=x;} publicintgetY(){returny;} publicvoidsetY(inty){this.y=y;} publicbooleanequals(Objectother){ if(otherinstanceofPoint){ PointotherPoint=(Point)other; returnotherPoint.getX()==getX()&&otherPoint.getY()==getY(); }else{ returnfalse; } } publicinthashCode(){ return(newInteger[]{getX(),getY()}).hashCode(); } }
  15. 15. case class Point(var x: Int, var y: Int)
  16. 16. Traits abstract class Animal { def speak } trait FourLeggedAnimal { def walk def run } trait WaggingTail { val tailLength: Int def startTail { println("tail started") } def stopTail { println("tail stopped") } } class Dog extends Animal with WaggingTail with FourLeggedAnimal { def speak { println("Dog says 'woof'") } def walk { println("Dog is walking") } def run { println("Dog is running") } }
  17. 17. Immutable Classes public final class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } Java Scala case class Point(x: Int, y: Int)
  18. 18. Immutable Data Structures Scala List(1, 2, 3) Set(1, 2, 3) Map(("foo", 1), ("bar", 2)) List uList = Arrays.asList(1, 2, 3); Set set = new HashSet(); set.add(1); set.add(2); Set uSet = Collections.unmodifiableSet(set); Map map = new HashMap(); map.put("foo", 1); map.put("bar", 2); Map uMap = Collections.unmodifiableMap(map); Java
  19. 19. Lambdas Scala peoples.filter(person => person.firstName == Jon)! ! peoples.stream().filter( person -> ! person.firstName.equals(Jon)).collect(Collectors.toList()) Java peoples.map(person => person.firstName) List(1, 2, 3).reduce(_+_) // 6 List(1, 2, 3).foreach(println)
  20. 20. public class Person { private String lastName; private String firstName; private String middleName; private String salutation; private String suffix; public Person(String lastName, String firstName, String middleName, String salutation, String suffix) { this.lastName = lastName; this.firstName = firstName; this.middleName = middleName; this.salutation = salutation; this.suffix = suffix; } }
  21. 21. public class Person { public static class PersonBuilder { public PersonBuilder lastName(String newLastName) { this.lastName = newLastName; return this; } public PersonBuilder firstName(String newFirstName) { this.firstName = newFirstName; return this; } public PersonBuilder middleName(String newMiddleName) { this.middleName = newMiddleName; return this; } public PersonBuilder salutation(String newSalutation) { this.salutation = newSalutation; return this; } public PersonBuilder suffix(String newSuffix) { this.suffix = newSuffix; return this; } public Person build() { return new Person(this); } } }
  22. 22. Named and Default Parameters case class Person(salutation: String = "Unknown", firstName: String = "Unknown, middleName: String = "Unknown", lastName: String = "Unknown", suffix: String = "Unknown") Person("Mr", "John", "M, "Doe, Sr") Person(salutation = "Mr", firstName = "John", lastName = "Doe") Person(firstName = "John", middleName = "M", lastName = "Doe") Person(firstName = "John") Person(lastName = "Doe") Person(firstName = "John", lastName = "Doe")
  23. 23. Pattern Matching defparseArgument(arg:String)
  24. 24. Pattern Matching defparseArgument(arg:String)=argmatch{ }
  25. 25. Pattern Matching defparseArgument(arg:String)=argmatch{ case("-h"|"--help)=>displayHelp() }
  26. 26. Pattern Matching defparseArgument(arg:String)=argmatch{ case("-h"|"--help)=>displayHelp() casebad=>badArgument(bad) }
  27. 27. Pattern Matching defparseArgument(arg:String,value:Any)
  28. 28. Pattern Matching defparseArgument(arg:String,value:Any)=(arg,value)match{ }
  29. 29. Pattern Matching defparseArgument(arg:String,value:Any)=(arg,value)match{ case("-h"|"--help",null)=>displayHelp() case("-l",lang)=>setLanguageTo(lang) casebad=>badArgument(bad) }
  30. 30. Pattern Matching defparseArgument(arg:String,value:Any)=(arg,value)match{ case("-h"|"--help",null)=>displayHelp() case("-l",lang)=>setLanguageTo(lang) case("-o"|"--optim",n:Int)if((0badOptimizationLevel(badLevel) casebad=>badArgument(bad) }
  31. 31. Patterns // constant patterns case 0 => "zero case true => "true case "hello" => "you said 'hello' case Nil => "an empty List" // sequence patterns case List(0, _, _) => "a three-element list with 0 as the first element case List(1, _*) => "a list beginning with 1, having any number of elements case Vector(1, _*) => "a vector starting with 1, having any number of elements // constructor patterns case Person(first, "Alexander") => s"found an Alexander, first name = $first case Dog("Suka") => "found a dog named Suka
  32. 32. I call it my billion-dollar mistake. It was the invention of the null reference in 1965. - Tony Hoare
  33. 33. Option def toInt(in: String): Option[Int] = { try { Some(Integer.parseInt(in.trim)) } catch { case ex: NumberFormatException => None } } toInt(someString) match { case Some(i) => println(i) case None => println("That didn't work.") }
  34. 34. Collections with Option val bag = List("1", "2", "foo", "3", "bar") bag.map(toInt) // List[Option[Int]] = List(Some(1), Some(2), None, Some(3), None) bag.flatMap(toInt) // List(1, 2, 3)
  35. 35. Summary Parallel Processing made easier Developer Productivity Java interoperable Still relevant in a Java 8 world