Weitere ähnliche Inhalte Ähnlich wie Given Groovy Who Needs Java (20) Mehr von Russel Winder (20) Kürzlich hochgeladen (20) Given Groovy Who Needs Java1. Given Groovy Who Needs Java?
Prof Russel Winder
http://www.russel.org.uk
email: russel@winder.org.uk
xmpp: russel@winder.org.uk
twitter: @russel_winder
Copyright © 2012 Russel Winder 1
4. Groovy was…
● …designed to be a dynamic symbiote to Java.
● Java is statically typed.
● Groovy is optionally typed.
Copyright © 2012 Russel Winder 4
5. Groovy was…
● …designed to have a very lightweight syntax.
● Literal syntax for lists and maps.
● As little punctuation as possible.
Copyright © 2012 Russel Winder 5
6. Groovy was…
● …designed to work with code as first class entity.
● Closures and functional approach from the outset.
● No waiting for Java 8.
Copyright © 2012 Russel Winder 6
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
Copyright © 2012 Russel Winder 9
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'
}
}
Copyright © 2012 Russel Winder 10
11. def datum = ['Hello', ' ', 'World', '.'].join('')
println datum
Copyright © 2012 Russel Winder 11
12. String datum = ['Hello', ' ', 'World', '.'].join('')
println datum
Copyright © 2012 Russel Winder 12
13. datum = ['Hello', ' ', 'World', '.'].join('')
println datum
Copyright © 2012 Russel Winder 13
14. words = [:]
words.third = 'World'
words << [first: 'Hello', fourth: '.']
words['second'] = ' '
sequence = ['first', 'second', 'third', 'fourth']
println(sequence.collect{words[it]}.join(''))
Copyright © 2012 Russel Winder 14
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()
Copyright © 2012 Russel Winder 15
16. import groovy.xml.MarkupBuilder
new MarkupBuilder().html{
head{
title 'Hello World.'
}
body{
(0 ..< 3).each {
em('Hello')
}
}
}
Copyright © 2012 Russel Winder 16
17. <html>
<head>
<title>Hello World.</title>
</head>
<body>
<em>Hello</em>
<em>Hello</em>
<em>Hello</em>
</body>
</html>
Copyright © 2012 Russel Winder 17
18. html = new XmlParser().parse(System.in)
assert html.head.title.text() == 'Hello World.'
assert html.body.em.text() == 'HelloHelloHello'
Copyright © 2012 Russel Winder 18
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()
Copyright © 2012 Russel Winder 19
21. final n = 100000
final delta = 1.0 / n
final startTime = System.nanoTime()
def sum = 0.0
for (i in 1 .. n) { sum += 1 / (1 + ((i - 0.5) * delta) ** 2) }
final pi = 4 * delta * sum
final elapseTime = (System.nanoTime() - startTime) / 1e9
Output.out(getClass().name, pi, n, elapseTime)
Copyright © 2012 Russel Winder 21
22. 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)
Copyright © 2012 Russel Winder 22
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()
Copyright © 2012 Russel Winder 23
24. final int n = 10000000
final double delta = 1.0 / n
final 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) / 1e9
Output.out(getClass().name, pi, n, elapseTime)
Copyright © 2012 Russel Winder 24
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()
Copyright © 2012 Russel Winder 25
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 1
execute 2
execute 8
execute 32
Copyright © 2012 Russel Winder 26
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
}
Copyright © 2012 Russel Winder 27
29. There is nothing that Java can do
that Groovy cannot do better.
Copyright © 2012 Russel Winder 29
31. Given Groovy Who Needs Java?
Prof Russel Winder
http://www.russel.org.uk
email: russel@winder.org.uk
xmpp: russel@winder.org.uk
twitter: @russel_winder
Copyright © 2012 Russel Winder 31
33. Problem
Groovy
Java
Develop faster
Execute faster
Can be done at all
Copyright © 2012 Russel Winder 33
34. Given Groovy Who Needs Java?
Prof Russel Winder
http://www.russel.org.uk
email: russel@winder.org.uk
xmpp: russel@winder.org.uk
twitter: @russel_winder
Copyright © 2012 Russel Winder 34