Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Scala

4,116 views

Published on

Introduction to the Scala programming language @ the Java Klassentreffen 2013 in Vienna and Linz organized by http://www.javaklassentreffen.at

Introducing the background, design goals, and some examples (from a Java perspective)

http://java.at/web/guest/jkt13

Published in: Technology, News & Politics

Introduction to Scala

  1. 1. SCALA Java Klassentreffen 2013 Manuel Bernhardt
  2. 2. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools & more
  3. 3. @elmanu Java Klassentreffen 2013 - javatraining.at YOUR SPEAKER • Independent software consultant • Web, web, web • Java & Scala & Javascript • Open-Source • Delving
  4. 4. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools & more
  5. 5. @elmanu Java Klassentreffen 2013 - javatraining.at HISTORY • Martin Odersky, EPFL • Espresso, Pizza, GJ, Javac • Funnel, Scala • First Scala release in 2003
  6. 6. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice
  7. 7. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS
  8. 8. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS • Full interoperability with Java
  9. 9. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate
  10. 10. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure object orientation & functional programming
  11. 11. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure object orientation & functional programming • Move away from null
  12. 12. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure object orientation & functional programming • Move away from null • Multi-core programming
  13. 13. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools & more
  14. 14. @elmanu Java Klassentreffen 2013 - javatraining.at WHAT PEOPLE SAY If I were to pick a language today other than Java, it would be Scala. James Gosling Father of Java
  15. 15. @elmanu Java Klassentreffen 2013 - javatraining.at WHAT PEOPLE SAY I can honestly say if someone had shown me the Programming Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably have never created Groovy. James Strachan Creator of Groovy
  16. 16. @elmanu Java Klassentreffen 2013 - javatraining.at ThoughtWorksTechRadar May 2013 http://www.thoughtworks.com/radar
  17. 17. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA INTHE WILD
  18. 18. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA INTHE WILD etc.
  19. 19. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA INVIENNA • openForce - http://openforce.com • x-tradesoft - http://xtradesoft.com • Delving - http://www.delving.eu • dimocom - http://www.dimocom.com • emarsys - http://www.emarsys.com • Miavia - https://miavia.in
  20. 20. @elmanu Java Klassentreffen 2013 - javatraining.at SCALA INVIENNA • ScalaVienna User Group • http://scala-vienna.org/ Next meeting: 26th September 6 PM - Sektor 5
  21. 21. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools & more
  22. 22. @elmanu Java Klassentreffen 2013 - javatraining.at AVOIDINGTHE BILLION- DOLLAR MISTAKE But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement Tony Hoare Creator of ALGOL
  23. 23. @elmanu Java Klassentreffen 2013 - javatraining.at AVOIDINGTHE BILLION- DOLLAR MISTAKE val maybeUser: Option[User] = User.findOneByName("bob") // returns Some[User] maybeUser == None // false maybeUser.foreach { user => println(user.fullName) // prints "Bob Marley" if there is a user! } val name = maybeUser.map(_.name).getOrElse("Unknown user")
  24. 24. @elmanu Java Klassentreffen 2013 - javatraining.at CONCISENESS public class User { ! private String name; ! private String surname; ! private String email; ! public User(String name, String surname, String email) { ! ! this.name = name; ! ! this.surname = surname; ! ! this.email = email; ! } ! public void setName(String name) { ! ! this.name = name; ! } ! public void setSurname(String surname) { ! ! this.surname = surname; ! } ! public void setEmail(String email) { ! ! this.email = email ! } ! public String getName() { ! ! return this.name; ! } ! public String getSurname() { ! ! return this.surname; ! } ! public String getEmail() { ! ! return this.surname; ! } }
  25. 25. @elmanu Java Klassentreffen 2013 - javatraining.at CONCISENESS class User( var name: String, var surname: String, var email: String) val bob = new User("Bob", "Marley", "bob@marley.org") // bob: User = User@5c3f1224 bob.name // res0: String = Bob bob.name = "Bobby" // bob.name: String = Bobby
  26. 26. @elmanu Java Klassentreffen 2013 - javatraining.at CONCISENESS public class ImmutableUser { ! private final String name; ! private final String surname; ! private final String email; ! public ImmutableUser(String name, String surname, String email) { ! ! this.name = name; ! ! this.surname = surname; ! ! this.email = email; ! } ! public String getName() { ! ! return this.name; ! } ! public String getSurname() { ! ! return this.surname; ! } ! public String getEmail() { ! ! return this.surname; ! } ! @Override public int hashCode() { ! ! // yada yada yada ! } ! @Override public boolean equals(Object that) { ! ! // yada yada yada ! } }
  27. 27. @elmanu Java Klassentreffen 2013 - javatraining.at CONCISENESS case class ImmutableUser( name: String, surname: String, email: String) val bob = ImmutableUser("Bob", "Marley", "bob@marley.org") // hashcode and equals for free! val namedBob = ImmutableUser(name = "Bob", surname = "Marley", email = "email") val bobby = bob.copy(name = "Bobby") // returns a User with name Bobby bob.toString // res0: String = ImmutableUser(Bob,Marley,email)
  28. 28. @elmanu Java Klassentreffen 2013 - javatraining.at USEFULTYPE INFERENCE val foo = "Bar" // foo: String = Bar val answer = 42 // answer: Int = 42 val price = 9.99 // price: Double = 9.99 val nums = List(1, 2, 3) // nums: List[Int] = List(1, 2, 3) val map = Map("abc" -> List(1, 2, 3)) // map: scala.collection.immutable.Map[String,List[Int]] = Map(abc -> List(1, 2, 3))
  29. 29. @elmanu Java Klassentreffen 2013 - javatraining.at EXPLICITTYPING val foo: String = "Bar" // foo: String = Bar val answer: Int = 42 // answer: Int = 42 val price: Double = 9.99 // price: Double = 9.99 val nums: List[Int] = List(1, 2, 3) // nums: List[Int] = List(1, 2, 3) val map: Map[String, List[Int]] = Map("abc" -> List(1, 2, 3)) // map: scala.collection.immutable.Map[String,List[Int]] = Map(abc -> List(1, 2, 3))
  30. 30. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE users.sort(new Comparator { public int compare(Object user1, Object user2) { ! int userAge1 = ((User) user1).getAge(); ! int userAge2 = ((User) user2).getAge(); ! if (userAge1 > userAge2) { ! ! return 1; ! } else if userAge1 < userAge2) { ! ! ! return -1; ! ! } else { ! ! ! return 0; ! ! } ! } });
  31. 31. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE def sortByAge(user1: User, user2: User) = user1.age > user2.age users.sortWith(sortByAge)
  32. 32. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE users.sortWith((user1, user2) => user1.age > user2.age)
  33. 33. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE users.sortWith(_.age > _.age)
  34. 34. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE List<User> minors = new ArrayList<User>(); List<User> majors = new ArrayList<User>(); for (User u : users) { ! if (u.getAge() < 18) { ! ! minors.add(u); ! } else { ! ! majors.add(u); ! } }
  35. 35. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE val (minors, majors) = users.partition(_.age < 18)
  36. 36. @elmanu Java Klassentreffen 2013 - javatraining.at COLLECTION LIBRARY & FUNCTIONAL STYLE val minors = users.filter(_.age < 18)
  37. 37. @elmanu Java Klassentreffen 2013 - javatraining.at • Minimal language, powerful library • Language features for extensibility EXTENSIBLE LANGUAGE
  38. 38. @elmanu Java Klassentreffen 2013 - javatraining.at DOMAIN SPECIFIC LANGUAGES import collection.mutable.Stack import org.scalatest._ class ExampleSpec extends FlatSpec with Matchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should be (2) stack.pop() should be (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[Int] a [NoSuchElementException] should be thrownBy { emptyStack.pop() } } }
  39. 39. @elmanu Java Klassentreffen 2013 - javatraining.at MACROS PLAY JSON DE/SERIALIZATION
  40. 40. @elmanu Java Klassentreffen 2013 - javatraining.at MACROS PLAY JSON DE/SERIALIZATION case class Creature(name: String, isDead: Boolean, weight: Float) implicit val creatureReads: Reads[Creature] = ( (__ "name").read[String] and (__ "isDead").read[Boolean] and (__ "weight").read[Float] )(Creature) implicit val creatureWrites: Writes[Creature] = ( (__ "name").write[String] and (__ "isDead").write[Boolean] and (__ "weight").write[Float] )(unlift(Creature.unapply))
  41. 41. @elmanu Java Klassentreffen 2013 - javatraining.at MACROS PLAY JSON DE/SERIALIZATION import play.api.json._ implicit val creatureFormat = Json.format[Creature] // format is a macro
  42. 42. @elmanu Java Klassentreffen 2013 - javatraining.at AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools & more
  43. 43. @elmanu Java Klassentreffen 2013 - javatraining.at IDE • IntelliJ IDEA • Eclipse • SublimeText
  44. 44. @elmanu Java Klassentreffen 2013 - javatraining.at SIMPLE BUILDTOOL name := "My Project" version := "1.0" organization := "org.myproject" libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.8" % "test" libraryDependencies ++= Seq( "net.databinder" %% "dispatch-meetup" % "0.7.8", "net.databinder" %% "dispatch-twitter" % "0.7.8" ) javaOptions += "-Xmx256m" logLevel in compile := Level.Warn
  45. 45. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS:AKKA • Actor concurrency model based on Erlang • “Human” design: actors talk to eachother and form hierarchies • Much, much, much simpler to work and reason with than threads
  46. 46. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS:AKKA Source: http://prabhubuzz.wordpress.com/2012/09/28/akka-really-mountains-of-concurrency/
  47. 47. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS:AKKA class Master extends Actor { ! val workers = context.actorOf(Props[Worker].withRouter( ! RoundRobinRouter(nrOfInstances = 5)) ! ) ! def receive = { ! ! case Start => ! ! ! getDocumentsFromDb.foreach { document => ! ! ! ! workers ! Process(document) ! ! ! } ! ! case Result(processed) => writeResult(processed) ! ! case Stop => children.foreach(stop) ! } }
  48. 48. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS:AKKA class Worker extends Actor { ! def receive = { ! ! case Process(doc: Document) => ! ! ! val processed = doSomeHardWork(doc) ! ! ! sender ! Result(processed) ! } }
  49. 49. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS: PLAY • MVC framework à la Rails • Real-time web, streams (WebSocket, ...) • Everything is compiled • I mean everything: CSS, JavaScripts,Templates, URLs, JSON, ...
  50. 50. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS: PLAY GET /users controllers.Users.list POST /users controllers.Users.create PUT /users/:id/update controllers.Users.update(id: Long) DELETE /users/:id controllers.Users.delete(id: Long)
  51. 51. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS: PLAY class Users extends Controller { def list = Action { request => val users = User.findAll Ok(Json.toJson(users)) } }
  52. 52. @elmanu Java Klassentreffen 2013 - javatraining.at FRAMEWORKS: PLAY class Users extends Controller { def list = Action { request => val users = User.findAll Ok(Json.toJson(users)) } } 200 OK Content-Type: application/json
  53. 53. @elmanu Java Klassentreffen 2013 - javatraining.at THANKYOU! • Questions, comments ? • http://logician.eu • manuel@bernhardt.io • @elmanu

×