SlideShare a Scribd company logo
1 of 61
Download to read offline
SCALA
Java2Days 2013
Manuel Bernhardt

Dienstag, 03. Dezember 13
AGENDA
• History
• Why

Scala?

• Scala

in the wild

• The

Code / Scala in
practice

• Tools
@elmanu
Dienstag, 03. Dezember 13

& more
YOUR SPEAKER
• Independent

software
consultant based in Vienna

• Web, web, web
• Scala

& Java & Javascript

• Open-Source

@elmanu
Dienstag, 03. Dezember 13
AGENDA
• History
• Why

Scala?

• Scala

in the wild

• The

Code / Scala in
practice

• Tools
@elmanu
Dienstag, 03. Dezember 13

& more
HISTORY
• Martin

Odersky, EPFL

• Espresso, Pizza, GJ,

javac

• Funnel, Scala
• First

Scala release in
2003

@elmanu
Dienstag, 03. Dezember 13
AGENDA
• History
• Why
• Scala
• The

Scala?

in the wild

Code / Scala in
practice

@elmanu
Dienstag, 03. Dezember 13
SCALA DESIGN GOALS

@elmanu
Dienstag, 03. Dezember 13
SCALA DESIGN GOALS
• Full

interoperability with Java

@elmanu
Dienstag, 03. Dezember 13
SCALA DESIGN GOALS
• Full

interoperability with Java

• Cut

down boilerplate

@elmanu
Dienstag, 03. Dezember 13
SCALA DESIGN GOALS
• Full

interoperability with Java

• Cut

down boilerplate

• Pure

@elmanu
Dienstag, 03. Dezember 13

object orientation & functional programming
SCALA DESIGN GOALS
• Full

interoperability with Java

• Cut

down boilerplate

• Pure

object orientation & functional programming

• Move

@elmanu
Dienstag, 03. Dezember 13

away from null
SCALA DESIGN GOALS
• Full

interoperability with Java

• Cut

down boilerplate

• Pure

object orientation & functional programming

• Move

away from null

• Multi-core
@elmanu
Dienstag, 03. Dezember 13

programming
AGENDA
• History
• Why

Scala?

• Scala

in the wild

• The

Code / Scala in
practice

• Tools
@elmanu
Dienstag, 03. Dezember 13

& more
WHAT PEOPLE SAY
If I were to pick a language today
other than Java, it would be Scala.
James Gosling
Father of Java

@elmanu
Dienstag, 03. Dezember 13
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

http://macstrac.blogspot.co.at/2009/04/scala-as-long-term-replacement-for.html
@elmanu
Dienstag, 03. Dezember 13
@elmanu
Dienstag, 03. Dezember 13

ThoughtWorks TechRadar May 2013
http://www.thoughtworks.com/radar
@elmanu
Dienstag, 03. Dezember 13

ThoughtWorks TechRadar May 2013
http://www.thoughtworks.com/radar
SCALA IN THE WILD

etc.
@elmanu
Dienstag, 03. Dezember 13
SCALA IN THE WILD

@elmanu
Dienstag, 03. Dezember 13
SCALA IN THE WORLD

http://www.scala-tribes.org/
@elmanu
Dienstag, 03. Dezember 13
SCALA IN VIENNA
• Scala Vienna
•1

User Group

year old, 124+ members

• http://scala-vienna.org/

@elmanu
Dienstag, 03. Dezember 13
AGENDA
• History
• Why

Scala?

• Scala

in the wild

• The

Code / Scala in
practice

• Tools
@elmanu
Dienstag, 03. Dezember 13

& more
AVOIDING THE BILLIONDOLLAR 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

@elmanu
Dienstag, 03. Dezember 13
AVOIDING THE BILLIONDOLLAR 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")

@elmanu
Dienstag, 03. Dezember 13
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;
}

}

@elmanu
Dienstag, 03. Dezember 13
CONCISENESS
class
var
var
var

User(
name: String,
surname: String,
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

@elmanu
Dienstag, 03. Dezember 13
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() {

!
!

!
}

!

@Override public boolean equals(Object that) {

!

!

!

}

}

@elmanu
Dienstag, 03. Dezember 13

// yada yada yada

// yada yada yada
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)

@elmanu
Dienstag, 03. Dezember 13
USEFUL TYPE INFERENCE

@elmanu
Dienstag, 03. Dezember 13
USEFUL TYPE 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))

@elmanu
Dienstag, 03. Dezember 13
EXPLICIT TYPING
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))

