Productive programming with groovy  - an introduction
Upcoming SlideShare
Loading in...5
×
 

Productive programming with groovy - an introduction

on

  • 2,406 views

Are you a Java programmer having wrist pain typing a lot (yes, I have!)? Does your mind keep swinging from loving to hating Java? Do you want to become more productive without leaving your world of ...

Are you a Java programmer having wrist pain typing a lot (yes, I have!)? Does your mind keep swinging from loving to hating Java? Do you want to become more productive without leaving your world of Java? If so, this presentation is for you. I have been experimenting with Groovy for the last three months and I'm loving it - I am not going back to programming in plan old Java anymore. Yes, I have become Groovy - have you?

Statistics

Views

Total Views
2,406
Views on SlideShare
859
Embed Views
1,547

Actions

Likes
0
Downloads
1
Comments
0

39 Embeds 1,547

http://ocpjp7.blogspot.in 902
http://ocpjp7.blogspot.com 295
http://ocpjp7.blogspot.com.br 40
http://ocpjp7.blogspot.co.uk 38
http://ocpjp7.blogspot.fi 22
http://ocpjp7.blogspot.de 21
http://ocpjp7.blogspot.ru 20
http://ocpjp7.blogspot.ca 18
http://ocpjp7.blogspot.it 17
http://ocpjp7.blogspot.ro 16
http://ocpjp7.blogspot.fr 16
http://ocpjp7.blogspot.nl 15
http://ocpjp7.blogspot.mx 15
http://ocpjp7.blogspot.com.es 12
http://ocpjp7.blogspot.ch 12
http://ocpjp7.blogspot.com.au 11
http://ocpjp7.blogspot.be 11
http://ocpjp7.blogspot.ie 6
http://ocpjp7.blogspot.hu 6
http://ocpjp7.blogspot.com.ar 5
http://ocpjp7.blogspot.co.nz 4
http://ocpjp7.blogspot.no 4
http://ocpjp7.blogspot.pt 4
http://ocpjp7.blogspot.hk 4
http://ocpjp7.blogspot.se 3
http://ocpjp7.blogspot.gr 3
http://ocpjp7.blogspot.dk 3
http://ocpjp7.blogspot.co.at 3
http://java.meximas.com 3
http://cloud.feedly.com 3
http://ocpjp7.blogspot.sg 2
http://www.feedspot.com 2
http://ocpjp7.blogspot.jp 2
http://www.blogger.com 2
http://ocpjp7.blogspot.tw 2
http://ocpjp7.blogspot.ae 2
http://ocpjp7.blogspot.sk 1
http://ocpjp7.blogspot.co.il 1
http://ocpjp7.blogspot.cz 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Productive programming with groovy  - an introduction Productive programming with groovy - an introduction Presentation Transcript

  • Productive Programming with Groovy S G Ganesh http://ocpjp7.blogspot.in
  • Introducing Groovy
  • How I Felt Programming in Groovy vs. Java!
  • Simple (first) Example import java.io.*; class Type { public static void main(String []files) { // process each file passed as argument for(String file : files) { // try opening the file with FileReader try (FileReader inputFile = new FileReader(file)) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } catch (FileNotFoundException fnfe) { System.err.printf("Cannot open the given file %s ", file); } catch(IOException ioe) { System.err.printf("Error when processing file %s; skipping it", file); } // try-with-resources will automatically release FileReader object } } } args.each { println new File(it).getText() }
  • Mere Syntactic Sugar? Map<String, String> inglishWords = new HashMap<>(); inglishWords.put("Shampoo", "'Chapmo' (Hindi)"); inglishWords.put("Sugar", "'Sarkkarai' (Tamil)"); inglishWords.put("Copra", "'Koppara' (Malayalam)"); inglishWords.put("Jute", "'Jhuto' (Bengali)"); inglishWords.put("Cot", "'Khatva' (Sanskrit)"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } def inglishWords = [ "Shampoo" : "'Chapmo' (Hindi)", "Sugar" : "'Sarkkarai' (Tamil)", "Copra" : "'Koppara' (Malayalam)", "Jute" : "'Jhuto' (Bengali)", "Cot" : "'Khatva' (Sanskrit)" ] inglishWords.each { key, value -> println "$key => $value" }
  • Just Concise Expression? System.out.println("The (key-based) sorted map is: "); Map<String, String> keySortedMap = new TreeMap<>(inglishWords); for(Map.Entry<String, String> word : keySortedMap.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } println "The (key-based) sorted map is: " def keySortedMap = inglishWords.sort() keySortedMap.each { key, value -> println "$key => $value" }
  • Now for the Real Magic!  class MapUtil { // this code segment from http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map ) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() ); Collections.sort( list, new Comparator<Map.Entry<K, V>>() { public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 ) { return (o1.getValue()).compareTo( o2.getValue() ); } } ); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put( entry.getKey(), entry.getValue() ); } return result; } } System.out.println("The (value-based) sorted map is: "); Map<String, String> valueSortedMap = MapUtil.sortByValue(inglishWords); for(Map.Entry<String, String> word : valueSortedMap.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } println "The (value-based) sorted map is: " def valueSortedMap = inglishWords.sort { it.value } valueSortedMap.each { key, value -> println "$key => $value" }
  • HTML Creation Example try (PrintWriter pw = new PrintWriter(new FileWriter("./index.java.html"))) { pw.println("<html> <head> <title>Words from Indian origin in English</title> </head> <body>"); pw.println("<h1>Words from Indian origin in English</h1>"); pw.println("<table border='1'> <tr> <th>Inglish word</th> <th>Origin</th></tr>"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { pw.printf("<tr> <td> %s </td> <td> %s </td> </tr>", word.getKey(), word.getValue()); } pw.println("</table> <p style='font-style:italic;font-size:small;float:right'>Results obtained at " + new Date() + "</p> </body> </html>"); }
  • HTML Creation Example def writer = new StringWriter() def doc = new MarkupBuilder(writer) doc.html() { head { title("Words from Indian origin in English") } body { h1("Words from Indian origin in English") table(border:1) { tr { th("Inglish word") th("Origin") } inglishWords.each { word, root -> tr { td("$word") td("$root") } } } p(style:'font-style:italic;font-size:small;float:right', "Results obtained at ${new Date().dateTimeString}") } } def filename = 'index.groovy.html' new File(filename).write(writer.toString())
  • Not Convinced?! Check XML Creation Example try (PrintWriter pw = new PrintWriter(new FileWriter("./words.xml"))) { pw.println("<inglishwords>"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { pw.printf("t<language word='%s'> n tt <origin> '%s'</origin> n t </language> n", word.getKey(), word.getValue()); } pw.println("</inglishwords>"); } xmlbuilder = new groovy.xml.MarkupBuilder() xmlbuilder.inglishwords { words.each { key, value -> language(word:key) { origin(value) } } }
  • Where to Learn More About Groovy?
  • Some Dynamic Languages for .NET World