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 NEWYORKYORK
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 , speakerUA Web Challenge , , , judge
Java Stage Producer
Skype: anton.naumow
2011 2012I II III IV
HotCode
@antonnaumovua.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 MOREDaniel 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#/
OPTIONOPTIONI 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#/
TUPPLETUPPLEUp 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#/
MATCHMATCHIt'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#/
COLLECTIONSCOLLECTIONSSome 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 HANDLINGSomething 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#/
TRAITTRAITNo 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#/
MULTITHREADINGMULTITHREADINGSimple. 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#/
XMLXMLLike 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 TIMEScala 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 READABILITYSomekind 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, IMPLICITNot 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 COMPATIBILITYScala 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 PROBLEMMultiple 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 FEATURESSomething 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 FILTERINGJAVAJAVA
... 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 FILTERINGSCALASCALA
... 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 CONVERSIONJAVAJAVA
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 CONVERSIONSCALA :: 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 CONVERSIONSCALA ::: 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#/
RESUMERESUMEScala 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!
Top Related