polyglot programming @ confess

46
Click to edit Master /tle style Polyglot Programming in the JVM Or how I learned to stop worrying and love the JVM

Upload: andres-almiray

Post on 10-May-2015

2.173 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Polyglot  Programming  in  the  JVM  Or  how  I  learned  to  stop  worrying  and  love  the  JVM  

Page 2: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  About  the  Speaker  

•  Java  developer  since  the  beginning  •  True  believer  in  Open  Source  •  Groovy  commiBer  since  2007  

•  Project  lead  of  the  Griffon  framework  •  Currently  working  for    

Page 3: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  I  confess…  

<j:jelly  xmlns:j="jelly:core"                    xmlns:define="jelly:define"                    xmlns:my="myTagLib">    <define:taglib  uri="myTagLib">            <define:jellybean  name="foo"  className="MyTask"/>    </define:taglib>    Now  lets  use  the  new  tag    <my:foo  x="2"  y="cheese"/>    

</j:jelly>  

Page 4: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Some  facts  about  Java  

  Previous  name  was  Oak    

–  Bonus  points  for  knowing  its  real  name  before  that    Made  its  public  appearance  in  1995  

  C/C++  were  king  at  the  /me    Networking,  mul/threading  were  baked  right  into  the  language  

  Developers  came  for  the  applets  and  stayed  for  the  components  (JEE  and  all  that  jazz)  

Page 5: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  However...  

  It‘s  already  in  its  teens  

  It  has  not  seen  a  major  feature  upgrade  since  JDK5  was  released  back  in  2004  -­‐>  generics    

(and  we  do  know  how  that  turned  out  to  be,  don’t  we?)  

  JDK7  was  been  delayed  again  (scheduled  for  Q3  2011)  

Some  features  will  not  make  the  cut    (closures  are  in  apparently)  

Page 6: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  More  so...  

  It  is  rather  verbose  when  compared  to  how  other  languages  do  the  same  task  

  Its  threading  features  are  no  longer  enough.    Concurrent  programs  desperately  cry  for  immutability  these  days  

Page 7: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Truth  or  myth?  

Is  Java  ofenly  referred  as  overengineered?  

Page 8: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Truth  or  myth?  

Can  you  build  a  Java  based  web  applica/on  (for  argument  sake  a  basic  TwiBer  clone)  in  less  than  a  day‘s  work  WITHOUT  an  IDE?  

Page 9: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Truth  or  myth?  

Did  James  Gosling  ever  say  he  was  threatened  with  bodily  harm  should  operator  

overloading  find  its  way  into  Java?  

Page 10: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

 The  JVM  is  a  great  place  to  work  however  Java  makes  it  painful  some/mes...  

Page 11: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

What  can  we  do  about  it?!  

Page 12: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Page 13: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Disclaimer  

(this  is  not  a  bash-­‐the-­‐other-­‐languages  talk)  

Page 14: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Reduced  Verbosity  

Page 15: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

