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

Intro to Groovy Gr8Ladies Gr8Workshop December 2014

jlstrater
December 06, 2014

Intro to Groovy Gr8Ladies Gr8Workshop December 2014

Slides for the Intro to Groovy at the Gr8Ladies Gr8Workshop December 6, 2014

jlstrater

December 06, 2014
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. 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:
  2. Open source -- maintained by and programmers like you! contribute:

    Pivotal http://groovy-lang.org https://github.com/groovy/groovy-core
  3. WHEN TO USE GROOVY existing java libraries developers know java

    or another JVM language quick prototyping scripting writing tests
  4. WHEN NOT TO USE GROOVY lots of legacy code in

    another language especially Ruby need 'bare metal' level performance
  5. 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.
  6. FUNCTIONS int double(x) { x * 2 } Flexible, familiar

    syntax Optional parenthesis when calling functions parameter typing return statements
  7. 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]
  8. EACH Map m = [val1: 2, val2: 4, val3: 6]

    List dl = [] m.each { key, value ‐> dl << value * 2 } println dl ‐‐> [4, 8, 12]
  9. COLLECT Map m = [var1: 2, var2: 4, var3: 6]

    List dl = m.collect { name, value ‐> value * 2 } println dl ‐‐> [4, 8, 12]
  10. 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()
  11. 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()
  12. 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]
  13. 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 + ' ' } }
  14. CREATING & WRITING TO FILES def myFile = new File('foo.txt')

    myFile.write 'hello, world!!\n' myFile.append('and hello universe!')
  15. READING FILES def myFile = new File('foo.txt') myFile.eachLine { line

    ‐> def processedLine = line.replaceAll('hello','hi') println processedLine } ‐‐> hi, world! ‐‐> and hi universe!