KEMBAR78
Live coding scala 'the java of the future' | PDF
Live Coding                         'the Java of the future’ 	
  
	
  




       Urs Peter
       19:45 uur - Zaal Zand (boven)	
  
Core ingredients of Scala

                       1.7	
  




            Java	
  
Who uses Scala?
Scala Dev environment
Options
          Our first sip of
Classes




 Tuples

                             Traits



               Implicits
Loops




A single ride with many loops
Pattern Matching
The future is bright, the future is fun
                 -ctional
Scala Collections




The beginning of a beautiful friendship
Google Code Jam Exercise
Problem	
  
The	
  aim	
  of	
  this	
  task	
  is	
  to	
  translate	
  a	
  language	
  into	
  a	
  new	
  language	
  called	
  Googlerese.	
  To	
  
translate	
  we	
  take	
  any	
  message	
  and	
  replace	
  each	
  English	
  le<er	
  with	
  another	
  English	
  le<er.	
  
This	
  mapping	
  is	
  one-­‐to-­‐one	
  and	
  onto,	
  which	
  means	
  that	
  the	
  same	
  input	
  le<er	
  always	
  gets	
  
replaced	
  with	
  the	
  same	
  output	
  le<er,	
  and	
  different	
  input	
  le<ers	
  always	
  get	
  replaced	
  with	
  
different	
  output	
  le<ers.	
  A	
  le<er	
  may	
  be	
  replaced	
  by	
  itself.	
  Spaces	
  are	
  leC	
  as-­‐is.	
  
	
  
For	
  example	
  (and	
  here	
  is	
  a	
  hint!),	
  the	
  translaIon	
  algorithm	
  includes	
  the	
  following	
  three	
  
mappings:	
  'a'	
  -­‐>	
  'y',	
  'o'	
  -­‐>	
  'e',	
  and	
  'z'	
  -­‐>	
  'q'.	
  This	
  means	
  that	
  "a	
  zoo"	
  will	
  become	
  "y	
  qee".	
  	
  
	
  
Sample	
  Input/Output	
  
Input:	
   	
  Case	
  #1:	
  ejp mysljylc kd kxveddknmc re jsicpdrysi

               	
  Case	
  #2:	
  rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd

               	
  Case	
  #3:	
  de kr kd eoya kw aej tysr re ujdr lkgc jv"
	
  
Output: 	
  Case	
  #1:	
  our language is impossible to understand

               	
  Case	
  #2:	
  there are twenty six factorial possibilities

               	
  Case	
  #3:	
  so it is okay if you want to just give up"
	
  
Lets go parallel
(Back to)
The future of Java
Java8 will have Lambda Expressions   	
  
 Java
   root.listFiles((File f) -> f.isDirectory());"
   "
   //even with type inference"
   root.listFiles(f -> f.isDirectory());"
   "
   //... and shorthand notation"
   root.listFiles(File::isDirectory);"
   "
   "
   "
 Scala	
  
   root.listFiles(f:File => f.isDirectory)"
   "
   root.listFiles(f => f.isDirectory)"
   "
   root.listFiles(_.isDirectory)"
Java8 will have internal iteration with Lambda expressions	
  
 Java
   double highestScore ="
         " students.filter(Student s -> s.getGradYear() == 2011)"
         "       " .map(Student s -> s.getScore())"
         "       " .max();"
   "
   "



 Scala	
  
   val highestScore = "
         "studens.filter(s:Student => s.gradYear == 2011)"
         "       ".map(s:Student => s.score)"
         "       ".max"
Java8 will have default implementations for interfaces
(virtual extension methods)  	
  
    Java
     "
"    "
"    interface List<T> … {"
           "// existing methods, plus"
           "void sort(Comparator<? super T> cmp) "default { "
           "       "Collections.sort(this, cmp); }"
           "}"
     //however: no fields, no self types, "
     //... And therefore ‘no multiple inheritance without the issues’"



    Scala	
  
     Traits ..."
     //with fields, self types"
     //... And therefore: ‘multiple inheritance without the issues’"
Java8 will have support for parallel Collections	
  
    Java
     "
"
     Set<Album> favs ="
