Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>One aspect of Groovy ( and Ruby, Python , etc ) I really like is the combination of closures and support for them in the Groovy DK .</p> <p>As an example, consider iterating through every line in a text file :</p> <h2>Groovy </h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> file = new File("c:/dynamic.ini"); file.eachLine{ println(it) } </pre></td></tr></table> <p>Very elegant and easy on the old eyes.</p> <h2>Java</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> BufferedReader input = null; try { input = new BufferedReader (new FileReader("c:/dynamic.ini")); String line; while ((line = input.readLine()) != null) { System.out.println(line); } } finally { if(input != null){ input.close(); } } </pre></td></tr></table> <p>Oh dear.</p> <h2>Java with some help</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Iteration.eachLine("c:/dynamic.ini", new LineHandler() { public void handleLine(String line) { System.out.println(line); } }); </pre></td></tr></table> <p>and the supporting code :</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public static void eachLine(String path, LineHandler handler) throws IOException { FileReader file = null; try { file = new FileReader(path); // Open the file. BufferedReader input = new BufferedReader(file); String line; while ((line = input.readLine()) != null) { handler.handleLine(line); } } finally { if (file != null) { file.close(); } } } </pre></td></tr></table> <h2>C# </h2> <p>Apologies for my C#, I have not been writing it for very long...</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> string line; using (StreamReader reader = File.OpenText(@"c:\dynamic.ini")) { while((line = reader.ReadLine()) != null){ Console.WriteLine(line); } } </pre></td></tr></table> <p>No need to escape path string is nice</p> <h2>C# 2.0 with some help</h2> <p>With C# 2.0 and the 2.0 .NET framework the addition of anonymous delegates we can produce a very concise and readable solution</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Iterators.EachLine(@"c:\dynamic.ini", delegate(string it) { Console.WriteLine(it); }); </pre></td></tr></table> <p>similar in elegance to the Groovy one.</p> <p>This depends on the following utility class </p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public static class Iterators { public delegate void Closure<T>(T it); public static void EachLine(string path, Closure<string> closure) { string line = null; using (StreamReader reader = File.OpenText(path)) { line = reader.ReadLine(); while (line != null) { closure(line); line = reader.ReadLine(); } } } } </pre></td></tr></table> <h2>Groovinating C#</h2> <p>You will notice that I have started implementing the Groovy DK set of collection iterators (each, findAll, find , etc ) for C# 2.0.</p> <p>I do wish Microsoft and Sun would have built this kind of simplicity in....</p> <p>There are 2 possible approaches to the Collections :</p> <ol> <li>Subclass and add the grooviness</li> <li>Add the grooviness as a set statci methods you plass the collection to</li> </ol> <p>Here is some perlimenary code and tests of both types.<br /> This subclasses List<T> and adds the groovination, and offers the same support ina static class </p> <h2>C# 2.0 Code </h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> #region Using directives using System; using System.Collections.Generic; using System.IO; #endregion namespace Groovinator { #region Delegatros public class Delegatros { public delegate void Closure<T>(T it); public delegate T Functor<T>(T it); public delegate bool Predicate<T>(T it); } #endregion #region Collection Iterators public class List<T> : System.Collections.Generic.List<T> { public void Each(Delegatros.Closure<T> closure) { foreach (T it in this) { closure(it); } } public List<T> FindAll(Delegatros.Predicate<T> predicate) { //this is a bit poor List<T> result = new List<T>(); foreach (T it in this) { if (predicate(it)) { result.Add(it); } } return result; } public T Find(Delegatros.Predicate<T> predicate) { foreach (T it in this) { if (predicate(it)) { return it; } } return default(T); } public void Map(Delegatros.Functor<T> functor) { List<T> result = new List<T>(); for(int i = 0;i< this.Count;++i){ this[i] = functor(this[i]); } } } public static class Iterators { // Collections public static void Each<T>(IEnumerable<T> enumerable, Delegatros.Closure<T> closure) { foreach (T it in enumerable) { closure(it); } } public static List<T> FindAll<T>(IEnumerable<T> enumerable, Delegatros.Predicate<T> predicate) { //this is a bit poor List<T> result = new List<T>(); foreach (T it in enumerable) { if (predicate(it)) { result.Add(it); } } return result; } public static T Find<T>(IEnumerable<T> enumerable, Delegatros.Predicate<T> predicate) { foreach (T it in enumerable) { if (predicate(it)) { return it; } } return default(T); } public static List<T> Map<T>(IEnumerable<T> enumerable, Delegatros.Functor<T> functor) { List<T> result = new List<T>(); foreach (T it in enumerable) { result.Add(functor(it)); } return result; } } #endregion #region File Iterators public static class IOIterators{ // File related iterators public static void EachLine(string path, Delegatros.Closure<string> closure) { string line = null; using (StreamReader reader = File.OpenText(path)) { line = reader.ReadLine(); while (line != null) { closure(line); line = reader.ReadLine(); } } } } #endregion } </pre></td></tr></table> <h2>Some Usage Examples</h2> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> // open, print all lines of a text file , close IOIterators.EachLine(@"c:\dynamic.ini", delegate(string it) { Console.WriteLine(it); }); List<string> list = new List<string>(); // populate list // print a list of string list.Each(delegate(string it) { Console.WriteLine(it); }); System.Collections.Generic.IEnumerable<string> res; // find all items matching the predicate item.Contaons("8") res = list.FindAll(delegate(string it) { return it.Contains("8"); }); // find the first item where Item.Equals("10") string ten = list.Find(delegate(string it) { return it.Equals("10"); }); List<int> nums = new List<int>(); for (int j = 1; j < 11; ++j) { nums.Add(j); } // apply a functor ( multiply by 2 ) to a list of ints nums.Map(delegate(int num) { return num * 2; }); </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced