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

2014_GHC_Groovy_Gr8Ladies_Workshop_v16.pdf

jlstrater
October 11, 2014
460

 2014_GHC_Groovy_Gr8Ladies_Workshop_v16.pdf

jlstrater

October 11, 2014
Tweet

Transcript

  1. 2014
    Groovy & Gr8Ladies
    Jenn Strater & Allison F
    October 10, 2014
    @gr8ladies
    #GHC14
    2014

    View Slide

  2. 2014
    Who are We?
    §  Jenn Strater
    −  Software Engineer at Altegra Health
    −  Gr8Ladies Co-Founder
    •  Focus on retention and professional development
    §  Allison Figus
    −  Software Engineer at Bloom Health
    −  Gr8Ladies Co-Founder
    •  Focus on education and career changes

    View Slide

  3. 2014
    Setup
    §  http://bit.ly/graceHopperSetup
    §  https://groovyconsole.appspot.com/

    View Slide

  4. 2014
    Gr8Ladies
    §  Organization for the support of women in
    the Gr8 (Groovy, Grails, Gradle, etc)
    community
    −  Education
    −  Professional Development
    −  Networking
    −  Retention

    View Slide

  5. 2014
    What’s Groovy
    §  Groovy is a dynamic compiled language
    for the Java Virtual Machine (JVM).
    §  Interoperable with existing Java Libraries
    §  Used by many major companies including:

    View Slide

  6. 2014
    Groovy Console
    §  In the shell, type groovyConsole
    then type enter to execute the console.

    View Slide

  7. 2014
    Groovy Console Commands
    §  Mac:
    −  Cmd-R to run the script
    −  Select text and press delete to clear the white
    input box
    −  Cmd-W to clear the yellow output box
    §  Windows:
    −  Ctrl-R to run the script
    −  Select text and press delete to clear the white
    input box
    −  Ctrl-W to clear the yellow output box

    View Slide

  8. 2014
    Hello, Grace!
    §  In Java:
    public class Main {!
    public static void main(String[] arguments) {!
    System.out.println(“Hello, Grace!”);!
    }!
    }!
    à Hello, Grace!
    §  In Groovy:
    println “Hello, Grace!”!
    à Hello, Grace!!
    Try It!

    View Slide

  9. 2014
    Data types
    §  Strings
    §  Collections
    −  Lists
    −  Maps
    §  Closures
    §  def

    View Slide

  10. 2014
    Strings
    §  Single Quoted
    println 'Hello, World!’!
    à Hello, World!!
    §  Double Quoted (Groovy String)
    String name = ‘Gr8Ladies’!
    println "Hello, $name”!
    à Hello, Gr8Ladies!!
    §  Multi-line
    '''Hello, everyone from !
    The Grace Hopper!
    Celebration of Women!
    in Computing 2014!'''!

    View Slide

  11. 2014
    String Manipulation
    §  Split
    String s = ‘I <3 Groovy’!
    s.tokenize(‘ <3 ’)!
    à [‘I’, ‘Groovy’]!
    §  Join
    List los = [‘I' , ‘<3’, ‘Groovy’]!
    los.join(‘ ‘)!
    à I <3 Groovy!
    Try It!
    Try It!

    View Slide

  12. 2014
    Collections

    View Slide

  13. 2014
    Lists
    §  Definition
    List l = [‘a’,’b’,2]!
    §  Access: []!
    l[2] = 3!
    à  [a, b, 3]!
    §  Shovel operator: <<
    l << 5!
    à [a, b, 3, 5]!
    •  Multiply
    l.multiply(2)!
    à [a, b, 3, 5, a, b, 3, 5]!

    View Slide

  14. 2014
    §  Example
    List l = [‘a’, ‘b’, 2]!
    l[2] = 3!
    l << 5!
    l.multiply(2)!
    à [a, b, 3, 5, a, b, 3, 5]!
    Try It!

    View Slide

  15. 2014
    Maps
    §  Definition
    Map m = [val1: ‘a’, val2: ‘b’, val3: 2]!
    à[val1: a, val2: b, val3: 2]!
    §  Access
    −  by key
    m.val1!
    à a!
    m.val4 = 5!
    à [val1: a, val2: b, val3: 2, val4: 5]
    −  all values
    m.values()!
    à [a, b, 2, 5]!

    View Slide

  16. 2014
    §  Example
    Map m = [val1: ‘a’, val2: ‘b’, val3: 2]!
    m.val2 = ‘c’!
    m.val4 = 5!
    m.values()!
    à [a, c, 2, 5]!
    Try It!

    View Slide

  17. 2014
    Functions

    View Slide

  18. 2014
    Functions
    §  Extremely flexible, but familiar syntax
    §  Optional
    −  parenthesis around parameters when calling functions
    −  parameter typing
    −  return statements
    §  Example
    int foo (x) {!
    !x * 2!
    }!
    foo(4)!
    à  8!
    foo 5!
    à  10!
    int! foo! ( int! x )!
    return
    type!
    function
    name!
    parameter
    type!
    parameter
    name!

    View Slide

  19. 2014
    §  Example
    int foo(int x) {!
    x * 5!
    }!
    foo 6!
    à 30!
    Try It!

    View Slide

  20. 2014
    Closures
    §  Closures are code as data.
    §  Definition
    Closure myClosure = { name, int year = 2014 ->!
    println "Welcome to $name $year"!
    }!
    § Usage
    myClosure.call('Grace Hopper')!
    à Welcome to Grace Hopper 2014

    View Slide

  21. 2014
    §  Example
    def c = { name, int year = 2014 ->!
    println "Welcome to $name $year"!
    }!
    c.call('Grace Hopper')!
    à Welcome to Grace Hopper 2014
    !
    Try It!

    View Slide

  22. 2014
    Loops and Special Functions
    §  Definition
    −  Repeat the same functionality
    §  Basic For Loop
    §  Each
    §  Collect
    §  Spread Dot

    View Slide

  23. 2014
    Basic Loops
    §  Groovy
    List doubledList = []!
    for (i in [2, 4, 6]) {!
    doubledList << i * 2!
    }!
    à doubledList == [4, 8, 12]!
    §  Java!
    int[] myList = {2, 4, 6};!
    int[] doubledList = new int[3];!
    for (int i = 0; i < 3; i++) {!
    doubledList[i] = myList[i] * 2;!
    }!
    à  doubledList == [4, 8, 12]!

    View Slide

  24. 2014
    Each
    §  Example!
    Map m = [val1: 2, val2: 4, val3: 6]!
    List dl = []!
    m.each { key, value ->!
    dl << value * 2!
    }!
    println dl!
    à [4, 8, 12]!
    §  Loop equivalence
    Map myNewMap = [val1: 2, val2: 4, val3: 6]!
    List myDoubledList = []!
    for (keyValuePair in myNewMap) {!
    myDoubledList << keyValuePair.value * 2!
    }!
    println myDoubledList!
    à [4, 8, 12]!
    Try It!

    View Slide

  25. 2014
    Collect
    §  Example!
    Map m = [var1: 2, var2: 4, var3: 6]!
    List dl = m.collect { name, value ->!
    !value * 2!
    }!
    println dl!
    à [4, 8, 12]!
    §  Loop equivalence
    Map myNewMap = [val1: 2, val2: 4, val3: 6]!
    List myDoubledList = []!
    for (keyValuePair in myNewMap) {!
    myDoubledList << keyValuePair.value * 2!
    }!
    println myDoubledList!
    à [4, 8, 12]!
    Try It!

    View Slide

  26. 2014
    Spread Dot *.
    §  Example
    List l = [2, 4, 6]!
    l*.multiply(2)!
    à  [4, 8, 12]!
    §  Loop Equivalence
    List myList = [2, 4, 6]!
    List doubledList = []!
    for (value in myList) {!
    doubledList << value * 2!
    }!
    doubledList!
    à [4, 8, 12]!
    Try It!

    View Slide

  27. 2014
    §  Convert this loop to use *.
    List l = [-1, 2, -3, -17]!
    List al = []!
    for (i in l) {!
    al << i.abs()!
    }!
    al!
    à [1, 2, 3, 17]
    !
    Try It!

    View Slide

  28. 2014
    Spread Dot Exercise
    §  Answer
    List al = [-1, 2, -3, -17]*.abs()!
    à  [1, 2, 3, 17]!
    Or!
    List l = [-1, 2, -3, -17]!
    List al = l*.abs()!
    à [1, 2, 3, 17]
    !

    View Slide

  29. 2014
    §  Sum the numbers from 1 to 10
    Try It!

    View Slide

  30. 2014
    Possible Solutions
    1.  for in List!
    int answer = 0 !
    for (i in [1,2,3,4,5,6,7,8,9,10]) {!
    !answer += i!
    }!
    println answer!
    à 55!
    2.  Each!
    int answer = 0!
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each {!
    answer = answer + it!
    }!
    println answer!
    à 55!
    3.  Sum Function (Bonus)!
    int answer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sum()!

    View Slide

  31. 2014
    Search
    Map m = [var1: 2, var2: 4, var3: 6]!
    §  find
    m.find{ it.value%2 == 0 && it.value%3 !=0 }!
    à var1:2!
    §  findAll
    m.findAll{ it.value%2 == 0 && it.value%3 !=0 }!
    à[var1:2, var2:4]!
    Try It!
    Try It!

    View Slide

  32. 2014
    Questions?

    View Slide

  33. 2014
    Resources
    §  Groovy Documentation
    −  http://groovy-lang.org/documentation.html
    §  Groovy Communities
    −  Gr8Ladies
    •  http://gr8ladies.org
    •  http://forum.gr8ladies.org
    −  Local User Groups
    •  http://groovy-lang.org/usergroups.html

    View Slide

  34. 2014
    Got Feedback?
    Rate and Review the session using the
    GHC Mobile App
    To download visit www.gracehopper.org

    View Slide