SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 1 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
ENGLISHMAN IN NEWENGLISHMAN IN NEW
YORKYORK
Scala for Java Developer => Personal Experience
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 2 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
WHO IS THIS GUY?WHO IS THIS GUY?
12+ yeas in Java Development
JEEconf , speaker
UA Web Challenge , , , judge
Java Stage Producer
Skype: anton.naumow
2011 2012
I II III IV
HotCode
anton.naumow@gmail.com
@antonnaumov
ua.linkedin.com/in/antonnaumov/
http://corwin.calepin.co
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 3 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
WHY SCALA?WHY SCALA?
Buzz word?
Modern technology
Respect
New knowledge and new challenge
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 4 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
GETTING STARTEDGETTING STARTED
object HelloWorld {
def main(args: Array[String]) {
println("Hello world!")
}
}
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 5 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
GETTING STARTEDGETTING STARTED
...
def sum(bars: Iterable[Bar]): Long = {
bars.filter(_.type == SOME).foldLeft(0L)(_ + _.amount)
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 6 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
GETTING STARTEDGETTING STARTED
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 7 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
TRY ONCE MORETRY ONCE MORE
Daniel Spiewak "Scala for Java Refugees"
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 8 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
LIGHT SIDELIGHT SIDE
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 9 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
OPTIONOPTION
I think its no NullPointerExceptions anymore
...
Option(getBar("foo"))
def bar(index: java.lang.Integer): Option[Int] = {
Option(index) match {
case value: Some => value
case None => None
}
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 10 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
TUPPLETUPPLE
Up to 22 arguments. No stupid classes. No arrays.
No guessing.
...
def bar(pattern: String):(Int, String) = (pattern.length, pattern)
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 11 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
MATCHMATCH
It's something like switch... but much better
...
(number, string) match {
case (1, "test") => ...
case (_, "another test") => ...
case _ => ...
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 12 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
COLLECTIONSCOLLECTIONS
Some magic happens here
...
final Collection<Integer> coll = Lists.newArrayList(1, 2, 3, 4, 5);
...
for (final int item : coll) {
if (item == 5) {
return item;
}
}
return -1;
...
...
val coll = Seq(1, 2, 3, 4, 5)
...
coll.find(_ == 5) match {
case Some(item) => item
case None => ...
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 13 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
EXCEPTION HANDLINGEXCEPTION HANDLING
Something like Java 1.9
...
try {
//some code throws exceptions
catch {
case e: IllegalAccessException => ...
case e: Exception => ...
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 14 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
TRAITTRAIT
No comments
trait First {
def convert(input: String): String
}
trait Second {
def out(input: String) => System.out.println(bar(input))
}
class Third extends First with Second {
override def convert(input: String): String => input.toLowerCase()
}
...
val obj = new Third
obj.out("HeLlO TrAiTs!")
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 15 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
MULTITHREADINGMULTITHREADING
Simple. More simple. The simplest
Iterable[String] seq = Seq("one", "two", "three")
seq.par.map { str => str.toUpperCase() }
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 16 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
XMLXML
Like a charm
val xml =
<root>
<node name="first">Some text</node>
<node name="second">Some other text</node>
</root>
...
val source = XML.load(IOUtils.toInputStream(xml)
(source  "node"  "@name").text match {
case "first" => "First node found"
case _ => (source  "node").text
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 17 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
DARK SIDEDARK SIDE
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 18 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
COMPILATION TIMECOMPILATION TIME
Scala code compiles about 30% longer than Java code
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 19 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
CODE READABILITYCODE READABILITY
Somekind like this still mess a point
def parseResponse(response: String): Map[String, String] = {
(for (param <- response.split("&");
trimmed = URLDecoder.decode(param.trim, "UTF-8");
if (!trimmed.isEmpty) yield {
(trimmed.split("=", 2).toList: @unchecked) match {
case key :: value :: Nil => key -> value
case key :: Nil => key -> null
}
}).toMap
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 20 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
OPERATOR OVERRIDING, IMPLICITOPERATOR OVERRIDING, IMPLICIT
Not obvious language constructions always MAY produce a
mess
And always will produce a mess
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 21 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
NO BACKWARD COMPATIBILITYNO BACKWARD COMPATIBILITY
Scala 2.9 code produce an runtime exception running with
Scala 2.10
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 22 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
DIAMOND PROBLEMDIAMOND PROBLEM
Multiple inheritance solution is architecture bomb
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 23 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
TOP 3 SCALA FEATURESTOP 3 SCALA FEATURES
Something not obvious but very useful
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 24 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
MODELMODEL
class Fee {
public Long getAmount() { return amount; }
public void setAmount(final Long amount) { this.amount = amount; }
public String getType() { return type; }
public void setType(final String type) { this.type = type; }
private Long amount;
private String type;
}
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 25 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
COLLECTION FILTERINGCOLLECTION FILTERING
JAVAJAVA
...
public long sumFeesByType(final Collection<Fee> fees,
final String type) {
long sum = 0;
for (final Fee fee : fees) {
if (fee != null && fee.getType()
.equalsIgnoreCase(type)) {
sum += fee.getAmount() == null ? 0L :
fee.getAmount()
}
}
return sum;
}
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 26 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
COLLECTION FILTERINGCOLLECTION FILTERING
SCALASCALA
...
def sumFeesByType(fees: util.Collection[Fee], feeType: String) fees
.filter(_.getType.eqaualsIgnoreCase(feeType)
.foldLeft(0L)(_ + _.getAmount)
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 27 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
OPTION AS COLLECTIONOPTION AS COLLECTION
...
val amount = fees.find(_.getType.equalsIgnoreCase("PROMO"))
.map(_.getAmount).getOrElse(0L)
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 28 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
VALUE CONVERSIONVALUE CONVERSION
JAVAJAVA
public final class FeeExtension {
private FeeExtension() {}
public static long getFeeAmount(fee: Fee) {
return fee.getAmount() == null ? 0L : fee.getAmount();
}
}
...
import FeeExtension.*
...
final long amount = getFeeAmount(fee);
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 29 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
VALUE CONVERSIONVALUE CONVERSION
SCALA :: IMPLICIT + OBJECTSCALA :: IMPLICIT + OBJECT
class FeeExtension(fee: Fee) {
def amount: Long = Option(fee.getAmount) match {
case Some(value) => value
case None => 0L
}
}
object FeeExtension {
implicit def extendsFee(fee: Fee) = new FeeExtension(fee)
}
...
import FeeExtension._
...
val amount = fee.amount
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 30 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
VALUE CONVERSIONVALUE CONVERSION
SCALA ::: PACKAGE OBJECTSCALA ::: PACKAGE OBJECT
package com.github.sample;
package object sample {
def amount(fee: Fee) = Option(fee) match {
case Some(value) => value
case None => 0L
}
...
package com.github.sample;
...
val amount = fee.amount
...
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 31 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
RESUMERESUME
Scala or Not Scala?
5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer
Page 32 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/
THANK YOU!THANK YOU!

Más contenido relacionado

Destacado (7)

Actividades TIC
Actividades TICActividades TIC
Actividades TIC
 
Dev-Time Liferay
Dev-Time  LiferayDev-Time  Liferay
Dev-Time Liferay
 
Pres 21 05-13
Pres 21 05-13Pres 21 05-13
Pres 21 05-13
 
adiccion face
adiccion faceadiccion face
adiccion face
 
Caracteristicas de metabolitos secundarios
Caracteristicas de metabolitos secundariosCaracteristicas de metabolitos secundarios
Caracteristicas de metabolitos secundarios
 
Tarea. unidad 2
Tarea. unidad 2Tarea. unidad 2
Tarea. unidad 2
 
Use maven in_right_way
Use maven in_right_wayUse maven in_right_way
Use maven in_right_way
 

Similar a Englishman in new york => scala for java developer

Next Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
Next Generation Java - Ceylon, Kotlin, Scala & Fantom im ÜberblickNext Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
Next Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
Benjamin Schmid
 
Decorating code (Research Paper)
Decorating code (Research Paper)Decorating code (Research Paper)
Decorating code (Research Paper)
Jenna Pederson
 
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScriptThe Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Hazelcast
 
Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924
yohanbeschi
 

Similar a Englishman in new york => scala for java developer (20)

JavaOne 2017: Eclipse OpenJ9: Under the hood of the JVM
JavaOne 2017: Eclipse OpenJ9: Under the hood of the JVMJavaOne 2017: Eclipse OpenJ9: Under the hood of the JVM
JavaOne 2017: Eclipse OpenJ9: Under the hood of the JVM
 
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
 
Embrace the JVM
Embrace the JVMEmbrace the JVM
Embrace the JVM
 
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
Next Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
Next Generation Java - Ceylon, Kotlin, Scala & Fantom im ÜberblickNext Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
Next Generation Java - Ceylon, Kotlin, Scala & Fantom im Überblick
 
Decorating code (Research Paper)
Decorating code (Research Paper)Decorating code (Research Paper)
Decorating code (Research Paper)
 
Java 8 Lambda
Java 8 LambdaJava 8 Lambda
Java 8 Lambda
 
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScriptThe Power of the JVM: Applied Polyglot Projects with Java and JavaScript
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Adopting GraalVM - Scale by the Bay 2018
Adopting GraalVM - Scale by the Bay 2018Adopting GraalVM - Scale by the Bay 2018
Adopting GraalVM - Scale by the Bay 2018
 
Unit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptxUnit No. 1 Introduction to Java.pptx
Unit No. 1 Introduction to Java.pptx
 
XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>
 
JavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureJavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and Future
 
Expressive And Modular Predicate Dispatch In Java
Expressive And Modular Predicate Dispatch In JavaExpressive And Modular Predicate Dispatch In Java
Expressive And Modular Predicate Dispatch In Java
 
State of Akka 2017 - The best is yet to come
State of Akka 2017 - The best is yet to comeState of Akka 2017 - The best is yet to come
State of Akka 2017 - The best is yet to come
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
In Vogue Dynamic
In Vogue DynamicIn Vogue Dynamic
In Vogue Dynamic
 
Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924Introduction to the Java bytecode - So@t - 20130924
Introduction to the Java bytecode - So@t - 20130924
 
ConSol_IBM_webcast_quarkus_the_blue_hedgehog_of_java_web_frameworks
ConSol_IBM_webcast_quarkus_the_blue_hedgehog_of_java_web_frameworksConSol_IBM_webcast_quarkus_the_blue_hedgehog_of_java_web_frameworks
ConSol_IBM_webcast_quarkus_the_blue_hedgehog_of_java_web_frameworks
 

Último

Último (20)

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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 

Englishman in new york => scala for java developer

  • 1. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 1 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ ENGLISHMAN IN NEWENGLISHMAN IN NEW YORKYORK Scala for Java Developer => Personal Experience
  • 2. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 2 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ WHO IS THIS GUY?WHO IS THIS GUY? 12+ yeas in Java Development JEEconf , speaker UA Web Challenge , , , judge Java Stage Producer Skype: anton.naumow 2011 2012 I II III IV HotCode anton.naumow@gmail.com @antonnaumov ua.linkedin.com/in/antonnaumov/ http://corwin.calepin.co
  • 3. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 3 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ WHY SCALA?WHY SCALA? Buzz word? Modern technology Respect New knowledge and new challenge
  • 4. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 4 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ GETTING STARTEDGETTING STARTED object HelloWorld { def main(args: Array[String]) { println("Hello world!") } }
  • 5. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 5 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ GETTING STARTEDGETTING STARTED ... def sum(bars: Iterable[Bar]): Long = { bars.filter(_.type == SOME).foldLeft(0L)(_ + _.amount) } ...
  • 6. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 6 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ GETTING STARTEDGETTING STARTED
  • 7. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 7 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ TRY ONCE MORETRY ONCE MORE Daniel Spiewak "Scala for Java Refugees"
  • 8. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 8 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ LIGHT SIDELIGHT SIDE
  • 9. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 9 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ OPTIONOPTION I think its no NullPointerExceptions anymore ... Option(getBar("foo")) def bar(index: java.lang.Integer): Option[Int] = { Option(index) match { case value: Some => value case None => None } } ...
  • 10. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 10 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ TUPPLETUPPLE Up to 22 arguments. No stupid classes. No arrays. No guessing. ... def bar(pattern: String):(Int, String) = (pattern.length, pattern) ...
  • 11. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 11 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ MATCHMATCH It's something like switch... but much better ... (number, string) match { case (1, "test") => ... case (_, "another test") => ... case _ => ... } ...
  • 12. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 12 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ COLLECTIONSCOLLECTIONS Some magic happens here ... final Collection<Integer> coll = Lists.newArrayList(1, 2, 3, 4, 5); ... for (final int item : coll) { if (item == 5) { return item; } } return -1; ... ... val coll = Seq(1, 2, 3, 4, 5) ... coll.find(_ == 5) match { case Some(item) => item case None => ... } ...
  • 13. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 13 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ EXCEPTION HANDLINGEXCEPTION HANDLING Something like Java 1.9 ... try { //some code throws exceptions catch { case e: IllegalAccessException => ... case e: Exception => ... } ...
  • 14. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 14 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ TRAITTRAIT No comments trait First { def convert(input: String): String } trait Second { def out(input: String) => System.out.println(bar(input)) } class Third extends First with Second { override def convert(input: String): String => input.toLowerCase() } ... val obj = new Third obj.out("HeLlO TrAiTs!")
  • 15. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 15 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ MULTITHREADINGMULTITHREADING Simple. More simple. The simplest Iterable[String] seq = Seq("one", "two", "three") seq.par.map { str => str.toUpperCase() }
  • 16. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 16 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ XMLXML Like a charm val xml = <root> <node name="first">Some text</node> <node name="second">Some other text</node> </root> ... val source = XML.load(IOUtils.toInputStream(xml) (source "node" "@name").text match { case "first" => "First node found" case _ => (source "node").text ...
  • 17. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 17 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ DARK SIDEDARK SIDE
  • 18. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 18 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ COMPILATION TIMECOMPILATION TIME Scala code compiles about 30% longer than Java code
  • 19. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 19 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ CODE READABILITYCODE READABILITY Somekind like this still mess a point def parseResponse(response: String): Map[String, String] = { (for (param <- response.split("&"); trimmed = URLDecoder.decode(param.trim, "UTF-8"); if (!trimmed.isEmpty) yield { (trimmed.split("=", 2).toList: @unchecked) match { case key :: value :: Nil => key -> value case key :: Nil => key -> null } }).toMap
  • 20. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 20 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ OPERATOR OVERRIDING, IMPLICITOPERATOR OVERRIDING, IMPLICIT Not obvious language constructions always MAY produce a mess And always will produce a mess
  • 21. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 21 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ NO BACKWARD COMPATIBILITYNO BACKWARD COMPATIBILITY Scala 2.9 code produce an runtime exception running with Scala 2.10
  • 22. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 22 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ DIAMOND PROBLEMDIAMOND PROBLEM Multiple inheritance solution is architecture bomb
  • 23. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 23 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ TOP 3 SCALA FEATURESTOP 3 SCALA FEATURES Something not obvious but very useful
  • 24. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 24 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ MODELMODEL class Fee { public Long getAmount() { return amount; } public void setAmount(final Long amount) { this.amount = amount; } public String getType() { return type; } public void setType(final String type) { this.type = type; } private Long amount; private String type; }
  • 25. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 25 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ COLLECTION FILTERINGCOLLECTION FILTERING JAVAJAVA ... public long sumFeesByType(final Collection<Fee> fees, final String type) { long sum = 0; for (final Fee fee : fees) { if (fee != null && fee.getType() .equalsIgnoreCase(type)) { sum += fee.getAmount() == null ? 0L : fee.getAmount() } } return sum; } ...
  • 26. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 26 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ COLLECTION FILTERINGCOLLECTION FILTERING SCALASCALA ... def sumFeesByType(fees: util.Collection[Fee], feeType: String) fees .filter(_.getType.eqaualsIgnoreCase(feeType) .foldLeft(0L)(_ + _.getAmount) ...
  • 27. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 27 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ OPTION AS COLLECTIONOPTION AS COLLECTION ... val amount = fees.find(_.getType.equalsIgnoreCase("PROMO")) .map(_.getAmount).getOrElse(0L) ...
  • 28. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 28 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ VALUE CONVERSIONVALUE CONVERSION JAVAJAVA public final class FeeExtension { private FeeExtension() {} public static long getFeeAmount(fee: Fee) { return fee.getAmount() == null ? 0L : fee.getAmount(); } } ... import FeeExtension.* ... final long amount = getFeeAmount(fee); ...
  • 29. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 29 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ VALUE CONVERSIONVALUE CONVERSION SCALA :: IMPLICIT + OBJECTSCALA :: IMPLICIT + OBJECT class FeeExtension(fee: Fee) { def amount: Long = Option(fee.getAmount) match { case Some(value) => value case None => 0L } } object FeeExtension { implicit def extendsFee(fee: Fee) = new FeeExtension(fee) } ... import FeeExtension._ ... val amount = fee.amount ...
  • 30. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 30 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ VALUE CONVERSIONVALUE CONVERSION SCALA ::: PACKAGE OBJECTSCALA ::: PACKAGE OBJECT package com.github.sample; package object sample { def amount(fee: Fee) = Option(fee) match { case Some(value) => value case None => 0L } ... package com.github.sample; ... val amount = fee.amount ...
  • 31. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 31 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ RESUMERESUME Scala or Not Scala?
  • 32. 5/18/13 5:22 PMEnglishman in New York - Scala for Java Developer Page 32 of 32file:///Users/Corwin/Developer/Projects/Personal/slides/englishman-in-new-york/index.html?print-pdf#/ THANK YOU!THANK YOU!