public class Bean { private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

Page 16: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

public class Bean { private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

Page 17: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

public class Bean { private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

Page 18: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

class Bean {

String name

}

Page 19: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

class Bean(var name:String)

class Bean {

@scala.reflect.BeanProperty

var name: String }

Page 20: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Standard  Beans  

(defstruct Bean :name)

Page 21: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Closures  (or  Func/ons)  

public interface Adder {

int add(int a, int b);

}

public class MyAdder implements Adder {

public int add(int a, int b) {

return a + b; }

}

Page 22: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Closures  (or  Func/ons)  

// JDK7 Lambdas proposal

#(int a, int b)(a + b)

#(int a, int b) { return a + b; }

Page 23: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Closures  (or  Func/ons)  

def adder = { a , b -> a + b }

Page 24: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Closures  (or  Func/ons)  

val adder = (a:Int, b:Int) => a + b

Page 25: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Closures  (or  Func/ons)  

(defn adder [a b] (+ a b))

Page 26: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Enhanced  switch  

char letterGrade(int grade) { if(grade >= 0 && grade <= 60) return ‘F‘;

if(grade > 60 && grade <= 70) return ‘D‘;

if(grade > 70 && grade <= 80) return ‘C‘;

if(grade > 80 && grade <= 90) return ‘B‘;

if(grade > 90 && grade <= 100) return ‘A‘; throw new IllegalArgumentException("invalid grade "+grade);

}

Page 27: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Enhanced  Switch  

def letterGrade(grade) { switch(grade) { case 90..100: return ‘A‘ case 80..<90: return ‘B‘ case 70..<80: return ‘C‘ case 60..<70: return ‘D‘ case 0..<60: return ‘F‘ case ~"[ABCDFabcdf]": return grade.toUpperCase() } throw new IllegalArgumentException('invalid grade ‘+grade) }

Page 28: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Enhanced  Switch  

val VALID_GRADES = Set("A", "B", "C", "D", "F")

def letterGrade(value: Any):String = value match {

case x:Int if (90 to 100).contains(x) => "A"

case x:Int if (80 to 90).contains(x) => "B"

case x:Int if (70 to 80).contains(x) => "C"

case x:Int if (60 to 70).contains(x) => "D"

case x:Int if (0 to 60).contains(x) => "F"

case x:String if VALID_GRADES(x.toUpperCase) => x.toUpperCase()

}

Page 29: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Enhanced  Switch  

(defn letter-grade [grade]

(cond

(in grade 90 100) "A"

(in grade 80 90) "B"

(in grade 70 80) "C"

(in grade 60 70) "D"

(in grade 0 60) "F"

(re-find #"[ABCDFabcdf]" grade) (.toUpperCase grade)))

Page 30: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Java  Interoperability  

Page 31: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  All  of  these  are  true  

  Java  can  call  Groovy,  Scala  and  Clojure  classes  as  if  they  were  Java  classes  

  Groovy,  Scala  and  Clojure  can  call  Java  code  without  breaking  a  sweat!  

  In  other  words,  interoperability  with  Java  is  a  given.  

   No  need  for  complicated  bridges  between  languages  (i.e.  JSR  223)  

Page 32: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

OK,  so...  

What  else  can  these  languages  do?  

Page 33: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  All  of  them    

  Na/ve  syntax  for  collec/on  classes  

  Everything  is  an  object    Closures!  

  Regular  expressions  as  first  class  ci/zens    Operator  overloading  

Page 34: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Groovy  

  Metaprogramming  (HOT!)  both  at  build/me  and  run/me  

  Builders    Healthy  ecosystem:  Grails,  Griffon,  Gant,  Gradle,  Spock,  Gaelyk,  Gpars,  CodeNarc,  etc...  

  Not  an  exhaus/ve  list  of  features!  

Page 35: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Scala  

  Richer  type  system  

  Type  inference    PaBern  matching  (case  classes)  

  Actor  model    Traits  

  Not  an  exhaus/ve  list  of  features!  

Page 36: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Clojure  

  Data  as  code  and  viceversa  

  Immutable  structures    STM  

  Not  an  exhaus/ve  list  of  features!  

Page 37: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Demo  

Page 38: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Other  places  where  you‘ll  find  Polyglot  Programming  

Page 39: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Web  app  development  

  XML  

  SQL    JavaScript  

  JSON    CSS  

  Flash/Flex/Ac/onScript  

Page 40: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Next-­‐Gen  datastores  (NoSQL)  

  FleetDB  -­‐>  Clojure  

  FlockDB  -­‐>  Scala  

  CouchDB,  Riak  -­‐>  Erlang  

  By  the  way,  watch  out  for  Polyglot  Persistence  ;-­‐)  

Page 41: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Build  systems  

  Gradle,  Gant  -­‐>  Groovy  

  Rake  -­‐>  Ruby/JRuby  

  Maven3  -­‐>  XML,  Groovy,  Ruby  

Page 42: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Par/ng  thoughts  

  Java  (the  language)  may  have  reached  its  maturity  feature  wise  (it’s  not  dead  though!)  

  Other  JVM  languages  have  evolved  faster  

  Polyglot  Programming  is  not  a  new  concept  

  Download  and  play  with  each  of  the  demoed  languages,  maybe  one  of  them  strikes  your  fancy  

Page 43: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  Resources  

Page 44: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Q  &  A  

Page 45: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

hBp://people.canoo.com/share  

Page 46: Polyglot Programming @ CONFESS

Click  to  edit  Master  /tle  style  

Thank  you!