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

Groovy at Gr8Ladies Iowa Code Camp

jlstrater
November 01, 2014

Groovy at Gr8Ladies Iowa Code Camp

Presentation for Iowa Code Camp November 1, 2014

You can also see the slides with audio at: https://www.youtube.com/watch?v=CpR8AOzVo34

jlstrater

November 01, 2014
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. GROOVY AT
    GR8LADIES
    IOWA CODE CAMP 14
    NOVEMBER 1, 2014
    Presented By /
    Jenn Strater @jennstrater

    View Slide

  2. WHO AM I?
    Software Engineer
    Healthcare Communications Division
    Minneapolis, MN
    Co-Founder

    View Slide

  3. WHO ARE YOU?
    Students?
    Professional Developers?
    Java
    .NET
    PHP
    Groovy?

    View Slide

  4. DISCLAIMER
    I may say something wrong!

    View Slide

  5. OUTLINE
    Groovy Overview
    Basic Data Types
    Functions
    Loops
    Search
    Examples
    APIs*
    Files*

    View Slide

  6. WHAT'S GROOVY
    Dynamic, compiled language for the Java Virtual
    Machine(JVM).
    Interoperable with existing Java libraries
    Concise, easy to read code
    Used by many major companies including:

    View Slide

  7. Open source -- maintained by and programmers
    like you!
    contribute:
    Pivotal
    http://groovy-lang.org
    https://github.com/groovy/groovy-core

    View Slide

  8. THE GROOVY ECOSYSTEM
    Grails
    Griffon
    Gradle
    and
    And Many More!
    Spock Geb
    Ratpack

    View Slide

  9. GROOVY ARCHITECTURE

    View Slide

  10. WHEN TO USE GROOVY
    existing java libraries
    developers know java or another JVM language
    quick prototyping
    scripting
    writing tests

    View Slide

  11. WHEN NOT TO USE
    GROOVY
    lots of legacy code in another language especially Ruby
    need 'bare metal' level performance

    View Slide

  12. SCRIPTING
    Console
    Shell

    View Slide

  13. FOLLOW ALONG
    http://groovyconsole.appspot.com

    View Slide

  14. THINGS TO REMEMBER!
    1. There is more than one way to do almost anything.
    2. It's probably optional.
    semi-colons
    typing
    parenthesis around parameters
    return statements
    3. By default, the last command executed is what's returned.

    View Slide

  15. HELLO,WORLD!
    println "Hello, Iowa Code Camp!"
    -> Hello, Iowa Code Camp!

    View Slide

  16. DATA TYPES
    Strings
    Collections
    Lists
    Maps
    Ranges
    Closures
    Def

    View Slide

  17. STRINGS
    (examples in console)
    Single Quoted
    Double Quoted
    Multi-line

    View Slide

  18. COLLECTIONS

    View Slide

  19. LISTS
    (examples in console)
    definition
    get/set value
    append
    multiply

    View Slide

  20. MAPS
    (examples in console)
    definition
    get/set by keys
    get all keys/values

    View Slide

  21. RANGES
    println (1..10)
    -->[1,2,3,4,5,6,7,8,9,10]
    println (1..<10)
    -->[1,2,3,4,5,6,7,8,9]

    View Slide

  22. FUNCTIONS
    int double(x) {
    x * 2
    }
    Flexible, familiar syntax
    Optional
    parenthesis when calling functions
    parameter typing
    return statements

    View Slide

  23. double(2)
    --> 4
    double 8
    --> 16

    View Slide

  24. CLOSURES
    Code as Data
    (to the console!)
    code as data
    definition
    call

    View Slide

  25. LOOPS AND SPECIAL
    FUNCTIONS
    Basic For Loop
    Each
    Collect
    Spread Dot

    View Slide

  26. BASIC LOOPS
    List dl = [];
    for(int i=1; i<4; i++){
    dl.add(i * 2);
    }
    println dl;
    --> [2,4,6]
    List dl = []
    for(i in [1,2,3]) {
    dl << i * 2
    }
    println dl
    --> [2,4,6]

    View Slide

  27. EACH
    Map m = [val1: 2, val2: 4, val3: 6]
    List dl = []
    m.each { key, value ->
    dl << value * 2
    }
    println dl
    --> [4, 8, 12]

    View Slide

  28. COLLECT
    Map m = [var1: 2, var2: 4, var3: 6]
    List dl = m.collect { name, value ->
    value * 2
    }
    println dl
    --> [4, 8, 12]

    View Slide

  29. SPREAD DOT
    (1..3).multiply(2)
    -->[1,2,3,1,2,3]
    (1..3)*.multiply(2)
    --> [2,4,6]

    View Slide

  30. TRY IT!
    Let's groovy-fy this code
    List l = [-1,2,-3,-17]
    List al = []
    for ( i in l ) {
    al << i.abs()
    }
    println al
    --> [1,2,3,17]
    [-1,2,-3,-17]*.abs()

    View Slide

  31. TRY IT!
    Sum the numbers from 1 to 10
    option1:
    int answer = 0
    for (i in [1,2,3,4,5,6,7,8,9,10]) {
    answer += i
    }
    println answer
    option2:
    int answer = 0
    (1..10).each {
    answer = answer + it
    }
    println answer
    Bonus answer:
    (1..10).sum()

    View Slide

  32. SEARCH
    Map m = [val1: 2, val2: 4, val3: 6]
    find
    m.find{it.value % 2 == 0 && it.value % 3 != 0}
    --> val1:2
    findAll
    m.findAll{it.value % 2 == 0 && it.value % 3 != 0}
    --> [val1:2,val2:4]

    View Slide

  33. APIS
    (in console)
    @Grab(group='org.codehaus.groovy.modules.http-builder',
    module='http-builder', version='0.7.1')
    import groovyx.net.http.RESTClient
    def http = new RESTClient("http://api.icndb.com")
    http.get(path: '/jokes/random/5', query: [exclude: 'explicit']) { resp, reader ->
    reader.value.joke.each{
    println it + '
    '
    }
    }

    View Slide

  34. FILE PROCESING

    View Slide

  35. CREATING & WRITING TO
    FILES
    def myFile = new File('foo.txt')
    myFile.write 'hello, world!!\n'
    myFile.append('and hello universe!')

    View Slide

  36. READING FILES
    def myFile = new File('foo.txt')
    myFile.eachLine { line ->
    def processedLine = line.replaceAll('hello','hi')
    println processedLine
    }
    --> hi, world!
    --> and hi universe!

    View Slide

  37. LET'S PUT IT ALL TOGETHER
    (group coding in console)

    View Slide

  38. QUESTIONS?
    Resources
    http://groovy-lang.og
    http:/gr8ladies.org

    View Slide