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

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

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

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

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

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

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

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

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