Given Groovy Who Needs Java

1,005 views
907 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,005
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Given Groovy Who Needs Java

  1. 1. 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_winderCopyright © 2012 Russel Winder 1
  2. 2. Interstitial AdvertisementCopyright © 2012 Russel Winder 2
  3. 3. IntroductionCopyright © 2012 Russel Winder 3
  4. 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. 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. 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
  7. 7. CodeCopyright © 2012 Russel Winder 7
  8. 8. println Hello World.Copyright © 2012 Russel Winder 8
  9. 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 scenesCopyright © 2012 Russel Winder 9
  10. 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. 11. def datum = [Hello, , World, .].join() println datumCopyright © 2012 Russel Winder 11
  12. 12. String datum = [Hello, , World, .].join() println datumCopyright © 2012 Russel Winder 12
  13. 13. datum = [Hello, , World, .].join() println datumCopyright © 2012 Russel Winder 13
  14. 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. 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. 16. import groovy.xml.MarkupBuilder new MarkupBuilder().html{ head{ title Hello World. } body{ (0 ..< 3).each { em(Hello) } } }Copyright © 2012 Russel Winder 16
  17. 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. 18. html = new XmlParser().parse(System.in) assert html.head.title.text() == Hello World. assert html.body.em.text() == HelloHelloHelloCopyright © 2012 Russel Winder 18
  19. 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
  20. 20. Copyright © 2012 Russel Winder 20
  21. 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. 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. 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. 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. 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. 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 32Copyright © 2012 Russel Winder 26
  27. 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
  28. 28. ConclusionCopyright © 2012 Russel Winder 28
  29. 29. There is nothing that Java can do that Groovy cannot do better.Copyright © 2012 Russel Winder 29
  30. 30. Interstitial AdvertisementCopyright © 2012 Russel Winder 30
  31. 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_winderCopyright © 2012 Russel Winder 31
  32. 32. ChallengeCopyright © 2012 Russel Winder 32
  33. 33. Problem Groovy Java Develop faster Execute faster Can be done at allCopyright © 2012 Russel Winder 33
  34. 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_winderCopyright © 2012 Russel Winder 34

×