34
Copyright © 2012 Russel Winder 1 Given Groovy Who Needs Java? Prof Russel Winder http://www.russel.org.uk email: [email protected] xmpp: [email protected] twitter: @russel_winder

Given Groovy Who Needs Java

Embed Size (px)

DESCRIPTION

A session at the LJC UnConference 2012. A presentation showing that Groovy is already everything Java wants to be, and less verbose with it.

Citation preview

Page 1: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 1

Given Groovy Who Needs Java?

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder

Page 2: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 2

Interstitial Advertisement

Page 3: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 3

Introduction

Page 4: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 4

Groovy was…

● …designed to be a dynamic symbiote to Java.

● Java is statically typed.● Groovy is optionally typed.

Page 5: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 5

Groovy was…

● …designed to have a very lightweight syntax.

● Literal syntax for lists and maps.● As little punctuation as possible.

Page 6: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 6

Groovy was…

● …designed to work with code as first class entity.

● Closures and functional approach from the outset.● No waiting for Java 8.

Page 7: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 7

Code

Page 8: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 8

println 'Hello World.'

Page 9: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 9

public class testAll_GroovyTestCase extends GroovyTestCase { void test_helloWorld_trivial() { assert 'helloWorld_trivial.groovy'.execute().text == 'Hello World.\n' }}

Power asserts

JUnit3 behind the scenes

Page 10: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 10

@Grab('org.spockframework:spock:0.7-groovy2.0')import spock.lang.Specification

class TestAll_Spock extends Specification { def "ensure the hello world program prints hello world"() { expect: 'helloWorld_trivial.groovy'.execute().text == 'Hello World.\n' }}

Page 11: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 11

def datum = ['Hello', ' ', 'World', '.'].join('')

println datum

Page 12: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 12

String datum = ['Hello', ' ', 'World', '.'].join('')

println datum

Page 13: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 13

datum = ['Hello', ' ', 'World', '.'].join('')

println datum

Page 14: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 14

words = [:]

words.third = 'World'words << [first: 'Hello', fourth: '.']words['second'] = ' '

sequence = ['first', 'second', 'third', 'fourth']

println(sequence.collect{words[it]}.join(''))

Page 15: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 15

@GrabResolver(name='atlassian', root='https://maven.atlassian.com/content/groups/public/')@Grab('org.swift.tools:gint:1.5.0')import org.swift.tools.Gint

includeTool << Gint

gint.initialize(this)

new File('.').eachFileMatch(~/helloWorld_.*\.groovy/) { gint.add(name: it.name, inline: { it.name.execute().text == 'Hello World.\n' })}

gint.finalizeTest()

Page 16: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 16

import groovy.xml.MarkupBuilder

new MarkupBuilder().html{ head{ title 'Hello World.' } body{ (0 ..< 3).each { em('Hello') } }}

Page 17: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 17

<html>  <head>    <title>Hello World.</title>  </head>  <body>    <em>Hello</em>    <em>Hello</em>    <em>Hello</em>  </body></html>

Page 18: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 18

html = new XmlParser().parse(System.in)assert html.head.title.text() == 'Hello World.'assert html.body.em.text() == 'HelloHelloHello'

Page 19: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 19

import javax.swing.WindowConstants

import groovy.swing.SwingBuilder

def widget = new SwingBuilder().frame( title: 'Hello World Window', size: [200, 100], defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE) { panel { button(text:'Say Hello', actionPerformed: { println 'Hello.' }) }}widget.show()

Page 20: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 20

Page 21: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 21

final n = 100000final delta = 1.0 / nfinal startTime = System.nanoTime()def sum = 0.0for (i in 1 .. n) { sum += 1 / (1 + ((i - 0.5) * delta) ** 2) }final pi = 4 * delta * sumfinal elapseTime = (System.nanoTime() - startTime) / 1e9Output.out(getClass().name, pi, n, elapseTime)

Page 22: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 22

final int n = 1000000000final double delta = 1.0 / nfinal startTimeNanos = System.nanoTime()double sum = 0.0for (int i = 1; i <= n; ++i) { final double x = (i - 0.5d) * delta sum += 1.0d / (1.0d + x * x)}final double pi = 4.0 * delta * sumfinal elapseTime = (System.nanoTime() - startTimeNanos) / 1e9Output.out(getClass().name, pi, n, elapseTime)

Page 23: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 23

import groovy.transform.CompileStatic

@CompileStatic execute() { final int n = 1000000000 final double delta = 1.0 / n final startTimeNanos = System.nanoTime () double sum = 0.0 for (int i = 1; i <= n; ++i) { final double x = (i - 0.5d) * delta sum += 1.0d / (1.0d + x * x) } final double pi = 4.0 * delta * sum final elapseTime = (System.nanoTime() - startTimeNanos) / 1e9 Output.out(getClass().name, pi, n, elapseTime)}

execute()

Page 24: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 24

final int n = 10000000final double delta = 1.0 / nfinal startTime = System.nanoTime()final double pi = 4.0 * delta * (1i .. n).sum {int i -> final double x = (i - 0.5d) * delta 1.0d / (1.0d + x * x)}final elapseTime = (System.nanoTime() - startTime) / 1e9Output.out(getClass().name, pi, n, elapseTime)

Page 25: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 25

import groovy.transform.CompileStatic

@CompileStatic execute() { final int n = 10000000 final double delta = 1.0 / n final startTime = System.nanoTime() final double pi = 4.0 * delta * (double)((1i .. n).sum {int i -> final double x = (i - 0.5d) * delta 1.0d / (1.0d + x * x) }) final elapseTime = (System.nanoTime() - startTime) / 1e9 Output.out(getClass().name, pi, n, elapseTime)}

execute()

Page 26: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 26

import groovyx.gpars.ParallelEnhancer

void execute(final numberOfTasks) { final n = 1000000000 final delta = 1.0 / n final startTimeNanos = System.nanoTime () final sliceSize = (int)(n / numberOfTasks) final items = 0 ..< numberOfTasks ParallelEnhancer.enhanceInstance(items) final pi = 4.0 * delta * items.collectParallel {taskId -> PartialSum.dynamicCompile(taskId, sliceSize, delta) }.sumParallel() final elapseTime = (System.nanoTime() - startTimeNanos) / 1e9 Output.out(getClass().name, pi, n, elapseTime, numberOfTasks)}

execute 1execute 2execute 8execute 32

Page 27: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 27

static double dynamicCompile(final int taskId, final int sliceSize, final double delta) { final int start = 1i + taskId * sliceSize final int end = (taskId + 1i) * sliceSize double sum = 0.0d for (int i = start; i <= end; ++i) { final double x = (i - 0.5d) * delta sum += 1.0d / (1.0d + x * x) } sum}

Page 28: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 28

Conclusion

Page 29: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 29

There is nothing that Java can do

that Groovy cannot do better.

Page 30: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 30

Interstitial Advertisement

Page 31: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 31

Given Groovy Who Needs Java?

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder

Page 32: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 32

Challenge

Page 33: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 33

Problem

GroovyJava

Develop fasterExecute fasterCan be done at all

Page 34: Given Groovy Who Needs Java

Copyright © 2012 Russel Winder 34

Given Groovy Who Needs Java?

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder