Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How Languages Influence Each Other: Reflections...

How Languages Influence Each Other: Reflections on 14 Years of Apache Groovy

Languages have been influencing one another since the dawn of computer programming. There are families of languages: from Algol descendants with begin/end code blocks to those with curly braces such as C. Languages are not invented in a vacuum but are inspired by their predecessors. This session’s speaker, who has been working on Apache Groovy for the past 14 years, reflects on the influences that have driven the design of programming languages. In particular, Groovy’s base syntax was directly derived from Java’s but quickly developed its own flavor, adding closures, type inference, and operators from Ruby. Groovy also inspired other languages: C#, Swift, and JavaScript adopted Groovy’s null-safe navigation operator and the famous Elvis operator.

Guillaume Laforge

October 04, 2017
Tweet

More Decks by Guillaume Laforge

Other Decks in Technology

Transcript

  1. REFLECTION ON 14 YEARS OF APACHE GROOVY HOW LANGUAGES INFLUENCE

    EACH OTHER GUILLAUME LAFORGE Apache Groovy PMC Chair Developer Advocate @ Google Cloud
 @glaforge
  2. @glaforge INTRODUCTION APACHE GROOVY ▸ Groovy was born in 2003

    ▸ Created by James Strachan & Bob McWhirter ▸ I joined the project in 2003 — 14 years ago! ▸ Currently serving as Chair of the Project Management Committee ▸ Groovy moved to the Apache Software Foundation to become Apache Groovy ▸ after Pivotal stopped funding the project 3
  3. @glaforge INTRODUCTION DOWNLOAD NUMBERS TO BE PROUD OF 4 Move

    to ASF 30M+ for 2/3 of ’17 23M in 2016 13M in 2015 4.6M in 2014 3.2M in 2013 1.7 in 2012
  4. @glaforge C FAMILY APACHE GROOVY PART OF THE C-FAMILY ▸

    Like Java, C#, C++, JavaScript, D, Go, Swift, Dart, or even PHP :-)
 Apache Groovy is part of the C-family of languages ▸ Key syntax characteristics ▸ Curly braces delimit blocks ▸ No whitespace indentation ▸ Signatures of the form: type functionName(parameters) { … } ▸ Usual control structures: if/else, switch/case, for/while loops… 6
  5. @glaforge C FAMILY JAVA’S CLOSE COUSIN — A VALID GROOVY

    PROGRAM TOO! 8 public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println(helloWorld.greet()); } }
  6. @glaforge C FAMILY JAVA’S CLOSE COUSIN — MORE IDIOMATIC! 9

    class HelloWorld { String name def greet() { "Hello ${name}" } } def helloWorld = new HelloWorld(name: "Groovy") println helloWorld.greet()
  7. @glaforge STRINGS STRINGS INSPIRED BY PYTHON 12 def firstname =

    'Guillaume' def address = ''' 1st Main Street CA 94123 San Francisco '''
  8. @glaforge STRINGS STRINGS INSPIRED BY PYTHON 13 def firstname =

    "Guillaume" def address = """ 1st Main Street CA 94123 San Francisco """
  9. @glaforge STRINGS STRING INTERPOLATION SIMILAR TO RUBY AND ANT 14

    def firstname = 'Guillaume' def lastname = 'Laforge' def name = "Mr ${firstname} $lastname"
  10. @glaforge STRINGS LAZY INTERPOLATION 15 def a = 'a' def

    b = 'b' def s = "${a} ${-> b}" assert s == 'a b' a = 'x' b = 'y' assert s == 'a y'
  11. @glaforge FUNCTIONAL ORIENTATION A FIRST APPROACH WITH RUBY’S AND SMALLTALK

    SYNTAX 18 // smalltalk [ :a | a * 2 ] // ruby {| a | a * 2 }
  12. @glaforge FUNCTIONAL ORIENTATION PROBLEM WITH BINARY AND LOGICAL OR FOR

    DEFAULT PARAMETERS 20 def d = { a = 1 | 2 | a * 2 }
  13. @glaforge OBJECT ORIENTATION TRAITS… A LA SCALA THEATER 24 trait

    FlyingAbility { String fly() { "I'm flying!" } } trait SpeakingAbility { String speak() { "I'm speaking!" } } class Duck implements FlyingAbility, SpeakingAbility {} def d = new Duck() assert d.fly() == "I'm flying!" assert d.speak() == "I'm speaking!"
  14. @glaforge SMALLTALK INFLUENCE NAMED PARAMETERS 26 // smalltalk Rectangle red

    width: 100 height: 200 // groovy rectangle red, width: 100, height: 200 def rectangle(Map m, Color r) { println "$m.width:$m.height $r" }
  15. @glaforge SMALLTALK INFLUENCE COLLECTION METHODS NAMES 27 // map //

    #(1, 2, 3, 4) collect: [ :it | :it * 2 ] assert (1..4).collect { it * 2 } == [2, 4, 6, 8] // foldLeft // #(1, 2, 3) inject: 0 into: [ :count :item | count + item ] assert [1, 2, 3].inject(0) { count, item -> count + item } == 6
  16. @glaforge LISTS, MAPS, COLLECTIONS… NATIVE SYNTAX FOR DATA STRUCTURES 29

    // python lists def list = [1, 2, 3] // like python maps // but with square brackets def map = [a: 1, b: 2]
  17. @glaforge CLOSURE TRAILING CLOSURE 33 def unless(boolean b, Closure action)

    { if (!b) action() } unless(false, { println "xyz" }) unless(false) { println "xyz" }
  18. @glaforge KOTLIN KOTLIN, A MIX OF JAVA, SCALA, AND… APACHE

    GROOVY ▸ Kotlin adopted ▸ the syntax of Groovy closures and its ‘it’ default parameter, ▸ the notation for trailing closures ▸ Groovy’s builder concept ▸ Kotlin’s data classes, delegate and lazy mechanisms, follow Groovy’s @Immutable, @Delegate and @Lazy transformations ▸ Method name convention based operator overloading 35
  19. @glaforge KOTLIN KOTLIN, A MIX OF JAVA, SCALA, AND… APACHE

    GROOVY ▸ Kotlin adopted Groovy’s optional semicolons ▸ Groovy’s Elvis operator ▸ Groovy’s null-safe navigation operator ▸ but with a different approach with non/nullable types ▸ Groovy’s interpolated strings (dollar placeholders with & without curly braces) ▸ Groovy’s import aliasing 36
  20. @glaforge KOTLIN GROOVY’S BUILDER 37 html { head { title

    ("a title") } body { h1 ("first header") p ("a paragraph") } } html { head { title {+"a title"} } body { h1 {+"first header"} p {+"a paragraph"} } }
  21. @glaforge INSPIRED OTHERS RANGES 45 // Ruby style initially //

    1..4 == [1, 2, 3, 4] and 1...4 == [1, 2, 3] // ..< is more visual assert 1..4 == [1, 2, 3, 4] assert 1..<4 == [1, 2, 3] // Swift adopted 1..<4
  22. @glaforge INSPIRED OTHERS THE SPACESHIP OPERATOR 47 assert (1 <=>

    1) == 0 assert (1 <=> 2) == -1 assert (2 <=> 1) == 1 assert ('a' <=> 'z') == -1
  23. THANKS Q & A GUILLAUME LAFORGE Apache Groovy PMC Chair

    Developer Advocate @ Google Cloud
 @glaforge