"       albums.parallel()"
           " .filter(a -> a.tracks.anyMatch(t -> (t.rating >= 4)))"
           " .into(new ConcurrentHashSet<>());"



    Scala	
  
     val favs = "
        albums.par!
              .filter(a => a.tracks.exists(t => t.rating >= 4)"
              .toSet"
              "
Delivery date 	
  
Java8
"
"         2013 !
          in JDK 8!


Scala	
  

      "
      "
          since 2010 !
          in JDK 1.5, 1.6, 1.7       !
       "
      With an impressive track record of many mission
      critical applications by now ..."
My Advice:




    and	
  enjoy:	
  
def ask(question: Any) = question match {!
    case "?"   => "answer for ?"!
    case "???" => "answer for ???"!
    case _     => "42"!
 }"
Code samples can be found at:"
"
https://github.com/upeter/Java8vsScalaCodeSamples.git"
"
All rights reserved."
"

Live coding scala 'the java of the future'

  • 1.
    Live Coding 'the Java of the future’     Urs Peter 19:45 uur - Zaal Zand (boven)  
  • 2.
    Core ingredients ofScala 1.7   Java  
  • 3.
  • 4.
  • 5.
    Options Our first sip of Classes Tuples Traits Implicits
  • 6.
    Loops A single ridewith many loops
  • 8.
  • 9.
    The future isbright, the future is fun -ctional
  • 10.
    Scala Collections The beginningof a beautiful friendship
  • 11.
    Google Code JamExercise Problem   The  aim  of  this  task  is  to  translate  a  language  into  a  new  language  called  Googlerese.  To   translate  we  take  any  message  and  replace  each  English  le<er  with  another  English  le<er.   This  mapping  is  one-­‐to-­‐one  and  onto,  which  means  that  the  same  input  le<er  always  gets   replaced  with  the  same  output  le<er,  and  different  input  le<ers  always  get  replaced  with   different  output  le<ers.  A  le<er  may  be  replaced  by  itself.  Spaces  are  leC  as-­‐is.     For  example  (and  here  is  a  hint!),  the  translaIon  algorithm  includes  the  following  three   mappings:  'a'  -­‐>  'y',  'o'  -­‐>  'e',  and  'z'  -­‐>  'q'.  This  means  that  "a  zoo"  will  become  "y  qee".       Sample  Input/Output   Input:    Case  #1:  ejp mysljylc kd kxveddknmc re jsicpdrysi
  Case  #2:  rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd
  Case  #3:  de kr kd eoya kw aej tysr re ujdr lkgc jv"   Output:  Case  #1:  our language is impossible to understand
  Case  #2:  there are twenty six factorial possibilities
  Case  #3:  so it is okay if you want to just give up"  
  • 12.
  • 13.
  • 14.
    Java8 will haveLambda Expressions   Java root.listFiles((File f) -> f.isDirectory());" " //even with type inference" root.listFiles(f -> f.isDirectory());" " //... and shorthand notation" root.listFiles(File::isDirectory);" " " " Scala   root.listFiles(f:File => f.isDirectory)" " root.listFiles(f => f.isDirectory)" " root.listFiles(_.isDirectory)"
  • 15.
    Java8 will haveinternal iteration with Lambda expressions   Java double highestScore =" " students.filter(Student s -> s.getGradYear() == 2011)" " " .map(Student s -> s.getScore())" " " .max();" " " Scala   val highestScore = " "studens.filter(s:Student => s.gradYear == 2011)" " ".map(s:Student => s.score)" " ".max"
  • 16.
    Java8 will havedefault implementations for interfaces (virtual extension methods)   Java " " " " interface List<T> … {" "// existing methods, plus" "void sort(Comparator<? super T> cmp) "default { " " "Collections.sort(this, cmp); }" "}" //however: no fields, no self types, " //... And therefore ‘no multiple inheritance without the issues’" Scala   Traits ..." //with fields, self types" //... And therefore: ‘multiple inheritance without the issues’"
  • 17.
    Java8 will havesupport for parallel Collections   Java " " Set<Album> favs =" " albums.parallel()" " .filter(a -> a.tracks.anyMatch(t -> (t.rating >= 4)))" " .into(new ConcurrentHashSet<>());" Scala   val favs = " albums.par! .filter(a => a.tracks.exists(t => t.rating >= 4)" .toSet" "
  • 18.
    Delivery date   Java8 " " 2013 ! in JDK 8! Scala   " " since 2010 ! in JDK 1.5, 1.6, 1.7 ! " With an impressive track record of many mission critical applications by now ..."
  • 19.
    My Advice: and  enjoy:  
  • 20.
    def ask(question: Any)= question match {! case "?" => "answer for ?"! case "???" => "answer for ???"! case _ => "42"! }"
  • 21.
    Code samples canbe found at:" " https://github.com/upeter/Java8vsScalaCodeSamples.git" " All rights reserved." "