@elmanu
Dienstag, 03. Dezember 13
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;
! ! }
! }
});

@elmanu
Dienstag, 03. Dezember 13
COLLECTION LIBRARY &
FUNCTIONAL STYLE
def sortByAge(user1: User, user2: User) = user1.age > user2.age
users.sortWith(sortByAge)

@elmanu
Dienstag, 03. Dezember 13
COLLECTION LIBRARY &
FUNCTIONAL STYLE
users.sortWith((user1, user2) => user1.age > user2.age)

@elmanu
Dienstag, 03. Dezember 13
COLLECTION LIBRARY &
FUNCTIONAL STYLE
users.sortWith(_.age > _.age)

@elmanu
Dienstag, 03. Dezember 13
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);
! }
}

@elmanu
Dienstag, 03. Dezember 13
COLLECTION LIBRARY &
FUNCTIONAL STYLE
val (minors, majors) = users.partition(_.age < 18)

@elmanu
Dienstag, 03. Dezember 13
COLLECTION LIBRARY &
FUNCTIONAL STYLE
val minors = users.filter(_.age < 18)

@elmanu
Dienstag, 03. Dezember 13
EXTENSIBLE LANGUAGE

• Minimal

language, powerful library

• Language

@elmanu
Dienstag, 03. Dezember 13

features for extensibility
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()
}
}
}
@elmanu
Dienstag, 03. Dezember 13
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()
}
}
}
@elmanu
Dienstag, 03. Dezember 13
MACROS

• Compile-time, during
• Expanding

type checking

the AST

• Experimental

since Scala 2.10

http://scalamacros.org
@elmanu
Dienstag, 03. Dezember 13
MACROS
PLAY JSON DE/SERIALIZATION

@elmanu
Dienstag, 03. Dezember 13
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))

@elmanu
Dienstag, 03. Dezember 13
MACROS
PLAY JSON DE/SERIALIZATION

import play.api.json._
implicit val creatureFormat = Json.format[Creature] // format is a macro

@elmanu
Dienstag, 03. Dezember 13
AGENDA
• History
• Why

Scala?

• Scala

in the wild

• The

Code / Scala in
practice

• Tools
@elmanu
Dienstag, 03. Dezember 13

& more
IDE

• IntelliJ

IDEA

• Eclipse
• SublimeText

@elmanu
Dienstag, 03. Dezember 13
SIMPLE BUILD TOOL
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

@elmanu
Dienstag, 03. Dezember 13
PLAY! FRAMEWORK
• MVC

web framework,
inspired by RoR and Django

• Java, Scala
• Everything

is compiled

& scalable
performance

• Predictable

• Fun

to work with!

@elmanu
Dienstag, 03. Dezember 13
PLAY! FRAMEWORK
• MVC

web framework,
inspired by RoR and Django

• Java, Scala
• Everything

is compiled

& scalable
performance

• Predictable

• Fun

to work with!

@elmanu
Dienstag, 03. Dezember 13
AKKA
• Toolkit

for building
concurrent & distributed
applications more easily

• Actors

as concurrency
model, simpler to reason
with than threads

• Remoting

and
clustering support
@elmanu

Dienstag, 03. Dezember 13
REACTIVE

http://www.reactivemanifesto.org

Dienstag, 03. Dezember 13
THANK YOU!

Questions, comments ?
http://manuel.bernhardt.io
manuel@bernhardt.io
@elmanu

@elmanu
Dienstag, 03. Dezember 13
FRAMEWORKS: AKKA
• Actor

concurrency model based on Erlang

• “Human” design: actors

hierarchies

• Much, much, much

threads

@elmanu
Dienstag, 03. Dezember 13

talk to eachother and form

simpler to work and reason with than
FRAMEWORKS: AKKA

Source: http://prabhubuzz.wordpress.com/2012/09/28/akka-really-mountains-of-concurrency/

@elmanu
Dienstag, 03. Dezember 13
FRAMEWORKS: AKKA
class Master extends Actor {
!

val workers = context.actorOf(Props[Worker].withRouter(
! RoundRobinRouter(nrOfInstances = 5))
! )
!
!
!
!
!

case Start =>
! getDocumentsFromDb.foreach { document =>
! ! workers ! Process(document)
! }

!
!

case Result(processed) =>
! writeResult(processed)

!
!

!
!

case Stop =>
! children.foreach(stop)

!
}

Dienstag, 03. Dezember 13

!
!
!
!

!
!

