SlideShare a Scribd company logo
1 of 27
Why Scala is the better Java
Thomas Kaiser
Technologieplauscherl
14.01.2016
Agenda
• Why this talk
• Cool language features
• Pitfalls and how to avoid them
• Resources
Quick survey
• Who...
• ... Writes Java code for production?
• ... Has ever written any Scala code?
• ... Has ever written Scala for production?
• Me
• Java background (Spring, Hibernate, Grails, GWT, Android)
• Scala webapp w/ Postgres, Mongo, Play, Angular, REST
What is Scala
• JVM based functional/OO language
• Powerful type system
• Traits/mixins
• Higher-order functions
• Pattern matching
• Concurrency abstractions
Why this talk
• People are interested in Scala, but also afraid?
• Scala is extremely powerful and flexible
• Syntax
• Huge library
• Lots of ways to do the same thing
• Very intimidating type system
• BUT! You don‘t have to use any of the advanced features and still
benefit massively
Why Scala
• Extremely expressive (LOC--)
• Static typing
• Very clean style comes built-in
• Awesome (awesome) library (collections etc)
• Writing Scala code is actually fun!
Language features (why I love Scala)
• Tuples
• String concatenation
• Local functions
• Import aliases
• Super-easy collection/map creation
• Powerful collections library
• Traits
• Case classes
• Pattern matching
• Function values/function literals
• Multiple parameter lists
• Option[T]
• For-Comprehensions
• Everything is a value
• Syntax flexibility
• Type inference
• JSON/XML handling
• Scalatest/specs2
• Implicit conversions
• Annymous/structural types, type aliases
• Algebraic data types
S vs. J: Boilerplate
public class User {
private String name;
private List<Order> orders;
public User() {
orders = new ArrayList<Order>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order>
orders) {
this.orders = orders;
}
}
public class Order {
private int id;
private List<Product> products;
public Order() {
products = new ArrayList<Product>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product>
products) {
this.products = products;
}
}
public class Product {
private int id;
private String category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String
category) {
this.category = category;
}
}
S vs. J: Boilerplate
case class User(name: String, orders: List[Order])
case class Order(id: Int, products: List[Product])
case class Product(id: Int, category: String)
S vs. J: Standard use case – extract data
• Get products of a User
public class User {
...
public List<Product> getProducts() {
List<Product> products = new ArrayList<Product>();
for (Order order : orders) {
products.addAll(order.getProducts());
}
return products;
}
}
S vs. J: Standard use case – extract data
• Get ordered products of a User
def products = orders.flatMap(o => o.products)
S vs. J: Remove x highest values
public static List<Integer> removeHighestValuesJ6(
List<Integer> values, int amount) {
List<Integer> localValues = new ArrayList<>(values);
Collections.sort(localValues);
int toIndex = localValues.size() - amount;
if (toIndex < 0) {
toIndex = 0;
}
return localValues.subList(0, toIndex);
}
public static List<Integer> removeHighestValuesJ8(
List<Integer> values, int amount) {
Collections.sort(values); // in-place! boo!
Collections.reverse(values); // in-place!
List<Integer> result = values.stream()
.skip(amount)
.collect(Collectors.toList());
return result;
}
S vs. J: Remove x highest values
def removeHighestValues(list: List[Int], amount: Int) = {
list.sorted.reverse.drop(amount)
}
For comprehensions each++
for (i <- 1 to 7) print(i) // 1234567
for (i <- 1 to 7 if i % 2 == 0) print(i) // 246
case class Person(age: Int)
val persons =
for (i <- 20 to 35 by 5) yield Person(i)
// Person(20), Person(25), Person(30), Person(35)
Pattern matching
case class Person(name: String, age: Int)
val p1 = Person("thomas", 29)
val p2 = Person("dominik", 32)
p2 match {
case Person("thomas", 29) => "exactly that person"
case Person(_, age) if age > 30 => "anyone over 30"
case p : Person => s"just binding to a variable (${p.name})"
}
Pattern matching
val list = List(1, 2, 3)
list match {
case 1 :: 2 :: rest => "list starts with 1,2 and has " + rest.length + " more"
}
Everything is a value
val bool = true
val yepNope = if (bool) "yep" else "nope"
case class AuthUser(email: String, roles: Set[String])
val currentUser = AuthUser(”user", Set("ADMIN"))
val isAuthorized = currentUser match {
case AuthUser(_, roles) if roles.contains("ADMIN") => true
case _ => false
}
Everything is a value
def russianRoulette() =
if (Random.nextInt(6) == 0)
throw new RuntimeException(“bang“)
else
"phew“
val dangerousResult: String =
try {
russianRoulette()
} catch {
case RuntimeException => "dead :(“
}
Implicit conversions
val hello: String = "hello"
hello.emphasize // Error:(32, 8) value emphasize is not a member of String
„Pimp my library pattern“
implicit class PimpedString(base: String) {
def emphasize = base + "!!!11"
}
import PimpedString
val pimpedString = hello.emphasize // hello!!!11
Implicit conversions
for (i <- 1 to 7) <=> 1.to(7) <=> val r: Range = new RichInt(1).to(7)
DSLs
class HelloWorldSpec extends Specification {
"Hello world" should {
val hw = "Hello world"
“be 11 chars long" in {
hw should have size 11
}
"start with hello" in {
hw should startWith "hello“
}
}
}
DSLs
"show off some mocking DSL" in {
val m = mock[List[String]]
m.get(0) returns “0"
// .. code under test ...
there was one(m).get(0)
}
DSLs
val messageFlow =
filter { payload: String => payload == "World" }
-->
transform { payload: String => "Hello " + payload }
-->
handle { payload: String => println(payload) }
messageFlow.send("World") // "Hello World"
Implicit conversions
for (i <- 1 to 7) <=> 1.to(7) <=> val r: Range = new RichInt(1).to(7)
Map(1 -> "one") <=> 1.->("one") <=> val entry : (Int, String) = new ArrowAssoc[Int](1).->("one”)
val x = Future { ... }
Await.result(x, 5 seconds) <=> new DurationInt(5)
Pitfalls
• Implicits can be confusing
• Many different codestyles in the wild
• Hard to understand libraries
• Yet another build tool (SBT)
Take away
• Just do it!
• You can always go back or mix Java and Scala code
• ... But you won‘t
• Be wary of advanced language features
• Don‘t be scared, you don‘t have to use anything
• Use it as a very expressive, fun, supercharged Java
• ... And you won‘t look back
• „Scalable language“
Resources
• http://scala-lang.org/
• http://docs.scala-lang.org/cheatsheets/
• List of links (!!)
• Learning functional programming without growing a neckbeard
• https://github.com/pocorall/scaloid (Scala4Android, super cool)

More Related Content

What's hot

The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 41 of 202
The Ring programming language version 1.8 book - Part 41 of 202The Ring programming language version 1.8 book - Part 41 of 202
The Ring programming language version 1.8 book - Part 41 of 202Mahmoud Samir Fayed
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation streamRuslan Shevchenko
 
Scala jeff
Scala jeffScala jeff
Scala jeffjeff kit
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84Mahmoud Samir Fayed
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
Introduction aux Macros
Introduction aux MacrosIntroduction aux Macros
Introduction aux Macrosunivalence
 

What's hot (17)

1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181
 
All about scala
All about scalaAll about scala
All about scala
 
The Ring programming language version 1.8 book - Part 41 of 202
The Ring programming language version 1.8 book - Part 41 of 202The Ring programming language version 1.8 book - Part 41 of 202
The Ring programming language version 1.8 book - Part 41 of 202
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Scala jeff
Scala jeffScala jeff
Scala jeff
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84
 
Introduction to-scala
Introduction to-scalaIntroduction to-scala
Introduction to-scala
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
 
Introduction aux Macros
Introduction aux MacrosIntroduction aux Macros
Introduction aux Macros
 

Viewers also liked

Meenakshi Article Feb 2016 Chartered Secretary Issue
Meenakshi Article Feb  2016 Chartered Secretary IssueMeenakshi Article Feb  2016 Chartered Secretary Issue
Meenakshi Article Feb 2016 Chartered Secretary IssueMeenakshi Narasimhan
 
Prog i encuentromovimiento
Prog i encuentromovimientoProg i encuentromovimiento
Prog i encuentromovimientosatelite1
 
Folleto marzo 2016
Folleto marzo 2016Folleto marzo 2016
Folleto marzo 2016satelite1
 
Circular glorr campaña recollida de roupa familias setembro 2016
Circular glorr campaña recollida de roupa familias setembro 2016Circular glorr campaña recollida de roupa familias setembro 2016
Circular glorr campaña recollida de roupa familias setembro 2016satelite1
 
Proyecto 2
Proyecto 2Proyecto 2
Proyecto 2Jossy98
 
Nós tamén creamos
Nós tamén creamosNós tamén creamos
Nós tamén creamossatelite1
 
Nuevas siete maravillas del mundo moderno
Nuevas siete maravillas del mundo modernoNuevas siete maravillas del mundo moderno
Nuevas siete maravillas del mundo modernoAntony Loza Perez
 
Folla de inscrición no concurso logotipo
Folla de inscrición no concurso logotipoFolla de inscrición no concurso logotipo
Folla de inscrición no concurso logotipoYolanda Castro
 
Xornadas Educación para o Desenvolvemento e Cidadanía Global
Xornadas Educación para o Desenvolvemento e Cidadanía GlobalXornadas Educación para o Desenvolvemento e Cidadanía Global
Xornadas Educación para o Desenvolvemento e Cidadanía Globalsatelite1
 
Süreç Yönetimi
Süreç YönetimiSüreç Yönetimi
Süreç YönetimiFirat Ozel
 
το πιο γλυκό ψωμί
το πιο γλυκό ψωμίτο πιο γλυκό ψωμί
το πιο γλυκό ψωμίvaralig
 
ιταλο καλβινο
ιταλο καλβινοιταλο καλβινο
ιταλο καλβινοvaralig
 

Viewers also liked (14)

Meenakshi Article Feb 2016 Chartered Secretary Issue
Meenakshi Article Feb  2016 Chartered Secretary IssueMeenakshi Article Feb  2016 Chartered Secretary Issue
Meenakshi Article Feb 2016 Chartered Secretary Issue
 
Prog i encuentromovimiento
Prog i encuentromovimientoProg i encuentromovimiento
Prog i encuentromovimiento
 
Folleto marzo 2016
Folleto marzo 2016Folleto marzo 2016
Folleto marzo 2016
 
Circular glorr campaña recollida de roupa familias setembro 2016
Circular glorr campaña recollida de roupa familias setembro 2016Circular glorr campaña recollida de roupa familias setembro 2016
Circular glorr campaña recollida de roupa familias setembro 2016
 
Proyecto 2
Proyecto 2Proyecto 2
Proyecto 2
 
PROYECTO 1
PROYECTO 1PROYECTO 1
PROYECTO 1
 
Día del joven
Día del jovenDía del joven
Día del joven
 
Nós tamén creamos
Nós tamén creamosNós tamén creamos
Nós tamén creamos
 
Nuevas siete maravillas del mundo moderno
Nuevas siete maravillas del mundo modernoNuevas siete maravillas del mundo moderno
Nuevas siete maravillas del mundo moderno
 
Folla de inscrición no concurso logotipo
Folla de inscrición no concurso logotipoFolla de inscrición no concurso logotipo
Folla de inscrición no concurso logotipo
 
Xornadas Educación para o Desenvolvemento e Cidadanía Global
Xornadas Educación para o Desenvolvemento e Cidadanía GlobalXornadas Educación para o Desenvolvemento e Cidadanía Global
Xornadas Educación para o Desenvolvemento e Cidadanía Global
 
Süreç Yönetimi
Süreç YönetimiSüreç Yönetimi
Süreç Yönetimi
 
το πιο γλυκό ψωμί
το πιο γλυκό ψωμίτο πιο γλυκό ψωμί
το πιο γλυκό ψωμί
 
ιταλο καλβινο
ιταλο καλβινοιταλο καλβινο
ιταλο καλβινο
 

Similar to Why Scala is the better Java

An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Hello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebHello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebMuhammad Raza
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212Mahmoud Samir Fayed
 
Kotlin: maybe it's the right time
Kotlin: maybe it's the right timeKotlin: maybe it's the right time
Kotlin: maybe it's the right timeDavide Cerbo
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersMatthew Farwell
 
The Ring programming language version 1.5.4 book - Part 37 of 185
The Ring programming language version 1.5.4 book - Part 37 of 185The Ring programming language version 1.5.4 book - Part 37 of 185
The Ring programming language version 1.5.4 book - Part 37 of 185Mahmoud Samir Fayed
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 

Similar to Why Scala is the better Java (20)

An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Hello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebHello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC Unideb
 
Scala
ScalaScala
Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Benefits of Kotlin
Benefits of KotlinBenefits of Kotlin
Benefits of Kotlin
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Kotlin: maybe it's the right time
Kotlin: maybe it's the right timeKotlin: maybe it's the right time
Kotlin: maybe it's the right time
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
The Ring programming language version 1.5.4 book - Part 37 of 185
The Ring programming language version 1.5.4 book - Part 37 of 185The Ring programming language version 1.5.4 book - Part 37 of 185
The Ring programming language version 1.5.4 book - Part 37 of 185
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 

Recently uploaded

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 

Recently uploaded (20)

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

Why Scala is the better Java

  • 1. Why Scala is the better Java Thomas Kaiser Technologieplauscherl 14.01.2016
  • 2. Agenda • Why this talk • Cool language features • Pitfalls and how to avoid them • Resources
  • 3. Quick survey • Who... • ... Writes Java code for production? • ... Has ever written any Scala code? • ... Has ever written Scala for production? • Me • Java background (Spring, Hibernate, Grails, GWT, Android) • Scala webapp w/ Postgres, Mongo, Play, Angular, REST
  • 4. What is Scala • JVM based functional/OO language • Powerful type system • Traits/mixins • Higher-order functions • Pattern matching • Concurrency abstractions
  • 5. Why this talk • People are interested in Scala, but also afraid? • Scala is extremely powerful and flexible • Syntax • Huge library • Lots of ways to do the same thing • Very intimidating type system • BUT! You don‘t have to use any of the advanced features and still benefit massively
  • 6. Why Scala • Extremely expressive (LOC--) • Static typing • Very clean style comes built-in • Awesome (awesome) library (collections etc) • Writing Scala code is actually fun!
  • 7. Language features (why I love Scala) • Tuples • String concatenation • Local functions • Import aliases • Super-easy collection/map creation • Powerful collections library • Traits • Case classes • Pattern matching • Function values/function literals • Multiple parameter lists • Option[T] • For-Comprehensions • Everything is a value • Syntax flexibility • Type inference • JSON/XML handling • Scalatest/specs2 • Implicit conversions • Annymous/structural types, type aliases • Algebraic data types
  • 8. S vs. J: Boilerplate public class User { private String name; private List<Order> orders; public User() { orders = new ArrayList<Order>(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } } public class Order { private int id; private List<Product> products; public Order() { products = new ArrayList<Product>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public List<Product> getProducts() { return products; } public void setProducts(List<Product> products) { this.products = products; } } public class Product { private int id; private String category; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } }
  • 9. S vs. J: Boilerplate case class User(name: String, orders: List[Order]) case class Order(id: Int, products: List[Product]) case class Product(id: Int, category: String)
  • 10. S vs. J: Standard use case – extract data • Get products of a User public class User { ... public List<Product> getProducts() { List<Product> products = new ArrayList<Product>(); for (Order order : orders) { products.addAll(order.getProducts()); } return products; } }
  • 11. S vs. J: Standard use case – extract data • Get ordered products of a User def products = orders.flatMap(o => o.products)
  • 12. S vs. J: Remove x highest values public static List<Integer> removeHighestValuesJ6( List<Integer> values, int amount) { List<Integer> localValues = new ArrayList<>(values); Collections.sort(localValues); int toIndex = localValues.size() - amount; if (toIndex < 0) { toIndex = 0; } return localValues.subList(0, toIndex); } public static List<Integer> removeHighestValuesJ8( List<Integer> values, int amount) { Collections.sort(values); // in-place! boo! Collections.reverse(values); // in-place! List<Integer> result = values.stream() .skip(amount) .collect(Collectors.toList()); return result; }
  • 13. S vs. J: Remove x highest values def removeHighestValues(list: List[Int], amount: Int) = { list.sorted.reverse.drop(amount) }
  • 14. For comprehensions each++ for (i <- 1 to 7) print(i) // 1234567 for (i <- 1 to 7 if i % 2 == 0) print(i) // 246 case class Person(age: Int) val persons = for (i <- 20 to 35 by 5) yield Person(i) // Person(20), Person(25), Person(30), Person(35)
  • 15. Pattern matching case class Person(name: String, age: Int) val p1 = Person("thomas", 29) val p2 = Person("dominik", 32) p2 match { case Person("thomas", 29) => "exactly that person" case Person(_, age) if age > 30 => "anyone over 30" case p : Person => s"just binding to a variable (${p.name})" }
  • 16. Pattern matching val list = List(1, 2, 3) list match { case 1 :: 2 :: rest => "list starts with 1,2 and has " + rest.length + " more" }
  • 17. Everything is a value val bool = true val yepNope = if (bool) "yep" else "nope" case class AuthUser(email: String, roles: Set[String]) val currentUser = AuthUser(”user", Set("ADMIN")) val isAuthorized = currentUser match { case AuthUser(_, roles) if roles.contains("ADMIN") => true case _ => false }
  • 18. Everything is a value def russianRoulette() = if (Random.nextInt(6) == 0) throw new RuntimeException(“bang“) else "phew“ val dangerousResult: String = try { russianRoulette() } catch { case RuntimeException => "dead :(“ }
  • 19. Implicit conversions val hello: String = "hello" hello.emphasize // Error:(32, 8) value emphasize is not a member of String „Pimp my library pattern“ implicit class PimpedString(base: String) { def emphasize = base + "!!!11" } import PimpedString val pimpedString = hello.emphasize // hello!!!11
  • 20. Implicit conversions for (i <- 1 to 7) <=> 1.to(7) <=> val r: Range = new RichInt(1).to(7)
  • 21. DSLs class HelloWorldSpec extends Specification { "Hello world" should { val hw = "Hello world" “be 11 chars long" in { hw should have size 11 } "start with hello" in { hw should startWith "hello“ } } }
  • 22. DSLs "show off some mocking DSL" in { val m = mock[List[String]] m.get(0) returns “0" // .. code under test ... there was one(m).get(0) }
  • 23. DSLs val messageFlow = filter { payload: String => payload == "World" } --> transform { payload: String => "Hello " + payload } --> handle { payload: String => println(payload) } messageFlow.send("World") // "Hello World"
  • 24. Implicit conversions for (i <- 1 to 7) <=> 1.to(7) <=> val r: Range = new RichInt(1).to(7) Map(1 -> "one") <=> 1.->("one") <=> val entry : (Int, String) = new ArrowAssoc[Int](1).->("one”) val x = Future { ... } Await.result(x, 5 seconds) <=> new DurationInt(5)
  • 25. Pitfalls • Implicits can be confusing • Many different codestyles in the wild • Hard to understand libraries • Yet another build tool (SBT)
  • 26. Take away • Just do it! • You can always go back or mix Java and Scala code • ... But you won‘t • Be wary of advanced language features • Don‘t be scared, you don‘t have to use anything • Use it as a very expressive, fun, supercharged Java • ... And you won‘t look back • „Scalable language“
  • 27. Resources • http://scala-lang.org/ • http://docs.scala-lang.org/cheatsheets/ • List of links (!!) • Learning functional programming without growing a neckbeard • https://github.com/pocorall/scaloid (Scala4Android, super cool)

Editor's Notes

  1. Seamless interop
  2. NULL MUST DIE - immutable/final stuff, a var is a code smell, you end up with very short classes/methods - Fluent iterable example/ power at your fingertips
  3. + equals/hashcode, copy constructor
  4. val list = List(1, 2, 3) vs. List<Integer> list = new ArrayList<>(); list.add(1); … OK Arrays.asList gibts aber für maps? val map = Map(“tom” -> 3, “joe” -> 6)
  5. Spring MVC with scala controllers/services I can give you lots of examples of really large companies who risked it and very successful The language takes some learning, some investment but it also rewards you Groovy vs scala