the groovy puzzlers (as presented at javaone 2014)

146
THE GROOVY PUZZLERS AS USUAL - TRAPS, PITFALLS, AND END CASES

Upload: groovypuzzlers

Post on 16-Jun-2015

240 views

Category:

Technology


2 download

TRANSCRIPT

  • 1. 1. Two entertaining guys on stage2. Funny Puzzling questions3. You think and vote4. Lots of T-shirts flying in the air5. Official twitter handle!groovypuzzlers

2. Cdric?! 3. class Conference {def name; def year}def gr = new Conference(name: 'Greach', year: 2014)gr.each {println it} 4. -3.abs() 5. (-3).abs()int value = -3value.abs() 6. println (-3).abs() 7. println (-3).abs()-3Caught: java.lang.NullPointerException: Cannot invoke method abs() on nullobjectjava.lang.NullPointerException: Cannot invoke method abs() on null objectat AbsolutelyGroovy.run(AbsolutelyGroovy.groovy:7)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 8. All problems in computer science can besolved by another pair of parenthesesJohn McCarthy, the inventor of LISP 9. println ((-3).abs())int value = -3println value.abs() 10. def x = intprintln xif ((x = long)) {println x}if (x = boolean ) {println x} 11. def x = intprintln xif ((x = long)) {println x}if (x = boolean ) {println x} 12. Closure whodunit() {{'The butler did it.'}}println whodunit() 13. Closure whodunit() {{ ->'The butler did it.'}} 14. PUBLIC -PROPERTY! 15. trait Public {public String property = "I am all public!"}class Property implements Public {}Property publicProperty = new Property() 16. trait Public {public String property = "I am all public!"}class Property implements Public {}Property publicProperty = new Property() 17. http://beta.groovy-lang.org/docs/groovy-2.3.0/html/documentation/core- 18. def range = 1.0..10.0assert range.contains(5.0)println range.contains(5.6) 19. Iterator iterator = (1.0..10.0).iterator()while (iterator.hasNext()) {print "${iterator.next()} "}1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 20. [0..9].each { println(it - 1) } 21. def key = 'x'def map = [key: 'treasure']def value = map.get(key)println value 22. def key = 'x'def map = [key: 'treasure']def value = map.get(key)println value 23. 1.def map = [(key): 'treasure']2.map.put(key, 'treasure')3.map."$key" = 'treasure'4.map[key] = 'treasure' 24. def map = [2: 'treasure']def key = 2def value = map."$key"println value 25. def map = [2: 'treasure']def key = 2def value = map."$key"println value 26. def map = [2: 'treasure']println map.keySet().first().class.namejava.lang.Integer 27. List list = [1,2,3]def now = new Date()list