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!