278
THE EPIC GROOVY PUZZLERS!

The Groovy Puzzlers – The Complete 01 and 02 Seasons

Embed Size (px)

Citation preview

The Groovy Pz

The epic Groovy puzzlers!

Who's [email protected]/jbaruch

linkd.in/jbaruch

stackoverflow.com/users/402053/jbaruch

2

Developer who?

Developer who?

What Frog?

What Frog?

What Frog?

What Frog?

Frogs Groovy

Hosting Groovy on BintrayHosting Groovy on ArtifactoryBintray UI is GrailsArtifactory plugins are GroovyGradle and Grails plugins on Bintray

Frogs Groovy

Click and hackthe typing brothers

CURRENT JBNEXT - FRED2001 OracleWorld: Joshua Bloch, Neal Gafter14

BTW

CURRENT JBNEXT - FRED

The Car Show (1977-2012) Click and Clack, the Tappet Brothers - Tom and Ray Magliozzi

15

Two entertaining guys on stage

CURRENT JBNEXT - FRED

16

Ryan!

Two entertaining guys on stageFunny Puzzling questionsYou think and voteLots of T-shirts flying in the airOfficial twitter handle! groovypuzzlers

CURRENT JBNEXT - FRED

18

First rule of the puzzlers:No cheating!

CURRENT JBNEXT - FRED

19

First rule of the puzzlers:No cheating!Cdric?!

CURRENT JBNEXT - FRED

20

Consider the puzzlers correct as of 2.4.5

21

CURRENT JBNEXT - FRED

22

CURRENT FREDNEXT - FRED23

class Conference {def name; def year}

def gr = new Conference(name: 'Greach', year: 2014)

gr.each {println it}class=class Conferencename=Greachyear=2014 Conference@XXXXXX

Startup failure Greach 2014

CURRENT FREDNEXT - FRED

24

Not exactly iteration, is it?

CURRENT FREDNEXT - FRED

25

Read the source, Luke.

CURRENT FREDNEXT - FRED

26

Absolutely Groovy

CURRENT FREDNEXT - JB27

3NoSuchMethodError-3Execution Failure-3.abs()

CURRENT FREDNEXT - JB

28

CURRENT FREDNEXT - JB

29

Fix is simple(-3).abs()orint value = -3value.abs()

CURRENT FREDNEXT - JB

30

My idea prints nothing when I copy paste this code. What gives?

CURRENT FREDNEXT - JB

31

-333 and NullPointerException-3 and NullPointerExceptionPrintln for the rescue!println (-3).abs()

CURRENT FREDNEXT - JB

32

CURRENT FREDNEXT - JB

33

Delete all parentheses!Compiler will figure it out!

CURRENT FREDNEXT - JB

34

-3Caught: java.lang.NullPointerException: Cannot invoke method abs() on null objectjava.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)println (-3).abs()

CURRENT FREDNEXT - JB

35

All problems in computer science can be solved by another pair of parentheses

John McCarthy, the inventor of LISP

CURRENT FREDNEXT - JB

36

Just Throw in some more parentheses!println ((-3).abs())orint value = -3println value.abs()

CURRENT FREDNEXT - JB

37

And the t-shirt goes to

CURRENT FREDNEXT - JB

38

'a'..'z'.each { println it }NoSuchMethodErrorzWont run

a b c . . . z

You knew it, right?('a'..'z').each { println it }

And the t-shirt goes to

CURRENT FREDNEXT - JB

43

Max power

CURRENT JBNEXT - FRED44

Max powerList list = [56, '9', 74]def max = list.max { item -> (item < 50) ? item : null}println max

CURRENT JBNEXT - FRED

45

List list = [56, '9', 74]def max = list.max { item -> (item < 50) ? item : null}println maxClassCastException956null

CURRENT JBNEXT - FRED

46

Tricky, but I got it!>groovysh (('9' as Character) as Integer)===> 57

CURRENT JBNEXT - FRED

47

List list = [56, 57, 74]def max = list.max { item -> (item < 50) ? item : null}println max

CURRENT JBNEXT - FRED

48

Hmm, now they all null?!

CURRENT JBNEXT - FRED

49

ClassCastException956null

List list = [56, '9', 74]def max = list.max { item -> (item < 50) ? item : null}println max

CURRENT JBNEXT - FRED

50

CURRENT JBNEXT - FRED

51

What the hell happened?!def random = new Random()

def randomList = []0..10.each {randomList delegate.class.simpleName}.call())

class Ghostwriter { String book = '****** in Action, *** Edition'}

DierkGuillaumePaulCedricHamletErikJon.groovyGhostwriter Startup error DierkGuillaumePaulCedricHamletErikJon SimpleGroovyDoc

CURRENT JBNEXT - FRED

307

CURRENT JBNEXT - FRED

308

import org.codehaus.groovy.tools.groovydoc.SimpleGroovyDoc as delegate

// def delegate = new Ghostwriter()

println ({ -> delegate.class.name}.call())

/* class Ghostwriter { String book = '****** in Action, *** Edition'}*/

DierkGuillaumePaulCedricHamletErikJon.groovy

Startup error DierkGuillaumePaulCedricHamletErikJon SimpleGroovyDoc

CURRENT JBNEXT - FRED309

That explains *a lot*

CURRENT JBNEXT - FRED

310

CURRENT JBNEXT - FRED

311

1985

CURRENT FREDNEXT - JB312

class CountDown { int counter = 10 }

CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown }}

println finalCountDown().counter

CURRENT FREDNEXT - JB

313

class CountDown { int counter = 10 }

CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown }}

println finalCountDown().counter

Startup failureThat will never happen.-2147483648910

CURRENT FREDNEXT - JB

314

CURRENT FREDNEXT - JB

315

class CountDown { int counter = 10 }

CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { ignored.printStackTrace() countDown.counter = Integer.MIN_VALUE } finally { return countDown }}

println finalCountDown().counterorg.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '9' with class 'java.lang.Integer' to class 'CountDown-2147483648

CURRENT FREDNEXT - JB

316

What the hell happened?!

CURRENT FREDNEXT - JB

317

Fix? Just use the answer.class CountDown { int counter = 10 }

CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown } 42}

println finalCountDown().counter

CURRENT FREDNEXT - JB

318

CURRENT FREDNEXT - JB319

How that even possible?!

CURRENT FREDNEXT - JB

320

CURRENT FREDNEXT - JB

321

And the t-shirt goes to

CURRENT FREDNEXT - JB

322

And the t-shirts go to

CURRENT FREDNEXT - JB

323

Conclusions

Write readable codeComment neat tricksSometimes it is just a bugUse static code analysis (intellij IDEA!)Rtfm

Dont code like my brother

We have just started!(may end up in proper uniform)

Puzzlers? Gotchas? Fetal position inducing behavior?

puzzlers jfrog.com Groovypuzzlers

Jfrog always pays its debts

Positive feedback?Praise us on twitter groovypuzzlers Groovypuzzlers ryanvanderwerf jbaruch

Negative feeback?/dev/null

No, Thank you!

null10161.629The Final Countdown - fragmentEuropeTop Disco Hits vol. 1, track 179743.676