Save 37% off PRO during our Black Friday Sale! »

Groovy at Gr8Ladies Iowa Code Camp

1f28a0c1988421be3268026bd6bb6f49?s=47 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

1f28a0c1988421be3268026bd6bb6f49?s=128

jlstrater

November 01, 2014
Tweet

Transcript

  1. GROOVY AT GR8LADIES IOWA CODE CAMP 14 NOVEMBER 1, 2014

    Presented By / Jenn Strater @jennstrater
  2. WHO AM I? Software Engineer Healthcare Communications Division Minneapolis, MN

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

  4. DISCLAIMER I may say something wrong!

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

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

    Pivotal http://groovy-lang.org https://github.com/groovy/groovy-core
  8. THE GROOVY ECOSYSTEM Grails Griffon Gradle and And Many More!

    Spock Geb Ratpack
  9. GROOVY ARCHITECTURE

  10. WHEN TO USE GROOVY existing java libraries developers know java

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

    another language especially Ruby need 'bare metal' level performance
  12. SCRIPTING Console Shell

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

  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.
  15. HELLO,WORLD! println "Hello, Iowa Code Camp!" -> Hello, Iowa Code

    Camp!
  16. DATA TYPES Strings Collections Lists Maps Ranges Closures Def

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

  18. COLLECTIONS

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

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

    keys/values
  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]

  22. FUNCTIONS int double(x) { x * 2 } Flexible, familiar

    syntax Optional parenthesis when calling functions parameter typing return statements
  23. double(2) --> 4 double 8 --> 16

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

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

    Dot
  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]
  27. EACH Map m = [val1: 2, val2: 4, val3: 6]

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

    List dl = m.collect { name, value -> value * 2 } println dl --> [4, 8, 12]
  29. SPREAD DOT (1..3).multiply(2) -->[1,2,3,1,2,3] (1..3)*.multiply(2) --> [2,4,6]

  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()
  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()
  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]
  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 + ' ' } }
  34. FILE PROCESING

  35. CREATING & WRITING TO FILES def myFile = new File('foo.txt')

    myFile.write 'hello, world!!\n' myFile.append('and hello universe!')
  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!
  37. LET'S PUT IT ALL TOGETHER (group coding in console)

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