@elmanu

def receive = {

}
FRAMEWORKS: AKKA
class Worker extends Actor {
! def receive = {
!
!
!
!
}

@elmanu
Dienstag, 03. Dezember 13

! case Process(doc: Document) =>
! ! val processed = doSomeHardWork(doc)
! ! sender ! Result(processed)
}
FRAMEWORKS: PLAY

• MVC

framework à la Rails

• Everything
•I

is compiled

mean everything: CSS, JavaScripts, Templates, URLs, JSON, ...

@elmanu
Dienstag, 03. Dezember 13
FRAMEWORKS: PLAY
GET
POST
PUT
DELETE

@elmanu
Dienstag, 03. Dezember 13

/users
/users
/users/:id/update
/users/:id

controllers.Users.list
controllers.Users.create
controllers.Users.update(id: Long)
controllers.Users.delete(id: Long)
FRAMEWORKS: PLAY
class Users extends Controller {
def list = Action { request =>
val users = User.findAll
Ok(Json.toJson(users))
}
}

@elmanu
Dienstag, 03. Dezember 13
FRAMEWORKS: PLAY
class Users extends Controller {
def list = Action { request =>
val users = User.findAll
Ok(Json.toJson(users))
}
}

@elmanu
Dienstag, 03. Dezember 13

200 OK
Content-Type: application/json

More Related Content

Similar to Scala - Java2Days Sofia

Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 

Similar to Scala - Java2Days Sofia (10)

Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013   Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013
 
C* path
C* pathC* path
C* path
 
Scala 101-bcndevcon
Scala 101-bcndevconScala 101-bcndevcon
Scala 101-bcndevcon
 
Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]
 
Rails Intro & Tutorial
Rails Intro & TutorialRails Intro & Tutorial
Rails Intro & Tutorial
 
Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.
 
Basics of Metaprogramming in Ruby
Basics of Metaprogramming in RubyBasics of Metaprogramming in Ruby
Basics of Metaprogramming in Ruby
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Kotlin techtalk
Kotlin techtalkKotlin techtalk
Kotlin techtalk
 
De vuelta al pasado con SQL y stored procedures
De vuelta al pasado con SQL y stored proceduresDe vuelta al pasado con SQL y stored procedures
De vuelta al pasado con SQL y stored procedures
 

More from Manuel Bernhardt

More from Manuel Bernhardt (18)

Is there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems HamburgIs there anybody out there? Reactive Systems Hamburg
Is there anybody out there? Reactive Systems Hamburg
 
Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018Is there anybody out there? Scala Days Berlin 2018
Is there anybody out there? Scala Days Berlin 2018
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?
 
Is there anybody out there?
Is there anybody out there?Is there anybody out there?
Is there anybody out there?
 
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 20178 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
8 akka anti-patterns you'd better be aware of - Reactive Summit Austin 2017
 
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware ofScala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
Scala Days Copenhagen - 8 Akka anti-patterns you'd better be aware of
 
8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware of8 Akka anti-patterns you'd better be aware of
8 Akka anti-patterns you'd better be aware of
 
Beyond the buzzword: a reactive web-appliction in practice
Beyond the buzzword: a reactive web-appliction in practiceBeyond the buzzword: a reactive web-appliction in practice
Beyond the buzzword: a reactive web-appliction in practice
 
Beyond the Buzzword - a reactive application in practice
Beyond the Buzzword - a reactive application in practiceBeyond the Buzzword - a reactive application in practice
Beyond the Buzzword - a reactive application in practice
 
Six years of Scala and counting
Six years of Scala and countingSix years of Scala and counting
Six years of Scala and counting
 
3 things you must know to think reactive - Geecon Kraków 2015
3 things you must know to think reactive - Geecon Kraków 20153 things you must know to think reactive - Geecon Kraków 2015
3 things you must know to think reactive - Geecon Kraków 2015
 
Reactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDaysReactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDays
 
Writing a technical book
Writing a technical bookWriting a technical book
Writing a technical book
 
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVMVoxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
Voxxed Days Vienna - The Why and How of Reactive Web-Applications on the JVM
 
Back to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migrationBack to the futures, actors and pipes: using Akka for large-scale data migration
Back to the futures, actors and pipes: using Akka for large-scale data migration
 
Project Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 monthsProject Phoenix - From PHP to the Play Framework in 3 months
Project Phoenix - From PHP to the Play Framework in 3 months
 
Tips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 projectTips and tricks for setting up a Play 2 project
Tips and tricks for setting up a Play 2 project
 
Scala pitfalls
Scala pitfallsScala pitfalls
Scala pitfalls
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Scala - Java2Days Sofia

  • 2. AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools @elmanu Dienstag, 03. Dezember 13 & more
  • 3. YOUR SPEAKER • Independent software consultant based in Vienna • Web, web, web • Scala & Java & Javascript • Open-Source @elmanu Dienstag, 03. Dezember 13
  • 4. AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools @elmanu Dienstag, 03. Dezember 13 & more
  • 5. HISTORY • Martin Odersky, EPFL • Espresso, Pizza, GJ, javac • Funnel, Scala • First Scala release in 2003 @elmanu Dienstag, 03. Dezember 13
  • 6. AGENDA • History • Why • Scala • The Scala? in the wild Code / Scala in practice @elmanu Dienstag, 03. Dezember 13
  • 8. SCALA DESIGN GOALS • Full interoperability with Java @elmanu Dienstag, 03. Dezember 13
  • 9. SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate @elmanu Dienstag, 03. Dezember 13
  • 10. SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure @elmanu Dienstag, 03. Dezember 13 object orientation & functional programming
  • 11. SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure object orientation & functional programming • Move @elmanu Dienstag, 03. Dezember 13 away from null
  • 12. SCALA DESIGN GOALS • Full interoperability with Java • Cut down boilerplate • Pure object orientation & functional programming • Move away from null • Multi-core @elmanu Dienstag, 03. Dezember 13 programming
  • 13. AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools @elmanu Dienstag, 03. Dezember 13 & more
  • 14. WHAT PEOPLE SAY If I were to pick a language today other than Java, it would be Scala. James Gosling Father of Java @elmanu Dienstag, 03. Dezember 13
  • 15. 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 http://macstrac.blogspot.co.at/2009/04/scala-as-long-term-replacement-for.html @elmanu Dienstag, 03. Dezember 13
  • 16. @elmanu Dienstag, 03. Dezember 13 ThoughtWorks TechRadar May 2013 http://www.thoughtworks.com/radar
  • 17. @elmanu Dienstag, 03. Dezember 13 ThoughtWorks TechRadar May 2013 http://www.thoughtworks.com/radar
  • 18. SCALA IN THE WILD etc. @elmanu Dienstag, 03. Dezember 13
  • 19. SCALA IN THE WILD @elmanu Dienstag, 03. Dezember 13
  • 20. SCALA IN THE WORLD http://www.scala-tribes.org/ @elmanu Dienstag, 03. Dezember 13
  • 21. SCALA IN VIENNA • Scala Vienna •1 User Group year old, 124+ members • http://scala-vienna.org/ @elmanu Dienstag, 03. Dezember 13
  • 22. AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools @elmanu Dienstag, 03. Dezember 13 & more
  • 23. AVOIDING THE BILLIONDOLLAR 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 @elmanu Dienstag, 03. Dezember 13
  • 24. AVOIDING THE BILLIONDOLLAR 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") @elmanu Dienstag, 03. Dezember 13
  • 25. 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; } } @elmanu Dienstag, 03. Dezember 13
  • 26. CONCISENESS class var var var User( name: String, surname: String, 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 @elmanu Dienstag, 03. Dezember 13
  • 27. 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() { ! ! ! } ! @Override public boolean equals(Object that) { ! ! ! } } @elmanu Dienstag, 03. Dezember 13 // yada yada yada // yada yada yada
  • 28. 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) @elmanu Dienstag, 03. Dezember 13
  • 30. USEFUL TYPE 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)) @elmanu Dienstag, 03. Dezember 13
  • 31. EXPLICIT TYPING 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)) @elmanu Dienstag, 03. Dezember 13
  • 32. 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; ! ! } ! } }); @elmanu Dienstag, 03. Dezember 13
  • 33. COLLECTION LIBRARY & FUNCTIONAL STYLE def sortByAge(user1: User, user2: User) = user1.age > user2.age users.sortWith(sortByAge) @elmanu Dienstag, 03. Dezember 13
  • 34. COLLECTION LIBRARY & FUNCTIONAL STYLE users.sortWith((user1, user2) => user1.age > user2.age) @elmanu Dienstag, 03. Dezember 13
  • 35. COLLECTION LIBRARY & FUNCTIONAL STYLE users.sortWith(_.age > _.age) @elmanu Dienstag, 03. Dezember 13
  • 36. 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); ! } } @elmanu Dienstag, 03. Dezember 13
  • 37. COLLECTION LIBRARY & FUNCTIONAL STYLE val (minors, majors) = users.partition(_.age < 18) @elmanu Dienstag, 03. Dezember 13
  • 38. COLLECTION LIBRARY & FUNCTIONAL STYLE val minors = users.filter(_.age < 18) @elmanu Dienstag, 03. Dezember 13
  • 39. EXTENSIBLE LANGUAGE • Minimal language, powerful library • Language @elmanu Dienstag, 03. Dezember 13 features for extensibility
  • 40. 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() } } } @elmanu Dienstag, 03. Dezember 13
  • 41. 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() } } } @elmanu Dienstag, 03. Dezember 13
  • 42. MACROS • Compile-time, during • Expanding type checking the AST • Experimental since Scala 2.10 http://scalamacros.org @elmanu Dienstag, 03. Dezember 13
  • 44. 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)) @elmanu Dienstag, 03. Dezember 13
  • 45. MACROS PLAY JSON DE/SERIALIZATION import play.api.json._ implicit val creatureFormat = Json.format[Creature] // format is a macro @elmanu Dienstag, 03. Dezember 13
  • 46. AGENDA • History • Why Scala? • Scala in the wild • The Code / Scala in practice • Tools @elmanu Dienstag, 03. Dezember 13 & more
  • 47. IDE • IntelliJ IDEA • Eclipse • SublimeText @elmanu Dienstag, 03. Dezember 13
  • 48. SIMPLE BUILD TOOL 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 @elmanu Dienstag, 03. Dezember 13
  • 49. PLAY! FRAMEWORK • MVC web framework, inspired by RoR and Django • Java, Scala • Everything is compiled & scalable performance • Predictable • Fun to work with! @elmanu Dienstag, 03. Dezember 13
  • 50. PLAY! FRAMEWORK • MVC web framework, inspired by RoR and Django • Java, Scala • Everything is compiled & scalable performance • Predictable • Fun to work with! @elmanu Dienstag, 03. Dezember 13
  • 51. AKKA • Toolkit for building concurrent & distributed applications more easily • Actors as concurrency model, simpler to reason with than threads • Remoting and clustering support @elmanu Dienstag, 03. Dezember 13
  • 53. THANK YOU! Questions, comments ? http://manuel.bernhardt.io manuel@bernhardt.io @elmanu @elmanu Dienstag, 03. Dezember 13
  • 54. FRAMEWORKS: AKKA • Actor concurrency model based on Erlang • “Human” design: actors hierarchies • Much, much, much threads @elmanu Dienstag, 03. Dezember 13 talk to eachother and form simpler to work and reason with than
  • 56. FRAMEWORKS: AKKA class Master extends Actor { ! val workers = context.actorOf(Props[Worker].withRouter( ! RoundRobinRouter(nrOfInstances = 5)) ! ) ! ! ! ! ! case Start => ! getDocumentsFromDb.foreach { document => ! ! workers ! Process(document) ! } ! ! case Result(processed) => ! writeResult(processed) ! ! ! ! case Stop => ! children.foreach(stop) ! } Dienstag, 03. Dezember 13 ! ! ! ! ! ! @elmanu def receive = { }
  • 57. FRAMEWORKS: AKKA class Worker extends Actor { ! def receive = { ! ! ! ! } @elmanu Dienstag, 03. Dezember 13 ! case Process(doc: Document) => ! ! val processed = doSomeHardWork(doc) ! ! sender ! Result(processed) }
  • 58. FRAMEWORKS: PLAY • MVC framework à la Rails • Everything •I is compiled mean everything: CSS, JavaScripts, Templates, URLs, JSON, ... @elmanu Dienstag, 03. Dezember 13
  • 59. FRAMEWORKS: PLAY GET POST PUT DELETE @elmanu Dienstag, 03. Dezember 13 /users /users /users/:id/update /users/:id controllers.Users.list controllers.Users.create controllers.Users.update(id: Long) controllers.Users.delete(id: Long)
  • 60. FRAMEWORKS: PLAY class Users extends Controller { def list = Action { request => val users = User.findAll Ok(Json.toJson(users)) } } @elmanu Dienstag, 03. Dezember 13
  • 61. FRAMEWORKS: PLAY class Users extends Controller { def list = Action { request => val users = User.findAll Ok(Json.toJson(users)) } } @elmanu Dienstag, 03. Dezember 13 200 OK Content-Type: application/json